Skip to content

Commit f35210b

Browse files
committed
revisit sparsetexture example
1 parent 5319aa2 commit f35210b

File tree

5 files changed

+138
-110
lines changed

5 files changed

+138
-110
lines changed

source/examples/shaderincludes/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ void deinitialize()
5656
void draw()
5757
{
5858
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
59+
60+
glViewport(0, 0, g_size.x, g_size.y);
5961
g_quad->draw();
6062
}
6163

source/examples/sparsetexture/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ message(STATUS "Example ${target}")
2929

3030
set(sources
3131
main.cpp
32+
3233
ScreenAlignedQuad.h
3334
ScreenAlignedQuad.cpp
35+
contextinfo.inl
36+
datapath.inl
3437
)
3538

3639

@@ -80,6 +83,7 @@ target_link_libraries(${target}
8083
${DEFAULT_LIBRARIES}
8184
${GLFW_LIBRARIES}
8285
${META_PROJECT_NAME}::globjects
86+
cpplocate::cpplocate
8387
)
8488

8589

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
#include <iostream>
3+
4+
5+
namespace common
6+
{
7+
8+
void printContextInfo()
9+
{
10+
std::cout << std::endl
11+
<< "OpenGL Version: " << glbinding::ContextInfo::version() << std::endl
12+
<< "OpenGL Vendor: " << glbinding::ContextInfo::vendor() << std::endl
13+
<< "OpenGL Renderer: " << glbinding::ContextInfo::renderer() << std::endl << std::endl;
14+
}
15+
16+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
#include <string>
3+
4+
#include <cpplocate/cpplocate.h>
5+
#include <cpplocate/ModuleInfo.h>
6+
7+
8+
namespace common
9+
{
10+
11+
std::string normalizePath(const std::string & filepath)
12+
{
13+
auto copy = filepath;
14+
std::replace(copy.begin(), copy.end(), '\\', '/');
15+
16+
auto i = copy.find_last_of('/');
17+
if (i == copy.size() - 1)
18+
copy = copy.substr(0, copy.size() - 1);
19+
20+
return copy;
21+
}
22+
23+
std::string retrieveDataPath(const std::string & module, const std::string & key)
24+
{
25+
const auto moduleInfo = cpplocate::findModule(module);
26+
27+
auto dataPath = moduleInfo.value(key);
28+
dataPath = normalizePath(dataPath);
29+
30+
if (dataPath.empty())
31+
dataPath = "data/";
32+
else
33+
dataPath += "/";
34+
35+
return dataPath;
36+
}
37+
38+
}

source/examples/sparsetexture/main.cpp

Lines changed: 78 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#include <algorithm>
33
#include <random>
44

5-
#include <glm/glm.hpp>
5+
#include <glm/vec2.hpp>
6+
#include <glm/vec3.hpp>
67

78
#include <glbinding/gl/gl.h>
89
#include <glbinding/gl/extension.h>
@@ -19,136 +20,72 @@
1920

2021
#include "ScreenAlignedQuad.h"
2122

23+
// example commons
24+
#include "contextinfo.inl"
25+
#include "datapath.inl"
2226

23-
using namespace gl;
24-
using namespace glm;
25-
using namespace globjects;
26-
27-
28-
namespace {
29-
bool g_toggleFS = false;
30-
bool g_isFS = false;
31-
32-
Texture * g_texture = nullptr;
33-
ScreenAlignedQuad * g_quad = nullptr;
34-
ivec2 g_pageSize;
35-
ivec2 g_numPages;
36-
int g_totalPages;
37-
38-
const ivec2 g_textureSize(4096);
39-
const int g_maxResidentPages(512);
40-
}
41-
42-
43-
void key_callback(GLFWwindow * window, int key, int /*scancode*/, int action, int /*modes*/)
44-
{
45-
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
46-
glfwSetWindowShouldClose(window, true);
4727

48-
if (key == GLFW_KEY_F5 && action == GLFW_RELEASE)
49-
File::reloadAll();
28+
using namespace gl;
5029

51-
if (key == GLFW_KEY_F11 && action == GLFW_RELEASE)
52-
g_toggleFS = true;
53-
}
5430

55-
GLFWwindow * createWindow(bool fs = false)
31+
namespace
5632
{
57-
// Set GLFW window hints
58-
glfwSetErrorCallback( [] (int /*error*/, const char * description) { puts(description); } );
59-
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
60-
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
61-
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
62-
63-
// Create a context and, if valid, make it current
64-
GLFWwindow * window = glfwCreateWindow(1024, 768, "", fs ? glfwGetPrimaryMonitor() : NULL, NULL);
65-
if (window == nullptr)
66-
{
67-
critical() << "Context creation failed. Terminate execution.";
68-
69-
glfwTerminate();
70-
exit(1);
71-
}
72-
glfwMakeContextCurrent(window);
73-
74-
// Create callback that when user presses ESC, the context should be destroyed and window closed
75-
glfwSetKeyCallback(window, key_callback);
76-
77-
// Initialize globjects (internally initializes glbinding, and registers the current context)
78-
globjects::init();
79-
80-
// Do only on startup
81-
if (!g_toggleFS)
82-
{
83-
// Dump information about context and graphics card
84-
info() << std::endl
85-
<< "OpenGL Version: " << glbinding::ContextInfo::version() << std::endl
86-
<< "OpenGL Vendor: " << glbinding::ContextInfo::vendor() << std::endl
87-
<< "OpenGL Renderer: " << glbinding::ContextInfo::renderer() << std::endl;
88-
}
89-
90-
if (!hasExtension(GLextension::GL_ARB_sparse_texture))
91-
{
92-
critical() << "Sparse textues not supported.";
33+
globjects::Texture * g_texture = nullptr;
34+
ScreenAlignedQuad * g_quad = nullptr;
9335

94-
glfwTerminate();
95-
exit(1);
96-
}
36+
auto g_pageSize = glm::ivec2{ };
37+
auto g_numPages = glm::ivec2{ };
38+
auto g_totalPages = 0u;
9739

98-
glClearColor(0.2f, 0.3f, 0.4f, 1.f);
40+
const auto g_textureSize = glm::ivec2{4096};
41+
const auto g_maxResidentPages = 512;
9942

100-
g_isFS = fs;
101-
return window;
43+
auto g_size = glm::ivec2{};
10244
}
10345

104-
void destroyWindow(GLFWwindow * window)
105-
{
106-
globjects::detachAllObjects();
107-
glfwDestroyWindow(window);
108-
}
10946

11047
void initialize()
11148
{
11249
// Initialize OpenGL objects
11350
int numPageSizes;
11451
glGetInternalformativ(GL_TEXTURE_2D, GL_RGBA8, GL_NUM_VIRTUAL_PAGE_SIZES_ARB, sizeof(int), &numPageSizes);
115-
info("GL_NUM_VIRTUAL_PAGE_SIZES_ARB = %d;", numPageSizes);
52+
globjects::info("GL_NUM_VIRTUAL_PAGE_SIZES_ARB = %d;", numPageSizes);
11653

11754
if (numPageSizes == 0)
11855
{
119-
fatal("Sparse Texture not supported for GL_RGBA8");
56+
globjects::fatal("Sparse Texture not supported for GL_RGBA8");
12057
exit(1);
12158
}
12259

12360
std::vector<int> pageSizesX(numPageSizes);
12461
glGetInternalformativ(GL_TEXTURE_2D, GL_RGBA8, GL_VIRTUAL_PAGE_SIZE_X_ARB
12562
, static_cast<GLsizei>(numPageSizes * sizeof(int)), pageSizesX.data());
12663
for (int i = 0; i < numPageSizes; ++i)
127-
info("GL_VIRTUAL_PAGE_SIZE_X_ARB[%;] = %;", i, pageSizesX[i]);
64+
globjects::info("GL_VIRTUAL_PAGE_SIZE_X_ARB[%;] = %;", i, pageSizesX[i]);
12865

12966
std::vector<int> pageSizesY(numPageSizes);
13067
glGetInternalformativ(GL_TEXTURE_2D, GL_RGBA8, GL_VIRTUAL_PAGE_SIZE_Y_ARB
13168
, static_cast<GLsizei>(numPageSizes * sizeof(int)), pageSizesY.data());
13269
for (int i = 0; i < numPageSizes; ++i)
133-
info("GL_VIRTUAL_PAGE_SIZE_Y_ARB[%;] = %;", i, pageSizesY[i]);
70+
globjects::info("GL_VIRTUAL_PAGE_SIZE_Y_ARB[%;] = %;", i, pageSizesY[i]);
13471

13572
std::vector<int> pageSizesZ(numPageSizes);
13673
glGetInternalformativ(GL_TEXTURE_2D, GL_RGBA8, GL_VIRTUAL_PAGE_SIZE_Z_ARB
13774
, static_cast<GLsizei>(numPageSizes * sizeof(int)), pageSizesZ.data());
13875
for (int i = 0; i < numPageSizes; ++i)
139-
info("GL_VIRTUAL_PAGE_SIZE_Z_ARB[%;] = %;", i, pageSizesZ[i]);
76+
globjects::info("GL_VIRTUAL_PAGE_SIZE_Z_ARB[%;] = %;", i, pageSizesZ[i]);
14077

141-
g_pageSize = ivec2(pageSizesX[0], pageSizesY[0]);
78+
g_pageSize = glm::ivec2(pageSizesX[0], pageSizesY[0]);
14279
g_numPages = g_textureSize / g_pageSize;
14380
g_totalPages = g_numPages.x * g_numPages.y;
14481

14582
// Get maximum sparse texture size
14683

14784
int maxSparseTextureSize;
14885
glGetIntegerv(GL_MAX_SPARSE_TEXTURE_SIZE_ARB, &maxSparseTextureSize);
149-
info("GL_MAX_SPARSE_TEXTURE_SIZE_ARB = %d;", maxSparseTextureSize);
86+
globjects::info("GL_MAX_SPARSE_TEXTURE_SIZE_ARB = %d;", maxSparseTextureSize);
15087

151-
g_texture = new Texture(GL_TEXTURE_2D);
88+
g_texture = new globjects::Texture(GL_TEXTURE_2D);
15289
g_texture->ref();
15390

15491
// make texture sparse
@@ -195,14 +132,14 @@ void mapNextPage()
195132
data[i] = static_cast<unsigned char>(255 - static_cast<unsigned char>(r(generator) * 255));
196133

197134
// unmap oldest page
198-
int oldestPage = (currentPage + g_totalPages - g_maxResidentPages) % g_totalPages;
199-
ivec2 oldOffset = ivec2(oldestPage % g_numPages.x, oldestPage / g_numPages.x) * g_pageSize;
200-
g_texture->pageCommitment(0, ivec3(oldOffset, 0), ivec3(g_pageSize, 1), GL_FALSE);
135+
const auto oldestPage = (currentPage + g_totalPages - g_maxResidentPages) % g_totalPages;
136+
const auto oldOffset = glm::ivec2(oldestPage % g_numPages.x, oldestPage / g_numPages.x) * g_pageSize;
137+
g_texture->pageCommitment(0, glm::ivec3(oldOffset, 0), glm::ivec3(g_pageSize, 1), GL_FALSE);
201138

202139
// map next page
203-
ivec2 newOffset = ivec2(currentPage % g_numPages.x, currentPage / g_numPages.x) * g_pageSize;
140+
glm::ivec2 newOffset = glm::ivec2(currentPage % g_numPages.x, currentPage / g_numPages.x) * g_pageSize;
204141

205-
g_texture->pageCommitment(0, ivec3(newOffset, 0), ivec3(g_pageSize, 1), GL_TRUE);
142+
g_texture->pageCommitment(0, glm::ivec3(newOffset, 0), glm::ivec3(g_pageSize, 1), GL_TRUE);
206143
g_texture->subImage2D(0, newOffset, g_pageSize, GL_RGBA, GL_UNSIGNED_BYTE, data.data());
207144

208145
currentPage = (currentPage + 1) % g_totalPages;
@@ -214,42 +151,73 @@ void draw()
214151

215152
mapNextPage();
216153

154+
glViewport(0, 0, g_size.x, g_size.y);
217155
g_quad->draw();
218156
}
219157

220158

221-
/**
222-
* @brief This example shows how to set up a sparse texture and then map/unmap pages using the ARB_sparse_texture extension.
223-
*
224-
* See http://www.opengl.org/registry/specs/ARB/sparse_texture.txt
225-
*/
159+
void error(int errnum, const char * errmsg)
160+
{
161+
globjects::critical() << errnum << ": " << errmsg << std::endl;
162+
}
163+
164+
void framebuffer_size_callback(GLFWwindow * /*window*/, int width, int height)
165+
{
166+
g_size = glm::ivec2{ width, height };
167+
}
168+
169+
void key_callback(GLFWwindow * window, int key, int /*scancode*/, int action, int /*modes*/)
170+
{
171+
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
172+
glfwSetWindowShouldClose(window, true);
173+
}
174+
175+
226176
int main(int /*argc*/, char * /*argv*/[])
227177
{
228178
// Initialize GLFW
229-
glfwInit();
179+
if (!glfwInit())
180+
return 1;
181+
182+
glfwSetErrorCallback(error);
183+
glfwDefaultWindowHints();
184+
185+
glfwSetErrorCallback([](int /*error*/, const char * description) { puts(description); });
186+
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
187+
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
188+
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
189+
190+
// Create a context and, if valid, make it current
191+
GLFWwindow * window = glfwCreateWindow(320, 240, "globjects Sparse Textures", NULL, NULL);
192+
if (window == nullptr)
193+
{
194+
globjects::critical() << "Context creation failed. Terminate execution.";
195+
196+
glfwTerminate();
197+
return -1;
198+
}
199+
glfwSetKeyCallback(window, key_callback);
200+
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
201+
202+
glfwMakeContextCurrent(window);
203+
204+
// Initialize globjects (internally initializes glbinding, and registers the current context)
205+
globjects::init();
206+
common::printContextInfo();
207+
208+
globjects::info() << "Press F5 to reload shaders." << std::endl;
209+
230210

231-
GLFWwindow * window = createWindow();
232211
initialize();
212+
glfwGetFramebufferSize(window, &g_size[0], &g_size[1]);
233213

234214
// Main loop
235215
while (!glfwWindowShouldClose(window))
236216
{
237217
glfwPollEvents();
238-
239-
if (g_toggleFS)
240-
{
241-
deinitialize();
242-
destroyWindow(window);
243-
window = createWindow(!g_isFS);
244-
initialize();
245-
246-
g_toggleFS = false;
247-
}
248-
249218
draw();
250219
glfwSwapBuffers(window);
251220
}
252-
253221
deinitialize();
254222

255223
// Properly shutdown GLFW

0 commit comments

Comments
 (0)