gl_rasterizer: manage texture ids in one place

master
wwylele 2017-06-16 16:16:28 +07:00
parent ab60414122
commit be9e952bdc
3 changed files with 55 additions and 31 deletions

@ -102,7 +102,7 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
glBufferData(GL_TEXTURE_BUFFER, glBufferData(GL_TEXTURE_BUFFER,
sizeof(GLfloat) * 2 * 256 * Pica::LightingRegs::NumLightingSampler, nullptr, sizeof(GLfloat) * 2 * 256 * Pica::LightingRegs::NumLightingSampler, nullptr,
GL_DYNAMIC_DRAW); GL_DYNAMIC_DRAW);
glActiveTexture(GL_TEXTURE15); glActiveTexture(TextureUnits::LightingLUT.Enum());
glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, lighting_lut_buffer.handle); glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, lighting_lut_buffer.handle);
// Setup the LUT for the fog // Setup the LUT for the fog
@ -112,7 +112,7 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
} }
state.Apply(); state.Apply();
glActiveTexture(GL_TEXTURE9); glActiveTexture(TextureUnits::FogLUT.Enum());
glTexImage1D(GL_TEXTURE_1D, 0, GL_R32UI, 128, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, nullptr); glTexImage1D(GL_TEXTURE_1D, 0, GL_R32UI, 128, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -121,7 +121,7 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
proctex_noise_lut.Create(); proctex_noise_lut.Create();
state.proctex_noise_lut.texture_1d = proctex_noise_lut.handle; state.proctex_noise_lut.texture_1d = proctex_noise_lut.handle;
state.Apply(); state.Apply();
glActiveTexture(GL_TEXTURE10); glActiveTexture(TextureUnits::ProcTexNoiseLUT.Enum());
glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr); glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -130,7 +130,7 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
proctex_color_map.Create(); proctex_color_map.Create();
state.proctex_color_map.texture_1d = proctex_color_map.handle; state.proctex_color_map.texture_1d = proctex_color_map.handle;
state.Apply(); state.Apply();
glActiveTexture(GL_TEXTURE11); glActiveTexture(TextureUnits::ProcTexColorMap.Enum());
glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr); glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -139,7 +139,7 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
proctex_alpha_map.Create(); proctex_alpha_map.Create();
state.proctex_alpha_map.texture_1d = proctex_alpha_map.handle; state.proctex_alpha_map.texture_1d = proctex_alpha_map.handle;
state.Apply(); state.Apply();
glActiveTexture(GL_TEXTURE12); glActiveTexture(TextureUnits::ProcTexAlphaMap.Enum());
glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr); glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -148,7 +148,7 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
proctex_lut.Create(); proctex_lut.Create();
state.proctex_lut.texture_1d = proctex_lut.handle; state.proctex_lut.texture_1d = proctex_lut.handle;
state.Apply(); state.Apply();
glActiveTexture(GL_TEXTURE13); glActiveTexture(TextureUnits::ProcTexLUT.Enum());
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr); glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -157,7 +157,7 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
proctex_diff_lut.Create(); proctex_diff_lut.Create();
state.proctex_diff_lut.texture_1d = proctex_diff_lut.handle; state.proctex_diff_lut.texture_1d = proctex_diff_lut.handle;
state.Apply(); state.Apply();
glActiveTexture(GL_TEXTURE14); glActiveTexture(TextureUnits::ProcTexDiffLUT.Enum());
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr); glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -1185,55 +1185,55 @@ void RasterizerOpenGL::SetShader() {
// Set the texture samplers to correspond to different texture units // Set the texture samplers to correspond to different texture units
GLuint uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[0]"); GLuint uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[0]");
if (uniform_tex != -1) { if (uniform_tex != -1) {
glUniform1i(uniform_tex, 0); glUniform1i(uniform_tex, TextureUnits::PicaTexture(0).id);
} }
uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[1]"); uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[1]");
if (uniform_tex != -1) { if (uniform_tex != -1) {
glUniform1i(uniform_tex, 1); glUniform1i(uniform_tex, TextureUnits::PicaTexture(1).id);
} }
uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[2]"); uniform_tex = glGetUniformLocation(shader->shader.handle, "tex[2]");
if (uniform_tex != -1) { if (uniform_tex != -1) {
glUniform1i(uniform_tex, 2); glUniform1i(uniform_tex, TextureUnits::PicaTexture(2).id);
} }
// Set the texture samplers to correspond to different lookup table texture units // Set the texture samplers to correspond to different lookup table texture units
GLuint uniform_lut = glGetUniformLocation(shader->shader.handle, "lighting_lut"); GLuint uniform_lut = glGetUniformLocation(shader->shader.handle, "lighting_lut");
if (uniform_lut != -1) { if (uniform_lut != -1) {
glUniform1i(uniform_lut, 15); glUniform1i(uniform_lut, TextureUnits::LightingLUT.id);
} }
GLuint uniform_fog_lut = glGetUniformLocation(shader->shader.handle, "fog_lut"); GLuint uniform_fog_lut = glGetUniformLocation(shader->shader.handle, "fog_lut");
if (uniform_fog_lut != -1) { if (uniform_fog_lut != -1) {
glUniform1i(uniform_fog_lut, 9); glUniform1i(uniform_fog_lut, TextureUnits::FogLUT.id);
} }
GLuint uniform_proctex_noise_lut = GLuint uniform_proctex_noise_lut =
glGetUniformLocation(shader->shader.handle, "proctex_noise_lut"); glGetUniformLocation(shader->shader.handle, "proctex_noise_lut");
if (uniform_proctex_noise_lut != -1) { if (uniform_proctex_noise_lut != -1) {
glUniform1i(uniform_proctex_noise_lut, 10); glUniform1i(uniform_proctex_noise_lut, TextureUnits::ProcTexNoiseLUT.id);
} }
GLuint uniform_proctex_color_map = GLuint uniform_proctex_color_map =
glGetUniformLocation(shader->shader.handle, "proctex_color_map"); glGetUniformLocation(shader->shader.handle, "proctex_color_map");
if (uniform_proctex_color_map != -1) { if (uniform_proctex_color_map != -1) {
glUniform1i(uniform_proctex_color_map, 11); glUniform1i(uniform_proctex_color_map, TextureUnits::ProcTexColorMap.id);
} }
GLuint uniform_proctex_alpha_map = GLuint uniform_proctex_alpha_map =
glGetUniformLocation(shader->shader.handle, "proctex_alpha_map"); glGetUniformLocation(shader->shader.handle, "proctex_alpha_map");
if (uniform_proctex_alpha_map != -1) { if (uniform_proctex_alpha_map != -1) {
glUniform1i(uniform_proctex_alpha_map, 12); glUniform1i(uniform_proctex_alpha_map, TextureUnits::ProcTexAlphaMap.id);
} }
GLuint uniform_proctex_lut = glGetUniformLocation(shader->shader.handle, "proctex_lut"); GLuint uniform_proctex_lut = glGetUniformLocation(shader->shader.handle, "proctex_lut");
if (uniform_proctex_lut != -1) { if (uniform_proctex_lut != -1) {
glUniform1i(uniform_proctex_lut, 13); glUniform1i(uniform_proctex_lut, TextureUnits::ProcTexLUT.id);
} }
GLuint uniform_proctex_diff_lut = GLuint uniform_proctex_diff_lut =
glGetUniformLocation(shader->shader.handle, "proctex_diff_lut"); glGetUniformLocation(shader->shader.handle, "proctex_diff_lut");
if (uniform_proctex_diff_lut != -1) { if (uniform_proctex_diff_lut != -1) {
glUniform1i(uniform_proctex_diff_lut, 14); glUniform1i(uniform_proctex_diff_lut, TextureUnits::ProcTexDiffLUT.id);
} }
current_shader = shader_cache.emplace(config, std::move(shader)).first->second.get(); current_shader = shader_cache.emplace(config, std::move(shader)).first->second.get();
@ -1363,7 +1363,7 @@ void RasterizerOpenGL::SyncFogLUT() {
if (new_data != fog_lut_data) { if (new_data != fog_lut_data) {
fog_lut_data = new_data; fog_lut_data = new_data;
glActiveTexture(GL_TEXTURE9); glActiveTexture(TextureUnits::FogLUT.Enum());
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 128, GL_RED_INTEGER, GL_UNSIGNED_INT, glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 128, GL_RED_INTEGER, GL_UNSIGNED_INT,
fog_lut_data.data()); fog_lut_data.data());
} }
@ -1402,17 +1402,18 @@ static void SyncProcTexValueLUT(const std::array<Pica::State::ProcTex::ValueEntr
} }
void RasterizerOpenGL::SyncProcTexNoiseLUT() { void RasterizerOpenGL::SyncProcTexNoiseLUT() {
SyncProcTexValueLUT(Pica::g_state.proctex.noise_table, proctex_noise_lut_data, GL_TEXTURE10); SyncProcTexValueLUT(Pica::g_state.proctex.noise_table, proctex_noise_lut_data,
TextureUnits::ProcTexNoiseLUT.Enum());
} }
void RasterizerOpenGL::SyncProcTexColorMap() { void RasterizerOpenGL::SyncProcTexColorMap() {
SyncProcTexValueLUT(Pica::g_state.proctex.color_map_table, proctex_color_map_data, SyncProcTexValueLUT(Pica::g_state.proctex.color_map_table, proctex_color_map_data,
GL_TEXTURE11); TextureUnits::ProcTexColorMap.Enum());
} }
void RasterizerOpenGL::SyncProcTexAlphaMap() { void RasterizerOpenGL::SyncProcTexAlphaMap() {
SyncProcTexValueLUT(Pica::g_state.proctex.alpha_map_table, proctex_alpha_map_data, SyncProcTexValueLUT(Pica::g_state.proctex.alpha_map_table, proctex_alpha_map_data,
GL_TEXTURE12); TextureUnits::ProcTexAlphaMap.Enum());
} }
void RasterizerOpenGL::SyncProcTexLUT() { void RasterizerOpenGL::SyncProcTexLUT() {
@ -1427,7 +1428,7 @@ void RasterizerOpenGL::SyncProcTexLUT() {
if (new_data != proctex_lut_data) { if (new_data != proctex_lut_data) {
proctex_lut_data = new_data; proctex_lut_data = new_data;
glActiveTexture(GL_TEXTURE13); glActiveTexture(TextureUnits::ProcTexLUT.Enum());
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, proctex_lut_data.data()); glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, proctex_lut_data.data());
} }
} }
@ -1444,7 +1445,7 @@ void RasterizerOpenGL::SyncProcTexDiffLUT() {
if (new_data != proctex_diff_lut_data) { if (new_data != proctex_diff_lut_data) {
proctex_diff_lut_data = new_data; proctex_diff_lut_data = new_data;
glActiveTexture(GL_TEXTURE14); glActiveTexture(TextureUnits::ProcTexDiffLUT.Enum());
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, proctex_diff_lut_data.data()); glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, proctex_diff_lut_data.data());
} }
} }

@ -183,7 +183,7 @@ void OpenGLState::Apply() const {
// Textures // Textures
for (unsigned i = 0; i < ARRAY_SIZE(texture_units); ++i) { for (unsigned i = 0; i < ARRAY_SIZE(texture_units); ++i) {
if (texture_units[i].texture_2d != cur_state.texture_units[i].texture_2d) { if (texture_units[i].texture_2d != cur_state.texture_units[i].texture_2d) {
glActiveTexture(GL_TEXTURE0 + i); glActiveTexture(TextureUnits::PicaTexture(i).Enum());
glBindTexture(GL_TEXTURE_2D, texture_units[i].texture_2d); glBindTexture(GL_TEXTURE_2D, texture_units[i].texture_2d);
} }
if (texture_units[i].sampler != cur_state.texture_units[i].sampler) { if (texture_units[i].sampler != cur_state.texture_units[i].sampler) {
@ -193,43 +193,43 @@ void OpenGLState::Apply() const {
// Lighting LUTs // Lighting LUTs
if (lighting_lut.texture_buffer != cur_state.lighting_lut.texture_buffer) { if (lighting_lut.texture_buffer != cur_state.lighting_lut.texture_buffer) {
glActiveTexture(GL_TEXTURE15); glActiveTexture(TextureUnits::LightingLUT.Enum());
glBindTexture(GL_TEXTURE_BUFFER, cur_state.lighting_lut.texture_buffer); glBindTexture(GL_TEXTURE_BUFFER, cur_state.lighting_lut.texture_buffer);
} }
// Fog LUT // Fog LUT
if (fog_lut.texture_1d != cur_state.fog_lut.texture_1d) { if (fog_lut.texture_1d != cur_state.fog_lut.texture_1d) {
glActiveTexture(GL_TEXTURE9); glActiveTexture(TextureUnits::FogLUT.Enum());
glBindTexture(GL_TEXTURE_1D, fog_lut.texture_1d); glBindTexture(GL_TEXTURE_1D, fog_lut.texture_1d);
} }
// ProcTex Noise LUT // ProcTex Noise LUT
if (proctex_noise_lut.texture_1d != cur_state.proctex_noise_lut.texture_1d) { if (proctex_noise_lut.texture_1d != cur_state.proctex_noise_lut.texture_1d) {
glActiveTexture(GL_TEXTURE10); glActiveTexture(TextureUnits::ProcTexNoiseLUT.Enum());
glBindTexture(GL_TEXTURE_1D, proctex_noise_lut.texture_1d); glBindTexture(GL_TEXTURE_1D, proctex_noise_lut.texture_1d);
} }
// ProcTex Color Map // ProcTex Color Map
if (proctex_color_map.texture_1d != cur_state.proctex_color_map.texture_1d) { if (proctex_color_map.texture_1d != cur_state.proctex_color_map.texture_1d) {
glActiveTexture(GL_TEXTURE11); glActiveTexture(TextureUnits::ProcTexColorMap.Enum());
glBindTexture(GL_TEXTURE_1D, proctex_color_map.texture_1d); glBindTexture(GL_TEXTURE_1D, proctex_color_map.texture_1d);
} }
// ProcTex Alpha Map // ProcTex Alpha Map
if (proctex_alpha_map.texture_1d != cur_state.proctex_alpha_map.texture_1d) { if (proctex_alpha_map.texture_1d != cur_state.proctex_alpha_map.texture_1d) {
glActiveTexture(GL_TEXTURE12); glActiveTexture(TextureUnits::ProcTexAlphaMap.Enum());
glBindTexture(GL_TEXTURE_1D, proctex_alpha_map.texture_1d); glBindTexture(GL_TEXTURE_1D, proctex_alpha_map.texture_1d);
} }
// ProcTex LUT // ProcTex LUT
if (proctex_lut.texture_1d != cur_state.proctex_lut.texture_1d) { if (proctex_lut.texture_1d != cur_state.proctex_lut.texture_1d) {
glActiveTexture(GL_TEXTURE13); glActiveTexture(TextureUnits::ProcTexLUT.Enum());
glBindTexture(GL_TEXTURE_1D, proctex_lut.texture_1d); glBindTexture(GL_TEXTURE_1D, proctex_lut.texture_1d);
} }
// ProcTex Diff LUT // ProcTex Diff LUT
if (proctex_diff_lut.texture_1d != cur_state.proctex_diff_lut.texture_1d) { if (proctex_diff_lut.texture_1d != cur_state.proctex_diff_lut.texture_1d) {
glActiveTexture(GL_TEXTURE14); glActiveTexture(TextureUnits::ProcTexDiffLUT.Enum());
glBindTexture(GL_TEXTURE_1D, proctex_diff_lut.texture_1d); glBindTexture(GL_TEXTURE_1D, proctex_diff_lut.texture_1d);
} }

@ -6,6 +6,29 @@
#include <glad/glad.h> #include <glad/glad.h>
namespace TextureUnits {
struct TextureUnit {
GLint id;
constexpr GLenum Enum() const {
return static_cast<GLenum>(GL_TEXTURE0 + id);
}
};
constexpr TextureUnit PicaTexture(int unit) {
return TextureUnit{unit};
}
constexpr TextureUnit LightingLUT{3};
constexpr TextureUnit FogLUT{4};
constexpr TextureUnit ProcTexNoiseLUT{5};
constexpr TextureUnit ProcTexColorMap{6};
constexpr TextureUnit ProcTexAlphaMap{7};
constexpr TextureUnit ProcTexLUT{8};
constexpr TextureUnit ProcTexDiffLUT{9};
} // namespace TextureUnits
class OpenGLState { class OpenGLState {
public: public:
struct { struct {