2015-05-19 06:21:33 +02:00
|
|
|
// Copyright 2015 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-04-20 04:36:48 +02:00
|
|
|
#include <iterator>
|
2016-04-30 17:34:51 +02:00
|
|
|
#include <glad/glad.h>
|
2018-09-22 00:22:40 +02:00
|
|
|
#include "common/assert.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "common/logging/log.h"
|
2019-07-10 02:58:30 +02:00
|
|
|
#include "common/microprofile.h"
|
2016-09-21 08:52:38 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_state.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
|
2019-07-10 02:58:30 +02:00
|
|
|
MICROPROFILE_DEFINE(OpenGL_State, "OpenGL", "State Change", MP_RGB(192, 128, 128));
|
|
|
|
|
2018-08-21 10:18:27 +02:00
|
|
|
namespace OpenGL {
|
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
using Maxwell = Tegra::Engines::Maxwell3D::Regs;
|
2019-02-15 05:30:14 +01:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
OpenGLState OpenGLState::cur_state;
|
2018-10-24 22:09:40 +02:00
|
|
|
bool OpenGLState::s_rgb_used;
|
2019-02-15 05:30:14 +01:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool UpdateValue(T& current_value, const T new_value) {
|
|
|
|
const bool changed = current_value != new_value;
|
|
|
|
current_value = new_value;
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
bool UpdateTie(T1 current_value, const T2 new_value) {
|
|
|
|
const bool changed = current_value != new_value;
|
|
|
|
current_value = new_value;
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Enable(GLenum cap, bool enable) {
|
|
|
|
if (enable) {
|
|
|
|
glEnable(cap);
|
|
|
|
} else {
|
|
|
|
glDisable(cap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Enable(GLenum cap, GLuint index, bool enable) {
|
|
|
|
if (enable) {
|
|
|
|
glEnablei(cap, index);
|
|
|
|
} else {
|
|
|
|
glDisablei(cap, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Enable(GLenum cap, bool& current_value, bool new_value) {
|
|
|
|
if (UpdateValue(current_value, new_value))
|
|
|
|
Enable(cap, new_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Enable(GLenum cap, GLuint index, bool& current_value, bool new_value) {
|
|
|
|
if (UpdateValue(current_value, new_value))
|
|
|
|
Enable(cap, index, new_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
OpenGLState::OpenGLState() {
|
|
|
|
// These all match default OpenGL values
|
2018-10-24 22:09:40 +02:00
|
|
|
framebuffer_srgb.enabled = false;
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-11-14 04:02:54 +01:00
|
|
|
multisample_control.alpha_to_coverage = false;
|
|
|
|
multisample_control.alpha_to_one = false;
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
cull.enabled = false;
|
|
|
|
cull.mode = GL_BACK;
|
2015-08-31 17:03:21 +02:00
|
|
|
cull.front_face = GL_CCW;
|
2015-05-19 06:21:33 +02:00
|
|
|
|
|
|
|
depth.test_enabled = false;
|
|
|
|
depth.test_func = GL_LESS;
|
|
|
|
depth.write_mask = GL_TRUE;
|
|
|
|
|
2018-10-26 01:04:13 +02:00
|
|
|
primitive_restart.enabled = false;
|
|
|
|
primitive_restart.index = 0;
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-11-05 03:46:06 +01:00
|
|
|
for (auto& item : color_mask) {
|
|
|
|
item.red_enabled = GL_TRUE;
|
|
|
|
item.green_enabled = GL_TRUE;
|
|
|
|
item.blue_enabled = GL_TRUE;
|
|
|
|
item.alpha_enabled = GL_TRUE;
|
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
|
|
|
|
const auto ResetStencil = [](auto& config) {
|
2018-08-22 06:17:54 +02:00
|
|
|
config.test_func = GL_ALWAYS;
|
|
|
|
config.test_ref = 0;
|
|
|
|
config.test_mask = 0xFFFFFFFF;
|
|
|
|
config.write_mask = 0xFFFFFFFF;
|
|
|
|
config.action_depth_fail = GL_KEEP;
|
|
|
|
config.action_depth_pass = GL_KEEP;
|
|
|
|
config.action_stencil_fail = GL_KEEP;
|
|
|
|
};
|
2019-02-27 06:21:33 +01:00
|
|
|
stencil.test_enabled = false;
|
|
|
|
ResetStencil(stencil.front);
|
|
|
|
ResetStencil(stencil.back);
|
|
|
|
|
2018-11-02 04:21:25 +01:00
|
|
|
for (auto& item : viewports) {
|
|
|
|
item.x = 0;
|
|
|
|
item.y = 0;
|
|
|
|
item.width = 0;
|
|
|
|
item.height = 0;
|
|
|
|
item.depth_range_near = 0.0f;
|
|
|
|
item.depth_range_far = 1.0f;
|
2018-11-14 00:13:16 +01:00
|
|
|
item.scissor.enabled = false;
|
|
|
|
item.scissor.x = 0;
|
|
|
|
item.scissor.y = 0;
|
|
|
|
item.scissor.width = 0;
|
|
|
|
item.scissor.height = 0;
|
2018-11-02 04:21:25 +01:00
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-11-02 04:21:25 +01:00
|
|
|
for (auto& item : blend) {
|
|
|
|
item.enabled = true;
|
|
|
|
item.rgb_equation = GL_FUNC_ADD;
|
|
|
|
item.a_equation = GL_FUNC_ADD;
|
|
|
|
item.src_rgb_func = GL_ONE;
|
|
|
|
item.dst_rgb_func = GL_ZERO;
|
|
|
|
item.src_a_func = GL_ONE;
|
|
|
|
item.dst_a_func = GL_ZERO;
|
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-11-02 04:21:25 +01:00
|
|
|
independant_blend.enabled = false;
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-11-02 04:21:25 +01:00
|
|
|
blend_color.red = 0.0f;
|
|
|
|
blend_color.green = 0.0f;
|
|
|
|
blend_color.blue = 0.0f;
|
|
|
|
blend_color.alpha = 0.0f;
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-08-21 01:43:11 +02:00
|
|
|
logic_op.enabled = false;
|
|
|
|
logic_op.operation = GL_COPY;
|
2015-05-26 00:39:03 +02:00
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
for (auto& texture_unit : texture_units) {
|
2018-06-26 22:58:35 +02:00
|
|
|
texture_unit.Reset();
|
2016-02-26 01:12:38 +01:00
|
|
|
}
|
|
|
|
|
2016-04-17 00:57:57 +02:00
|
|
|
draw.read_framebuffer = 0;
|
|
|
|
draw.draw_framebuffer = 0;
|
2015-05-19 06:21:33 +02:00
|
|
|
draw.vertex_array = 0;
|
|
|
|
draw.shader_program = 0;
|
2018-03-19 22:05:15 +01:00
|
|
|
draw.program_pipeline = 0;
|
|
|
|
|
2017-08-17 09:46:59 +02:00
|
|
|
clip_distance = {};
|
2018-09-28 06:31:01 +02:00
|
|
|
|
|
|
|
point.size = 1;
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-11-14 02:09:01 +01:00
|
|
|
fragment_color_clamp.enabled = false;
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-11-23 16:11:21 +01:00
|
|
|
depth_clamp.far_plane = false;
|
|
|
|
depth_clamp.near_plane = false;
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-11-27 00:31:44 +01:00
|
|
|
polygon_offset.fill_enable = false;
|
|
|
|
polygon_offset.line_enable = false;
|
|
|
|
polygon_offset.point_enable = false;
|
|
|
|
polygon_offset.factor = 0.0f;
|
|
|
|
polygon_offset.units = 0.0f;
|
|
|
|
polygon_offset.clamp = 0.0f;
|
2019-05-22 01:28:09 +02:00
|
|
|
|
|
|
|
alpha_test.enabled = false;
|
|
|
|
alpha_test.func = GL_ALWAYS;
|
|
|
|
alpha_test.ref = 0.0f;
|
2015-05-19 06:21:33 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 04:55:53 +01:00
|
|
|
void OpenGLState::ApplyDefaultState() {
|
2019-02-27 06:21:33 +01:00
|
|
|
glEnable(GL_BLEND);
|
2018-10-30 04:55:53 +01:00
|
|
|
glDisable(GL_FRAMEBUFFER_SRGB);
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glDisable(GL_PRIMITIVE_RESTART);
|
|
|
|
glDisable(GL_STENCIL_TEST);
|
|
|
|
glDisable(GL_COLOR_LOGIC_OP);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}
|
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
void OpenGLState::ApplyFramebufferState() const {
|
|
|
|
if (UpdateValue(cur_state.draw.read_framebuffer, draw.read_framebuffer)) {
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
|
|
|
|
}
|
|
|
|
if (UpdateValue(cur_state.draw.draw_framebuffer, draw.draw_framebuffer)) {
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, draw.draw_framebuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::ApplyVertexArrayState() const {
|
|
|
|
if (UpdateValue(cur_state.draw.vertex_array, draw.vertex_array)) {
|
|
|
|
glBindVertexArray(draw.vertex_array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::ApplyShaderProgram() const {
|
|
|
|
if (UpdateValue(cur_state.draw.shader_program, draw.shader_program)) {
|
|
|
|
glUseProgram(draw.shader_program);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::ApplyProgramPipeline() const {
|
|
|
|
if (UpdateValue(cur_state.draw.program_pipeline, draw.program_pipeline)) {
|
|
|
|
glBindProgramPipeline(draw.program_pipeline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::ApplyClipDistances() const {
|
|
|
|
for (std::size_t i = 0; i < clip_distance.size(); ++i) {
|
|
|
|
Enable(GL_CLIP_DISTANCE0 + static_cast<GLenum>(i), cur_state.clip_distance[i],
|
|
|
|
clip_distance[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::ApplyPointSize() const {
|
|
|
|
if (UpdateValue(cur_state.point.size, point.size)) {
|
|
|
|
glPointSize(point.size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::ApplyFragmentColorClamp() const {
|
|
|
|
if (UpdateValue(cur_state.fragment_color_clamp.enabled, fragment_color_clamp.enabled)) {
|
|
|
|
glClampColor(GL_CLAMP_FRAGMENT_COLOR_ARB,
|
|
|
|
fragment_color_clamp.enabled ? GL_TRUE : GL_FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::ApplyMultisample() const {
|
|
|
|
Enable(GL_SAMPLE_ALPHA_TO_COVERAGE, cur_state.multisample_control.alpha_to_coverage,
|
|
|
|
multisample_control.alpha_to_coverage);
|
|
|
|
Enable(GL_SAMPLE_ALPHA_TO_ONE, cur_state.multisample_control.alpha_to_one,
|
|
|
|
multisample_control.alpha_to_one);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::ApplyDepthClamp() const {
|
|
|
|
if (depth_clamp.far_plane == cur_state.depth_clamp.far_plane &&
|
|
|
|
depth_clamp.near_plane == cur_state.depth_clamp.near_plane) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cur_state.depth_clamp = depth_clamp;
|
|
|
|
|
|
|
|
UNIMPLEMENTED_IF_MSG(depth_clamp.far_plane != depth_clamp.near_plane,
|
|
|
|
"Unimplemented Depth Clamp Separation!");
|
|
|
|
|
|
|
|
Enable(GL_DEPTH_CLAMP, depth_clamp.far_plane || depth_clamp.near_plane);
|
|
|
|
}
|
|
|
|
|
2018-10-30 04:55:53 +01:00
|
|
|
void OpenGLState::ApplySRgb() const {
|
2019-02-27 06:21:33 +01:00
|
|
|
if (cur_state.framebuffer_srgb.enabled == framebuffer_srgb.enabled)
|
|
|
|
return;
|
|
|
|
cur_state.framebuffer_srgb.enabled = framebuffer_srgb.enabled;
|
|
|
|
if (framebuffer_srgb.enabled) {
|
|
|
|
// Track if sRGB is used
|
|
|
|
s_rgb_used = true;
|
|
|
|
glEnable(GL_FRAMEBUFFER_SRGB);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_FRAMEBUFFER_SRGB);
|
2018-10-24 22:09:40 +02:00
|
|
|
}
|
2018-10-30 04:55:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::ApplyCulling() const {
|
2019-02-27 06:21:33 +01:00
|
|
|
Enable(GL_CULL_FACE, cur_state.cull.enabled, cull.enabled);
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
if (UpdateValue(cur_state.cull.mode, cull.mode)) {
|
2019-02-15 05:30:14 +01:00
|
|
|
glCullFace(cull.mode);
|
|
|
|
}
|
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
if (UpdateValue(cur_state.cull.front_face, cull.front_face)) {
|
2019-02-15 05:30:14 +01:00
|
|
|
glFrontFace(cull.front_face);
|
2015-08-31 17:03:21 +02:00
|
|
|
}
|
2018-10-30 04:55:53 +01:00
|
|
|
}
|
2015-08-31 17:03:21 +02:00
|
|
|
|
2018-11-05 03:46:06 +01:00
|
|
|
void OpenGLState::ApplyColorMask() const {
|
2019-02-27 06:21:33 +01:00
|
|
|
for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
|
|
|
|
const auto& updated = color_mask[i];
|
|
|
|
auto& current = cur_state.color_mask[i];
|
2018-11-05 03:46:06 +01:00
|
|
|
if (updated.red_enabled != current.red_enabled ||
|
|
|
|
updated.green_enabled != current.green_enabled ||
|
|
|
|
updated.blue_enabled != current.blue_enabled ||
|
|
|
|
updated.alpha_enabled != current.alpha_enabled) {
|
2019-02-27 06:21:33 +01:00
|
|
|
current = updated;
|
|
|
|
glColorMaski(static_cast<GLuint>(i), updated.red_enabled, updated.green_enabled,
|
|
|
|
updated.blue_enabled, updated.alpha_enabled);
|
2018-11-05 03:46:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 04:55:53 +01:00
|
|
|
void OpenGLState::ApplyDepth() const {
|
2019-02-27 06:21:33 +01:00
|
|
|
Enable(GL_DEPTH_TEST, cur_state.depth.test_enabled, depth.test_enabled);
|
2019-02-15 05:30:14 +01:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
if (cur_state.depth.test_func != depth.test_func) {
|
|
|
|
cur_state.depth.test_func = depth.test_func;
|
2015-05-19 06:21:33 +02:00
|
|
|
glDepthFunc(depth.test_func);
|
|
|
|
}
|
2019-02-15 05:30:14 +01:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
if (cur_state.depth.write_mask != depth.write_mask) {
|
|
|
|
cur_state.depth.write_mask = depth.write_mask;
|
2015-05-19 06:21:33 +02:00
|
|
|
glDepthMask(depth.write_mask);
|
|
|
|
}
|
2018-10-30 04:55:53 +01:00
|
|
|
}
|
2018-10-26 00:39:57 +02:00
|
|
|
|
2018-10-30 04:55:53 +01:00
|
|
|
void OpenGLState::ApplyPrimitiveRestart() const {
|
2019-02-27 06:21:33 +01:00
|
|
|
Enable(GL_PRIMITIVE_RESTART, cur_state.primitive_restart.enabled, primitive_restart.enabled);
|
2019-02-15 05:30:14 +01:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
if (cur_state.primitive_restart.index != primitive_restart.index) {
|
|
|
|
cur_state.primitive_restart.index = primitive_restart.index;
|
2018-10-26 01:04:13 +02:00
|
|
|
glPrimitiveRestartIndex(primitive_restart.index);
|
|
|
|
}
|
2018-10-30 04:55:53 +01:00
|
|
|
}
|
2018-10-26 01:04:13 +02:00
|
|
|
|
2018-10-30 04:55:53 +01:00
|
|
|
void OpenGLState::ApplyStencilTest() const {
|
2019-02-27 06:21:33 +01:00
|
|
|
Enable(GL_STENCIL_TEST, cur_state.stencil.test_enabled, stencil.test_enabled);
|
|
|
|
|
|
|
|
const auto ConfigStencil = [](GLenum face, const auto& config, auto& current) {
|
|
|
|
if (current.test_func != config.test_func || current.test_ref != config.test_ref ||
|
|
|
|
current.test_mask != config.test_mask) {
|
|
|
|
current.test_func = config.test_func;
|
|
|
|
current.test_ref = config.test_ref;
|
|
|
|
current.test_mask = config.test_mask;
|
2019-02-15 05:30:14 +01:00
|
|
|
glStencilFuncSeparate(face, config.test_func, config.test_ref, config.test_mask);
|
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
if (current.action_depth_fail != config.action_depth_fail ||
|
|
|
|
current.action_depth_pass != config.action_depth_pass ||
|
|
|
|
current.action_stencil_fail != config.action_stencil_fail) {
|
|
|
|
current.action_depth_fail = config.action_depth_fail;
|
|
|
|
current.action_depth_pass = config.action_depth_pass;
|
|
|
|
current.action_stencil_fail = config.action_stencil_fail;
|
2019-02-15 05:30:14 +01:00
|
|
|
glStencilOpSeparate(face, config.action_stencil_fail, config.action_depth_fail,
|
|
|
|
config.action_depth_pass);
|
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
if (current.write_mask != config.write_mask) {
|
|
|
|
current.write_mask = config.write_mask;
|
2019-02-15 05:30:14 +01:00
|
|
|
glStencilMaskSeparate(face, config.write_mask);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ConfigStencil(GL_FRONT, stencil.front, cur_state.stencil.front);
|
|
|
|
ConfigStencil(GL_BACK, stencil.back, cur_state.stencil.back);
|
2018-10-30 04:55:53 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 04:21:25 +01:00
|
|
|
void OpenGLState::ApplyViewport() const {
|
2019-02-27 06:21:33 +01:00
|
|
|
for (GLuint i = 0; i < static_cast<GLuint>(Maxwell::NumViewports); ++i) {
|
|
|
|
const auto& updated = viewports[i];
|
|
|
|
auto& current = cur_state.viewports[i];
|
|
|
|
|
|
|
|
if (current.x != updated.x || current.y != updated.y || current.width != updated.width ||
|
|
|
|
current.height != updated.height) {
|
|
|
|
current.x = updated.x;
|
|
|
|
current.y = updated.y;
|
|
|
|
current.width = updated.width;
|
|
|
|
current.height = updated.height;
|
|
|
|
glViewportIndexedf(i, static_cast<GLfloat>(updated.x), static_cast<GLfloat>(updated.y),
|
|
|
|
static_cast<GLfloat>(updated.width),
|
|
|
|
static_cast<GLfloat>(updated.height));
|
2018-11-02 04:21:25 +01:00
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
if (current.depth_range_near != updated.depth_range_near ||
|
|
|
|
current.depth_range_far != updated.depth_range_far) {
|
|
|
|
current.depth_range_near = updated.depth_range_near;
|
|
|
|
current.depth_range_far = updated.depth_range_far;
|
|
|
|
glDepthRangeIndexed(i, updated.depth_range_near, updated.depth_range_far);
|
2018-11-14 00:13:16 +01:00
|
|
|
}
|
2019-02-15 05:30:14 +01:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
Enable(GL_SCISSOR_TEST, i, current.scissor.enabled, updated.scissor.enabled);
|
|
|
|
|
|
|
|
if (current.scissor.x != updated.scissor.x || current.scissor.y != updated.scissor.y ||
|
|
|
|
current.scissor.width != updated.scissor.width ||
|
|
|
|
current.scissor.height != updated.scissor.height) {
|
|
|
|
current.scissor.x = updated.scissor.x;
|
|
|
|
current.scissor.y = updated.scissor.y;
|
|
|
|
current.scissor.width = updated.scissor.width;
|
|
|
|
current.scissor.height = updated.scissor.height;
|
|
|
|
glScissorIndexed(i, updated.scissor.x, updated.scissor.y, updated.scissor.width,
|
|
|
|
updated.scissor.height);
|
2018-11-14 00:13:16 +01:00
|
|
|
}
|
2018-11-02 04:21:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::ApplyGlobalBlending() const {
|
|
|
|
const Blend& updated = blend[0];
|
2019-02-27 06:21:33 +01:00
|
|
|
Blend& current = cur_state.blend[0];
|
|
|
|
|
|
|
|
Enable(GL_BLEND, current.enabled, updated.enabled);
|
|
|
|
|
|
|
|
if (current.src_rgb_func != updated.src_rgb_func ||
|
|
|
|
current.dst_rgb_func != updated.dst_rgb_func || current.src_a_func != updated.src_a_func ||
|
|
|
|
current.dst_a_func != updated.dst_a_func) {
|
|
|
|
current.src_rgb_func = updated.src_rgb_func;
|
|
|
|
current.dst_rgb_func = updated.dst_rgb_func;
|
|
|
|
current.src_a_func = updated.src_a_func;
|
|
|
|
current.dst_a_func = updated.dst_a_func;
|
2018-11-18 07:44:48 +01:00
|
|
|
glBlendFuncSeparate(updated.src_rgb_func, updated.dst_rgb_func, updated.src_a_func,
|
|
|
|
updated.dst_a_func);
|
|
|
|
}
|
2018-11-02 04:21:25 +01:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
if (current.rgb_equation != updated.rgb_equation || current.a_equation != updated.a_equation) {
|
|
|
|
current.rgb_equation = updated.rgb_equation;
|
|
|
|
current.a_equation = updated.a_equation;
|
2018-11-18 07:44:48 +01:00
|
|
|
glBlendEquationSeparate(updated.rgb_equation, updated.a_equation);
|
2018-11-02 04:21:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:11 +01:00
|
|
|
void OpenGLState::ApplyTargetBlending(std::size_t target, bool force) const {
|
2018-11-02 04:21:25 +01:00
|
|
|
const Blend& updated = blend[target];
|
2019-02-27 06:21:33 +01:00
|
|
|
Blend& current = cur_state.blend[target];
|
|
|
|
|
|
|
|
if (current.enabled != updated.enabled || force) {
|
|
|
|
current.enabled = updated.enabled;
|
|
|
|
Enable(GL_BLEND, static_cast<GLuint>(target), updated.enabled);
|
2018-11-02 04:21:25 +01:00
|
|
|
}
|
2019-02-15 05:30:14 +01:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
if (UpdateTie(std::tie(current.src_rgb_func, current.dst_rgb_func, current.src_a_func,
|
|
|
|
current.dst_a_func),
|
|
|
|
std::tie(updated.src_rgb_func, updated.dst_rgb_func, updated.src_a_func,
|
|
|
|
updated.dst_a_func))) {
|
2018-11-18 01:28:14 +01:00
|
|
|
glBlendFuncSeparatei(static_cast<GLuint>(target), updated.src_rgb_func,
|
|
|
|
updated.dst_rgb_func, updated.src_a_func, updated.dst_a_func);
|
2018-11-18 07:44:48 +01:00
|
|
|
}
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
if (UpdateTie(std::tie(current.rgb_equation, current.a_equation),
|
|
|
|
std::tie(updated.rgb_equation, updated.a_equation))) {
|
2018-11-18 01:28:14 +01:00
|
|
|
glBlendEquationSeparatei(static_cast<GLuint>(target), updated.rgb_equation,
|
|
|
|
updated.a_equation);
|
2018-11-02 04:21:25 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2018-11-02 04:21:25 +01:00
|
|
|
void OpenGLState::ApplyBlending() const {
|
|
|
|
if (independant_blend.enabled) {
|
2019-02-27 06:21:33 +01:00
|
|
|
const bool force = independant_blend.enabled != cur_state.independant_blend.enabled;
|
|
|
|
for (std::size_t target = 0; target < Maxwell::NumRenderTargets; ++target) {
|
|
|
|
ApplyTargetBlending(target, force);
|
2018-10-30 04:55:53 +01:00
|
|
|
}
|
2018-11-02 04:21:25 +01:00
|
|
|
} else {
|
|
|
|
ApplyGlobalBlending();
|
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
cur_state.independant_blend.enabled = independant_blend.enabled;
|
|
|
|
|
|
|
|
if (UpdateTie(
|
|
|
|
std::tie(cur_state.blend_color.red, cur_state.blend_color.green,
|
|
|
|
cur_state.blend_color.blue, cur_state.blend_color.alpha),
|
|
|
|
std::tie(blend_color.red, blend_color.green, blend_color.blue, blend_color.alpha))) {
|
2018-11-02 04:21:25 +01:00
|
|
|
glBlendColor(blend_color.red, blend_color.green, blend_color.blue, blend_color.alpha);
|
2016-05-12 22:57:15 +02:00
|
|
|
}
|
2018-10-30 04:55:53 +01:00
|
|
|
}
|
2016-05-12 22:57:15 +02:00
|
|
|
|
2018-10-30 04:55:53 +01:00
|
|
|
void OpenGLState::ApplyLogicOp() const {
|
2019-02-27 06:21:33 +01:00
|
|
|
Enable(GL_COLOR_LOGIC_OP, cur_state.logic_op.enabled, logic_op.enabled);
|
2018-08-21 01:43:11 +02:00
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
if (UpdateValue(cur_state.logic_op.operation, logic_op.operation)) {
|
2018-08-21 01:43:11 +02:00
|
|
|
glLogicOp(logic_op.operation);
|
2015-05-26 00:39:03 +02:00
|
|
|
}
|
2018-10-30 04:55:53 +01:00
|
|
|
}
|
2015-05-26 00:39:03 +02:00
|
|
|
|
2018-11-27 00:31:44 +01:00
|
|
|
void OpenGLState::ApplyPolygonOffset() const {
|
2019-02-27 06:21:33 +01:00
|
|
|
Enable(GL_POLYGON_OFFSET_FILL, cur_state.polygon_offset.fill_enable,
|
|
|
|
polygon_offset.fill_enable);
|
|
|
|
Enable(GL_POLYGON_OFFSET_LINE, cur_state.polygon_offset.line_enable,
|
|
|
|
polygon_offset.line_enable);
|
|
|
|
Enable(GL_POLYGON_OFFSET_POINT, cur_state.polygon_offset.point_enable,
|
|
|
|
polygon_offset.point_enable);
|
|
|
|
|
|
|
|
if (UpdateTie(std::tie(cur_state.polygon_offset.factor, cur_state.polygon_offset.units,
|
|
|
|
cur_state.polygon_offset.clamp),
|
|
|
|
std::tie(polygon_offset.factor, polygon_offset.units, polygon_offset.clamp))) {
|
2018-11-27 00:31:44 +01:00
|
|
|
if (GLAD_GL_EXT_polygon_offset_clamp && polygon_offset.clamp != 0) {
|
|
|
|
glPolygonOffsetClamp(polygon_offset.factor, polygon_offset.units, polygon_offset.clamp);
|
|
|
|
} else {
|
|
|
|
UNIMPLEMENTED_IF_MSG(polygon_offset.clamp != 0,
|
|
|
|
"Unimplemented Depth polygon offset clamp.");
|
2019-02-27 06:21:33 +01:00
|
|
|
glPolygonOffset(polygon_offset.factor, polygon_offset.units);
|
2018-11-27 00:31:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 01:28:09 +02:00
|
|
|
void OpenGLState::ApplyAlphaTest() const {
|
|
|
|
Enable(GL_ALPHA_TEST, cur_state.alpha_test.enabled, alpha_test.enabled);
|
|
|
|
if (UpdateTie(std::tie(cur_state.alpha_test.func, cur_state.alpha_test.ref),
|
|
|
|
std::tie(alpha_test.func, alpha_test.ref))) {
|
|
|
|
glAlphaFunc(alpha_test.func, alpha_test.ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 04:55:53 +01:00
|
|
|
void OpenGLState::ApplyTextures() const {
|
2019-01-07 02:36:51 +01:00
|
|
|
bool has_delta{};
|
2019-01-07 03:02:27 +01:00
|
|
|
std::size_t first{};
|
|
|
|
std::size_t last{};
|
2019-02-27 06:21:33 +01:00
|
|
|
std::array<GLuint, Maxwell::NumTextureSamplers> textures;
|
2019-01-07 02:36:51 +01:00
|
|
|
|
2018-07-20 07:04:32 +02:00
|
|
|
for (std::size_t i = 0; i < std::size(texture_units); ++i) {
|
|
|
|
const auto& texture_unit = texture_units[i];
|
2019-02-27 06:21:33 +01:00
|
|
|
auto& cur_state_texture_unit = cur_state.texture_units[i];
|
2019-01-07 02:36:51 +01:00
|
|
|
textures[i] = texture_unit.texture;
|
2019-04-19 22:04:58 +02:00
|
|
|
if (cur_state_texture_unit.texture == textures[i]) {
|
2019-02-27 06:21:33 +01:00
|
|
|
continue;
|
2019-04-19 22:04:58 +02:00
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
cur_state_texture_unit.texture = textures[i];
|
|
|
|
if (!has_delta) {
|
|
|
|
first = i;
|
|
|
|
has_delta = true;
|
2015-08-30 13:41:28 +02:00
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
last = i;
|
2015-05-19 06:21:33 +02:00
|
|
|
}
|
2019-01-07 02:36:51 +01:00
|
|
|
if (has_delta) {
|
|
|
|
glBindTextures(static_cast<GLuint>(first), static_cast<GLsizei>(last - first + 1),
|
2019-02-26 17:37:08 +01:00
|
|
|
textures.data() + first);
|
2019-01-07 02:36:51 +01:00
|
|
|
}
|
2018-10-30 04:55:53 +01:00
|
|
|
}
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2018-10-30 04:55:53 +01:00
|
|
|
void OpenGLState::ApplySamplers() const {
|
|
|
|
bool has_delta{};
|
2019-01-07 03:02:27 +01:00
|
|
|
std::size_t first{};
|
|
|
|
std::size_t last{};
|
2019-02-27 06:21:33 +01:00
|
|
|
std::array<GLuint, Maxwell::NumTextureSamplers> samplers;
|
|
|
|
|
2018-10-30 04:55:53 +01:00
|
|
|
for (std::size_t i = 0; i < std::size(samplers); ++i) {
|
2019-04-19 22:04:58 +02:00
|
|
|
samplers[i] = texture_units[i].sampler;
|
|
|
|
if (cur_state.texture_units[i].sampler == texture_units[i].sampler) {
|
2019-02-27 06:21:33 +01:00
|
|
|
continue;
|
2019-04-19 22:04:58 +02:00
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
cur_state.texture_units[i].sampler = texture_units[i].sampler;
|
|
|
|
if (!has_delta) {
|
|
|
|
first = i;
|
|
|
|
has_delta = true;
|
2018-09-27 02:52:31 +02:00
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
last = i;
|
2018-09-27 02:52:31 +02:00
|
|
|
}
|
2018-10-30 04:55:53 +01:00
|
|
|
if (has_delta) {
|
|
|
|
glBindSamplers(static_cast<GLuint>(first), static_cast<GLsizei>(last - first + 1),
|
2019-02-26 17:37:08 +01:00
|
|
|
samplers.data() + first);
|
2018-10-30 04:55:53 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-27 02:52:31 +02:00
|
|
|
|
2019-07-13 22:52:32 +02:00
|
|
|
void OpenGLState::Apply() {
|
2019-07-10 02:58:30 +02:00
|
|
|
MICROPROFILE_SCOPE(OpenGL_State);
|
2018-11-08 02:27:47 +01:00
|
|
|
ApplyFramebufferState();
|
2019-01-09 06:40:19 +01:00
|
|
|
ApplyVertexArrayState();
|
2019-02-27 06:21:33 +01:00
|
|
|
ApplyShaderProgram();
|
|
|
|
ApplyProgramPipeline();
|
|
|
|
ApplyClipDistances();
|
|
|
|
ApplyPointSize();
|
|
|
|
ApplyFragmentColorClamp();
|
|
|
|
ApplyMultisample();
|
2019-07-13 22:52:32 +02:00
|
|
|
if (dirty.color_mask) {
|
|
|
|
ApplyColorMask();
|
|
|
|
dirty.color_mask = false;
|
|
|
|
}
|
2018-11-23 16:11:21 +01:00
|
|
|
ApplyDepthClamp();
|
2018-11-02 04:21:25 +01:00
|
|
|
ApplyViewport();
|
2019-07-13 22:52:32 +02:00
|
|
|
if (dirty.stencil_state) {
|
|
|
|
ApplyStencilTest();
|
|
|
|
dirty.stencil_state = false;
|
|
|
|
}
|
2018-10-30 04:55:53 +01:00
|
|
|
ApplySRgb();
|
|
|
|
ApplyCulling();
|
|
|
|
ApplyDepth();
|
|
|
|
ApplyPrimitiveRestart();
|
2019-07-13 22:52:32 +02:00
|
|
|
if (dirty.blend_state) {
|
|
|
|
ApplyBlending();
|
|
|
|
dirty.blend_state = false;
|
|
|
|
}
|
2018-10-30 04:55:53 +01:00
|
|
|
ApplyLogicOp();
|
|
|
|
ApplyTextures();
|
|
|
|
ApplySamplers();
|
2019-07-13 22:52:32 +02:00
|
|
|
if (dirty.polygon_offset) {
|
|
|
|
ApplyPolygonOffset();
|
|
|
|
dirty.polygon_offset = false;
|
|
|
|
}
|
2019-05-22 01:28:09 +02:00
|
|
|
ApplyAlphaTest();
|
2019-02-27 06:21:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLState::EmulateViewportWithScissor() {
|
|
|
|
auto& current = viewports[0];
|
|
|
|
if (current.scissor.enabled) {
|
|
|
|
const GLint left = std::max(current.x, current.scissor.x);
|
|
|
|
const GLint right =
|
|
|
|
std::max(current.x + current.width, current.scissor.x + current.scissor.width);
|
|
|
|
const GLint bottom = std::max(current.y, current.scissor.y);
|
|
|
|
const GLint top =
|
|
|
|
std::max(current.y + current.height, current.scissor.y + current.scissor.height);
|
|
|
|
current.scissor.x = std::max(left, 0);
|
|
|
|
current.scissor.y = std::max(bottom, 0);
|
|
|
|
current.scissor.width = std::max(right - left, 0);
|
|
|
|
current.scissor.height = std::max(top - bottom, 0);
|
|
|
|
} else {
|
|
|
|
current.scissor.enabled = true;
|
|
|
|
current.scissor.x = current.x;
|
|
|
|
current.scissor.y = current.y;
|
|
|
|
current.scissor.width = current.width;
|
|
|
|
current.scissor.height = current.height;
|
|
|
|
}
|
2015-05-19 06:21:33 +02:00
|
|
|
}
|
2015-08-06 04:22:06 +02:00
|
|
|
|
2018-06-26 22:58:35 +02:00
|
|
|
OpenGLState& OpenGLState::UnbindTexture(GLuint handle) {
|
2018-03-19 22:05:15 +01:00
|
|
|
for (auto& unit : texture_units) {
|
2018-09-01 05:54:31 +02:00
|
|
|
if (unit.texture == handle) {
|
2018-06-26 22:58:35 +02:00
|
|
|
unit.Unbind();
|
2015-08-06 04:22:06 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-19 22:05:15 +01:00
|
|
|
return *this;
|
2015-08-06 04:22:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:05:15 +01:00
|
|
|
OpenGLState& OpenGLState::ResetSampler(GLuint handle) {
|
|
|
|
for (auto& unit : texture_units) {
|
2016-04-17 00:57:57 +02:00
|
|
|
if (unit.sampler == handle) {
|
2015-08-30 13:41:28 +02:00
|
|
|
unit.sampler = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 22:05:15 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenGLState& OpenGLState::ResetProgram(GLuint handle) {
|
|
|
|
if (draw.shader_program == handle) {
|
|
|
|
draw.shader_program = 0;
|
|
|
|
}
|
|
|
|
return *this;
|
2015-08-30 13:41:28 +02:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:05:15 +01:00
|
|
|
OpenGLState& OpenGLState::ResetPipeline(GLuint handle) {
|
|
|
|
if (draw.program_pipeline == handle) {
|
|
|
|
draw.program_pipeline = 0;
|
2015-08-06 04:22:06 +02:00
|
|
|
}
|
2018-03-19 22:05:15 +01:00
|
|
|
return *this;
|
2015-08-06 04:22:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:05:15 +01:00
|
|
|
OpenGLState& OpenGLState::ResetVertexArray(GLuint handle) {
|
|
|
|
if (draw.vertex_array == handle) {
|
|
|
|
draw.vertex_array = 0;
|
2015-08-06 04:22:06 +02:00
|
|
|
}
|
2018-03-19 22:05:15 +01:00
|
|
|
return *this;
|
2015-08-06 04:22:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:05:15 +01:00
|
|
|
OpenGLState& OpenGLState::ResetFramebuffer(GLuint handle) {
|
|
|
|
if (draw.read_framebuffer == handle) {
|
|
|
|
draw.read_framebuffer = 0;
|
2016-04-17 00:57:57 +02:00
|
|
|
}
|
2018-03-19 22:05:15 +01:00
|
|
|
if (draw.draw_framebuffer == handle) {
|
|
|
|
draw.draw_framebuffer = 0;
|
2015-08-06 04:22:06 +02:00
|
|
|
}
|
2018-03-19 22:05:15 +01:00
|
|
|
return *this;
|
2015-08-06 04:22:06 +02:00
|
|
|
}
|
2018-08-21 10:18:27 +02:00
|
|
|
|
|
|
|
} // namespace OpenGL
|