Skip to content

Commit

Permalink
GL: Fix fallback when glDrawRangeElementsBaseVertex is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
wheremyfoodat committed Oct 23, 2024
1 parent 7b1afc8 commit 66940ca
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
6 changes: 3 additions & 3 deletions include/PICA/pica_simd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,18 @@ namespace PICA::IndexBuffer {
// So we need to subtract the base vertex index from every index in the index buffer ourselves
// This is not really common, so we do it without SIMD for the moment, just to be able to run on Android Studio
template <bool useShortIndices>
void subtractBaseIndex(u8* indexBuffer, u32 vertexCount, u16 baseIndex) {
void subtractBaseIndex(u8* indexBuffer, u32 indexCount, u16 baseIndex) {
// Calculate the minimum and maximum indices used in the index buffer, so we'll only upload them
if constexpr (useShortIndices) {
u16* indexBuffer16 = reinterpret_cast<u16*>(indexBuffer);

for (u32 i = 0; i < vertexCount; i++) {
for (u32 i = 0; i < indexCount; i++) {
indexBuffer16[i] -= baseIndex;
}
} else {
u8 baseIndex8 = u8(baseIndex);

for (u32 i = 0; i < vertexCount; i++) {
for (u32 i = 0; i < indexCount; i++) {
indexBuffer[i] -= baseIndex8;
}
}
Expand Down
2 changes: 0 additions & 2 deletions include/renderer_gl/gl_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ namespace OpenGL {
struct Driver {
bool supportsExtFbFetch = false;
bool supportsArmFbFetch = false;
// Does this driver support glDraw(Range)ElementsBaseVertex?
bool supportsDrawElementsBaseVertex = false;

bool supportFbFetch() const { return supportsExtFbFetch || supportsArmFbFetch; }
};
Expand Down
10 changes: 5 additions & 5 deletions src/core/renderer_gl/renderer_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ void RendererGL::initGraphicsContextInternal() {
// Populate our driver info structure
driverInfo.supportsExtFbFetch = (GLAD_GL_EXT_shader_framebuffer_fetch != 0);
driverInfo.supportsArmFbFetch = (GLAD_GL_ARM_shader_framebuffer_fetch != 0);
driverInfo.supportsDrawElementsBaseVertex = (glDrawRangeElementsBaseVertex != nullptr);

// Initialize the default vertex shader used with shadergen
std::string defaultShadergenVSSource = fragShaderGen.getDefaultVertexShader();
Expand Down Expand Up @@ -531,7 +530,7 @@ void RendererGL::drawVertices(PICA::PrimType primType, std::span<const Vertex> v
// If glDrawRangeElementsBaseVertex is not available then prepareForDraw will have subtracted the base vertex from the index buffer
// for us, so just use glDrawRangeElements
glDrawRangeElements(
primitiveTopology, 0, maximumIndex - minimumIndex, GLsizei(vertices.size()),
primitiveTopology, 0, GLint(maximumIndex - minimumIndex), GLsizei(vertices.size()),
usingShortIndices ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE, hwIndexBufferOffset
);
}
Expand Down Expand Up @@ -1183,9 +1182,10 @@ void RendererGL::accelerateVertexUpload(ShaderUnit& shaderUnit, PICA::DrawAccele

std::memcpy(indexBufferRes.pointer, accel->indexBuffer, indexBufferSize);
// If we don't have glDrawRangeElementsBaseVertex, we must subtract the base index value from our index buffer manually
if (!driverInfo.supportsDrawElementsBaseVertex) [[unlikely]] {
usingShortIndices ? PICA::IndexBuffer::subtractBaseIndex<true>((u8*)indexBufferRes.pointer, vertexCount, accel->minimumIndex)
: PICA::IndexBuffer::subtractBaseIndex<false>((u8*)indexBufferRes.pointer, vertexCount, accel->minimumIndex);
if (glDrawRangeElementsBaseVertex == nullptr) [[unlikely]] {
const u32 indexCount = regs[PICA::InternalRegs::VertexCountReg];
usingShortIndices ? PICA::IndexBuffer::subtractBaseIndex<true>((u8*)indexBufferRes.pointer, indexCount, accel->minimumIndex)
: PICA::IndexBuffer::subtractBaseIndex<false>((u8*)indexBufferRes.pointer, indexCount, accel->minimumIndex);
}

hwIndexBuffer->Unmap(indexBufferSize);
Expand Down

0 comments on commit 66940ca

Please sign in to comment.