From b1901ae7763bc48b3d3cbc84ca026a5c277ab4bb Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Thu, 25 Jul 2013 23:13:48 -0700 Subject: [PATCH] Fixed instancing. --- examples/06-bump/bump.cpp | 74 ++++++++++++++++++--------------------- src/renderer_d3d11.cpp | 9 ++++- src/renderer_d3d9.cpp | 9 ++++- src/renderer_gl.cpp | 18 ++++++++-- 4 files changed, 65 insertions(+), 45 deletions(-) diff --git a/examples/06-bump/bump.cpp b/examples/06-bump/bump.cpp index 857006f7..9128f27c 100644 --- a/examples/06-bump/bump.cpp +++ b/examples/06-bump/bump.cpp @@ -408,14 +408,16 @@ int _main_(int /*_argc*/, char** /*_argv*/) bgfx::setViewTransform(0, view, proj); const uint16_t instanceStride = 64; - const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(9, instanceStride); - if (NULL != idb) - { - uint8_t* data = idb->data; + const uint16_t numInstances = 3; - // Write instance data for 3x3 cubes. - for (uint32_t yy = 0; yy < 3; ++yy) + // Write instance data for 3x3 cubes. + for (uint32_t yy = 0; yy < 3; ++yy) + { + const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(numInstances, instanceStride); + if (NULL != idb) { + uint8_t* data = idb->data; + for (uint32_t xx = 0; xx < 3; ++xx) { float* mtx = (float*)data; @@ -424,43 +426,35 @@ int _main_(int /*_argc*/, char** /*_argv*/) mtx[13] = -3.0f + float(yy)*3.0f; mtx[14] = 0.0f; - float* color = (float*)&data[64]; - color[0] = sin(time+float(xx)/11.0f)*0.5f+0.5f; - color[1] = cos(time+float(yy)/11.0f)*0.5f+0.5f; - color[2] = sin(time*3.0f)*0.5f+0.5f; - color[3] = 1.0f; - data += instanceStride; } + + // Set vertex and fragment shaders. + bgfx::setProgram(program); + + // Set vertex and index buffer. + bgfx::setVertexBuffer(vbh); + bgfx::setIndexBuffer(ibh); + + // Set instance data buffer. + bgfx::setInstanceDataBuffer(idb, numInstances); + + // Bind textures. + bgfx::setTexture(0, u_texColor, textureColor); + bgfx::setTexture(1, u_texNormal, textureNormal); + + // Set render states. + bgfx::setState(0 + |BGFX_STATE_RGB_WRITE + |BGFX_STATE_ALPHA_WRITE + |BGFX_STATE_DEPTH_WRITE + |BGFX_STATE_DEPTH_TEST_LESS + |BGFX_STATE_MSAA + ); + + // Submit primitive for rendering to view 0. + bgfx::submit(0); } - - uint16_t numInstances = (uint16_t)( (data - idb->data)/instanceStride); - - // Set vertex and fragment shaders. - bgfx::setProgram(program); - - // Set vertex and index buffer. - bgfx::setVertexBuffer(vbh); - bgfx::setIndexBuffer(ibh); - - // Set instance data buffer. - bgfx::setInstanceDataBuffer(idb, numInstances); - - // Bind textures. - bgfx::setTexture(0, u_texColor, textureColor); - bgfx::setTexture(1, u_texNormal, textureNormal); - - // Set render states. - bgfx::setState(0 - |BGFX_STATE_RGB_WRITE - |BGFX_STATE_ALPHA_WRITE - |BGFX_STATE_DEPTH_WRITE - |BGFX_STATE_DEPTH_TEST_LESS - |BGFX_STATE_MSAA - ); - - // Submit primitive for rendering to view 0. - bgfx::submit(0); } // Advance to next frame. Rendering thread will be kicked to diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index ba614115..449c5fa5 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -2672,9 +2672,16 @@ namespace bgfx } } - if (currentState.m_vertexBuffer.idx != state.m_vertexBuffer.idx || programChanged) + if (programChanged + || currentState.m_vertexBuffer.idx != state.m_vertexBuffer.idx + || currentState.m_instanceDataBuffer.idx != state.m_instanceDataBuffer.idx + || currentState.m_instanceDataOffset != state.m_instanceDataOffset + || currentState.m_instanceDataStride != state.m_instanceDataStride) { currentState.m_vertexBuffer = state.m_vertexBuffer; + currentState.m_instanceDataBuffer.idx = state.m_instanceDataBuffer.idx; + currentState.m_instanceDataOffset = state.m_instanceDataOffset; + currentState.m_instanceDataStride = state.m_instanceDataStride; uint16_t handle = state.m_vertexBuffer.idx; if (invalidHandle != handle) diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index 232c481f..1abefe2a 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -2679,9 +2679,16 @@ namespace bgfx } } - if (currentState.m_vertexBuffer.idx != state.m_vertexBuffer.idx || programChanged) + if (programChanged + || currentState.m_vertexBuffer.idx != state.m_vertexBuffer.idx + || currentState.m_instanceDataBuffer.idx != state.m_instanceDataBuffer.idx + || currentState.m_instanceDataOffset != state.m_instanceDataOffset + || currentState.m_instanceDataStride != state.m_instanceDataStride) { currentState.m_vertexBuffer = state.m_vertexBuffer; + currentState.m_instanceDataBuffer.idx = state.m_instanceDataBuffer.idx; + currentState.m_instanceDataOffset = state.m_instanceDataOffset; + currentState.m_instanceDataStride = state.m_instanceDataStride; uint16_t handle = state.m_vertexBuffer.idx; if (invalidHandle != handle) diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index c5fe1031..2b1adc3c 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -2754,7 +2754,7 @@ namespace bgfx void Context::rendererSubmit() { - const GLuint defaultVao = s_renderCtx.m_vaoSupport; + const GLuint defaultVao = s_renderCtx.m_vao; if (0 != defaultVao) { GL_CHECK(glBindVertexArray(defaultVao) ); @@ -3274,18 +3274,24 @@ namespace bgfx if (programChanged || currentState.m_vertexBuffer.idx != state.m_vertexBuffer.idx || currentState.m_indexBuffer.idx != state.m_indexBuffer.idx - || currentState.m_instanceDataBuffer.idx != state.m_instanceDataBuffer.idx) + || currentState.m_instanceDataBuffer.idx != state.m_instanceDataBuffer.idx + || currentState.m_instanceDataOffset != state.m_instanceDataOffset + || currentState.m_instanceDataStride != state.m_instanceDataStride) { bx::HashMurmur2A murmur; murmur.begin(); murmur.add(state.m_vertexBuffer.idx); murmur.add(state.m_indexBuffer.idx); murmur.add(state.m_instanceDataBuffer.idx); + murmur.add(state.m_instanceDataOffset); + murmur.add(state.m_instanceDataStride); murmur.add(programIdx); uint32_t hash = murmur.end(); currentState.m_vertexBuffer = state.m_vertexBuffer; currentState.m_indexBuffer = state.m_indexBuffer; + currentState.m_instanceDataOffset = state.m_instanceDataOffset; + currentState.m_instanceDataStride = state.m_instanceDataStride; baseVertex = state.m_startVertex; GLuint id = s_renderCtx.m_vaoStateCache.find(hash); @@ -3351,9 +3357,15 @@ namespace bgfx } if (programChanged - || currentState.m_vertexBuffer.idx != state.m_vertexBuffer.idx) + || currentState.m_vertexBuffer.idx != state.m_vertexBuffer.idx + || currentState.m_instanceDataBuffer.idx != state.m_instanceDataBuffer.idx + || currentState.m_instanceDataOffset != state.m_instanceDataOffset + || currentState.m_instanceDataStride != state.m_instanceDataStride) { currentState.m_vertexBuffer = state.m_vertexBuffer; + currentState.m_instanceDataBuffer.idx = state.m_instanceDataBuffer.idx; + currentState.m_instanceDataOffset = state.m_instanceDataOffset; + currentState.m_instanceDataStride = state.m_instanceDataStride; uint16_t handle = state.m_vertexBuffer.idx; if (invalidHandle != handle)