-
Notifications
You must be signed in to change notification settings - Fork 4
/
xxGraphicMetal2.mm
409 lines (360 loc) · 17.9 KB
/
xxGraphicMetal2.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//==============================================================================
// xxGraphic : Metal 2 Source
//
// Copyright (c) 2019-2024 TAiGA
// https://github.com/metarutaiga/xxGraphic
//==============================================================================
#define xxCreateInstanceMetal xxConcate(xxCreateInstanceMetal, __LINE__)
#define xxCreateInstanceMetal2 xxConcate(xxCreateInstanceMetal2, __LINE__)
#include "internal/xxGraphicInternalMetal.h"
#include "xxGraphicMetal.h"
#include "xxGraphicMetal2.h"
//==============================================================================
// Instance
//==============================================================================
id xxCreateInstanceMetal2()
{
id instance = xxCreateInstanceMetal();
if (instance == 0)
return 0;
xxRegisterFunctionSingle(xxCreateInstance, xxCreateInstanceMetal2);
xxRegisterFunctionSingle(xxGetInstanceName, xxGetInstanceNameMetal2);
xxRegisterFunctionSingle(xxGetCommandBuffer, xxGetCommandBufferMetal2);
xxRegisterFunctionSingle(xxBeginRenderPass, xxBeginRenderPassMetal2);
xxRegisterFunctionSingle(xxEndRenderPass, xxEndRenderPassMetal2);
xxRegisterFunctionSingle(xxCreateConstantBuffer, xxCreateConstantBufferMetal2);
xxRegisterFunctionSingle(xxCreateIndexBuffer, xxCreateIndexBufferMetal2);
xxRegisterFunctionSingle(xxCreateVertexBuffer, xxCreateVertexBufferMetal2);
xxRegisterFunctionSingle(xxDestroyBuffer, xxDestroyBufferMetal2);
xxRegisterFunctionSingle(xxMapBuffer, xxMapBufferMetal2);
xxRegisterFunctionSingle(xxUnmapBuffer, xxUnmapBufferMetal2);
xxRegisterFunctionSingle(xxCreateSampler, xxCreateSamplerMetal2);
xxRegisterFunctionSingle(xxCreateVertexShader, xxCreateVertexShaderMetal2);
xxRegisterFunctionSingle(xxCreateFragmentShader, xxCreateFragmentShaderMetal2);
xxRegisterFunctionSingle(xxCreatePipeline, xxCreatePipelineMetal2);
xxRegisterFunctionSingle(xxSetViewport, xxSetViewportMetal2);
xxRegisterFunctionSingle(xxSetScissor, xxSetScissorMetal2);
xxRegisterFunctionSingle(xxSetPipeline, xxSetPipelineMetal2);
xxRegisterFunctionSingle(xxSetVertexBuffers, xxSetVertexBuffersMetal2);
xxRegisterFunctionSingle(xxSetVertexTextures, xxSetVertexTexturesMetal2);
xxRegisterFunctionSingle(xxSetFragmentTextures, xxSetFragmentTexturesMetal2);
xxRegisterFunctionSingle(xxSetVertexSamplers, xxSetVertexSamplersMetal2);
xxRegisterFunctionSingle(xxSetFragmentSamplers, xxSetFragmentSamplersMetal2);
xxRegisterFunctionSingle(xxSetVertexConstantBuffer, xxSetVertexConstantBufferMetal2);
xxRegisterFunctionSingle(xxSetFragmentConstantBuffer, xxSetFragmentConstantBufferMetal2);
xxRegisterFunctionSingle(xxDraw, xxDrawMetal2);
xxRegisterFunctionSingle(xxDrawIndexed, xxDrawIndexedMetal2);
return instance;
}
//==============================================================================
// Swapchain
//==============================================================================
MTLSWAPCHAIN* xxGetCommandBufferMetal2(id <MTLDevice> __unsafe_unretained device, MTLSWAPCHAIN* swapchain)
{
id commandBuffer = xxGetCommandBufferMetal(device, swapchain);
if (commandBuffer == 0)
return 0;
swapchain->pipeline = nullptr;
swapchain->frameCount++;
swapchain->commandEncoder = nil;
swapchain->argumentBufferIndex = (swapchain->argumentBufferIndex + 1) % xxCountOf(swapchain->argumentBuffers);
swapchain->argumentBufferStep = 0;
swapchain->vertexArgumentEncoder = nil;
swapchain->fragmentArgumentEncoder = nil;
swapchain->argumentEncoderComplete = true;
return swapchain;
}
//==============================================================================
// Render Pass
//==============================================================================
MTLSWAPCHAIN* xxBeginRenderPassMetal2(MTLSWAPCHAIN* swapchain, MTLFRAMEBUFFER* framebuffer, MTLRenderPassDescriptor* __unsafe_unretained renderPass, int width, int height, float color[4], float depth, unsigned char stencil)
{
id commandEncoder = xxBeginRenderPassMetal(swapchain->commandBuffer, framebuffer, renderPass, width, height, color, depth, stencil);
if (commandEncoder == 0)
return 0;
swapchain->commandEncoder = commandEncoder;
return swapchain;
}
//------------------------------------------------------------------------------
void xxEndRenderPassMetal2(MTLSWAPCHAIN* swapchain, MTLFRAMEBUFFER* framebuffer, MTLRenderPassDescriptor* __unsafe_unretained renderPass)
{
[swapchain->commandEncoder endEncoding];
swapchain->commandEncoder = nil;
}
//==============================================================================
// Buffer
//==============================================================================
MTLBUFFER* xxCreateConstantBufferMetal2(id <MTLDevice> __unsafe_unretained device, int size)
{
if (device == nil)
return 0;
MTLBUFFER* buffer = new MTLBUFFER{};
if (buffer == nullptr)
return 0;
buffer->buffer = [device newBufferWithLength:size
options:MTLResourceStorageModeShared];
return buffer;
}
//------------------------------------------------------------------------------
MTLBUFFER* xxCreateIndexBufferMetal2(id <MTLDevice> __unsafe_unretained device, int size)
{
if (device == nil)
return 0;
MTLBUFFER* buffer = new MTLBUFFER{};
if (buffer == nullptr)
return 0;
buffer->buffer = [device newBufferWithLength:size
options:MTLResourceStorageModeShared];
return buffer;
}
//------------------------------------------------------------------------------
MTLBUFFER* xxCreateVertexBufferMetal2(id <MTLDevice> __unsafe_unretained device, int size, MTLVertexDescriptor* __unsafe_unretained vertexAttribute)
{
if (device == nil)
return 0;
MTLBUFFER* buffer = new MTLBUFFER{};
if (buffer == nullptr)
return 0;
buffer->buffer = [device newBufferWithLength:size
options:MTLResourceStorageModeShared];
return buffer;
}
//------------------------------------------------------------------------------
void xxDestroyBufferMetal2(id <MTLDevice> __unsafe_unretained device, MTLBUFFER* buffer)
{
delete buffer;
}
//------------------------------------------------------------------------------
void* xxMapBufferMetal2(id <MTLDevice> __unsafe_unretained device, MTLBUFFER* buffer)
{
return [buffer->buffer contents];
}
//------------------------------------------------------------------------------
void xxUnmapBufferMetal2(id <MTLDevice> __unsafe_unretained device, MTLBUFFER* buffer)
{
}
//==============================================================================
// Sampler
//==============================================================================
id xxCreateSamplerMetal2(id <MTLDevice> __unsafe_unretained device, bool clampU, bool clampV, bool clampW, bool linearMag, bool linearMin, bool linearMip, int anisotropy)
{
if (device == nil)
return 0;
MTLSamplerDescriptor* desc = [classMTLSamplerDescriptor new];
desc.sAddressMode = clampU ? MTLSamplerAddressModeClampToEdge : MTLSamplerAddressModeRepeat;
desc.tAddressMode = clampV ? MTLSamplerAddressModeClampToEdge : MTLSamplerAddressModeRepeat;
desc.rAddressMode = clampW ? MTLSamplerAddressModeClampToEdge : MTLSamplerAddressModeRepeat;
desc.magFilter = linearMag ? MTLSamplerMinMagFilterLinear : MTLSamplerMinMagFilterNearest;
desc.minFilter = linearMin ? MTLSamplerMinMagFilterLinear : MTLSamplerMinMagFilterNearest;
desc.mipFilter = linearMip ? MTLSamplerMipFilterLinear : MTLSamplerMipFilterNearest;
desc.maxAnisotropy = anisotropy;
desc.supportArgumentBuffers = YES;
id <MTLSamplerState> sampler = [device newSamplerStateWithDescriptor:desc];
objc_retain(sampler);
return sampler;
}
//==============================================================================
// Shader
//==============================================================================
id xxCreateVertexShaderMetal2(id <MTLDevice> __unsafe_unretained device, char const* shader, MTLVertexDescriptor* __unsafe_unretained vertexAttribute)
{
if (device == nil)
return 0;
if (strcmp(shader, "default") == 0)
{
shader = mtlDefaultShaderCode;
}
NSError* error;
MTLCompileOptions* options = [classMTLCompileOptions new];
options.preprocessorMacros = @{ @"SHADER_MSL" : @(2),
@"SHADER_VERTEX" : @(1) };;
options.fastMathEnabled = YES;
NSMutableString* code = [NSMutableString new];
[code appendString:@"#include <metal_stdlib>"];
[code appendString:@"\n"];
[code appendString:@"using namespace metal;"];
[code appendString:@"\n"];
[code appendString:@(shader)];
id <MTLLibrary> library = [device newLibraryWithSource:code
options:options
error:&error];
if (library == nil)
{
xxLog("xxGraphic", "%s", error.localizedDescription.UTF8String);
return 0;
}
id <MTLFunction> function = [library newFunctionWithName:@"Main"];
objc_retain(function);
return function;
}
//------------------------------------------------------------------------------
id xxCreateFragmentShaderMetal2(id <MTLDevice> __unsafe_unretained device, char const* shader)
{
if (device == nil)
return 0;
if (strcmp(shader, "default") == 0)
{
shader = mtlDefaultShaderCode;
}
NSError* error;
MTLCompileOptions* options = [classMTLCompileOptions new];
options.preprocessorMacros = @{ @"SHADER_MSL" : @(2),
@"SHADER_FRAGMENT" : @(1) };;
options.fastMathEnabled = YES;
NSMutableString* code = [NSMutableString new];
[code appendString:@"#include <metal_stdlib>"];
[code appendString:@"\n"];
[code appendString:@"using namespace metal;"];
[code appendString:@"\n"];
[code appendString:@(shader)];
id <MTLLibrary> library = [device newLibraryWithSource:code
options:options
error:&error];
if (library == nil)
{
xxLog("xxGraphic", "%s", error.localizedDescription.UTF8String);
return 0;
}
id <MTLFunction> function = [library newFunctionWithName:@"Main"];
objc_retain(function);
return function;
}
//==============================================================================
// Pipeline
//==============================================================================
MTLPIPELINE* xxCreatePipelineMetal2(id <MTLDevice> __unsafe_unretained device, MTLRenderPassDescriptor* __unsafe_unretained renderPass, MTLRenderPipelineColorAttachmentDescriptor* __unsafe_unretained blendState, id <MTLDepthStencilState> __unsafe_unretained depthStencilState, uint64_t rasterizerState, MTLVertexDescriptor* __unsafe_unretained vertexAttribute, id <MTLFunction> __unsafe_unretained vertexShader, id <MTLFunction> __unsafe_unretained fragmentShader)
{
MTLPIPELINE* pipeline = xxCreatePipelineMetal(device, renderPass, blendState, depthStencilState, rasterizerState, vertexAttribute, vertexShader, fragmentShader);
if (pipeline == 0)
return 0;
pipeline->vertexShader = vertexShader;
pipeline->fragmentShader = fragmentShader;
pipeline->vertexArgumentEncoder = [pipeline->vertexShader newArgumentEncoderWithBufferIndex:0];
pipeline->fragmentArgumentEncoder = [pipeline->fragmentShader newArgumentEncoderWithBufferIndex:0];
pipeline->vertexArgumentEncodedLength = pipeline->vertexArgumentEncoder.encodedLength;
pipeline->fragmentArgumentEncodedLength = pipeline->fragmentArgumentEncoder.encodedLength;
pipeline->vertexArgumentEncodedLength = (pipeline->vertexArgumentEncodedLength + 255) & ~255;
pipeline->fragmentArgumentEncodedLength = (pipeline->fragmentArgumentEncodedLength + 255) & ~255;
return pipeline;
}
//==============================================================================
// Command
//==============================================================================
void xxSetViewportMetal2(MTLSWAPCHAIN* swapchain, int x, int y, int width, int height, float minZ, float maxZ)
{
xxSetViewportMetal(swapchain->commandEncoder, x, y, width, height, minZ, maxZ);
}
//------------------------------------------------------------------------------
void xxSetScissorMetal2(MTLSWAPCHAIN* swapchain, int x, int y, int width, int height)
{
xxSetScissorMetal(swapchain->commandEncoder, x, y, width, height);
}
//------------------------------------------------------------------------------
void xxSetPipelineMetal2(MTLSWAPCHAIN* swapchain, MTLPIPELINE* pipeline)
{
swapchain->pipeline = pipeline;
xxSetPipelineMetal(swapchain->commandEncoder, pipeline);
}
//------------------------------------------------------------------------------
void xxSetVertexBuffersMetal2(MTLSWAPCHAIN* swapchain, int count, MTLBUFFER** buffers, MTLVertexDescriptor* __unsafe_unretained vertexAttribute)
{
id <MTLBuffer> __unsafe_unretained vertexBuffers[8];
for (int i = 0; i < count; ++i)
{
vertexBuffers[i] = buffers[i]->buffer;
}
xxSetVertexBuffersMetal(swapchain->commandEncoder, count, vertexBuffers, vertexAttribute);
}
//------------------------------------------------------------------------------
void xxSetVertexTexturesMetal2(MTLSWAPCHAIN* swapchain, int count, MTLTEXTURE** textures)
{
id <MTLTexture> __unsafe_unretained vertexTextures[8];
for (int i = 0; i < count; ++i)
{
id <MTLTexture> __unsafe_unretained texture = textures[i]->texture;
vertexTextures[i] = texture;
if (textures[i]->frameCount != swapchain->frameCount)
{
textures[i]->frameCount = swapchain->frameCount;
[swapchain->commandEncoder useResource:texture
usage:MTLResourceUsageSample];
}
}
mtlUpdateArgumentEncoder(swapchain);
[swapchain->vertexArgumentEncoder setTextures:vertexTextures
withRange:NSMakeRange(xxGraphicDescriptor::VERTEX_TEXTURE, count)];
}
//------------------------------------------------------------------------------
void xxSetFragmentTexturesMetal2(MTLSWAPCHAIN* swapchain, int count, MTLTEXTURE** textures)
{
id <MTLTexture> __unsafe_unretained fragmentTextures[8];
for (int i = 0; i < count; ++i)
{
id <MTLTexture> __unsafe_unretained texture = textures[i]->texture;
fragmentTextures[i] = texture;
if (textures[i]->frameCount != swapchain->frameCount)
{
textures[i]->frameCount = swapchain->frameCount;
[swapchain->commandEncoder useResource:texture
usage:MTLResourceUsageSample];
}
}
mtlUpdateArgumentEncoder(swapchain);
[swapchain->fragmentArgumentEncoder setTextures:fragmentTextures
withRange:NSMakeRange(xxGraphicDescriptor::FRAGMENT_TEXTURE, count)];
}
//------------------------------------------------------------------------------
void xxSetVertexSamplersMetal2(MTLSWAPCHAIN* swapchain, int count, id <MTLSamplerState> __unsafe_unretained* samplers)
{
mtlUpdateArgumentEncoder(swapchain);
[swapchain->vertexArgumentEncoder setSamplerStates:samplers
withRange:NSMakeRange(xxGraphicDescriptor::VERTEX_SAMPLER, count)];
}
//------------------------------------------------------------------------------
void xxSetFragmentSamplersMetal2(MTLSWAPCHAIN* swapchain, int count, id <MTLSamplerState> __unsafe_unretained* samplers)
{
mtlUpdateArgumentEncoder(swapchain);
[swapchain->fragmentArgumentEncoder setSamplerStates:samplers
withRange:NSMakeRange(xxGraphicDescriptor::FRAGMENT_SAMPLER, count)];
}
//------------------------------------------------------------------------------
void xxSetVertexConstantBufferMetal2(MTLSWAPCHAIN* swapchain, MTLBUFFER* buffer, int size)
{
if (buffer->frameCount != swapchain->frameCount)
{
buffer->frameCount = swapchain->frameCount;
[swapchain->commandEncoder useResource:buffer->buffer
usage:MTLResourceUsageRead];
}
mtlUpdateArgumentEncoder(swapchain);
[swapchain->vertexArgumentEncoder setBuffer:buffer->buffer
offset:0
atIndex:xxGraphicDescriptor::VERTEX_UNIFORM];
}
//------------------------------------------------------------------------------
void xxSetFragmentConstantBufferMetal2(MTLSWAPCHAIN* swapchain, MTLBUFFER* buffer, int size)
{
if (buffer->frameCount != swapchain->frameCount)
{
buffer->frameCount = swapchain->frameCount;
[swapchain->commandEncoder useResource:buffer->buffer
usage:MTLResourceUsageRead];
}
mtlUpdateArgumentEncoder(swapchain);
[swapchain->fragmentArgumentEncoder setBuffer:buffer->buffer
offset:0
atIndex:xxGraphicDescriptor::FRAGMENT_UNIFORM];
}
//------------------------------------------------------------------------------
void xxDrawMetal2(MTLSWAPCHAIN* swapchain, int vertexCount, int instanceCount, int firstVertex, int firstInstance)
{
swapchain->argumentEncoderComplete = true;
xxDrawMetal(swapchain->commandEncoder, vertexCount, instanceCount, firstVertex, firstInstance);
}
//------------------------------------------------------------------------------
void xxDrawIndexedMetal2(MTLSWAPCHAIN* swapchain, MTLBUFFER* indexBuffer, int indexCount, int instanceCount, int firstIndex, int vertexOffset, int firstInstance)
{
swapchain->argumentEncoderComplete = true;
xxDrawIndexedMetal(swapchain->commandEncoder, indexBuffer->buffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
}
//------------------------------------------------------------------------------