forked from GeertArien/learnopengl-examples
-
Notifications
You must be signed in to change notification settings - Fork 4
/
4-sharpen.c
320 lines (269 loc) · 10.9 KB
/
4-sharpen.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
//------------------------------------------------------------------------------
// Framebuffers (4)
//------------------------------------------------------------------------------
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_helper.h"
#include "HandmadeMath.h"
#include "4-sharpen.glsl.h"
#define LOPGL_APP_IMPL
#include "../lopgl_app.h"
/* application state */
static struct {
struct {
sg_attachments attachment;
sg_attachments_desc attachment_desc;
sg_pass_action pass_action;
sg_pipeline pip;
sg_bindings bind_cube;
sg_bindings bind_plane;
} offscreen;
struct {
sg_pass_action pass_action;
sg_pipeline pip;
sg_bindings bind;
} display;
uint8_t file_buffer[2 * 1024 * 1024];
} state;
static void fail_callback() {
state.display.pass_action = (sg_pass_action) {
.colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value = { 1.0f, 0.0f, 0.0f, 1.0f } }
};
}
/* called initially and when window size changes */
void create_offscreen_pass(int width, int height) {
/* destroy previous resource (can be called for invalid id) */
sg_destroy_attachments(state.offscreen.attachment);
sg_destroy_image(state.offscreen.attachment_desc.colors[0].image);
sg_destroy_image(state.offscreen.attachment_desc.depth_stencil.image);
/* create offscreen rendertarget images and pass */
sg_sampler_desc color_smp_desc = {
/* Webgl 1.0 does not support repeat for textures that are not a power of two in size */
.min_filter = SG_FILTER_LINEAR,
.mag_filter = SG_FILTER_LINEAR,
.wrap_u = SG_WRAP_CLAMP_TO_EDGE,
.wrap_v = SG_WRAP_CLAMP_TO_EDGE,
.compare = SG_COMPAREFUNC_NEVER,
};
sg_image_desc color_img_desc = {
.render_target = true,
.width = width,
.height = height,
.pixel_format = SG_PIXELFORMAT_RGBA8,
.label = "color-image"
};
sg_image color_img = sg_make_image(&color_img_desc);
sg_sampler color_smp = sg_make_sampler(&color_smp_desc);
sg_image_desc depth_img_desc = color_img_desc;
depth_img_desc.pixel_format = SG_PIXELFORMAT_DEPTH;
depth_img_desc.label = "depth-image";
sg_image depth_img = sg_make_image(&depth_img_desc);
state.offscreen.attachment_desc = (sg_attachments_desc){
.colors[0].image = color_img,
.depth_stencil.image = depth_img,
.label = "offscreen-pass"
};
state.offscreen.attachment = sg_make_attachments(&state.offscreen.attachment_desc);
/* also need to update the fullscreen-quad texture bindings */
state.display.bind.fs.images[SLOT__diffuse_texture] = color_img;
state.display.bind.fs.samplers[SLOT_diffuse_texture_smp] = color_smp;
}
static void init(void) {
lopgl_setup();
/* a render pass with one color- and one depth-attachment image */
create_offscreen_pass(sapp_width(), sapp_height());
/* a pass action to clear offscreen framebuffer */
state.offscreen.pass_action = (sg_pass_action) {
.colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.1f, 0.1f, 0.1f, 1.0f} }
};
/* a pass action for rendering the fullscreen-quad */
state.display.pass_action = (sg_pass_action) {
.colors[0].load_action=SG_LOADACTION_DONTCARE,
.depth.load_action=SG_LOADACTION_DONTCARE,
.stencil.load_action=SG_LOADACTION_DONTCARE
};
float cube_vertices[] = {
// positions // texture Coords
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
sg_buffer cube_buffer = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(cube_vertices),
.data = SG_RANGE(cube_vertices),
.label = "cube-vertices"
});
float plane_vertices[] = {
// positions // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat)
5.0f, -0.5f, 5.0f, 2.0f, 0.0f,
-5.0f, -0.5f, 5.0f, 0.0f, 0.0f,
-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,
5.0f, -0.5f, 5.0f, 2.0f, 0.0f,
-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,
5.0f, -0.5f, -5.0f, 2.0f, 2.0f
};
sg_buffer plane_buffer = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(plane_vertices),
.data = SG_RANGE(plane_vertices),
.label = "plane-vertices"
});
float quad_vertices[] = { // vertex attributes for a quad that fills the entire screen in Normalized Device Coordinates.
// positions // texCoords
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
sg_buffer quad_buffer = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(quad_vertices),
.data = SG_RANGE(quad_vertices),
.label = "quad-vertices"
});
state.offscreen.bind_cube.vertex_buffers[0] = cube_buffer;
state.offscreen.bind_plane.vertex_buffers[0] = plane_buffer;
/* resource bindings to render an fullscreen-quad */
state.display.bind.vertex_buffers[0] = quad_buffer;
/* create a pipeline object for offscreen pass */
state.offscreen.pip = sg_make_pipeline(&(sg_pipeline_desc){
.shader = sg_make_shader(offscreen_shader_desc(sg_query_backend())),
.layout = {
.attrs = {
[ATTR_vs_offscreen_a_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_offscreen_a_tex_coords].format = SG_VERTEXFORMAT_FLOAT2
}
},
.depth = {
.compare =SG_COMPAREFUNC_LESS,
.write_enabled =true,
.pixel_format =SG_PIXELFORMAT_DEPTH,
},
.colors[0] = {
.pixel_format = SG_PIXELFORMAT_RGBA8,
},
.color_count =1,
.label = "offscreen-pipeline"
});
/* and another pipeline-state-object for the display pass */
state.display.pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[ATTR_vs_display_a_pos].format = SG_VERTEXFORMAT_FLOAT2,
[ATTR_vs_display_a_tex_coords].format = SG_VERTEXFORMAT_FLOAT2
}
},
.shader = sg_make_shader(display_shader_desc(sg_query_backend())),
.label = "display-pipeline"
});
sg_alloc_image_smp(state.offscreen.bind_cube.fs, SLOT__diffuse_texture, SLOT_diffuse_texture_smp);
sg_alloc_image_smp(state.offscreen.bind_plane.fs, SLOT__diffuse_texture, SLOT_diffuse_texture_smp);
sg_image container_img_id = state.offscreen.bind_cube.fs.images[SLOT__diffuse_texture];
sg_image metal_img_id = state.offscreen.bind_plane.fs.images[SLOT__diffuse_texture];
lopgl_load_image(&(lopgl_image_request_t){
.path = "metal.png",
.img_id = metal_img_id,
.buffer_ptr = state.file_buffer,
.buffer_size = sizeof(state.file_buffer),
.fail_callback = fail_callback
});
lopgl_load_image(&(lopgl_image_request_t){
.path = "container.jpg",
.img_id = container_img_id,
.buffer_ptr = state.file_buffer,
.buffer_size = sizeof(state.file_buffer),
.fail_callback = fail_callback
});
}
void frame(void) {
lopgl_update();
HMM_Mat4 view = lopgl_view_matrix();
HMM_Mat4 projection = HMM_Perspective_RH_NO(lopgl_fov(), (float)sapp_width() / (float)sapp_height(), 0.1f, 100.0f);
vs_params_t vs_params = {
.view = view,
.projection = projection
};
/* the offscreen pass, rendering an rotating, untextured cube into a render target image */
sg_begin_pass(&(sg_pass){ .action = state.offscreen.pass_action, .attachments = state.offscreen.attachment });
sg_apply_pipeline(state.offscreen.pip);
sg_apply_bindings(&state.offscreen.bind_cube);
vs_params.model = HMM_Translate(HMM_V3(-1.0f, 0.0f, -1.0f));
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
vs_params.model = HMM_Translate(HMM_V3(2.0f, 0.0f, 0.0f));
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
sg_apply_bindings(&state.offscreen.bind_plane);
vs_params.model = HMM_M4D(1.0f);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_params, &SG_RANGE(vs_params));
sg_draw(0, 6, 1);
sg_end_pass();
/* and the display-pass, rendering a quad, using the previously rendered
offscreen render-target as texture */
sg_begin_pass(&(sg_pass){ .action = state.display.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.display.pip);
sg_apply_bindings(&state.display.bind);
/* offset should scale with dimensions of offscreen framebuffer */
fs_params_t fs_params = {
.offset = HMM_V2(2.f / sapp_width(), 2.f / sapp_height())
};
sg_apply_uniforms(SG_SHADERSTAGE_FS, SLOT_fs_params, &SG_RANGE(fs_params));
sg_draw(0, 6, 1);
lopgl_render_help();
sg_end_pass();
sg_commit();
}
void event(const sapp_event* e) {
if (e->type == SAPP_EVENTTYPE_RESIZED) {
create_offscreen_pass(e->framebuffer_width, e->framebuffer_height);
}
lopgl_handle_input(e);
}
void cleanup(void) {
lopgl_shutdown();
}
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc){
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = event,
.width = 800,
.height = 600,
.high_dpi = true,
.window_title = "Sharpen (LearnOpenGL)",
};
}