-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnoisebgfx.cpp
More file actions
423 lines (356 loc) · 9.75 KB
/
noisebgfx.cpp
File metadata and controls
423 lines (356 loc) · 9.75 KB
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
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#undef min
#undef max
#include "SDL.h"
#include "SDL_syswm.h"
#else
#include "SDL2/SDL.h"
#include "SDL2/SDL_syswm.h"
#endif
#include "bx/bx.h"
#include "bx/math.h"
#include "bx/timer.h"
#include "bgfx/platform.h" // it must be included after SDL to enable SDL
// integration code path.
#include "bgfx/bgfx.h"
#pragma comment (lib, "sdl2.lib")
#pragma comment (lib, "sdl2main.lib")
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::VertexLayout ms_decl;
};
bgfx::VertexLayout PosColorVertex::ms_decl;
static PosColorVertex s_cubeVertices[] =
{
{-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_cubeTriList[] =
{
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 const uint16_t s_cubeTriStrip[] =
{
0, 1, 2,
3,
7,
1,
5,
0,
4,
2,
6,
7,
4,
5,
};
static const uint16_t s_cubeLineList[] =
{
0, 1,
0, 2,
0, 4,
1, 3,
1, 5,
2, 3,
2, 6,
3, 7,
4, 5,
4, 6,
5, 7,
6, 7,
};
static const uint16_t s_cubeLineStrip[] =
{
0, 2, 3, 1, 5, 7, 6, 4,
0, 2, 6, 4, 5, 7, 3, 1,
0,
};
static const uint16_t s_cubePoints[] =
{
0, 1, 2, 3, 4, 5, 6, 7
};
static const char* s_ptNames[]
{
"Triangle List",
"Triangle Strip",
"Lines",
"Line Strip",
"Points",
};
static const uint64_t s_ptState[]
{
UINT64_C(0),
BGFX_STATE_PT_TRISTRIP,
BGFX_STATE_PT_LINES,
BGFX_STATE_PT_LINESTRIP,
BGFX_STATE_PT_POINTS,
};
inline bool sdlSetWindow(SDL_Window* _window)
{
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if ( !SDL_GetWindowWMInfo(_window, &wmi) )
{
return false;
}
bgfx::PlatformData pd;
# if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
pd.ndt = wmi.info.x11.display;
pd.nwh = (void*)(uintptr_t)wmi.info.x11.window;
# elif BX_PLATFORM_OSX
pd.ndt = NULL;
pd.nwh = wmi.info.cocoa.window;
# elif BX_PLATFORM_WINDOWS
pd.ndt = NULL;
pd.nwh = wmi.info.win.window;
# elif BX_PLATFORM_STEAMLINK
pd.ndt = wmi.info.vivante.display;
pd.nwh = wmi.info.vivante.window;
# endif // BX_PLATFORM_
pd.context = NULL;
pd.backBuffer = NULL;
pd.backBufferDS = NULL;
bgfx::setPlatformData(pd);
return true;
}
/*static bgfx::ShaderHandle loadShader(bx::FileReaderI* _reader, const char* _name)
{
char filePath[512];
const char* shaderPath = "???";
switch (bgfx::getRendererType())
{
case bgfx::RendererType::Noop:
case bgfx::RendererType::Direct3D9: shaderPath = "shaders/dx9/"; break;
case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12: shaderPath = "shaders/dx11/"; break;
case bgfx::RendererType::Gnm: shaderPath = "shaders/pssl/"; break;
case bgfx::RendererType::Metal: shaderPath = "shaders/metal/"; break;
case bgfx::RendererType::OpenGL: shaderPath = "shaders/glsl/"; break;
case bgfx::RendererType::OpenGLES: shaderPath = "shaders/essl/"; break;
case bgfx::RendererType::Vulkan: shaderPath = "shaders/spirv/"; break;
case bgfx::RendererType::Count:
BX_CHECK(false, "You should not be here!");
break;
}
bx::strCopy(filePath, BX_COUNTOF(filePath), shaderPath);
bx::strCat(filePath, BX_COUNTOF(filePath), _name);
bx::strCat(filePath, BX_COUNTOF(filePath), ".bin");
bgfx::ShaderHandle handle = bgfx::createShader(loadMem(_reader, filePath));
bgfx::setName(handle, filePath);
return handle;
}*/
/*
* main(argc, argv) - the standard C entry point for the program
*/
int main(int argc, char *argv[])
{
// initialise out rendering context.
if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 )
return 0;
// Setup window
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
//SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
//SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
//SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_Window *sdl_window = SDL_CreateWindow("SDL2 + BGFX test app", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
sdlSetWindow(sdl_window);
bgfx::init();
BX_UNUSED(s_cubeTriList, s_cubeTriStrip);
uint32_t m_width = 1280;
uint32_t m_height = 720;
uint32_t m_debug = BGFX_DEBUG_TEXT;
uint32_t m_reset = BGFX_RESET_VSYNC;
bgfx::reset(m_width, m_height, m_reset);
// Enable debug text.
bgfx::setDebug(m_debug);
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
bgfx::VertexBufferHandle m_vbh;
bgfx::IndexBufferHandle m_ibh[BX_COUNTOF(s_ptState)];
bgfx::ProgramHandle m_program;
int32_t m_pt = 0;
// Create program from shaders.
{
//bgfx::createShader(
bgfx::ShaderHandle vsh = BGFX_INVALID_HANDLE;// loadShader(_reader, _vsName);
bgfx::ShaderHandle fsh = BGFX_INVALID_HANDLE;
//if (NULL != _fsName)
//{
// fsh = loadShader(_reader, _fsName);
//}
m_program = bgfx::createProgram(vsh, fsh, true /* destroy shaders when program is destroyed */);
//m_program = loadProgram("vs_cubes", "fs_cubes");
}
// Create vertex stream declaration.
PosColorVertex::init();
// Create static vertex buffer.
m_vbh = bgfx::createVertexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices))
, PosColorVertex::ms_decl
);
// Create static index buffer for triangle list rendering.
m_ibh[0] = bgfx::createIndexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_cubeTriList, sizeof(s_cubeTriList))
);
// Create static index buffer for triangle strip rendering.
m_ibh[1] = bgfx::createIndexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip))
);
// Create static index buffer for line list rendering.
m_ibh[2] = bgfx::createIndexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_cubeLineList, sizeof(s_cubeLineList))
);
// Create static index buffer for line strip rendering.
m_ibh[3] = bgfx::createIndexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_cubeLineStrip, sizeof(s_cubeLineStrip))
);
// Create static index buffer for point list rendering.
m_ibh[4] = bgfx::createIndexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_cubePoints, sizeof(s_cubePoints))
);
// Create program from shaders.
//bgfx::ProgramHandle m_program = loadProgram("vs_cubes", "fs_cubes");
int64_t m_timeOffset = bx::getHPCounter();
// Main loop
bool done = false;
while ( !done )
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
done = true;
}
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 - m_timeOffset) / double(bx::getHPFrequency()));
// Use debug font to print information about this example.
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 1, 0x4f, "Title: bgfx test app");
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Rendering test.");
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
bx::Vec3 at{ 0.0f, 0.0f, 0.0f };
bx::Vec3 eye{ 0.0f, 0.0f, -35.0f };
// Set view and projection matrix for view 0.
{
float view[16];
bx::mtxLookAt(view, eye, at);
float proj[16];
bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
bgfx::setViewTransform(0, view, proj);
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height));
}
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
bgfx::touch(0);
bgfx::IndexBufferHandle ibh = m_ibh[m_pt];
uint64_t state = 0
| BGFX_STATE_WRITE_R
| BGFX_STATE_WRITE_G
| BGFX_STATE_WRITE_B
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CW
| BGFX_STATE_MSAA
| s_ptState[m_pt]
;
// Submit 11x11 cubes.
for (uint32_t yy = 0; yy < 11; ++yy)
{
for (uint32_t xx = 0; xx < 11; ++xx)
{
float mtx[16];
bx::mtxRotateXY(mtx, time + xx * 0.21f, time + yy * 0.37f);
mtx[12] = -15.0f + float(xx)*3.0f;
mtx[13] = -15.0f + float(yy)*3.0f;
mtx[14] = 0.0f;
// Set model matrix for rendering.
bgfx::setTransform(mtx);
// Set vertex and index buffer.
bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(ibh);
// Set render states.
bgfx::setState(state);
// Submit primitive for rendering to view 0.
bgfx::submit(0, m_program);
}
}
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
}
// Cleanup.
for (uint32_t ii = 0; ii < BX_COUNTOF(m_ibh); ++ii)
{
bgfx::destroy(m_ibh[ii]);
}
bgfx::destroy(m_vbh);
bgfx::destroy(m_program);
// Shutdown bgfx.
bgfx::shutdown();
SDL_DestroyWindow(sdl_window);
SDL_Quit();
return 0;
}
#ifdef WIN32 // because win32 sucks
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
return main(0, nullptr);
}
#endif // WIN32