-
Notifications
You must be signed in to change notification settings - Fork 0
/
deferred_renderer.cpp
582 lines (462 loc) · 22.7 KB
/
deferred_renderer.cpp
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include "deferred_renderer.h"
#include "gameos.hpp"
#include "res_man.h"
#include "renderer.h"
#include "utils/gl_fbo.h"
#include <functional>
bool DeferredRenderer::Init(uint32_t width, uint32_t height)
{
GLuint fbos[5];
glGenFramebuffers(5, fbos);
deferred_fbo_ = fbos[0];
lighting_fbo_ = fbos[1];
forward_fbo_ = fbos[2];
stencil_fbo_ = fbos[3];
downsampled_fbo_ = fbos[4];
// as we downsample make sure we are at least multiple of 2
assert(0 == (height&0x1) && 0 == (width&0x1));
width_ = width;
height_ = height;
uint32_t wh = (height_<<16) | width_;
bool use_stencil = false;
gos_TextureFormat depth_format = use_stencil ? gos_Texture_Depth_Stencil : gos_Texture_Depth;
gos_g_buffer_depth =
gos_NewRenderTarget(depth_format, "g_bufer_depth", wh);
g_buffer_depth = gos_TextureGetNativeId(gos_g_buffer_depth);
gos_g_buffer_albedo =
gos_NewRenderTarget(gos_Texture_RGBA8, "g_buffer_albedo", wh);
g_buffer_albedo = gos_TextureGetNativeId(gos_g_buffer_albedo);
gos_g_buffer_normal =
gos_NewRenderTarget(gos_Texture_RGBA8, "g_buffer_normal", wh);
g_buffer_normal = gos_TextureGetNativeId(gos_g_buffer_normal);
gos_backbuffer =
gos_NewRenderTarget(gos_Texture_RGBA8, "backbuffer", wh);
backbuffer = gos_TextureGetNativeId(gos_backbuffer);
// downsampled buffers (used for particle rendering)
// TODO: move them to particle related code
ds_width_ = width / 2;
ds_height_ = height / 2;
uint32_t ds_wh = (ds_height_<<16) | ds_width_;
gos_downsampled_color =
gos_NewRenderTarget(gos_Texture_RGBA8, "downsampled_color", ds_wh);
downsampled_color = gos_TextureGetNativeId(gos_downsampled_color);
gos_downsampled_depth =
gos_NewRenderTarget(depth_format, "downsampled_depth", ds_wh);
downsampled_depth = gos_TextureGetNativeId(gos_downsampled_depth);
GLuint t[] = {g_buffer_depth, g_buffer_albedo, g_buffer_normal,
backbuffer, downsampled_color, downsampled_depth};
for(GLuint tex: t) {
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
}
smp_nearest_clamp_nomips_ = gos_CreateTextureSampler(
gos_TextureClamp, gos_TextureClamp, gos_TextureClamp, gos_FilterNone,
gos_FilterNone, gos_FilterNone, false);
smp_linear_wrap_nomips_ = gos_CreateTextureSampler(
gos_TextureWrap, gos_TextureWrap, gos_TextureWrap, gos_FilterBiLinear,
gos_FilterBiLinear, gos_FilterNone, false);
smp_linear_clamp_nomips_ = gos_CreateTextureSampler(
gos_TextureClamp, gos_TextureClamp, gos_TextureClamp, gos_FilterBiLinear,
gos_FilterBiLinear, gos_FilterNone, false);
checker_tex_ = gos_NewTextureFromFile(gos_Texture_Detect,
"data/textures/texture_density.tga");
gos_AddRenderMaterial("deferred");
gos_AddRenderMaterial("lightpass");
gos_AddRenderMaterial("pointlightpass");
gos_AddRenderMaterial("null");
gos_AddRenderMaterial("upsample_bilateral");
gos_AddRenderMaterial("copy_depth");
bool status = false;
// setup deferred fbo
glBindFramebuffer(GL_FRAMEBUFFER, deferred_fbo_);
{
GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(sizeof(drawBuffers)/sizeof(drawBuffers[0]), drawBuffers);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_buffer_albedo, 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, g_buffer_normal, 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, g_buffer_depth, 0);
if(use_stencil)
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, g_buffer_depth, 0);
status = checkFramebufferStatus(GL_FRAMEBUFFER);
assert(status);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// setup forward fbo
glBindFramebuffer(GL_FRAMEBUFFER, lighting_fbo_);
{
GLuint drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(sizeof(drawBuffers)/sizeof(drawBuffers[0]), drawBuffers);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, backbuffer, 0);
status = checkFramebufferStatus(GL_FRAMEBUFFER);
assert(status);
}
// setup forward fbo
glBindFramebuffer(GL_FRAMEBUFFER, forward_fbo_);
{
GLuint drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(sizeof(drawBuffers)/sizeof(drawBuffers[0]), drawBuffers);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, backbuffer, 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, g_buffer_depth, 0);
if(use_stencil)
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, g_buffer_depth, 0);
status = checkFramebufferStatus(GL_FRAMEBUFFER);
assert(status);
}
// setup stencil fbo
glBindFramebuffer(GL_FRAMEBUFFER, stencil_fbo_);
{
GLuint drawBuffers[] = { GL_NONE };
glDrawBuffers(sizeof(drawBuffers)/sizeof(drawBuffers[0]), drawBuffers);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, g_buffer_depth, 0);
if(use_stencil)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, g_buffer_depth, 0);
status = checkFramebufferStatus(GL_FRAMEBUFFER);
assert(status);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, downsampled_fbo_);
{
GLuint drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(sizeof(drawBuffers)/sizeof(drawBuffers[0]), drawBuffers);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_TEXTURE_2D, downsampled_color, 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, downsampled_depth, 0);
if(use_stencil)
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, downsampled_depth, 0);
status = checkFramebufferStatus(GL_FRAMEBUFFER);
assert(status);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
CHECK_GL_ERROR;
return true;
}
class DeferredShapeRenderer {
mat4 view_proj_;
mat4 view_;
DWORD missing_tex_;
HGOSTEXTURESAMPLER sampler_;
public:
void setup(const mat4& view, const mat4& proj, DWORD missing_tex, HGOSTEXTURESAMPLER sampler) {
view_proj_ = proj * view;
view_ = view;
missing_tex_ = missing_tex;
sampler_ = sampler;
}
void render(const RenderPacket &rp) {
const RenderMesh& ro = rp.mesh_;
HGOSRENDERMATERIAL mat = gos_getRenderMaterial("deferred");
if (ro.mat_)
mat = ro.mat_;
DWORD texid = ro.tex_id_ ? ro.tex_id_ : missing_tex_;
gos_SetRenderState(gos_State_Texture, texid);
gos_SetSamplerState(0, sampler_);
gos_SetRenderState(gos_State_Culling, ro.two_sided_ ? gos_Cull_None : gos_Cull_CCW);
mat4 wvp = view_proj_ * rp.m_;
mat4 wv = view_ * rp.m_;
gos_SetRenderMaterialParameterMat4(mat, "wvp_", (const float*)wvp);
gos_SetRenderMaterialParameterMat4(mat, "wv_", (const float*)wv);
gos_ApplyRenderMaterial(mat);
if (ro.ib_) {
if (ro.inst_vb_) {
gos_RenderIndexedInstanced(ro.ib_, ro.vb_, ro.inst_vb_, ro.num_instances, ro.vdecl_, ro.prim_type_);
} else {
gos_RenderIndexedArray(ro.ib_, ro.vb_, ro.vdecl_, ro.prim_type_);
}
} else if(ro.inst_vb_) {
gos_RenderArrayInstanced(ro.vb_, ro.inst_vb_, ro.num_instances, ro.vdecl_, ro.prim_type_);
} else {
gos_RenderArray(ro.vb_, ro.vdecl_, ro.prim_type_, ro.vb_first_, ro.vb_count_);
}
gos_SetSamplerState(0, 0);
}
};
void DeferredRenderer::RenderGeometry(struct RenderFrameContext* rfc)
{
const RenderPacketList_t& rpl = rfc->rl_->GetRenderPackets();
RenderPacketList_t::const_iterator it = rpl.begin();
RenderPacketList_t::const_iterator end = rpl.end();
DeferredShapeRenderer r;
r.setup(rfc->view_, rfc->proj_, checker_tex_, smp_linear_wrap_nomips_);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, deferred_fbo_);
gos_SetRenderViewport(0, 0, width_, height_);
glViewport(0, 0, (GLsizei)width_, (GLsizei)height_);
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
gos_SetRenderState(gos_State_StencilEnable, 0);
gos_SetRenderState(gos_State_ZCompare, 1);
gos_SetRenderState(gos_State_ZWrite, true);
gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_OneZero);
gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
for(;it!=end;++it)
{
const RenderPacket& rp = (*it);
if(rp.is_opaque_pass)
r.render(rp);
}
}
void DeferredRenderer::RenderDirectionalLighting(const struct RenderFrameContext* rfc)
{
glBindFramebuffer(GL_FRAMEBUFFER, lighting_fbo_);
gos_SetRenderViewport(0, 0, width_, height_);
glViewport(0, 0, (GLsizei)width_, (GLsizei)height_);
glClear(GL_COLOR_BUFFER_BIT);
gos_SetRenderState(gos_State_Texture, gos_g_buffer_albedo);
gos_SetSamplerState(0, smp_nearest_clamp_nomips_);
gos_SetRenderState(gos_State_Texture2, gos_g_buffer_normal);
gos_SetSamplerState(1, smp_nearest_clamp_nomips_);
gos_SetRenderState(gos_State_Texture3, gos_g_buffer_depth);
gos_SetSamplerState(2, smp_nearest_clamp_nomips_);
gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
gos_SetRenderState(gos_State_ZCompare, false);
gos_SetRenderState(gos_State_ZWrite, false);
vec4 lightdir = -rfc->shadow_view_.getRow(2);
vec4 lightdir_view_space = rfc->view_ * vec4(lightdir.x, lightdir.y, lightdir.z, 0.0f);
HGOSRENDERMATERIAL mat = gos_getRenderMaterial("lightpass");
const float ld_vs[4] = {lightdir_view_space.x, lightdir_view_space.y, lightdir_view_space.z, 0.0f };
gos_SetRenderMaterialParameterFloat4(mat, "lightdir_view_space_", ld_vs);
gos_SetRenderMaterialParameterMat4(mat, "proj_", rfc->proj_);
gos_SetRenderMaterialParameterMat4(mat, "inv_proj_", rfc->inv_proj_);
const float debug_params[] = { 0,0,0,0 };
gos_SetRenderMaterialParameterFloat4(mat, "debug_params_", debug_params);
gos_ApplyRenderMaterial(mat);
RenderMesh* fs_quad = res_man_load_mesh("fs_quad");
gos_RenderIndexedArray(fs_quad->ib_, fs_quad->vb_, fs_quad->vdecl_, fs_quad->prim_type_);
}
void DeferredRenderer::RenderPointLighting(struct RenderFrameContext* rfc)
{
// forward_fbo_ has depth so shold be a bit faster due to depth culling
//glBindFramebuffer(GL_FRAMEBUFFER, lighting_fbo_);
glBindFramebuffer(GL_FRAMEBUFFER, forward_fbo_);
gos_SetRenderViewport(0, 0, width_, height_);
glViewport(0, 0, (GLsizei)width_, (GLsizei)height_);
gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
gos_SetRenderState(gos_State_ZCompare, 3);
//make sure we do not write in depth because we are using it as depth rt
gos_SetRenderState(gos_State_ZWrite, false);
// additive blending
gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_AlphaOne);
gos_SetRenderState(gos_State_Texture, gos_g_buffer_albedo);
gos_SetSamplerState(0, smp_nearest_clamp_nomips_);
gos_SetRenderState(gos_State_Texture2, gos_g_buffer_normal);
gos_SetSamplerState(1, smp_nearest_clamp_nomips_);
gos_SetRenderState(gos_State_Texture3, gos_g_buffer_depth);
gos_SetSamplerState(2, smp_nearest_clamp_nomips_);
HGOSRENDERMATERIAL mat = gos_getRenderMaterial("pointlightpass");
draw_point_lights(rfc, mat);
}
void DeferredRenderer::draw_point_lights(const struct RenderFrameContext* rfc, HGOSRENDERMATERIAL mat)
{
const auto &point_lights = rfc->point_lights_;
const mat4 view_proj = rfc->proj_ * rfc->view_;
const bool is_null_mat = mat == gos_getRenderMaterial("null");
RenderMesh *sphere = res_man_load_mesh("sphere");
for (const PointLight &l : point_lights) {
const mat4 wvp = view_proj * l.transform_;
if (!is_null_mat) {
const mat4 wv = rfc->view_ * l.transform_;
// what is better?
const vec4 viewpos = wv * vec4(0, 0, 0, 1);
// const vec4 viewpos = rfc->view_ * vec4(l.pos.x, l.pos.y, l.pos.z, // 1);
const float viewpos_radius[4] = {viewpos.x, viewpos.y, viewpos.z, l.radius_};
gos_SetRenderMaterialParameterFloat4(mat, "color_", l.color_);
gos_SetRenderMaterialParameterFloat4(mat, "viewpos_radius_", viewpos_radius);
gos_SetRenderMaterialParameterMat4(mat, "wv_", wv);
gos_SetRenderMaterialParameterMat4(mat, "proj_", rfc->proj_);
}
gos_SetRenderMaterialParameterMat4(mat, "wvp_", wvp);
gos_ApplyRenderMaterial(mat);
gos_RenderIndexedArray(sphere->ib_, sphere->vb_, sphere->vdecl_, sphere->prim_type_);
}
}
// fil stencil buffer where point lights are
void DeferredRenderer::stencil_pass(const struct RenderFrameContext* rfc) {
glBindFramebuffer(GL_FRAMEBUFFER, stencil_fbo_);
gos_SetRenderViewport(0, 0, width_, height_);
glViewport(0, 0, (GLsizei)width_, (GLsizei)height_);
// draw back and front faces
gos_SetRenderState(gos_State_Culling, gos_Cull_None);
gos_SetRenderState(gos_State_ZCompare, 1);
gos_SetRenderState(gos_State_ZWrite, false);
// no blending
gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_OneZero);
gos_SetRenderState(gos_State_StencilEnable, 1);
// values for stencil test are not interesting
gos_SetRenderState(gos_State_StencilRef_Front, 0);
gos_SetRenderState(gos_State_StencilRef_Back, 0);
gos_SetRenderState(gos_State_StencilMask_Front, 0);
gos_SetRenderState(gos_State_StencilMask_Back, 0);
gos_SetRenderState(gos_State_StencilFunc_Front, gos_Cmp_Always);
gos_SetRenderState(gos_State_StencilFunc_Back, gos_Cmp_Always);
// for front faces
gos_SetRenderState(gos_State_StencilZFail_Front, gos_Stencil_Decr); // Z fail
gos_SetRenderState(gos_State_StencilPass_Front, gos_Stencil_Keep); // Z pass
// for back faces
gos_SetRenderState(gos_State_StencilZFail_Back, gos_Stencil_Incr); // Z fail
gos_SetRenderState(gos_State_StencilPass_Back, gos_Stencil_Keep); // Z pass
// we always pass stencil test so nothing interesting here
gos_SetRenderState(gos_State_StencilFail_Front, gos_Stencil_Keep);
gos_SetRenderState(gos_State_StencilFail_Back, gos_Stencil_Keep);
glClear(GL_STENCIL_BUFFER_BIT);
HGOSRENDERMATERIAL mat = gos_getRenderMaterial("null");
draw_point_lights(rfc, mat);
}
void DeferredRenderer::RenderPointLighting2(struct RenderFrameContext* rfc)
{
stencil_pass(rfc);
{
glBindFramebuffer(GL_FRAMEBUFFER, forward_fbo_);
gos_SetRenderState(gos_State_ZCompare, 0);
gos_SetRenderState(gos_State_ZWrite, 0);
gos_SetRenderState(gos_State_StencilEnable, 1);
gos_SetRenderState(gos_State_StencilRef_Front, 0);
gos_SetRenderState(gos_State_StencilRef_Back, 0);
gos_SetRenderState(gos_State_StencilMask_Front, 0xff);
gos_SetRenderState(gos_State_StencilMask_Back, 0xff);
gos_SetRenderState(gos_State_StencilFunc_Front, gos_Cmp_NotEqual);
gos_SetRenderState(gos_State_StencilFunc_Back, gos_Cmp_NotEqual);
gos_SetRenderState(gos_State_StencilPass_Front, gos_Stencil_Keep);
gos_SetRenderState(gos_State_StencilPass_Back, gos_Stencil_Keep);
gos_SetRenderState(gos_State_StencilZFail_Front, gos_Stencil_Keep);
gos_SetRenderState(gos_State_StencilZFail_Back, gos_Stencil_Keep);
gos_SetRenderState(gos_State_StencilFail_Front, gos_Stencil_Keep);
gos_SetRenderState(gos_State_StencilFail_Back, gos_Stencil_Keep);
#if defined(DEBUG_POINT_LIGTH_STENCIL)
// draw fullscreen quad only where stencil is marked
gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
HGOSRENDERMATERIAL mat = gos_getRenderMaterial("coloured_quad");
float colour[4] = {1.0f,1.0f,1.0f,1.0f};
gos_SetRenderMaterialParameterFloat4(mat, "colour", colour);
gos_ApplyRenderMaterial(mat);
extern RenderMesh* g_fs_quad;
gos_RenderIndexedArray(g_fs_quad->ib_, g_fs_quad->vb_, g_fs_quad->vdecl_);
#else
// cw/ccw? does it matter for convex in this case?
gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
// additive blending
gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_AlphaOne);
gos_SetRenderState(gos_State_Texture, gos_g_buffer_albedo);
gos_SetRenderState(gos_State_Texture2, gos_g_buffer_normal);
gos_SetRenderState(gos_State_Texture3, gos_g_buffer_depth);
HGOSRENDERMATERIAL mat = gos_getRenderMaterial("pointlightpass");
draw_point_lights(rfc, mat);
#endif
}
gos_SetRenderState(gos_State_StencilEnable, 0);
}
void DeferredRenderer::RenderForward(std::function<void(void)> f)
{
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, forward_fbo_);
gos_SetRenderState(gos_State_Texture, 0);
gos_SetRenderState(gos_State_Texture2, 0);
gos_SetRenderState(gos_State_Texture3, 0);
gos_SetSamplerState(0, 0);
gos_SetSamplerState(1, 0);
gos_SetSamplerState(2, 0);
gos_SetRenderViewport(0, 0, width_, height_);
glViewport(0, 0, (GLsizei)width_, (GLsizei)height_);
gos_SetRenderState(gos_State_ZCompare, 1);
gos_SetRenderState(gos_State_ZWrite, false);
gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_OneZero);
gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
f();
}
void DeferredRenderer::RenderDownsampledForward(std::function<void(void)> f, const mat4& proj)
{
// NEEDED?????
// clear downsampled fbo before rendering into it
// glBindFramebuffer(GL_DRAW_FRAMEBUFFER, downsampled_fbo_);
// gos_SetRenderViewport(0, 0, width_, height_);
// glViewport(0, 0, (GLsizei)width_, (GLsizei)height_);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
//
// downsample depth
downlsampleFboDepth(stencil_fbo_, downsampled_fbo_, width_, height_,
ds_width_, ds_height_);
if(0)
{
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, downsampled_fbo_);
glDrawBuffer(GL_NONE);
gos_SetRenderState(gos_State_StencilEnable, 0);
gos_SetRenderState(gos_State_ZCompare, 0);
gos_SetRenderState(gos_State_ZWrite, 1);
gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_OneZero);
gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
gos_SetRenderState(gos_State_Texture, gos_g_buffer_depth);
gos_SetSamplerState(0, smp_nearest_clamp_nomips_);
HGOSRENDERMATERIAL mat = gos_getRenderMaterial("copy_depth");
gos_ApplyRenderMaterial(mat);
RenderMesh* fs_quad = res_man_load_mesh("fs_quad");
gos_RenderIndexedArray(fs_quad->ib_, fs_quad->vb_,
fs_quad->vdecl_, fs_quad->prim_type_);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
}
CHECK_GL_ERROR;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, downsampled_fbo_);
gos_SetRenderState(gos_State_Texture, 0);
gos_SetRenderState(gos_State_Texture2, 0);
gos_SetRenderState(gos_State_Texture3, 0);
gos_SetSamplerState(0, 0);
gos_SetSamplerState(0, 0);
gos_SetSamplerState(0, 0);
gos_SetRenderViewport(0, 0, ds_width_, ds_height_);
glViewport(0, 0, (GLsizei)ds_width_, (GLsizei)ds_height_);
//glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT); // leave depth!
//glClearColor(0,0,0,0);
gos_SetRenderState(gos_State_StencilEnable, 0);
gos_SetRenderState(gos_State_ZCompare, 1);
gos_SetRenderState(gos_State_ZWrite, false);
gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_OneZero);
gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
f();
// upsample & blend result with main color buffer by drawing quad
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, forward_fbo_);
gos_SetRenderViewport(0, 0, width_, height_);
glViewport(0, 0, (GLsizei)width_, (GLsizei)height_);
gos_SetRenderState(gos_State_StencilEnable, 0);
gos_SetRenderState(gos_State_ZCompare, 1);
gos_SetRenderState(gos_State_ZWrite, false);
//TODO: use separate blending to not overwrite destination alpha if it is used
// dst.rgb = src.rgb + (1.0 - src.a) * dst.rgb
// dst.a = 1.0 * src.rgb + 0.0 * dst.rgb
//glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_OneInvAlpha);
gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
gos_SetRenderState(gos_State_TextureAddress, gos_TextureClamp);
gos_SetRenderState(gos_State_Filter, gos_FilterNone);
gos_SetRenderState(gos_State_Texture, gos_downsampled_color);
gos_SetSamplerState(0, smp_nearest_clamp_nomips_);
gos_SetRenderState(gos_State_Texture2, gos_downsampled_depth);
gos_SetSamplerState(1, smp_nearest_clamp_nomips_);
gos_SetRenderState(gos_State_Texture3, gos_g_buffer_depth);
gos_SetSamplerState(2, smp_nearest_clamp_nomips_);
HGOSRENDERMATERIAL mat = gos_getRenderMaterial("upsample_bilateral");
const float low_res_texture_size[4] = { (float)ds_width_, (float)ds_height_, 0.0f, 0.0f };
const float low_res_texel_size[4] = { 1.0f / (float)ds_width_, 1.0f / (float)ds_height_, 0.0f, 0.0f };
gos_SetRenderMaterialParameterFloat4(mat, "low_res_texture_size_", low_res_texture_size);
gos_SetRenderMaterialParameterFloat4(mat, "low_res_texel_size_", low_res_texel_size);
gos_SetRenderMaterialParameterMat4(mat, "proj_", proj);
gos_ApplyRenderMaterial(mat);
RenderMesh* fs_quad = res_man_load_mesh("fs_quad");
gos_RenderIndexedArray(fs_quad->ib_, fs_quad->vb_, fs_quad->vdecl_, fs_quad->prim_type_);
}
void DeferredRenderer::Present(int w, int h)
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDrawBuffer(GL_BACK);
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
gos_SetRenderViewport(0, 0, w, h);
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
gos_SetRenderState(gos_State_Texture, gos_backbuffer);
gos_SetSamplerState(0, smp_nearest_clamp_nomips_);
gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
gos_SetRenderState(gos_State_ZCompare, false);
gos_SetRenderState(gos_State_ZWrite, 1);
HGOSRENDERMATERIAL mat = gos_getRenderMaterial("textured_quad");
gos_SetRenderMaterialParameterFloat4(mat, "scale_offset", vec4(1,1,0,0));
gos_ApplyRenderMaterial(mat);
RenderMesh* fs_quad = res_man_load_mesh("fs_quad");
gos_RenderIndexedArray(fs_quad->ib_, fs_quad->vb_, fs_quad->vdecl_, fs_quad->prim_type_);
}