void TopazSample::initFramebuffers(int32_t width, int32_t height) { if (textures.sceneColor && GLEW_ARB_bindless_texture) { glMakeTextureHandleNonResidentARB(texturesAddress64.sceneColor); glMakeTextureHandleNonResidentARB(texturesAddress64.sceneDepth); } if (textures.sceneColor) { glDeleteTextures(1, &textures.sceneColor); } glGenTextures(1, &textures.sceneColor); glBindTexture(GL_TEXTURE_RECTANGLE, textures.sceneColor); glTexStorage2D(GL_TEXTURE_RECTANGLE, 1, GL_RGBA16F, width, height); glBindTexture(GL_TEXTURE_RECTANGLE, 0); if (textures.sceneDepth) { glDeleteTextures(1, &textures.sceneDepth); } glGenTextures(1, &textures.sceneDepth); glBindTexture(GL_TEXTURE_RECTANGLE, textures.sceneDepth); glTexStorage2D(GL_TEXTURE_RECTANGLE, 1, GL_DEPTH_COMPONENT24, width, height); glBindTexture(GL_TEXTURE_RECTANGLE, 0); if (fbos.scene) { glDeleteFramebuffers(1, &fbos.scene); } glGenFramebuffers(1, &fbos.scene); glBindFramebuffer(GL_FRAMEBUFFER, fbos.scene); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, textures.sceneColor, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_RECTANGLE, textures.sceneDepth, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); if (GLEW_ARB_bindless_texture) { texturesAddress64.sceneColor = glGetTextureHandleARB(textures.sceneColor); texturesAddress64.sceneDepth = glGetTextureHandleARB(textures.sceneDepth); glMakeTextureHandleResidentARB(texturesAddress64.sceneColor); glMakeTextureHandleResidentARB(texturesAddress64.sceneDepth); } cmdlist.state.fboIncarnation++; }
// ------------------------------------------------------------------------------------------------ Texture2DContainer::~Texture2DContainer() { // If this fires, it means there was a texture leaked somewhere. assert(mFreeList.size() == mSlices); if (mHandle != 0) { glMakeTextureHandleNonResidentARB(mHandle); mHandle = 0; } glDeleteTextures(1, &mTexId); }
// -------------------------------------------------------------------------------------------------------------------- void TexturedQuadsGLBindlessMultiDraw::Render(const std::vector<Matrix>& _transforms) { // Program Vec3 dir = { 0, 0, 1 }; Vec3 at = { 0, 0, 0 }; Vec3 up = { 0, 1, 0 }; dir = normalize(dir); Vec3 eye = at - 250 * dir; Matrix view = matrix_look_at(eye, at, up); Matrix view_proj = mProj * view; glUseProgram(mProgram); glUniformMatrix4fv(0, 1, GL_TRUE, &view_proj.x.x); // Input Layout. First the IB glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer); // Then the VBs. glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TexturedQuadsProblem::Vertex), (void*)offsetof(TexturedQuadsProblem::Vertex, pos)); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedQuadsProblem::Vertex), (void*)offsetof(TexturedQuadsProblem::Vertex, tex)); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); // Rasterizer State glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glFrontFace(GL_CCW); glDisable(GL_SCISSOR_TEST); // Blend State glDisable(GL_BLEND); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // Depth Stencil State glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); for (auto it = mTexHandles.begin(); it != mTexHandles.end(); ++it) { glMakeTextureHandleResidentARB(*it); } glBindBuffer(GL_SHADER_STORAGE_BUFFER, mTransformBuffer); BufferData(GL_SHADER_STORAGE_BUFFER, _transforms, GL_DYNAMIC_DRAW); size_t xformCount = _transforms.size(); assert(xformCount <= mObjectCount); for (size_t u = 0; u < xformCount; ++u) { DrawElementsIndirectCommand *cmd = &mCommands[u]; cmd->count = mIndexCount; cmd->instanceCount = 1; cmd->firstIndex = 0; cmd->baseVertex = 0; cmd->baseInstance = 0; } glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_SHORT, &*mCommands.begin(), xformCount, 0); for (auto it = mTexHandles.begin(); it != mTexHandles.end(); ++it) { glMakeTextureHandleNonResidentARB(*it); } }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_ARBBindlessTexture_nglMakeTextureHandleNonResidentARB(JNIEnv *env, jclass clazz, jlong handle, jlong function_pointer) { glMakeTextureHandleNonResidentARBPROC glMakeTextureHandleNonResidentARB = (glMakeTextureHandleNonResidentARBPROC)((intptr_t)function_pointer); glMakeTextureHandleNonResidentARB(handle); }
// -------------------------------------------------------------------------------------------------------------------- void TexturedQuadsGLBindless::Render(const std::vector<Matrix>& _transforms) { // Program Vec3 dir = { 0, 0, 1 }; Vec3 at = { 0, 0, 0 }; Vec3 up = { 0, 1, 0 }; dir = normalize(dir); Vec3 eye = at - 250 * dir; Matrix view = matrix_look_at(eye, at, up); Matrix view_proj = mProj * view; glUseProgram(mProgram); glUniformMatrix4fv(mUniformLocation.ViewProjection, 1, GL_TRUE, &view_proj.x.x); // Input Layout. First the IB glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer); // Then the VBs. glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TexturedQuadsProblem::Vertex), (void*)offsetof(TexturedQuadsProblem::Vertex, pos)); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedQuadsProblem::Vertex), (void*)offsetof(TexturedQuadsProblem::Vertex, tex)); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); // Rasterizer State glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glFrontFace(GL_CCW); glDisable(GL_SCISSOR_TEST); // Blend State glDisable(GL_BLEND); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // Depth Stencil State glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); for (auto it = mTexHandles.begin(); it != mTexHandles.end(); ++it) { glMakeTextureHandleResidentARB(*it); } glBindBuffer(GL_SHADER_STORAGE_BUFFER, mTransformBuffer); BufferData(GL_SHADER_STORAGE_BUFFER, _transforms, GL_DYNAMIC_DRAW); size_t xformCount = _transforms.size(); assert(xformCount <= mObjectCount); // Code below assumes at least 1 texture. assert(mTexHandles.size() > 0); auto texIt = mTexHandles.begin(); for (size_t u = 0; u < xformCount; ++u) { // Update the Draw ID (since we cannot use multi_draw here glUniform1i(mUniformLocation.DrawID, u); if (texIt == mTexHandles.end()) { texIt = mTexHandles.begin(); } GLuint64 activeTex = *texIt; ++texIt; glUniformHandleui64ARB(mUniformLocation.gTex, activeTex); glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0); } for (auto it = mTexHandles.begin(); it != mTexHandles.end(); ++it) { glMakeTextureHandleNonResidentARB(*it); } }
inline void make_texture_handle_non_resident(uint64_t handle) { ARC_GL_CLEAR_ERRORS(); glMakeTextureHandleNonResidentARB(handle); ARC_GL_CHECK_FOR_ERRORS(); }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_ARBBindlessTexture_glMakeTextureHandleNonResidentARB(JNIEnv *__env, jclass clazz, jlong handle) { glMakeTextureHandleNonResidentARBPROC glMakeTextureHandleNonResidentARB = (glMakeTextureHandleNonResidentARBPROC)tlsGetFunction(1078); UNUSED_PARAM(clazz) glMakeTextureHandleNonResidentARB(handle); }