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
|
/*
* Copyright 2011-2016 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "common.h"
#include "bgfx_utils.h"
#include "imgui/imgui.h"
struct PosColorVertex
{
float m_x;
float m_y;
float m_z;
uint32_t m_abgr;
static void init()
{
ms_decl
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
}
static bgfx::VertexDecl ms_decl;
};
bgfx::VertexDecl PosColorVertex::ms_decl;
struct PosColorTexCoord0Vertex
{
float m_x;
float m_y;
float m_z;
uint32_t m_rgba;
float m_u;
float m_v;
static void init()
{
ms_decl
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.end();
}
static bgfx::VertexDecl ms_decl;
};
bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
static PosColorVertex s_cubeVertices[8] =
{
{-1.0f, 1.0f, 1.0f, 0xff000000 },
{ 1.0f, 1.0f, 1.0f, 0xff0000ff },
{-1.0f, -1.0f, 1.0f, 0xff00ff00 },
{ 1.0f, -1.0f, 1.0f, 0xff00ffff },
{-1.0f, 1.0f, -1.0f, 0xffff0000 },
{ 1.0f, 1.0f, -1.0f, 0xffff00ff },
{-1.0f, -1.0f, -1.0f, 0xffffff00 },
{ 1.0f, -1.0f, -1.0f, 0xffffffff },
};
static const uint16_t s_cubeIndices[36] =
{
0, 1, 2, // 0
1, 3, 2,
4, 6, 5, // 2
5, 6, 7,
0, 2, 4, // 4
4, 2, 6,
1, 5, 3, // 6
5, 7, 3,
0, 4, 1, // 8
4, 5, 1,
2, 3, 6, // 10
6, 3, 7,
};
static float s_texelHalf = 0.0f;
static bool s_flipV = false;
inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
{
bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_flipV);
}
void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBottomLeft = false, float _width = 1.0f, float _height = 1.0f)
{
if (bgfx::checkAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_decl) )
{
bgfx::TransientVertexBuffer vb;
bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_decl);
PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data;
const float zz = 0.0f;
const float minx = -_width;
const float maxx = _width;
const float miny = 0.0f;
const float maxy = _height*2.0f;
const float texelHalfW = s_texelHalf/_textureWidth;
const float texelHalfH = s_texelHalf/_textureHeight;
const float minu = -1.0f + texelHalfW;
const float maxu = 1.0f + texelHalfW;
float minv = texelHalfH;
float maxv = 2.0f + texelHalfH;
if (_originBottomLeft)
{
float tmp = minv;
minv = maxv;
maxv = tmp;
minv -= 1.0f;
maxv -= 1.0f;
}
vertex[0].m_x = minx;
vertex[0].m_y = miny;
vertex[0].m_z = zz;
vertex[0].m_rgba = 0xffffffff;
vertex[0].m_u = minu;
vertex[0].m_v = minv;
vertex[1].m_x = maxx;
vertex[1].m_y = miny;
vertex[1].m_z = zz;
vertex[1].m_rgba = 0xffffffff;
vertex[1].m_u = maxu;
vertex[1].m_v = minv;
vertex[2].m_x = maxx;
vertex[2].m_y = maxy;
vertex[2].m_z = zz;
vertex[2].m_rgba = 0xffffffff;
vertex[2].m_u = maxu;
vertex[2].m_v = maxv;
bgfx::setVertexBuffer(&vb);
}
}
int _main_(int _argc, char** _argv)
{
Args args(_argc, _argv);
uint32_t width = 1280;
uint32_t height = 720;
uint32_t debug = BGFX_DEBUG_TEXT;
uint32_t reset = BGFX_RESET_VSYNC;
bgfx::init(args.m_type, args.m_pciId);
bgfx::reset(width, height, reset);
// Create vertex stream declaration.
PosColorVertex::init();
PosColorTexCoord0Vertex::init();
// Enable debug text.
bgfx::setDebug(debug);
// Get renderer capabilities info.
const bgfx::Caps* caps = bgfx::getCaps();
// Setup root path for binary shaders. Shader binaries are different
// for each renderer.
switch (caps->rendererType)
{
default:
break;
case bgfx::RendererType::OpenGL:
case bgfx::RendererType::OpenGLES:
s_flipV = true;
break;
}
// Imgui.
imguiCreate();
const bgfx::Memory* mem;
// Create static vertex buffer.
mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, PosColorVertex::ms_decl);
// Create static index buffer.
mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
// Create texture sampler uniforms.
bgfx::UniformHandle s_texColor0 = bgfx::createUniform("s_texColor0", bgfx::UniformType::Int1);
bgfx::UniformHandle s_texColor1 = bgfx::createUniform("s_texColor1", bgfx::UniformType::Int1);
bgfx::UniformHandle u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4);
bgfx::ProgramHandle blend = loadProgram("vs_oit", "fs_oit" );
bgfx::ProgramHandle wbSeparatePass = loadProgram("vs_oit", "fs_oit_wb_separate" );
bgfx::ProgramHandle wbSeparateBlit = loadProgram("vs_oit_blit", "fs_oit_wb_separate_blit" );
bgfx::ProgramHandle wbPass = loadProgram("vs_oit", "fs_oit_wb" );
bgfx::ProgramHandle wbBlit = loadProgram("vs_oit_blit", "fs_oit_wb_blit" );
bgfx::TextureHandle fbtextures[2] = { BGFX_INVALID_HANDLE, BGFX_INVALID_HANDLE };
bgfx::FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
int64_t timeOffset = bx::getHPCounter();
uint32_t mode = 1;
int32_t scrollArea = 0;
bool frontToBack = true;
bool fadeInOut = false;
uint32_t oldWidth = 0;
uint32_t oldHeight = 0;
uint32_t oldReset = reset;
entry::MouseState mouseState;
while (!entry::processEvents(width, height, debug, reset, &mouseState) )
{
if (oldWidth != width
|| oldHeight != height
|| oldReset != reset
|| !bgfx::isValid(fbh) )
{
// Recreate variable size render targets when resolution changes.
oldWidth = width;
oldHeight = height;
oldReset = reset;
if (bgfx::isValid(fbh) )
{
bgfx::destroyFrameBuffer(fbh);
}
fbtextures[0] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_RT);
fbtextures[1] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::R16F, BGFX_TEXTURE_RT);
fbh = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
}
imguiBeginFrame(mouseState.m_mx
, mouseState.m_my
, (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
| (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
| (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
, mouseState.m_mz
, width
, height
);
imguiBeginScrollArea("Settings", width - width / 4 - 10, 10, width / 4, height / 3, &scrollArea);
imguiSeparatorLine();
imguiLabel("Blend mode:");
mode = imguiChoose(mode
, "None"
, "Separate"
, "MRT Independent"
);
imguiSeparatorLine();
if (imguiCheck("Front to back", frontToBack) )
{
frontToBack ^= true;
}
if (imguiCheck("Fade in/out", fadeInOut) )
{
fadeInOut ^= true;
}
imguiEndScrollArea();
imguiEndFrame();
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);
bgfx::setViewRect(1, 0, 0, width, height);
int64_t now = bx::getHPCounter();
static int64_t last = now;
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
const double toMs = 1000.0/freq;
float time = (float)( (now-timeOffset)/freq);
// Use debug font to print information about this example.
bgfx::dbgTextClear();
// Reference:
// Weighted, Blended Order-Independent Transparency
// http://jcgt.org/published/0002/02/09/
// http://casual-effects.blogspot.com/2014/03/weighted-blended-order-independent.html
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/19-oit");
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Weighted, Blended Order Independent Transparency.");
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
float at[3] = { 0.0f, 0.0f, 0.0f };
float eye[3] = { 0.0f, 0.0f, -7.0f };
float view[16];
float proj[16];
// Set view and projection matrix for view 0.
bx::mtxLookAt(view, eye, at);
mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
bgfx::setViewTransform(0, view, proj);
// Set palette color for index 0
bgfx::setPaletteColor(0, 0.0f, 0.0f, 0.0f, 0.0f);
// Set palette color for index 1
bgfx::setPaletteColor(1, 1.0f, 1.0f, 1.0f, 1.0f);
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 1.0f // Depth
, 0 // Stencil
, 0 // FB texture 0, color palette 0
, 1 == mode ? 1 : 0 // FB texture 1, color palette 1
);
bgfx::setViewClear(1
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 1.0f // Depth
, 0 // Stencil
, 0 // Color palette 0
);
bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE;
bgfx::setViewFrameBuffer(0, 0 == mode ? invalid : fbh);
// Set view and projection matrix for view 1.
bx::mtxIdentity(view);
bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
bgfx::setViewTransform(1, view, proj);
for (uint32_t depth = 0; depth < 3; ++depth)
{
uint32_t zz = frontToBack ? 2-depth : depth;
for (uint32_t yy = 0; yy < 3; ++yy)
{
for (uint32_t xx = 0; xx < 3; ++xx)
{
float color[4] = { xx*1.0f/3.0f, zz*1.0f/3.0f, yy*1.0f/3.0f, 0.5f };
if (fadeInOut
&& zz == 1)
{
color[3] = sinf(time*3.0f)*0.49f+0.5f;
}
bgfx::setUniform(u_color, color);
BX_UNUSED(time);
float mtx[16];
bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
//mtxIdentity(mtx);
mtx[12] = -2.5f + float(xx)*2.5f;
mtx[13] = -2.5f + float(yy)*2.5f;
mtx[14] = -2.5f + float(zz)*2.5f;
// Set transform for draw call.
bgfx::setTransform(mtx);
// Set vertex and index buffer.
bgfx::setVertexBuffer(vbh);
bgfx::setIndexBuffer(ibh);
const uint64_t state = 0
| BGFX_STATE_CULL_CW
| BGFX_STATE_RGB_WRITE
| BGFX_STATE_ALPHA_WRITE
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_MSAA
;
bgfx::ProgramHandle program = BGFX_INVALID_HANDLE;
switch (mode)
{
case 0:
// Set vertex and fragment shaders.
program = blend;
// Set render states.
bgfx::setState(state
| BGFX_STATE_BLEND_ALPHA
);
break;
case 1:
// Set vertex and fragment shaders.
program = wbSeparatePass;
// Set render states.
bgfx::setState(state
| BGFX_STATE_BLEND_FUNC_SEPARATE(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_INV_SRC_ALPHA)
);
break;
default:
// Set vertex and fragment shaders.
program = wbPass;
// Set render states.
bgfx::setState(state
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE)
| BGFX_STATE_BLEND_INDEPENDENT
, 0
| BGFX_STATE_BLEND_FUNC_RT_1(BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_SRC_COLOR)
);
break;
}
// Submit primitive for rendering to view 0.
bgfx::submit(0, program);
}
}
}
if (0 != mode)
{
bgfx::setTexture(0, s_texColor0, fbtextures[0]);
bgfx::setTexture(1, s_texColor1, fbtextures[1]);
bgfx::setState(0
| BGFX_STATE_RGB_WRITE
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_INV_SRC_ALPHA, BGFX_STATE_BLEND_SRC_ALPHA)
);
screenSpaceQuad( (float)width, (float)height, s_flipV);
bgfx::submit(1
, 1 == mode ? wbSeparateBlit : wbBlit
);
}
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
}
// Cleanup.
imguiDestroy();
bgfx::destroyFrameBuffer(fbh);
bgfx::destroyIndexBuffer(ibh);
bgfx::destroyVertexBuffer(vbh);
bgfx::destroyProgram(blend);
bgfx::destroyProgram(wbSeparatePass);
bgfx::destroyProgram(wbSeparateBlit);
bgfx::destroyProgram(wbPass);
bgfx::destroyProgram(wbBlit);
bgfx::destroyUniform(s_texColor0);
bgfx::destroyUniform(s_texColor1);
bgfx::destroyUniform(u_color);
// Shutdown bgfx.
bgfx::shutdown();
return 0;
}
|