OpenGL State: Change setters so they don't directly write to curstate

This commit is contained in:
James Rowe 2017-11-25 13:54:35 -07:00
parent 13606a6d0b
commit 160ac25527
3 changed files with 101 additions and 49 deletions

View file

@ -36,7 +36,7 @@ public:
if (handle == 0) if (handle == 0)
return; return;
glDeleteTextures(1, &handle); glDeleteTextures(1, &handle);
OpenGLState::ResetTexture(handle); OpenGLState::GetCurState().ResetTexture(handle).Apply();
handle = 0; handle = 0;
} }
@ -69,7 +69,7 @@ public:
if (handle == 0) if (handle == 0)
return; return;
glDeleteSamplers(1, &handle); glDeleteSamplers(1, &handle);
OpenGLState::ResetSampler(handle); OpenGLState::GetCurState().ResetSampler(handle).Apply();
handle = 0; handle = 0;
} }
@ -102,7 +102,7 @@ public:
if (handle == 0) if (handle == 0)
return; return;
glDeleteProgram(handle); glDeleteProgram(handle);
OpenGLState::ResetProgram(handle); OpenGLState::GetCurState().ResetProgram(handle).Apply();
handle = 0; handle = 0;
} }
@ -135,7 +135,7 @@ public:
if (handle == 0) if (handle == 0)
return; return;
glDeleteBuffers(1, &handle); glDeleteBuffers(1, &handle);
OpenGLState::ResetBuffer(handle); OpenGLState::GetCurState().OpenGLState::ResetBuffer(handle).Apply();
handle = 0; handle = 0;
} }
@ -168,7 +168,7 @@ public:
if (handle == 0) if (handle == 0)
return; return;
glDeleteVertexArrays(1, &handle); glDeleteVertexArrays(1, &handle);
OpenGLState::ResetVertexArray(handle); OpenGLState::GetCurState().ResetVertexArray(handle).Apply();
handle = 0; handle = 0;
} }
@ -201,7 +201,7 @@ public:
if (handle == 0) if (handle == 0)
return; return;
glDeleteFramebuffers(1, &handle); glDeleteFramebuffers(1, &handle);
OpenGLState::ResetFramebuffer(handle); OpenGLState::GetCurState().ResetFramebuffer(handle).Apply();
handle = 0; handle = 0;
} }

View file

@ -69,6 +69,17 @@ OpenGLState::OpenGLState() {
draw.uniform_buffer = 0; draw.uniform_buffer = 0;
draw.shader_program = 0; draw.shader_program = 0;
scissor.enabled = false;
scissor.x = 0;
scissor.y = 0;
scissor.width = 0;
scissor.height = 0;
viewport.x = 0;
viewport.y = 0;
viewport.width = 0;
viewport.height = 0;
clip_distance = {}; clip_distance = {};
} }
@ -193,7 +204,7 @@ 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(TextureUnits::LightingLUT.Enum()); glActiveTexture(TextureUnits::LightingLUT.Enum());
glBindTexture(GL_TEXTURE_BUFFER, cur_state.lighting_lut.texture_buffer); glBindTexture(GL_TEXTURE_BUFFER, lighting_lut.texture_buffer);
} }
// Fog LUT // Fog LUT
@ -260,6 +271,26 @@ void OpenGLState::Apply() const {
glUseProgram(draw.shader_program); glUseProgram(draw.shader_program);
} }
// Scissor test
if (scissor.enabled != cur_state.scissor.enabled) {
if (scissor.enabled) {
glEnable(GL_SCISSOR_TEST);
} else {
glDisable(GL_SCISSOR_TEST);
}
}
if (scissor.x != cur_state.scissor.x || scissor.y != cur_state.scissor.y ||
scissor.width != cur_state.scissor.width || scissor.height != cur_state.scissor.height) {
glScissor(scissor.x, scissor.y, scissor.width, scissor.height);
}
if (viewport.x != cur_state.viewport.x || viewport.y != cur_state.viewport.y ||
viewport.width != cur_state.viewport.width ||
viewport.height != cur_state.viewport.height) {
glViewport(viewport.x, viewport.y, viewport.width, viewport.height);
}
// Clip distance // Clip distance
for (size_t i = 0; i < clip_distance.size(); ++i) { for (size_t i = 0; i < clip_distance.size(); ++i) {
if (clip_distance[i] != cur_state.clip_distance[i]) { if (clip_distance[i] != cur_state.clip_distance[i]) {
@ -274,62 +305,68 @@ void OpenGLState::Apply() const {
cur_state = *this; cur_state = *this;
} }
void OpenGLState::ResetTexture(GLuint handle) { OpenGLState& OpenGLState::ResetTexture(GLuint handle) {
for (auto& unit : cur_state.texture_units) { for (auto& unit : texture_units) {
if (unit.texture_2d == handle) { if (unit.texture_2d == handle) {
unit.texture_2d = 0; unit.texture_2d = 0;
} }
} }
if (cur_state.lighting_lut.texture_buffer == handle) if (lighting_lut.texture_buffer == handle)
cur_state.lighting_lut.texture_buffer = 0; lighting_lut.texture_buffer = 0;
if (cur_state.fog_lut.texture_buffer == handle) if (fog_lut.texture_buffer == handle)
cur_state.fog_lut.texture_buffer = 0; fog_lut.texture_buffer = 0;
if (cur_state.proctex_noise_lut.texture_buffer == handle) if (proctex_noise_lut.texture_buffer == handle)
cur_state.proctex_noise_lut.texture_buffer = 0; proctex_noise_lut.texture_buffer = 0;
if (cur_state.proctex_color_map.texture_buffer == handle) if (proctex_color_map.texture_buffer == handle)
cur_state.proctex_color_map.texture_buffer = 0; proctex_color_map.texture_buffer = 0;
if (cur_state.proctex_alpha_map.texture_buffer == handle) if (proctex_alpha_map.texture_buffer == handle)
cur_state.proctex_alpha_map.texture_buffer = 0; proctex_alpha_map.texture_buffer = 0;
if (cur_state.proctex_lut.texture_buffer == handle) if (proctex_lut.texture_buffer == handle)
cur_state.proctex_lut.texture_buffer = 0; proctex_lut.texture_buffer = 0;
if (cur_state.proctex_diff_lut.texture_buffer == handle) if (proctex_diff_lut.texture_buffer == handle)
cur_state.proctex_diff_lut.texture_buffer = 0; proctex_diff_lut.texture_buffer = 0;
return *this;
} }
void OpenGLState::ResetSampler(GLuint handle) { OpenGLState& OpenGLState::ResetSampler(GLuint handle) {
for (auto& unit : cur_state.texture_units) { for (auto& unit : texture_units) {
if (unit.sampler == handle) { if (unit.sampler == handle) {
unit.sampler = 0; unit.sampler = 0;
} }
} }
return *this;
} }
void OpenGLState::ResetProgram(GLuint handle) { OpenGLState& OpenGLState::ResetProgram(GLuint handle) {
if (cur_state.draw.shader_program == handle) { if (draw.shader_program == handle) {
cur_state.draw.shader_program = 0; draw.shader_program = 0;
} }
return *this;
} }
void OpenGLState::ResetBuffer(GLuint handle) { OpenGLState& OpenGLState::ResetBuffer(GLuint handle) {
if (cur_state.draw.vertex_buffer == handle) { if (draw.vertex_buffer == handle) {
cur_state.draw.vertex_buffer = 0; draw.vertex_buffer = 0;
} }
if (cur_state.draw.uniform_buffer == handle) { if (draw.uniform_buffer == handle) {
cur_state.draw.uniform_buffer = 0; draw.uniform_buffer = 0;
} }
return *this;
} }
void OpenGLState::ResetVertexArray(GLuint handle) { OpenGLState& OpenGLState::ResetVertexArray(GLuint handle) {
if (cur_state.draw.vertex_array == handle) { if (draw.vertex_array == handle) {
cur_state.draw.vertex_array = 0; draw.vertex_array = 0;
} }
return *this;
} }
void OpenGLState::ResetFramebuffer(GLuint handle) { OpenGLState& OpenGLState::ResetFramebuffer(GLuint handle) {
if (cur_state.draw.read_framebuffer == handle) { if (draw.read_framebuffer == handle) {
cur_state.draw.read_framebuffer = 0; draw.read_framebuffer = 0;
} }
if (cur_state.draw.draw_framebuffer == handle) { if (draw.draw_framebuffer == handle) {
cur_state.draw.draw_framebuffer = 0; draw.draw_framebuffer = 0;
} }
return *this;
} }

View file

@ -124,25 +124,40 @@ public:
GLuint shader_program; // GL_CURRENT_PROGRAM GLuint shader_program; // GL_CURRENT_PROGRAM
} draw; } draw;
struct {
bool enabled; // GL_SCISSOR_TEST
GLint x;
GLint y;
GLsizei width;
GLsizei height;
} scissor;
struct {
GLint x;
GLint y;
GLsizei width;
GLsizei height;
} viewport;
std::array<bool, 2> clip_distance; // GL_CLIP_DISTANCE std::array<bool, 2> clip_distance; // GL_CLIP_DISTANCE
OpenGLState(); OpenGLState();
/// Get the currently active OpenGL state /// Get the currently active OpenGL state
static const OpenGLState& GetCurState() { static OpenGLState& GetCurState() {
return cur_state; return cur_state;
} }
/// Apply this state as the current OpenGL state /// Apply this state as the current OpenGL state
void Apply() const; void Apply() const;
/// Resets and unbinds any references to the given resource in the current OpenGL state /// Resets any references to the given resource
static void ResetTexture(GLuint handle); OpenGLState& ResetTexture(GLuint handle);
static void ResetSampler(GLuint handle); OpenGLState& ResetSampler(GLuint handle);
static void ResetProgram(GLuint handle); OpenGLState& ResetProgram(GLuint handle);
static void ResetBuffer(GLuint handle); OpenGLState& ResetBuffer(GLuint handle);
static void ResetVertexArray(GLuint handle); OpenGLState& ResetVertexArray(GLuint handle);
static void ResetFramebuffer(GLuint handle); OpenGLState& ResetFramebuffer(GLuint handle);
private: private:
static OpenGLState cur_state; static OpenGLState cur_state;