summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/40-svt/svt.cpp
blob: 3256f64952fb78b2bfaa842ae44386653efc86b4 (plain) (blame)
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
/*
 * Copyright 2018 Ales Mlakar. All rights reserved.
 * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
 */

 /*
  * Reference(s):
  * - Sparse Virtual Textures by Sean Barrett
  *   http://web.archive.org/web/20190103162611/http://silverspaceship.com/src/svt/
  * - Based on Virtual Texture Demo by Brad Blanchard
  *   http://web.archive.org/web/20190103162638/http://linedef.com/virtual-texture-demo.html
  * - Mars texture
  *   http://web.archive.org/web/20190103162730/http://www.celestiamotherlode.net/catalog/mars.php
  */

#include "common.h"
#include "bgfx_utils.h"
#include "imgui/imgui.h"
#include "camera.h"
#include "vt.h"

namespace
{

struct PosTexcoordVertex
{
	float    m_x;
	float    m_y;
	float    m_z;
	float    m_u;
	float    m_v;

	static void init()
	{
		ms_layout
			.begin()
			.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
			.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
			.end();
	};

	static bgfx::VertexLayout ms_layout;
};

bgfx::VertexLayout PosTexcoordVertex::ms_layout;

static const float s_planeScale = 50.0f;

static PosTexcoordVertex s_vplaneVertices[] =
{
	{ -s_planeScale, 0.0f,  s_planeScale, 1.0f, 1.0f },
	{  s_planeScale, 0.0f,  s_planeScale, 1.0f, 0.0f },
	{ -s_planeScale, 0.0f, -s_planeScale, 0.0f, 1.0f },
	{  s_planeScale, 0.0f, -s_planeScale, 0.0f, 0.0f },
};

static const uint16_t s_planeIndices[] =
{
	0, 1, 2,
	1, 3, 2,
};

class ExampleSVT : public entry::AppI
{
public:
	ExampleSVT(const char* _name, const char* _description, const char* _url)
		: entry::AppI(_name, _description, _url)
	{
	}

	void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
	{
		Args args(_argc, _argv);

		m_width = _width;
		m_height = _height;
		m_debug = BGFX_DEBUG_TEXT;
		m_reset = BGFX_RESET_VSYNC;

		bgfx::Init init;
		init.type     = args.m_type;
		init.vendorId = args.m_pciId;
		init.platformData.nwh  = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
		init.platformData.ndt  = entry::getNativeDisplayHandle();
		init.platformData.type = entry::getNativeWindowHandleType(entry::kDefaultWindowHandle);
		init.resolution.width  = m_width;
		init.resolution.height = m_height;
		init.resolution.reset  = m_reset;
		bgfx::init(init);

		// Enable m_debug text.
		bgfx::setDebug(m_debug);

		// Set views clear state (first pass to 0, second pass to some background color)
		for (uint16_t i = 0; i < 2; ++i)
		{
			bgfx::setViewClear(i
				, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
				, i == 0 ? 0 : 0x101050ff
				, 1.0f
				, 0
			);
		}

		// Create vertex stream declaration.
		PosTexcoordVertex::init();

		// Create static vertex buffer.
		m_vbh = bgfx::createVertexBuffer(
			  bgfx::makeRef(s_vplaneVertices, sizeof(s_vplaneVertices))
			, PosTexcoordVertex::ms_layout
		);

		m_ibh = bgfx::createIndexBuffer(
			  bgfx::makeRef(s_planeIndices, sizeof(s_planeIndices))
		);

		// Create program from shaders.
		m_vt_unlit = loadProgram("vs_vt_generic", "fs_vt_unlit");
		m_vt_mip = loadProgram("vs_vt_generic", "fs_vt_mip");

		// Imgui.
		imguiCreate();

		m_timeOffset = bx::getHPCounter();

		// Get renderer capabilities info.
		m_caps = bgfx::getCaps();

		m_scrollArea = 0;

		// Create and setup camera
		cameraCreate();

		cameraSetPosition({ 0.0f, 5.0f, 0.0f });
		cameraSetVerticalAngle(0.0f);

		// Set VirtualTexture system allocator
		vt::VirtualTexture::setAllocator(&m_vtAllocator);

		// Create Virtual texture info
		m_vti = new vt::VirtualTextureInfo();
		m_vti->m_virtualTextureSize = 8192; // The actual size will be read from the tile data file
		m_vti->m_tileSize = 128;
		m_vti->m_borderSize = 1;

		// Generate tile data file (if not yet created)
		{
			vt::TileGenerator tileGenerator(m_vti);
			tileGenerator.generate("textures/8k_mars.jpg");
		}

		// Load tile data file
		auto tileDataFile = new vt::TileDataFile("temp/8k_mars.vt", m_vti);
		tileDataFile->readInfo();

		// Create virtual texture and feedback buffer
		m_vt = new vt::VirtualTexture(tileDataFile, m_vti, 2048, 1);
		m_feedbackBuffer = new vt::FeedbackBuffer(m_vti, 64, 64);

	}

	virtual int shutdown() override
	{
		// Cleanup.
		bgfx::frame();

		cameraDestroy();
		imguiDestroy();

		bgfx::destroy(m_ibh);
		bgfx::destroy(m_vbh);

		bgfx::destroy(m_vt_unlit);
		bgfx::destroy(m_vt_mip);

		delete m_vti;
		delete m_vt;
		delete m_feedbackBuffer;

		// Shutdown bgfx.
		bgfx::shutdown();

		return 0;
	}

	bool update() override
	{
		if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState))
		{
			imguiBeginFrame(
				  m_mouseState.m_mx
				, m_mouseState.m_my
				, (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
				| (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
				| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
				, m_mouseState.m_mz
				, uint16_t(m_width)
				, uint16_t(m_height)
			);

			showExampleDialog(this);

			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 float deltaTime = float(frameTime / freq);

			float time = (float)((now - m_timeOffset) / freq);

			if ((BGFX_CAPS_TEXTURE_BLIT | BGFX_CAPS_TEXTURE_READ_BACK) != (bgfx::getCaps()->supported & (BGFX_CAPS_TEXTURE_BLIT | BGFX_CAPS_TEXTURE_READ_BACK)))
			{
				// When texture read-back or blit is not supported by GPU blink!
				bool blink = uint32_t(time*3.0f) & 1;
				bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " Texture read-back and/or blit not supported by GPU. ");

				// 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);
			}
			else
			{
				ImGui::SetNextWindowPos(
					  ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
					, ImGuiCond_FirstUseEver
				);
				ImGui::SetNextWindowSize(
					  ImVec2(m_width / 5.0f, m_height - 10.0f)
					, ImGuiCond_FirstUseEver
				);
				ImGui::Begin("Settings"
					, NULL
					, 0
				);

				//ImGui::SliderFloat("intensity", &m_intensity, 0.0f, 3.0f);
				auto showBorders = m_vt->isShowBoardersEnabled();
				if (ImGui::Checkbox("Show borders", &showBorders))
				{
					m_vt->enableShowBoarders(showBorders);
				}
				auto colorMipLevels = m_vt->isColorMipLevelsEnabled();
				if (ImGui::Checkbox("Color mip levels", &colorMipLevels))
				{
					m_vt->enableColorMipLevels(colorMipLevels);
				}
				auto uploadsperframe = m_vt->getUploadsPerFrame();
				if (ImGui::InputInt("Updates per frame", &uploadsperframe, 1, 2))
				{
					uploadsperframe = bx::clamp(uploadsperframe, 1, 100);
					m_vt->setUploadsPerFrame(uploadsperframe);
				}

				ImGui::ImageButton(m_vt->getAtlastTexture(), ImVec2(m_width / 5.0f - 16.0f, m_width / 5.0f - 16.0f));
				ImGui::ImageButton(bgfx::getTexture(m_feedbackBuffer->getFrameBuffer()), ImVec2(m_width / 5.0f - 16.0f, m_width / 5.0f - 16.0f));

				ImGui::End();

				// Update camera.
				cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );

				float view[16];
				cameraGetViewMtx(view);

				float proj[16];
				bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 1000.0f, m_caps->homogeneousDepth);

				// Setup views
				for (uint16_t i = 0; i < 2; ++i)
				{
					uint16_t viewWidth = 0;
					uint16_t viewHeight = 0;
					// Setup pass, first pass is into mip-map feedback buffer, second pass is on screen
					if (i == 0)
					{
						bgfx::setViewFrameBuffer(i, m_feedbackBuffer->getFrameBuffer());
						viewWidth = uint16_t(m_feedbackBuffer->getWidth());
						viewHeight = uint16_t(m_feedbackBuffer->getHeight());
					}
					else
					{
						bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE;
						bgfx::setViewFrameBuffer(i, invalid);
						viewWidth = uint16_t(m_width);
						viewHeight = uint16_t(m_height);
					}

					bgfx::setViewRect(i, 0, 0, viewWidth, viewHeight);
					bgfx::setViewTransform(i, view, proj);

					float mtx[16];
					bx::mtxIdentity(mtx);

					// Set identity transform for draw call.
					bgfx::setTransform(mtx);

					// Set vertex and index buffer.
					bgfx::setVertexBuffer(0, m_vbh);
					bgfx::setIndexBuffer(m_ibh);

					// Set render states.
					bgfx::setState(0
						| BGFX_STATE_WRITE_RGB
						| BGFX_STATE_WRITE_A
						| BGFX_STATE_WRITE_Z
						| BGFX_STATE_DEPTH_TEST_LESS
					);

					// Set virtual texture uniforms
					m_vt->setUniforms();

					// Submit primitive for rendering to first pass (to feedback buffer, where mip levels and tile x/y will be rendered
					if (i == 0)
					{
						bgfx::submit(i, m_vt_mip);
						// Download previous frame feedback info
						m_feedbackBuffer->download();
						// Update and upload new requests
						m_vt->update(m_feedbackBuffer->getRequests(), 4);
						// Clear feedback
						m_feedbackBuffer->clear();
						// Copy new frame feedback buffer
						m_feedbackBuffer->copy(3);
					}
					else
					{
						// Submit primitive for rendering to second pass (to back buffer, where virtual texture page table and atlas will be used)
						bgfx::submit(i, m_vt_unlit);
					}
				}
			}

			imguiEndFrame();

			// Advance to next frame. Rendering thread will be kicked to
			// process submitted rendering primitives.
			bgfx::frame();

			return true;
		}

		return false;
	}

	bgfx::VertexBufferHandle m_vbh;
	bgfx::IndexBufferHandle m_ibh;

	bgfx::ProgramHandle m_vt_unlit;
	bgfx::ProgramHandle m_vt_mip;

	uint32_t m_width;
	uint32_t m_height;
	uint32_t m_debug;
	uint32_t m_reset;

	int32_t m_scrollArea;

	entry::MouseState m_mouseState;

	const bgfx::Caps* m_caps;
	int64_t m_timeOffset;

	bx::DefaultAllocator m_vtAllocator;
	vt::VirtualTextureInfo* m_vti;
	vt::VirtualTexture* m_vt;
	vt::FeedbackBuffer* m_feedbackBuffer;
};

} // namespace

ENTRY_IMPLEMENT_MAIN(
	  ExampleSVT
	, "40-svt"
	, "Sparse Virtual Textures."
	, "https://bkaradzic.github.io/bgfx/examples.html#svt"
	);