Skip to content

Commit

Permalink
igl | opengl | Fix index offset for IndexFormat::UInt8 (#211)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #211

Reviewed By: KyleFung

Differential Revision: D66348858

Pulled By: corporateshark

fbshipit-source-id: 2be12c122638f7ed751082c1e954a035fa4ad8d1
  • Loading branch information
vinsentli authored and facebook-github-bot committed Nov 22, 2024
1 parent 72522b9 commit 3ae0635
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/igl/opengl/RenderCommandEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ GLenum toGlPrimitive(PrimitiveType t) {
return result;
}

int toGlType(IndexFormat format) {
GLenum toGlType(IndexFormat format) {
switch (format) {
case IndexFormat::UInt8:
return GL_UNSIGNED_BYTE;
Expand All @@ -55,6 +55,18 @@ int toGlType(IndexFormat format) {
IGL_UNREACHABLE_RETURN(GL_UNSIGNED_INT)
}

uint8_t getIndexByteSize(GLenum indexType) {
switch (indexType) {
case GL_UNSIGNED_BYTE:
return 1u;
case GL_UNSIGNED_SHORT:
return 2u;
case GL_UNSIGNED_INT:
return 4u;
}
IGL_UNREACHABLE_RETURN(4u)
}

} // namespace

RenderCommandEncoder::RenderCommandEncoder(const std::shared_ptr<CommandBuffer>& commandBuffer) :
Expand Down Expand Up @@ -361,8 +373,7 @@ void RenderCommandEncoder::drawIndexed(size_t indexCount,
IGL_DEBUG_ASSERT(baseInstance == 0, "Instancing is not implemented");
IGL_DEBUG_ASSERT(indexType_, "No index buffer bound");

const size_t indexOffsetBytes =
static_cast<size_t>(firstIndex) * (indexType_ == GL_UNSIGNED_INT ? 4u : 2u);
const size_t indexOffsetBytes = static_cast<size_t>(firstIndex) * getIndexByteSize(indexType_);

if (IGL_DEBUG_VERIFY(adapter_ && indexType_)) {
getCommandBuffer().incrementCurrentDrawCount();
Expand Down
2 changes: 1 addition & 1 deletion src/igl/opengl/RenderCommandEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class RenderCommandEncoder final : public IRenderCommandEncoder, public WithCont
private:
std::unique_ptr<RenderCommandAdapter> adapter_;
bool scissorEnabled_ = false;
int indexType_ = 0;
GLenum indexType_ = 0;
void* indexBufferOffset_ = nullptr;
std::shared_ptr<igl::opengl::Framebuffer> resolveFramebuffer_;
std::shared_ptr<igl::opengl::Framebuffer> framebuffer_;
Expand Down

0 comments on commit 3ae0635

Please sign in to comment.