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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-08-17 09:46:59 +02:00
|
|
|
#include <array>
|
2015-08-30 08:37:42 +02:00
|
|
|
#include <glad/glad.h>
|
2018-09-27 02:52:31 +02:00
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2018-08-21 10:18:27 +02:00
|
|
|
namespace OpenGL {
|
|
|
|
|
2017-06-16 15:16:28 +02:00
|
|
|
namespace TextureUnits {
|
|
|
|
|
|
|
|
struct TextureUnit {
|
|
|
|
GLint id;
|
|
|
|
constexpr GLenum Enum() const {
|
|
|
|
return static_cast<GLenum>(GL_TEXTURE0 + id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-27 04:41:47 +02:00
|
|
|
constexpr TextureUnit MaxwellTexture(int unit) {
|
2017-06-16 15:16:28 +02:00
|
|
|
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
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
class OpenGLState {
|
|
|
|
public:
|
2018-10-24 22:09:40 +02:00
|
|
|
struct {
|
|
|
|
bool enabled; // GL_FRAMEBUFFER_SRGB
|
|
|
|
} framebuffer_srgb;
|
|
|
|
|
2018-11-14 04:02:54 +01:00
|
|
|
struct {
|
|
|
|
bool alpha_to_coverage; // GL_ALPHA_TO_COVERAGE
|
|
|
|
bool alpha_to_one; // GL_ALPHA_TO_ONE
|
|
|
|
} multisample_control;
|
|
|
|
|
2018-11-14 02:09:01 +01:00
|
|
|
struct {
|
|
|
|
bool enabled; // GL_CLAMP_FRAGMENT_COLOR_ARB
|
|
|
|
} fragment_color_clamp;
|
|
|
|
|
2018-11-23 16:11:21 +01:00
|
|
|
struct {
|
|
|
|
bool far_plane;
|
|
|
|
bool near_plane;
|
|
|
|
} depth_clamp; // GL_DEPTH_CLAMP
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
struct {
|
2016-09-18 02:38:01 +02:00
|
|
|
bool enabled; // GL_CULL_FACE
|
|
|
|
GLenum mode; // GL_CULL_FACE_MODE
|
2015-08-31 17:03:21 +02:00
|
|
|
GLenum front_face; // GL_FRONT_FACE
|
2015-05-19 06:21:33 +02:00
|
|
|
} cull;
|
|
|
|
|
|
|
|
struct {
|
2018-11-02 04:21:25 +01:00
|
|
|
bool test_enabled; // GL_DEPTH_TEST
|
|
|
|
GLenum test_func; // GL_DEPTH_FUNC
|
|
|
|
GLboolean write_mask; // GL_DEPTH_WRITEMASK
|
2015-05-19 06:21:33 +02:00
|
|
|
} depth;
|
|
|
|
|
2018-10-26 01:04:13 +02:00
|
|
|
struct {
|
|
|
|
bool enabled;
|
|
|
|
GLuint index;
|
|
|
|
} primitive_restart; // GL_PRIMITIVE_RESTART
|
|
|
|
|
2018-11-05 03:46:06 +01:00
|
|
|
struct ColorMask {
|
2015-05-29 03:41:37 +02:00
|
|
|
GLboolean red_enabled;
|
|
|
|
GLboolean green_enabled;
|
|
|
|
GLboolean blue_enabled;
|
|
|
|
GLboolean alpha_enabled;
|
2018-11-05 03:46:06 +01:00
|
|
|
};
|
|
|
|
std::array<ColorMask, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets>
|
|
|
|
color_mask; // GL_COLOR_WRITEMASK
|
2015-05-19 06:21:33 +02:00
|
|
|
struct {
|
2018-08-22 06:17:54 +02:00
|
|
|
bool test_enabled; // GL_STENCIL_TEST
|
|
|
|
struct {
|
|
|
|
GLenum test_func; // GL_STENCIL_FUNC
|
|
|
|
GLint test_ref; // GL_STENCIL_REF
|
|
|
|
GLuint test_mask; // GL_STENCIL_VALUE_MASK
|
|
|
|
GLuint write_mask; // GL_STENCIL_WRITEMASK
|
|
|
|
GLenum action_stencil_fail; // GL_STENCIL_FAIL
|
|
|
|
GLenum action_depth_fail; // GL_STENCIL_PASS_DEPTH_FAIL
|
|
|
|
GLenum action_depth_pass; // GL_STENCIL_PASS_DEPTH_PASS
|
|
|
|
} front, back;
|
2015-05-19 06:21:33 +02:00
|
|
|
} stencil;
|
|
|
|
|
2018-11-02 04:21:25 +01:00
|
|
|
struct Blend {
|
2016-09-18 02:38:01 +02:00
|
|
|
bool enabled; // GL_BLEND
|
2016-05-12 22:57:15 +02:00
|
|
|
GLenum rgb_equation; // GL_BLEND_EQUATION_RGB
|
2016-09-18 02:38:01 +02:00
|
|
|
GLenum a_equation; // GL_BLEND_EQUATION_ALPHA
|
2015-05-19 06:21:33 +02:00
|
|
|
GLenum src_rgb_func; // GL_BLEND_SRC_RGB
|
|
|
|
GLenum dst_rgb_func; // GL_BLEND_DST_RGB
|
2016-09-18 02:38:01 +02:00
|
|
|
GLenum src_a_func; // GL_BLEND_SRC_ALPHA
|
|
|
|
GLenum dst_a_func; // GL_BLEND_DST_ALPHA
|
2018-11-02 04:21:25 +01:00
|
|
|
};
|
|
|
|
std::array<Blend, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> blend;
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2018-11-02 04:21:25 +01:00
|
|
|
struct {
|
|
|
|
bool enabled;
|
|
|
|
} independant_blend;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
GLclampf red;
|
|
|
|
GLclampf green;
|
|
|
|
GLclampf blue;
|
|
|
|
GLclampf alpha;
|
|
|
|
} blend_color; // GL_BLEND_COLOR
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2018-08-21 01:43:11 +02:00
|
|
|
struct {
|
|
|
|
bool enabled; // GL_LOGIC_OP_MODE
|
|
|
|
GLenum operation;
|
|
|
|
} logic_op;
|
2015-05-26 00:39:03 +02:00
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
// 3 texture units - one for each that is used in PICA fragment shader emulation
|
2018-08-02 17:19:55 +02:00
|
|
|
struct TextureUnit {
|
2018-09-01 05:54:31 +02:00
|
|
|
GLuint texture; // GL_TEXTURE_BINDING_2D
|
|
|
|
GLuint sampler; // GL_SAMPLER_BINDING
|
2018-06-26 22:58:35 +02:00
|
|
|
|
|
|
|
void Unbind() {
|
2018-09-01 05:54:31 +02:00
|
|
|
texture = 0;
|
2018-06-26 22:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
Unbind();
|
|
|
|
sampler = 0;
|
|
|
|
}
|
2018-08-02 17:19:55 +02:00
|
|
|
};
|
2018-09-27 02:52:31 +02:00
|
|
|
std::array<TextureUnit, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> texture_units;
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2017-05-05 14:25:04 +02:00
|
|
|
struct {
|
2016-04-17 00:57:57 +02:00
|
|
|
GLuint read_framebuffer; // GL_READ_FRAMEBUFFER_BINDING
|
|
|
|
GLuint draw_framebuffer; // GL_DRAW_FRAMEBUFFER_BINDING
|
2016-09-18 02:38:01 +02:00
|
|
|
GLuint vertex_array; // GL_VERTEX_ARRAY_BINDING
|
|
|
|
GLuint shader_program; // GL_CURRENT_PROGRAM
|
2018-03-19 22:05:15 +01:00
|
|
|
GLuint program_pipeline; // GL_PROGRAM_PIPELINE_BINDING
|
2015-05-19 06:21:33 +02:00
|
|
|
} draw;
|
|
|
|
|
2018-11-02 04:21:25 +01:00
|
|
|
struct viewport {
|
2018-11-21 04:40:32 +01:00
|
|
|
GLint x;
|
|
|
|
GLint y;
|
|
|
|
GLint width;
|
|
|
|
GLint height;
|
2018-11-02 04:21:25 +01:00
|
|
|
GLfloat depth_range_near; // GL_DEPTH_RANGE
|
|
|
|
GLfloat depth_range_far; // GL_DEPTH_RANGE
|
2018-11-14 00:13:16 +01:00
|
|
|
struct {
|
|
|
|
bool enabled; // GL_SCISSOR_TEST
|
|
|
|
GLint x;
|
|
|
|
GLint y;
|
|
|
|
GLsizei width;
|
|
|
|
GLsizei height;
|
|
|
|
} scissor;
|
2018-11-02 04:21:25 +01:00
|
|
|
};
|
2018-11-14 00:13:16 +01:00
|
|
|
std::array<viewport, Tegra::Engines::Maxwell3D::Regs::NumViewports> viewports;
|
2018-03-19 22:05:15 +01:00
|
|
|
|
2018-09-28 06:31:01 +02:00
|
|
|
struct {
|
|
|
|
float size; // GL_POINT_SIZE
|
|
|
|
} point;
|
|
|
|
|
2018-11-27 00:31:44 +01:00
|
|
|
struct {
|
|
|
|
bool point_enable;
|
|
|
|
bool line_enable;
|
|
|
|
bool fill_enable;
|
|
|
|
GLfloat units;
|
|
|
|
GLfloat factor;
|
|
|
|
GLfloat clamp;
|
|
|
|
} polygon_offset;
|
|
|
|
|
2019-05-22 01:28:09 +02:00
|
|
|
struct {
|
|
|
|
bool enabled; // GL_ALPHA_TEST
|
|
|
|
GLenum func; // GL_ALPHA_TEST_FUNC
|
|
|
|
GLfloat ref; // GL_ALPHA_TEST_REF
|
|
|
|
} alpha_test;
|
|
|
|
|
2018-11-27 00:45:21 +01:00
|
|
|
std::array<bool, 8> clip_distance; // GL_CLIP_DISTANCE
|
2017-08-17 09:46:59 +02:00
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
OpenGLState();
|
|
|
|
|
|
|
|
/// Get the currently active OpenGL state
|
2018-03-19 22:05:15 +01:00
|
|
|
static OpenGLState GetCurState() {
|
2015-05-19 06:21:33 +02:00
|
|
|
return cur_state;
|
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-10-24 22:09:40 +02:00
|
|
|
static bool GetsRGBUsed() {
|
|
|
|
return s_rgb_used;
|
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-10-24 22:09:40 +02:00
|
|
|
static void ClearsRGBUsed() {
|
|
|
|
s_rgb_used = false;
|
|
|
|
}
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
/// Apply this state as the current OpenGL state
|
2019-07-13 22:52:32 +02:00
|
|
|
void Apply();
|
2019-02-27 06:21:33 +01:00
|
|
|
|
2018-11-08 02:27:47 +01:00
|
|
|
void ApplyFramebufferState() const;
|
2019-01-09 06:40:19 +01:00
|
|
|
void ApplyVertexArrayState() const;
|
2019-02-27 06:21:33 +01:00
|
|
|
void ApplyShaderProgram() const;
|
|
|
|
void ApplyProgramPipeline() const;
|
|
|
|
void ApplyClipDistances() const;
|
|
|
|
void ApplyPointSize() const;
|
|
|
|
void ApplyFragmentColorClamp() const;
|
|
|
|
void ApplyMultisample() const;
|
2018-10-30 04:55:53 +01:00
|
|
|
void ApplySRgb() const;
|
|
|
|
void ApplyCulling() const;
|
2018-11-05 03:46:06 +01:00
|
|
|
void ApplyColorMask() const;
|
2018-10-30 04:55:53 +01:00
|
|
|
void ApplyDepth() const;
|
|
|
|
void ApplyPrimitiveRestart() const;
|
|
|
|
void ApplyStencilTest() const;
|
2018-11-02 04:21:25 +01:00
|
|
|
void ApplyViewport() const;
|
2018-11-13 13:08:11 +01:00
|
|
|
void ApplyTargetBlending(std::size_t target, bool force) const;
|
2018-11-02 04:21:25 +01:00
|
|
|
void ApplyGlobalBlending() const;
|
2018-10-30 04:55:53 +01:00
|
|
|
void ApplyBlending() const;
|
|
|
|
void ApplyLogicOp() const;
|
|
|
|
void ApplyTextures() const;
|
|
|
|
void ApplySamplers() const;
|
2018-11-23 16:11:21 +01:00
|
|
|
void ApplyDepthClamp() const;
|
2018-11-27 00:31:44 +01:00
|
|
|
void ApplyPolygonOffset() const;
|
2019-05-22 01:28:09 +02:00
|
|
|
void ApplyAlphaTest() const;
|
2019-02-27 06:21:33 +01:00
|
|
|
|
|
|
|
/// Set the initial OpenGL state
|
|
|
|
static void ApplyDefaultState();
|
|
|
|
|
|
|
|
/// Resets any references to the given resource
|
|
|
|
OpenGLState& UnbindTexture(GLuint handle);
|
|
|
|
OpenGLState& ResetSampler(GLuint handle);
|
|
|
|
OpenGLState& ResetProgram(GLuint handle);
|
|
|
|
OpenGLState& ResetPipeline(GLuint handle);
|
|
|
|
OpenGLState& ResetVertexArray(GLuint handle);
|
|
|
|
OpenGLState& ResetFramebuffer(GLuint handle);
|
|
|
|
|
|
|
|
/// Viewport does not affects glClearBuffer so emulate viewport using scissor test
|
|
|
|
void EmulateViewportWithScissor();
|
|
|
|
|
2019-07-13 22:52:32 +02:00
|
|
|
void MarkDirtyBlendState(const bool is_dirty) {
|
|
|
|
dirty.blend_state = is_dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkDirtyStencilState(const bool is_dirty) {
|
|
|
|
dirty.stencil_state = is_dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkDirtyViewportState(const bool is_dirty) {
|
|
|
|
dirty.viewport_state = is_dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkDirtyPolygonOffset(const bool is_dirty) {
|
|
|
|
dirty.polygon_offset = is_dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkDirtyColorMask(const bool is_dirty) {
|
|
|
|
dirty.color_mask = is_dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AllDirty() {
|
|
|
|
dirty.blend_state = true;
|
|
|
|
dirty.stencil_state = true;
|
|
|
|
dirty.viewport_state = true;
|
|
|
|
dirty.polygon_offset = true;
|
|
|
|
dirty.color_mask = true;
|
|
|
|
}
|
|
|
|
|
2019-02-27 06:21:33 +01:00
|
|
|
private:
|
|
|
|
static OpenGLState cur_state;
|
|
|
|
|
|
|
|
// Workaround for sRGB problems caused by QT not supporting srgb output
|
|
|
|
static bool s_rgb_used;
|
2019-07-13 22:52:32 +02:00
|
|
|
struct {
|
|
|
|
bool blend_state;
|
|
|
|
bool stencil_state;
|
|
|
|
bool viewport_state;
|
|
|
|
bool polygon_offset;
|
|
|
|
bool color_mask;
|
|
|
|
} dirty{};
|
2015-05-19 06:21:33 +02:00
|
|
|
};
|
2018-08-21 10:18:27 +02:00
|
|
|
|
|
|
|
} // namespace OpenGL
|