Skip to content

Commit 066247b

Browse files
jdolanCopilot
andcommitted
Use loadShader in Hello-iOS; auto-set entrypoints in loadShader
- RenderDevice::loadShader: auto-set entrypoint to 'main0' (MSL) or 'main' (SPIR-V) when caller passes NULL, matching shadercross convention - Hello.c: remove stale 'vs_main'/'fs_main' entrypoints; let loadShader pick the correct values based on format - Hello-iOS.c: replace hard-coded inline MSL strings with loadShader; replace manual matrix math with Mathlib (mat4/vec3); replace manual depth texture management with Framebuffer; use SDL_GetBasePath() as resource path - ObjectivelyGPU.xcodeproj: add Hello.vert.msl + Hello.frag.msl to Hello-iOS Resources build phase Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3369e31 commit 066247b

4 files changed

Lines changed: 47 additions & 142 deletions

File tree

Examples/Hello-iOS/Hello-iOS.c

Lines changed: 39 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,6 @@
2929
#include <Objectively.h>
3030
#include <ObjectivelyGPU.h>
3131

32-
static const char *vertex_shader_msl =
33-
"#include <metal_stdlib>\n"
34-
"using namespace metal;\n"
35-
"\n"
36-
"struct type_UBO {\n"
37-
" mat4 ModelViewProj;\n"
38-
"};\n"
39-
"\n"
40-
"struct main0_out {\n"
41-
" vec4 out_color [[user(locn0)]];\n"
42-
" vec4 gl_Position [[position]];\n"
43-
"};\n"
44-
"\n"
45-
"struct main0_in {\n"
46-
" vec3 in_position [[attribute(0)]];\n"
47-
" vec3 in_color [[attribute(1)]];\n"
48-
"};\n"
49-
"\n"
50-
"vertex main0_out main0(main0_in in [[stage_in]], constant type_UBO& UBO [[buffer(0)]]) {\n"
51-
" main0_out out = {};\n"
52-
" out.out_color = vec4(in.in_color, 1.0);\n"
53-
" out.gl_Position = UBO.ModelViewProj * vec4(in.in_position, 1.0);\n"
54-
" return out;\n"
55-
"}\n";
56-
57-
static const char *fragment_shader_msl =
58-
"#include <metal_stdlib>\n"
59-
"using namespace metal;\n"
60-
"\n"
61-
"struct main0_in {\n"
62-
" vec4 in_color [[user(locn0)]];\n"
63-
"};\n"
64-
"\n"
65-
"fragment vec4 main0(main0_in in [[stage_in]]) {\n"
66-
" return in.in_color;\n"
67-
"}\n";
68-
6932
typedef struct {
7033
float x, y, z;
7134
float r, g, b;
@@ -86,82 +49,18 @@ static const Vertex vertex_data[] = {
8649
{ -0.5f, -0.5f, -0.5f, 0, 1, 0 }, { 0.5f, -0.5f, -0.5f, 0, 0, 1 }, { 0.5f, -0.5f, 0.5f, 1, 0, 1 },
8750
};
8851

89-
static void rotate_matrix(float angle, float x, float y, float z, float *r) {
90-
float radians = angle * SDL_PI_F / 180.0f;
91-
float c = SDL_cosf(radians), s = SDL_sinf(radians), c1 = 1.0f - c;
92-
float length = SDL_sqrtf(x*x + y*y + z*z);
93-
float u[3] = { x/length, y/length, z/length };
94-
for (int i = 0; i < 16; i++) r[i] = 0.0f;
95-
r[15] = 1.0f;
96-
for (int i = 0; i < 3; i++) {
97-
r[i*4 + (i+1)%3] = u[(i+2)%3] * s;
98-
r[i*4 + (i+2)%3] = -u[(i+1)%3] * s;
99-
}
100-
for (int i = 0; i < 3; i++)
101-
for (int j = 0; j < 3; j++)
102-
r[i*4+j] += c1 * u[i] * u[j] + (i == j ? c : 0.0f);
103-
}
104-
105-
static void perspective_matrix(float fovy, float aspect, float znear, float zfar, float *r) {
106-
float f = 1.0f / SDL_tanf((fovy / 180.0f) * SDL_PI_F * 0.5f);
107-
for (int i = 0; i < 16; i++) r[i] = 0.0f;
108-
r[0] = f / aspect; r[5] = f;
109-
r[10] = (znear + zfar) / (znear - zfar); r[11] = -1.0f;
110-
r[14] = (2.0f * znear * zfar) / (znear - zfar);
111-
}
112-
113-
static void multiply_matrix(const float *lhs, const float *rhs, float *r) {
114-
float tmp[16];
115-
for (int i = 0; i < 4; i++)
116-
for (int j = 0; j < 4; j++) {
117-
tmp[j*4+i] = 0.0f;
118-
for (int k = 0; k < 4; k++)
119-
tmp[j*4+i] += lhs[k*4+i] * rhs[j*4+k];
120-
}
121-
for (int i = 0; i < 16; i++) r[i] = tmp[i];
122-
}
123-
12452
typedef struct {
12553
SDL_Window *window;
12654
RenderDevice *renderDevice;
55+
Framebuffer *framebuffer;
12756
SDL_GPUBuffer *vertexBuffer;
128-
SDL_GPUTexture *depthTexture;
12957
SDL_GPUGraphicsPipeline *pipeline;
130-
SDL_Size depthSize;
131-
float angleX, angleY;
58+
vec2 angles;
13259
Uint64 lastTicks;
13360
} App;
13461

13562
static App app;
13663

137-
static SDL_GPUShader *create_shader(SDL_GPUShaderStage stage, Uint32 numUniformBuffers) {
138-
const char *source = stage == SDL_GPU_SHADERSTAGE_VERTEX ? vertex_shader_msl : fragment_shader_msl;
139-
return $(app.renderDevice, createShader, &(SDL_GPUShaderCreateInfo) {
140-
.code_size = SDL_strlen(source),
141-
.code = (const Uint8 *) source,
142-
.entrypoint = "main0",
143-
.format = SDL_GPU_SHADERFORMAT_MSL,
144-
.stage = stage,
145-
.num_uniform_buffers = numUniformBuffers,
146-
});
147-
}
148-
149-
static SDL_GPUTexture *create_depth_texture(SDL_Size size) {
150-
if (size.w <= 0 || size.h <= 0) {
151-
return NULL;
152-
}
153-
return $(app.renderDevice, createTexture, &(SDL_GPUTextureCreateInfo) {
154-
.type = SDL_GPU_TEXTURETYPE_2D,
155-
.format = SDL_GPU_TEXTUREFORMAT_D16_UNORM,
156-
.usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET,
157-
.width = (Uint32) size.w,
158-
.height = (Uint32) size.h,
159-
.layer_count_or_depth = 1,
160-
.num_levels = 1,
161-
.sample_count = SDL_GPU_SAMPLECOUNT_1,
162-
}, NULL);
163-
}
164-
16564
SDL_AppResult SDL_AppInit(void **unused, int argc, char *argv[]) {
16665
(void) unused; (void) argc; (void) argv;
16766

@@ -172,6 +71,11 @@ SDL_AppResult SDL_AppInit(void **unused, int argc, char *argv[]) {
17271

17372
SDL_memset(&app, 0, sizeof(app));
17473

74+
const char *basePath = SDL_GetBasePath();
75+
if (basePath) {
76+
$$(Resource, addResourcePath, basePath);
77+
}
78+
17579
app.window = SDL_CreateWindow("ObjectivelyGPU Hello",
17680
0, 0, SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_FULLSCREEN);
17781
if (!app.window) {
@@ -181,6 +85,13 @@ SDL_AppResult SDL_AppInit(void **unused, int argc, char *argv[]) {
18185

18286
app.renderDevice = $(alloc(RenderDevice), initWithWindow, app.window);
18387

88+
int w = 0, h = 0;
89+
SDL_GetWindowSizeInPixels(app.window, &w, &h);
90+
app.framebuffer = $(alloc(Framebuffer), initWithDevice, app.renderDevice,
91+
&MakeSize(w, h),
92+
SDL_GPU_TEXTUREFORMAT_INVALID,
93+
SDL_GPU_TEXTUREFORMAT_D16_UNORM);
94+
18495
app.vertexBuffer = $(app.renderDevice, createBuffer, &(SDL_GPUBufferCreateInfo) {
18596
.usage = SDL_GPU_BUFFERUSAGE_VERTEX,
18697
.size = sizeof(vertex_data),
@@ -205,13 +116,13 @@ SDL_AppResult SDL_AppInit(void **unused, int argc, char *argv[]) {
205116
release(cmd);
206117
$(app.renderDevice, releaseTransferBuffer, transferBuffer);
207118

208-
int w = 0, h = 0;
209-
SDL_GetWindowSizeInPixels(app.window, &w, &h);
210-
app.depthSize = MakeSize(w, h);
211-
app.depthTexture = create_depth_texture(app.depthSize);
212-
213-
SDL_GPUShader *vertexShader = create_shader(SDL_GPU_SHADERSTAGE_VERTEX, 1);
214-
SDL_GPUShader *fragmentShader = create_shader(SDL_GPU_SHADERSTAGE_FRAGMENT, 0);
119+
SDL_GPUShader *vertexShader = $(app.renderDevice, loadShader, "Hello.vert", &(SDL_GPUShaderCreateInfo) {
120+
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
121+
.num_uniform_buffers = 1,
122+
});
123+
SDL_GPUShader *fragmentShader = $(app.renderDevice, loadShader, "Hello.frag", &(SDL_GPUShaderCreateInfo) {
124+
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
125+
});
215126

216127
SDL_GPUColorTargetDescription colorTarget = {
217128
.format = $(app.renderDevice, getSwapchainTextureFormat, app.window),
@@ -269,54 +180,42 @@ SDL_AppResult SDL_AppIterate(void *unused) {
269180
float dt = (float)(ticks - app.lastTicks) / 1000.0f;
270181
app.lastTicks = ticks;
271182

272-
app.angleX += dt * 30.0f;
273-
app.angleY += dt * 60.0f;
274-
while (app.angleX >= 360.0f) app.angleX -= 360.0f;
275-
while (app.angleY >= 360.0f) app.angleY -= 360.0f;
183+
app.angles.x += dt * 30.0f;
184+
app.angles.y += dt * 60.0f;
185+
while (app.angles.x >= 360.0f) app.angles.x -= 360.0f;
186+
while (app.angles.y >= 360.0f) app.angles.y -= 360.0f;
276187

277188
CommandBuffer *cmd = $(app.renderDevice, acquireCommandBuffer);
278189
SwapchainTexture swapchain = { 0 };
279-
if (!$(app.renderDevice, acquireSwapchainTexture, cmd, &swapchain)) {
190+
if (!$(cmd, acquireSwapchainTexture, &swapchain)) {
280191
$(cmd, cancel);
281192
release(cmd);
282193
return SDL_APP_CONTINUE;
283194
}
284195

285-
if (app.depthTexture == NULL ||
286-
swapchain.size.w != app.depthSize.w || swapchain.size.h != app.depthSize.h) {
287-
app.depthSize = swapchain.size;
288-
$(app.renderDevice, releaseTexture, app.depthTexture);
289-
app.depthTexture = create_depth_texture(app.depthSize);
290-
}
196+
$(app.framebuffer, resize, &swapchain.size);
291197

292-
float matModelView[16], matRotate[16], matProj[16], matFinal[16];
293-
rotate_matrix(app.angleX, 1, 0, 0, matModelView);
294-
rotate_matrix(app.angleY, 0, 1, 0, matRotate);
295-
multiply_matrix(matRotate, matModelView, matModelView);
296-
matModelView[14] -= 2.5f;
297-
perspective_matrix(45.0f, (float) swapchain.size.w / (float) swapchain.size.h,
298-
0.01f, 100.0f, matProj);
299-
multiply_matrix(matProj, matModelView, matFinal);
198+
mat4 modelView = mat4_rotation(app.angles.x, vec3_new(1.f, 0.f, 0.f));
199+
modelView = mat4_mul(mat4_rotation(app.angles.y, vec3_new(0.f, 1.f, 0.f)), modelView);
200+
modelView = mat4_mul(mat4_translation(vec3_new(0.f, 0.f, -2.5f)), modelView);
201+
202+
const mat4 projection = mat4_perspective(45.f,
203+
(float) swapchain.size.w / (float) swapchain.size.h, 0.01f, 100.f);
204+
const mat4 modelViewProjection = mat4_mul(projection, modelView);
300205

301206
SDL_GPUColorTargetInfo colorInfo = {
302207
.texture = swapchain.texture,
303208
.clear_color = { 0.1f, 0.1f, 0.2f, 1.0f },
304209
.load_op = SDL_GPU_LOADOP_CLEAR,
305210
.store_op = SDL_GPU_STOREOP_STORE,
306211
};
307-
SDL_GPUDepthStencilTargetInfo depthInfo = {
308-
.texture = app.depthTexture,
309-
.clear_depth = 1.0f,
310-
.load_op = SDL_GPU_LOADOP_CLEAR,
311-
.store_op = SDL_GPU_STOREOP_DONT_CARE,
312-
.stencil_load_op = SDL_GPU_LOADOP_DONT_CARE,
313-
.stencil_store_op = SDL_GPU_STOREOP_DONT_CARE,
314-
};
212+
SDL_GPUDepthStencilTargetInfo depthInfo = $(app.framebuffer, depthTargetInfo,
213+
SDL_GPU_LOADOP_CLEAR, SDL_GPU_STOREOP_DONT_CARE, 1.f);
315214

316215
RenderPass *renderPass = $(cmd, beginRenderPass, &colorInfo, 1, &depthInfo);
317216
$(renderPass, bindPipeline, app.pipeline);
318217
$(renderPass, bindVertexBuffers, 0, &(SDL_GPUBufferBinding){ .buffer = app.vertexBuffer }, 1);
319-
$(cmd, pushVertexUniformData, 0, matFinal, sizeof(matFinal));
218+
$(cmd, pushVertexUniformData, 0, modelViewProjection.f, sizeof(modelViewProjection));
320219
$(renderPass, drawPrimitives, (Uint32) SDL_arraysize(vertex_data), 1, 0, 0);
321220
release(renderPass);
322221

@@ -346,8 +245,8 @@ void SDL_AppQuit(void *unused, SDL_AppResult result) {
346245
if (app.vertexBuffer) {
347246
$(app.renderDevice, releaseBuffer, app.vertexBuffer);
348247
}
349-
if (app.depthTexture) {
350-
$(app.renderDevice, releaseTexture, app.depthTexture);
248+
if (app.framebuffer) {
249+
release(app.framebuffer);
351250
}
352251
if (app.renderDevice) {
353252
release(app.renderDevice);

Examples/Hello.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,11 @@ int main(int argc, char **argv) {
138138
SDL_GPU_TEXTUREFORMAT_D16_UNORM);
139139

140140
SDL_GPUShader *vertexShader = $(renderDevice, loadShader, "Hello.vert", &(SDL_GPUShaderCreateInfo) {
141-
.entrypoint = "vs_main",
142141
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
143142
.num_uniform_buffers = 1,
144143
});
145144

146145
SDL_GPUShader *fragmentShader = $(renderDevice, loadShader, "Hello.frag", &(SDL_GPUShaderCreateInfo) {
147-
.entrypoint = "fs_main",
148146
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
149147
});
150148

ObjectivelyGPU.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
CECF83B72FEF765400CB0CCA /* Hello.frag.spv in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83A62FEF712F00CB0CCA /* Hello.frag.spv */; };
5252
CECF83B82FEF765400CB0CCA /* Hello.vert.glsl in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83A72FEF712F00CB0CCA /* Hello.vert.glsl */; };
5353
CECF83B92FEF765400CB0CCA /* Hello.vert.msl in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83A82FEF712F00CB0CCA /* Hello.vert.msl */; };
54+
EI00200000000000000000B1 /* Hello.frag.msl in Resources */ = {isa = PBXBuildFile; fileRef = CECF83A52FEF712F00CB0CCA /* Hello.frag.msl */; };
55+
EI00300000000000000000B1 /* Hello.vert.msl in Resources */ = {isa = PBXBuildFile; fileRef = CECF83A82FEF712F00CB0CCA /* Hello.vert.msl */; };
5456
CECF83BA2FEF765400CB0CCA /* Hello.vert.spv in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83A92FEF712F00CB0CCA /* Hello.vert.spv */; };
5557
CECF83BB2FEF765400CB0CCA /* HelloCompute.c in CopyFiles */ = {isa = PBXBuildFile; fileRef = EE00020000000000000000B2 /* HelloCompute.c */; };
5658
CECF83BC2FEF765400CB0CCA /* HelloCompute.comp.glsl in CopyFiles */ = {isa = PBXBuildFile; fileRef = CECF83AA2FEF712F00CB0CCA /* HelloCompute.comp.glsl */; };
@@ -514,6 +516,8 @@
514516
isa = PBXResourcesBuildPhase;
515517
buildActionMask = 2147483647;
516518
files = (
519+
EI00200000000000000000B1 /* Hello.frag.msl in Resources */,
520+
EI00300000000000000000B1 /* Hello.vert.msl in Resources */,
517521
);
518522
runOnlyForDeploymentPostprocessing = 0;
519523
};

Sources/ObjectivelyGPU/RenderDevice.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,10 @@ static SDL_GPUShader *loadShader(const RenderDevice *self, const char *name, con
443443
filled.code_size = res->data->length;
444444
filled.format = formats[i].format;
445445

446+
if (!filled.entrypoint) {
447+
filled.entrypoint = (formats[i].format == SDL_GPU_SHADERFORMAT_MSL) ? "main0" : "main";
448+
}
449+
446450
SDL_GPUShader *shader = $(self, createShader, &filled);
447451
release(res);
448452
return shader;

0 commit comments

Comments
 (0)