-
-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed issue with mesh vertex buffers not updating properly #6505
Conversation
@@ -28,7 +28,8 @@ namespace dmGameSystem | |||
dmBufferDDF::BufferDesc* m_BufferDDF; | |||
dmBuffer::HBuffer m_Buffer; | |||
dmhash_t m_NameHash; | |||
uint32_t m_ElementCount; | |||
uint32_t m_ElementCount; // The number of vertices | |||
uint32_t m_Stride; // The vertex size (bytes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to keep it in the buffer resource and not the mesh resource
uint32_t m_ElementCount; | ||
uint32_t m_VertSize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be retrieved from the buffer resource
static uint32_t CalcBufferVersion(const MeshComponent* component, BufferResource* br) | ||
{ | ||
HashState32 state; | ||
dmHashInit32(&state, false); | ||
|
||
uint32_t version; | ||
dmBuffer::GetContentVersion(br->m_Buffer, &version); | ||
dmHashUpdateBuffer32(&state, &br->m_Buffer, sizeof(br->m_Buffer)); // The handle is not a pointer, but a version+index | ||
dmHashUpdateBuffer32(&state, &version, sizeof(version)); | ||
|
||
return dmHashFinal32(&state); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the issues was that the buffer content version starts at 0 and counts upwards, and many other buffers may have a similar version.
So detecting a changed buffer requires more than the content version.
BufferResource* br = GetVerticesBuffer(component, resource); | ||
dmHashUpdateBuffer32(&state, &br->m_NameHash, sizeof(br->m_NameHash)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hash is for batching, and as such we want to batch on the resource itself, not the content version which might be the same between the instances using different buffers.
dmRender::HMaterial material = GetMaterial(&component, component.m_Resource); | ||
if (dmRender::GetMaterialVertexSpace(material) == dmRenderDDF::MaterialDesc::VERTEX_SPACE_LOCAL) | ||
{ | ||
dmGameSystem::BufferResource* br = GetVerticesBuffer(&component, component.m_Resource); | ||
|
||
VertexBufferInfo* info = world->m_ResourceToVertexBuffer.Get(br->m_NameHash); | ||
assert(info != 0); | ||
|
||
if (component.m_ReHash || (component.m_RenderConstants && dmGameSystem::AreRenderConstantsUpdated(component.m_RenderConstants))) | ||
if (info->m_Version != component.m_BufferVersion) | ||
{ | ||
info->m_Version = component.m_BufferVersion; | ||
|
||
CopyBufferToVertexBuffer(br->m_Buffer, info->m_VertexBuffer, br->m_Stride, br->m_ElementCount, dmGraphics::BUFFER_USAGE_DYNAMIC_DRAW); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved this code to the Update(), from the RenderUpdate().
bool BuildVertexDeclaration(BufferResource* buffer_resource, | ||
dmGraphics::HVertexDeclaration* out_vert_decl, | ||
uint32_t* out_elem_count, uint32_t* out_vert_size) | ||
bool BuildVertexDeclaration(BufferResource* buffer_resource, dmGraphics::HVertexDeclaration* out_vert_decl) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the function to handle variables that can be retrieved from the buffer resource.
@@ -749,7 +749,7 @@ namespace dmGameSystem | |||
*s = 0; | |||
uint32_t version = 0; | |||
dmBuffer::GetContentVersion(hbuffer, &version); | |||
dmSnPrintf(buf, sizeof(buf), "buffer.%s(count = %d, version = %u", SCRIPT_TYPE_NAME_BUFFER, out_element_count, version); | |||
dmSnPrintf(buf, sizeof(buf), "buffer.%s(count = %d, version = %u, ", SCRIPT_TYPE_NAME_BUFFER, out_element_count, version); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Readability fix
|
||
uint32_t elem_count = br->m_ElementCount; | ||
VertexBufferInfo* info = world->m_ResourceToVertexBuffer.Get(br->m_NameHash); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused?
Under certain circumstances meshes did not update correctly even though the buffers changed. This fix ensures that a mesh is updated when the buffer content changes.