-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoffscreen.c
455 lines (371 loc) · 12.7 KB
/
offscreen.c
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// #define SOKOL_GLCORE33
// #define SOKOL_GLES3
#define SOKOL_GLES2
#define SOKOL_IMPL
#include "main.h"
SDL_Window *window = NULL;
SDL_GLContext gl_ctx;
const int WIDTH = 800;
const int HEIGHT = 600;
int active = 1;
/* a uniform block with a model-view-projection matrix */
typedef struct {
hmm_mat4 mvp;
} params_t;
#if __WINDOWS__
extern int __argc;
extern char **__argv;
int mainReal(int argc, char **argv);
// Based on Denis K response
// See: https://msdn.microsoft.com/library/dn727674.aspx
// This adds Windows specific entrypoint to clasic startpoint of your app:
// int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
return mainReal(__argc, __argv);
}
int mainReal(int argc, char **argv)
#else
int main(int argc, char **argv)
#endif
{
Uint8 *keys;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
#ifdef SOKOL_GLES2
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif
#ifdef SOKOL_GLES3
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif // MOBILE //
#ifdef SOKOL_GLCORE33
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
#endif
SDL_GL_LoadLibrary( NULL );
printf(glGetString(GL_EXTENSIONS) );
window = SDL_CreateWindow(
"Sokol",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WIDTH,
HEIGHT,
//SDL_WINDOW_OPENGL ||
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN
);
/* Create our opengl context and attach it to our window */
gl_ctx = SDL_GL_CreateContext(window);
printf("GL version: %s", glGetString(GL_VERSION) );
/* This makes our buffer swap syncronized with the monitor's vertical refresh */
SDL_GL_SetSwapInterval(-1);
/* Clear our buffer with a red background */
glClearColor ( 1.0, 0.0, 0.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );
/* setup sokol_gfx */
sg_desc desc = {0};
sg_setup(&desc);
assert(sg_isvalid());
/* create one color- and one depth-buffer render target image */
const int offscreen_sample_count = sg_query_feature(SG_FEATURE_MSAA_RENDER_TARGETS) ? 4:1;
sg_image_desc img_desc = {
.render_target = true,
.width = 512,
.height = 512,
//.pixel_format = SG_PIXELFORMAT_RGBA8, //opengl ES
.min_filter = SG_FILTER_LINEAR,
.mag_filter = SG_FILTER_LINEAR,
.sample_count = offscreen_sample_count
};
sg_image color_img = sg_make_image(&img_desc);
img_desc.pixel_format = SG_PIXELFORMAT_DEPTH;
sg_image depth_img = sg_make_image(&img_desc);
/* an offscreen render pass into those images */
sg_pass offscreen_pass = sg_make_pass(&(sg_pass_desc){
.color_attachments[0].image = color_img,
.depth_stencil_attachment.image = depth_img
});
/* pass action for offscreen pass, clearing to black */
sg_pass_action offscreen_pass_action = {
.colors[0] = { .action = SG_ACTION_CLEAR, .val = { 0.0f, 0.0f, 0.0f, 1.0f } }
};
/* pass action for default pass, clearing to blue-ish */
sg_pass_action default_pass_action = {
.colors[0] = { .action = SG_ACTION_CLEAR, .val = { 0.0f, 0.25f, 1.0f, 1.0f } }
};
/* cube vertex buffer with positions, colors and tex coords */
float vertices[] = {
/* pos color uvs */
-1.0f, -1.0f, -1.0f, 1.0f, 0.5f, 0.5f, 1.0f, 0.0f, 0.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f, 0.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f, 1.0f
};
sg_buffer_desc vbuf_desc = {
.size = sizeof(vertices),
.content = vertices,
};
sg_buffer vbuf = sg_make_buffer(&vbuf_desc);
/* an index buffer for the cube */
uint16_t indices[] = {
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
};
sg_buffer_desc ibuf_desc = {
.type = SG_BUFFERTYPE_INDEXBUFFER,
.size = sizeof(indices),
.content = indices,
};
sg_buffer ibuf = sg_make_buffer(&ibuf_desc);
/* shader for the non-textured cube, rendered in the offscreen pass
sg_shader offscreen_shd = sg_make_shader(&(sg_shader_desc){
.vs.uniform_blocks[0] = {
.size = sizeof(params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 }
}
},
.vs.source =
"#version 330\n"
"uniform mat4 mvp;\n"
"in vec4 position;\n"
"in vec4 color0;\n"
"out vec4 color;\n"
"void main() {\n"
" gl_Position = mvp * position;\n"
" color = color0;\n"
"}\n",
.fs.source =
"#version 330\n"
"in vec4 color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = color;\n"
"}\n"
});
*/
/* ...and a second shader for rendering a textured cube in the default pass
sg_shader default_shd = sg_make_shader(&(sg_shader_desc){
.vs.uniform_blocks[0] = {
.size = sizeof(params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 }
}
},
.fs.images[0] = { .name="tex", .type=SG_IMAGETYPE_2D },
.vs.source =
"#version 330\n"
"uniform mat4 mvp;\n"
"in vec4 position;\n"
"in vec4 color0;\n"
"in vec2 texcoord0;\n"
"out vec4 color;\n"
"out vec2 uv;\n"
"void main() {\n"
" gl_Position = mvp * position;\n"
" color = color0;\n"
" uv = texcoord0;\n"
"}\n",
.fs.source =
"#version 330\n"
"uniform sampler2D tex;\n"
"in vec4 color;\n"
"in vec2 uv;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = texture(tex, uv) + color * 0.5;\n"
"}\n"
});
*/
sg_shader offscreen_shd = sg_make_shader(&(sg_shader_desc){
.vs.uniform_blocks[0] = {
.size = sizeof(params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 }
}
},
.vs.source =
"uniform mat4 mvp;\n"
"attribute vec4 position;\n"
"attribute vec4 color0;\n"
"varying vec4 color;\n"
"void main() {\n"
" gl_Position = mvp * position;\n"
" color = color0;\n"
"}\n",
.fs.source =
"precision mediump float;\n"
"varying vec4 color;\n"
"void main() {\n"
" gl_FragColor = color;\n"
"}\n"
});
sg_shader default_shd = sg_make_shader(&(sg_shader_desc){
.vs.uniform_blocks[0] = {
.size = sizeof(params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 }
}
},
.fs.images[0] = { .name="tex", .type=SG_IMAGETYPE_2D },
.vs.source =
"uniform mat4 mvp;\n"
"attribute vec4 position;\n"
"attribute vec4 color0;\n"
"attribute vec2 texcoord0;\n"
"varying vec4 color;\n"
"varying vec2 uv;\n"
"void main() {\n"
" gl_Position = mvp * position;\n"
" color = color0;\n"
" uv = texcoord0;\n"
"}\n",
.fs.source =
"precision mediump float;"
"uniform sampler2D tex;\n"
"varying vec4 color;\n"
"varying vec2 uv;\n"
"void main() {\n"
" gl_FragColor = texture2D(tex, uv) + color * 0.5;\n"
"}\n"
});
/* pipeline object for offscreen rendering, don't need texcoords here */
sg_pipeline offscreen_pip = sg_make_pipeline(&(sg_pipeline_desc){
.vertex_layouts[0] = {
.stride = 36,
.attrs = {
[0] = { .name="position", .offset=0, .format=SG_VERTEXFORMAT_FLOAT3 },
[1] = { .name="color0", .offset=12, .format=SG_VERTEXFORMAT_FLOAT4 }
}
},
.shader = offscreen_shd,
.index_type = SG_INDEXTYPE_UINT16,
.depth_stencil = {
.depth_compare_func = SG_COMPAREFUNC_LESS_EQUAL,
.depth_write_enabled = true
},
.blend.depth_format = SG_PIXELFORMAT_DEPTH,
.rasterizer = {
.cull_mode = SG_CULLMODE_BACK,
.sample_count = offscreen_sample_count
}
});
/* and another pipeline object for the default pass */
sg_pipeline default_pip = sg_make_pipeline(&(sg_pipeline_desc){
.vertex_layouts[0] = {
.stride = 36,
.attrs = {
[0] = { .name="position", .offset=0, .format=SG_VERTEXFORMAT_FLOAT3 },
[1] = { .name="color0", .offset=12, .format=SG_VERTEXFORMAT_FLOAT4 },
[2] = { .name="texcoord0", .offset=28, .format=SG_VERTEXFORMAT_FLOAT2 }
}
},
.shader = default_shd,
.index_type = SG_INDEXTYPE_UINT16,
.depth_stencil = {
.depth_compare_func = SG_COMPAREFUNC_LESS_EQUAL,
.depth_write_enabled = true,
},
.rasterizer.cull_mode = SG_CULLMODE_BACK
});
/* the draw state for offscreen rendering with all the required resources */
sg_draw_state offscreen_ds = {
.pipeline = offscreen_pip,
.vertex_buffers[0] = vbuf,
.index_buffer = ibuf
};
/* and the draw state for the default pass where a textured cube will
rendered, note how the render-target image is used as texture here */
sg_draw_state default_ds = {
.pipeline = default_pip,
.vertex_buffers[0] = vbuf,
.index_buffer = ibuf,
.fs_images[0] = color_img
};
/* view-projection matrix */
hmm_mat4 proj = HMM_Perspective(60.0f, (float)WIDTH/(float)HEIGHT, 0.01f, 10.0f);
hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view);
/* everything ready, on to the draw loop! */
params_t vs_params;
float rx = 0.0f, ry = 0.0f;
SDL_Event sdl_event;
int cur_width, cur_height;
SDL_GetWindowSize(window, &cur_width, &cur_height);
const Uint8 *keyboard_state = SDL_GetKeyboardState(NULL);
/* draw loop */
while(active)
{
while( SDL_PollEvent( &sdl_event ) != 0 )
{
if( sdl_event.type == SDL_QUIT )
active = 0;
}
if ( keyboard_state[SDL_SCANCODE_ESCAPE ] || keyboard_state[SDL_QUIT ] )
{
active = 0;
}
/* prepare the uniform block with the model-view-projection matrix,
we just use the same matrix for the offscreen- and default-pass */
rx += 1.0f; ry += 2.0f;
hmm_mat4 model = HMM_MultiplyMat4(
HMM_Rotate(rx, HMM_Vec3(1.0f, 0.0f, 0.0f)),
HMM_Rotate(ry, HMM_Vec3(0.0f, 1.0f, 0.0f)));
vs_params.mvp = HMM_MultiplyMat4(view_proj, model);
/* offscreen pass, this renders a rotating, untextured cube to the
offscreen render target */
sg_begin_pass(offscreen_pass, &offscreen_pass_action);
sg_apply_draw_state(&offscreen_ds);
sg_apply_uniform_block(SG_SHADERSTAGE_VS, 0, &vs_params, sizeof(vs_params));
sg_draw(0, 36, 1);
sg_end_pass();
/* and the default pass, this renders a textured cube, using the
offscreen render target as texture image */
//int cur_width, cur_height;
//glfwGetFramebufferSize(w, &cur_width, &cur_height);
sg_begin_default_pass(&default_pass_action, cur_width, cur_height);
sg_apply_draw_state(&default_ds);
sg_apply_uniform_block(SG_SHADERSTAGE_VS, 0, &vs_params, sizeof(vs_params));
sg_draw(0, 36, 1);
sg_end_pass();
sg_commit();
SDL_GL_SwapWindow(window);
// glfwSwapBuffers(w);
// glfwPollEvents();
}
/* cleanup */
//glfwTerminate();
sg_shutdown();
SDL_DestroyWindow(window);
SDL_Quit();
}