mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-11-24 16:48:18 -05:00
Fixed instancing.
This commit is contained in:
parent
df3c562d47
commit
b1901ae776
4 changed files with 65 additions and 45 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue