mirror of
https://github.com/isledecomp/isle-portable.git
synced 2026-01-11 10:31:16 +00:00
OpenGL ES 2.0 Only lookup shader locations once (#460)
This commit is contained in:
parent
225adda309
commit
ab48ce60b0
@ -263,6 +263,23 @@ OpenGLES2Renderer::OpenGLES2Renderer(DWORD width, DWORD height, SDL_GLContext co
|
|||||||
};
|
};
|
||||||
m_uiMesh.indices = {0, 1, 2, 0, 2, 3};
|
m_uiMesh.indices = {0, 1, 2, 0, 2, 3};
|
||||||
m_uiMeshCache = GLES2UploadMesh(m_uiMesh, true);
|
m_uiMeshCache = GLES2UploadMesh(m_uiMesh, true);
|
||||||
|
m_posLoc = glGetAttribLocation(m_shaderProgram, "a_position");
|
||||||
|
m_normLoc = glGetAttribLocation(m_shaderProgram, "a_normal");
|
||||||
|
m_texLoc = glGetAttribLocation(m_shaderProgram, "a_texCoord");
|
||||||
|
m_colorLoc = glGetUniformLocation(m_shaderProgram, "u_color");
|
||||||
|
m_shinLoc = glGetUniformLocation(m_shaderProgram, "u_shininess");
|
||||||
|
m_lightCountLoc = glGetUniformLocation(m_shaderProgram, "u_lightCount");
|
||||||
|
m_useTextureLoc = glGetUniformLocation(m_shaderProgram, "u_useTexture");
|
||||||
|
m_textureLoc = glGetUniformLocation(m_shaderProgram, "u_texture");
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
std::string base = "u_lights[" + std::to_string(i) + "]";
|
||||||
|
u_lightLocs[i][0] = glGetUniformLocation(m_shaderProgram, (base + ".color").c_str());
|
||||||
|
u_lightLocs[i][1] = glGetUniformLocation(m_shaderProgram, (base + ".position").c_str());
|
||||||
|
u_lightLocs[i][2] = glGetUniformLocation(m_shaderProgram, (base + ".direction").c_str());
|
||||||
|
}
|
||||||
|
m_modelViewMatrixLoc = glGetUniformLocation(m_shaderProgram, "u_modelViewMatrix");
|
||||||
|
m_normalMatrixLoc = glGetUniformLocation(m_shaderProgram, "u_normalMatrix");
|
||||||
|
m_projectionMatrixLoc = glGetUniformLocation(m_shaderProgram, "u_projectionMatrix");
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenGLES2Renderer::~OpenGLES2Renderer()
|
OpenGLES2Renderer::~OpenGLES2Renderer()
|
||||||
@ -481,12 +498,11 @@ HRESULT OpenGLES2Renderer::BeginFrame()
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < lightCount; ++i) {
|
for (int i = 0; i < lightCount; ++i) {
|
||||||
std::string base = "u_lights[" + std::to_string(i) + "]";
|
glUniform4fv(u_lightLocs[i][0], 1, lightData[i].color);
|
||||||
glUniform4fv(glGetUniformLocation(m_shaderProgram, (base + ".color").c_str()), 1, lightData[i].color);
|
glUniform4fv(u_lightLocs[i][1], 1, lightData[i].position);
|
||||||
glUniform4fv(glGetUniformLocation(m_shaderProgram, (base + ".position").c_str()), 1, lightData[i].position);
|
glUniform4fv(u_lightLocs[i][2], 1, lightData[i].direction);
|
||||||
glUniform4fv(glGetUniformLocation(m_shaderProgram, (base + ".direction").c_str()), 1, lightData[i].direction);
|
|
||||||
}
|
}
|
||||||
glUniform1i(glGetUniformLocation(m_shaderProgram, "u_lightCount"), lightCount);
|
glUniform1i(m_lightCountLoc, lightCount);
|
||||||
return DD_OK;
|
return DD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -508,52 +524,49 @@ void OpenGLES2Renderer::SubmitDraw(
|
|||||||
{
|
{
|
||||||
auto& mesh = m_meshs[meshId];
|
auto& mesh = m_meshs[meshId];
|
||||||
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(m_shaderProgram, "u_modelViewMatrix"), 1, GL_FALSE, &modelViewMatrix[0][0]);
|
glUniformMatrix4fv(m_modelViewMatrixLoc, 1, GL_FALSE, &modelViewMatrix[0][0]);
|
||||||
glUniformMatrix3fv(glGetUniformLocation(m_shaderProgram, "u_normalMatrix"), 1, GL_FALSE, &normalMatrix[0][0]);
|
glUniformMatrix3fv(m_normalMatrixLoc, 1, GL_FALSE, &normalMatrix[0][0]);
|
||||||
glUniformMatrix4fv(glGetUniformLocation(m_shaderProgram, "u_projectionMatrix"), 1, GL_FALSE, &m_projection[0][0]);
|
glUniformMatrix4fv(m_projectionMatrixLoc, 1, GL_FALSE, &m_projection[0][0]);
|
||||||
|
|
||||||
glUniform4f(
|
glUniform4f(
|
||||||
glGetUniformLocation(m_shaderProgram, "u_color"),
|
m_colorLoc,
|
||||||
appearance.color.r / 255.0f,
|
appearance.color.r / 255.0f,
|
||||||
appearance.color.g / 255.0f,
|
appearance.color.g / 255.0f,
|
||||||
appearance.color.b / 255.0f,
|
appearance.color.b / 255.0f,
|
||||||
appearance.color.a / 255.0f
|
appearance.color.a / 255.0f
|
||||||
);
|
);
|
||||||
|
|
||||||
glUniform1f(glGetUniformLocation(m_shaderProgram, "u_shininess"), appearance.shininess);
|
glUniform1f(m_shinLoc, appearance.shininess);
|
||||||
|
|
||||||
if (appearance.textureId != NO_TEXTURE_ID) {
|
if (appearance.textureId != NO_TEXTURE_ID) {
|
||||||
glUniform1i(glGetUniformLocation(m_shaderProgram, "u_useTexture"), 1);
|
glUniform1i(m_useTextureLoc, 1);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, m_textures[appearance.textureId].glTextureId);
|
glBindTexture(GL_TEXTURE_2D, m_textures[appearance.textureId].glTextureId);
|
||||||
glUniform1i(glGetUniformLocation(m_shaderProgram, "u_texture"), 0);
|
glUniform1i(m_textureLoc, 0);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
glUniform1i(glGetUniformLocation(m_shaderProgram, "u_useTexture"), 0);
|
glUniform1i(m_useTextureLoc, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, mesh.vboPositions);
|
glBindBuffer(GL_ARRAY_BUFFER, mesh.vboPositions);
|
||||||
GLint posLoc = glGetAttribLocation(m_shaderProgram, "a_position");
|
glEnableVertexAttribArray(m_posLoc);
|
||||||
glEnableVertexAttribArray(posLoc);
|
glVertexAttribPointer(m_posLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
|
||||||
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, mesh.vboNormals);
|
glBindBuffer(GL_ARRAY_BUFFER, mesh.vboNormals);
|
||||||
GLint normLoc = glGetAttribLocation(m_shaderProgram, "a_normal");
|
glEnableVertexAttribArray(m_normLoc);
|
||||||
glEnableVertexAttribArray(normLoc);
|
glVertexAttribPointer(m_normLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
|
||||||
glVertexAttribPointer(normLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
|
|
||||||
|
|
||||||
GLint texLoc = glGetAttribLocation(m_shaderProgram, "a_texCoord");
|
|
||||||
if (appearance.textureId != NO_TEXTURE_ID) {
|
if (appearance.textureId != NO_TEXTURE_ID) {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, mesh.vboTexcoords);
|
glBindBuffer(GL_ARRAY_BUFFER, mesh.vboTexcoords);
|
||||||
glEnableVertexAttribArray(texLoc);
|
glEnableVertexAttribArray(m_texLoc);
|
||||||
glVertexAttribPointer(texLoc, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
|
glVertexAttribPointer(m_texLoc, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.ibo);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.ibo);
|
||||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(mesh.indices.size()), GL_UNSIGNED_SHORT, nullptr);
|
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(mesh.indices.size()), GL_UNSIGNED_SHORT, nullptr);
|
||||||
|
|
||||||
glDisableVertexAttribArray(normLoc);
|
glDisableVertexAttribArray(m_normLoc);
|
||||||
glDisableVertexAttribArray(texLoc);
|
glDisableVertexAttribArray(m_texLoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
HRESULT OpenGLES2Renderer::FinalizeFrame()
|
HRESULT OpenGLES2Renderer::FinalizeFrame()
|
||||||
@ -605,13 +618,13 @@ void OpenGLES2Renderer::Draw2DImage(Uint32 textureId, const SDL_Rect& srcRect, c
|
|||||||
|
|
||||||
float color[] = {1.0f, 1.0f, 1.0f, 1.0f};
|
float color[] = {1.0f, 1.0f, 1.0f, 1.0f};
|
||||||
float blank[] = {0.0f, 0.0f, 0.0f, 0.0f};
|
float blank[] = {0.0f, 0.0f, 0.0f, 0.0f};
|
||||||
glUniform4fv(glGetUniformLocation(m_shaderProgram, "u_lights[0].color"), 1, color);
|
glUniform4fv(u_lightLocs[0][0], 1, color);
|
||||||
glUniform4fv(glGetUniformLocation(m_shaderProgram, "u_lights[0].position"), 1, blank);
|
glUniform4fv(u_lightLocs[0][1], 1, blank);
|
||||||
glUniform4fv(glGetUniformLocation(m_shaderProgram, "u_lights[0].direction"), 1, blank);
|
glUniform4fv(u_lightLocs[0][2], 1, blank);
|
||||||
glUniform1i(glGetUniformLocation(m_shaderProgram, "u_lightCount"), 1);
|
glUniform1i(m_lightCountLoc, 1);
|
||||||
|
|
||||||
glUniform4f(glGetUniformLocation(m_shaderProgram, "u_color"), 1.0f, 1.0f, 1.0f, 1.0f);
|
glUniform4f(m_colorLoc, 1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
glUniform1f(glGetUniformLocation(m_shaderProgram, "u_shininess"), 0.0f);
|
glUniform1f(m_shinLoc, 0.0f);
|
||||||
|
|
||||||
const GLES2TextureCacheEntry& texture = m_textures[textureId];
|
const GLES2TextureCacheEntry& texture = m_textures[textureId];
|
||||||
float scaleX = static_cast<float>(dstRect.w) / srcRect.w;
|
float scaleX = static_cast<float>(dstRect.w) / srcRect.w;
|
||||||
@ -632,19 +645,19 @@ void OpenGLES2Renderer::Draw2DImage(Uint32 textureId, const SDL_Rect& srcRect, c
|
|||||||
modelView
|
modelView
|
||||||
);
|
);
|
||||||
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(m_shaderProgram, "u_modelViewMatrix"), 1, GL_FALSE, &modelView[0][0]);
|
glUniformMatrix4fv(m_modelViewMatrixLoc, 1, GL_FALSE, &modelView[0][0]);
|
||||||
Matrix3x3 identity = {{1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 0.f, 1.f}};
|
Matrix3x3 identity = {{1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 0.f, 1.f}};
|
||||||
glUniformMatrix3fv(glGetUniformLocation(m_shaderProgram, "u_normalMatrix"), 1, GL_FALSE, &identity[0][0]);
|
glUniformMatrix3fv(m_normalMatrixLoc, 1, GL_FALSE, &identity[0][0]);
|
||||||
CreateOrthographicProjection((float) m_width, (float) m_height, projection);
|
CreateOrthographicProjection((float) m_width, (float) m_height, projection);
|
||||||
glUniformMatrix4fv(glGetUniformLocation(m_shaderProgram, "u_projectionMatrix"), 1, GL_FALSE, &projection[0][0]);
|
glUniformMatrix4fv(m_projectionMatrixLoc, 1, GL_FALSE, &projection[0][0]);
|
||||||
|
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glUniform1i(glGetUniformLocation(m_shaderProgram, "u_useTexture"), 1);
|
glUniform1i(m_useTextureLoc, 1);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture.glTextureId);
|
glBindTexture(GL_TEXTURE_2D, texture.glTextureId);
|
||||||
glUniform1i(glGetUniformLocation(m_shaderProgram, "u_texture"), 0);
|
glUniform1i(m_textureLoc, 0);
|
||||||
|
|
||||||
glEnable(GL_SCISSOR_TEST);
|
glEnable(GL_SCISSOR_TEST);
|
||||||
glScissor(
|
glScissor(
|
||||||
@ -656,20 +669,18 @@ void OpenGLES2Renderer::Draw2DImage(Uint32 textureId, const SDL_Rect& srcRect, c
|
|||||||
static_cast<int>(std::round(dstRect.h * m_viewportTransform.scale))
|
static_cast<int>(std::round(dstRect.h * m_viewportTransform.scale))
|
||||||
);
|
);
|
||||||
|
|
||||||
GLint posLoc = glGetAttribLocation(m_shaderProgram, "a_position");
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_uiMeshCache.vboPositions);
|
glBindBuffer(GL_ARRAY_BUFFER, m_uiMeshCache.vboPositions);
|
||||||
glEnableVertexAttribArray(posLoc);
|
glEnableVertexAttribArray(m_posLoc);
|
||||||
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
|
glVertexAttribPointer(m_posLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
|
||||||
|
|
||||||
GLint texLoc = glGetAttribLocation(m_shaderProgram, "a_texCoord");
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_uiMeshCache.vboTexcoords);
|
glBindBuffer(GL_ARRAY_BUFFER, m_uiMeshCache.vboTexcoords);
|
||||||
glEnableVertexAttribArray(texLoc);
|
glEnableVertexAttribArray(m_texLoc);
|
||||||
glVertexAttribPointer(texLoc, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
|
glVertexAttribPointer(m_texLoc, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_uiMeshCache.ibo);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_uiMeshCache.ibo);
|
||||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_uiMeshCache.indices.size()), GL_UNSIGNED_SHORT, nullptr);
|
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_uiMeshCache.indices.size()), GL_UNSIGNED_SHORT, nullptr);
|
||||||
|
|
||||||
glDisableVertexAttribArray(texLoc);
|
glDisableVertexAttribArray(m_texLoc);
|
||||||
glDisable(GL_SCISSOR_TEST);
|
glDisable(GL_SCISSOR_TEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -72,6 +72,18 @@ class OpenGLES2Renderer : public Direct3DRMRenderer {
|
|||||||
std::vector<SceneLight> m_lights;
|
std::vector<SceneLight> m_lights;
|
||||||
SDL_GLContext m_context;
|
SDL_GLContext m_context;
|
||||||
GLuint m_shaderProgram;
|
GLuint m_shaderProgram;
|
||||||
|
GLint m_posLoc;
|
||||||
|
GLint m_normLoc;
|
||||||
|
GLint m_texLoc;
|
||||||
|
GLint m_colorLoc;
|
||||||
|
GLint m_shinLoc;
|
||||||
|
GLint m_lightCountLoc;
|
||||||
|
GLint m_useTextureLoc;
|
||||||
|
GLint m_textureLoc;
|
||||||
|
GLint u_lightLocs[3][3];
|
||||||
|
GLint m_modelViewMatrixLoc;
|
||||||
|
GLint m_normalMatrixLoc;
|
||||||
|
GLint m_projectionMatrixLoc;
|
||||||
ViewportTransform m_viewportTransform;
|
ViewportTransform m_viewportTransform;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user