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
|
|
|
|
|
2016-04-30 17:34:51 +02:00
|
|
|
#include <array>
|
2015-10-06 04:33:47 +02:00
|
|
|
#include <cstddef>
|
2015-10-10 04:46:47 +02:00
|
|
|
#include <cstring>
|
2015-10-06 04:33:47 +02:00
|
|
|
#include <memory>
|
2016-09-18 02:38:01 +02:00
|
|
|
#include <vector>
|
2016-04-30 17:34:51 +02:00
|
|
|
#include <glad/glad.h>
|
|
|
|
#include "common/bit_field.h"
|
2015-06-21 14:40:28 +02:00
|
|
|
#include "common/common_types.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "common/vector_math.h"
|
|
|
|
#include "core/hw/gpu.h"
|
2016-03-03 04:16:38 +01:00
|
|
|
#include "video_core/pica_state.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "video_core/pica_types.h"
|
2015-12-07 04:06:12 +01:00
|
|
|
#include "video_core/rasterizer_interface.h"
|
2017-01-29 00:12:09 +01:00
|
|
|
#include "video_core/regs_framebuffer.h"
|
|
|
|
#include "video_core/regs_lighting.h"
|
|
|
|
#include "video_core/regs_rasterizer.h"
|
|
|
|
#include "video_core/regs_texturing.h"
|
2015-09-11 13:20:02 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_rasterizer_cache.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_resource_manager.h"
|
2018-04-09 23:31:11 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_manager.h"
|
2015-09-11 13:20:02 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_state.h"
|
2018-03-14 23:36:54 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_stream_buffer.h"
|
2016-02-04 06:03:20 +01:00
|
|
|
#include "video_core/renderer_opengl/pica_to_gl.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "video_core/shader/shader.h"
|
|
|
|
|
2018-08-12 02:20:19 +02:00
|
|
|
namespace Frontend {
|
2018-08-24 15:18:46 +02:00
|
|
|
class EmuWindow;
|
2018-08-12 02:20:19 +02:00
|
|
|
}
|
|
|
|
|
2018-04-09 23:31:11 +02:00
|
|
|
class ShaderProgramManager;
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2018-11-17 08:29:10 +01:00
|
|
|
namespace OpenGL {
|
|
|
|
|
2015-12-07 04:06:12 +01:00
|
|
|
class RasterizerOpenGL : public VideoCore::RasterizerInterface {
|
2015-05-19 06:21:33 +02:00
|
|
|
public:
|
2018-08-12 02:20:19 +02:00
|
|
|
explicit RasterizerOpenGL(Frontend::EmuWindow& renderer);
|
2015-05-19 06:21:33 +02:00
|
|
|
~RasterizerOpenGL() override;
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
void AddTriangle(const Pica::Shader::OutputVertex& v0, const Pica::Shader::OutputVertex& v1,
|
2015-07-22 01:04:05 +02:00
|
|
|
const Pica::Shader::OutputVertex& v2) override;
|
2015-05-19 06:21:33 +02:00
|
|
|
void DrawTriangles() override;
|
|
|
|
void NotifyPicaRegisterChanged(u32 id) override;
|
2016-04-17 00:57:57 +02:00
|
|
|
void FlushAll() override;
|
2015-12-07 02:56:45 +01:00
|
|
|
void FlushRegion(PAddr addr, u32 size) override;
|
2017-12-12 18:21:55 +01:00
|
|
|
void InvalidateRegion(PAddr addr, u32 size) override;
|
2016-04-17 00:57:57 +02:00
|
|
|
void FlushAndInvalidateRegion(PAddr addr, u32 size) override;
|
|
|
|
bool AccelerateDisplayTransfer(const GPU::Regs::DisplayTransferConfig& config) override;
|
2016-09-27 12:09:53 +02:00
|
|
|
bool AccelerateTextureCopy(const GPU::Regs::DisplayTransferConfig& config) override;
|
2016-04-17 00:57:57 +02:00
|
|
|
bool AccelerateFill(const GPU::Regs::MemoryFillConfig& config) override;
|
2016-09-18 02:38:01 +02:00
|
|
|
bool AccelerateDisplay(const GPU::Regs::FramebufferConfig& config, PAddr framebuffer_addr,
|
|
|
|
u32 pixel_stride, ScreenInfo& screen_info) override;
|
2018-05-11 15:41:26 +02:00
|
|
|
bool AccelerateDrawBatch(bool is_indexed) override;
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2015-10-10 01:32:38 +02:00
|
|
|
private:
|
2015-08-30 15:05:56 +02:00
|
|
|
struct SamplerInfo {
|
2017-01-28 05:51:59 +01:00
|
|
|
using TextureConfig = Pica::TexturingRegs::TextureConfig;
|
2015-08-30 15:05:56 +02:00
|
|
|
|
|
|
|
OGLSampler sampler;
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
/// Creates the sampler object, initializing its state so that it's in sync with the
|
|
|
|
/// SamplerInfo struct.
|
2015-08-30 15:05:56 +02:00
|
|
|
void Create();
|
|
|
|
/// Syncs the sampler object with the config, updating any necessary state.
|
|
|
|
void SyncWithConfig(const TextureConfig& config);
|
|
|
|
|
|
|
|
private:
|
|
|
|
TextureConfig::TextureFilter mag_filter;
|
|
|
|
TextureConfig::TextureFilter min_filter;
|
2018-07-02 11:55:04 +02:00
|
|
|
TextureConfig::TextureFilter mip_filter;
|
2015-08-30 15:05:56 +02:00
|
|
|
TextureConfig::WrapMode wrap_s;
|
|
|
|
TextureConfig::WrapMode wrap_t;
|
|
|
|
u32 border_color;
|
2018-07-02 11:55:04 +02:00
|
|
|
u32 lod_min;
|
|
|
|
u32 lod_max;
|
|
|
|
s32 lod_bias;
|
2018-07-22 16:31:02 +02:00
|
|
|
|
|
|
|
// TODO(wwylele): remove this once mipmap for cube is implemented
|
|
|
|
bool supress_mipmap_for_cube = false;
|
2015-08-30 15:05:56 +02:00
|
|
|
};
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
/// Structure that the hardware rendered vertices are composed of
|
|
|
|
struct HardwareVertex {
|
2018-05-11 15:41:26 +02:00
|
|
|
HardwareVertex() = default;
|
2016-01-29 05:29:33 +01:00
|
|
|
HardwareVertex(const Pica::Shader::OutputVertex& v, bool flip_quaternion) {
|
2015-05-19 06:21:33 +02:00
|
|
|
position[0] = v.pos.x.ToFloat32();
|
|
|
|
position[1] = v.pos.y.ToFloat32();
|
|
|
|
position[2] = v.pos.z.ToFloat32();
|
|
|
|
position[3] = v.pos.w.ToFloat32();
|
|
|
|
color[0] = v.color.x.ToFloat32();
|
|
|
|
color[1] = v.color.y.ToFloat32();
|
|
|
|
color[2] = v.color.z.ToFloat32();
|
|
|
|
color[3] = v.color.w.ToFloat32();
|
|
|
|
tex_coord0[0] = v.tc0.x.ToFloat32();
|
|
|
|
tex_coord0[1] = v.tc0.y.ToFloat32();
|
|
|
|
tex_coord1[0] = v.tc1.x.ToFloat32();
|
|
|
|
tex_coord1[1] = v.tc1.y.ToFloat32();
|
|
|
|
tex_coord2[0] = v.tc2.x.ToFloat32();
|
|
|
|
tex_coord2[1] = v.tc2.y.ToFloat32();
|
2016-04-19 00:53:42 +02:00
|
|
|
tex_coord0_w = v.tc0_w.ToFloat32();
|
2015-11-12 23:33:21 +01:00
|
|
|
normquat[0] = v.quat.x.ToFloat32();
|
|
|
|
normquat[1] = v.quat.y.ToFloat32();
|
|
|
|
normquat[2] = v.quat.z.ToFloat32();
|
|
|
|
normquat[3] = v.quat.w.ToFloat32();
|
|
|
|
view[0] = v.view.x.ToFloat32();
|
|
|
|
view[1] = v.view.y.ToFloat32();
|
|
|
|
view[2] = v.view.z.ToFloat32();
|
2016-01-29 05:29:33 +01:00
|
|
|
|
|
|
|
if (flip_quaternion) {
|
|
|
|
for (float& x : normquat) {
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 06:21:33 +02:00
|
|
|
}
|
|
|
|
|
2017-12-27 14:19:45 +01:00
|
|
|
GLvec4 position;
|
|
|
|
GLvec4 color;
|
|
|
|
GLvec2 tex_coord0;
|
|
|
|
GLvec2 tex_coord1;
|
|
|
|
GLvec2 tex_coord2;
|
2016-04-19 00:53:42 +02:00
|
|
|
GLfloat tex_coord0_w;
|
2017-12-27 14:19:45 +01:00
|
|
|
GLvec4 normquat;
|
|
|
|
GLvec3 view;
|
2015-11-12 23:33:21 +01:00
|
|
|
};
|
|
|
|
|
2018-04-04 16:23:55 +02:00
|
|
|
/// Syncs entire status to match PICA registers
|
|
|
|
void SyncEntireState();
|
|
|
|
|
2017-08-22 08:49:53 +02:00
|
|
|
/// Syncs the clip enabled status to match the PICA register
|
|
|
|
void SyncClipEnabled();
|
|
|
|
|
|
|
|
/// Syncs the clip coefficients to match the PICA register
|
|
|
|
void SyncClipCoef();
|
|
|
|
|
2015-10-06 04:33:47 +02:00
|
|
|
/// Sets the OpenGL shader in accordance with the current PICA register state
|
|
|
|
void SetShader();
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
/// Syncs the cull mode to match the PICA register
|
|
|
|
void SyncCullMode();
|
|
|
|
|
2016-05-14 10:16:45 +02:00
|
|
|
/// Syncs the depth scale to match the PICA register
|
|
|
|
void SyncDepthScale();
|
|
|
|
|
|
|
|
/// Syncs the depth offset to match the PICA register
|
|
|
|
void SyncDepthOffset();
|
2016-01-04 00:46:54 +01:00
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
/// Syncs the blend enabled status to match the PICA register
|
|
|
|
void SyncBlendEnabled();
|
|
|
|
|
|
|
|
/// Syncs the blend functions to match the PICA register
|
|
|
|
void SyncBlendFuncs();
|
|
|
|
|
|
|
|
/// Syncs the blend color to match the PICA register
|
|
|
|
void SyncBlendColor();
|
|
|
|
|
2016-05-21 01:04:57 +02:00
|
|
|
/// Syncs the fog states to match the PICA register
|
|
|
|
void SyncFogColor();
|
|
|
|
|
2017-05-05 14:25:04 +02:00
|
|
|
/// Sync the procedural texture noise configuration to match the PICA register
|
|
|
|
void SyncProcTexNoise();
|
|
|
|
|
2018-07-04 16:16:38 +02:00
|
|
|
/// Sync the procedural texture bias configuration to match the PICA register
|
|
|
|
void SyncProcTexBias();
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
/// Syncs the alpha test states to match the PICA register
|
|
|
|
void SyncAlphaTest();
|
|
|
|
|
2015-05-26 00:39:03 +02:00
|
|
|
/// Syncs the logic op states to match the PICA register
|
|
|
|
void SyncLogicOp();
|
|
|
|
|
2016-03-31 15:36:00 +02:00
|
|
|
/// Syncs the color write mask to match the PICA register state
|
|
|
|
void SyncColorWriteMask();
|
|
|
|
|
|
|
|
/// Syncs the stencil write mask to match the PICA register state
|
|
|
|
void SyncStencilWriteMask();
|
|
|
|
|
|
|
|
/// Syncs the depth write mask to match the PICA register state
|
|
|
|
void SyncDepthWriteMask();
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
/// Syncs the stencil test states to match the PICA register
|
|
|
|
void SyncStencilTest();
|
|
|
|
|
|
|
|
/// Syncs the depth test states to match the PICA register
|
|
|
|
void SyncDepthTest();
|
|
|
|
|
|
|
|
/// Syncs the TEV combiner color buffer to match the PICA register
|
|
|
|
void SyncCombinerColor();
|
|
|
|
|
2016-04-22 16:52:02 +02:00
|
|
|
/// Syncs the TEV constant color to match the PICA register
|
2017-01-28 05:51:59 +01:00
|
|
|
void SyncTevConstColor(int tev_index, const Pica::TexturingRegs::TevStageConfig& tev_stage);
|
2016-04-22 16:52:02 +02:00
|
|
|
|
2015-11-12 23:33:21 +01:00
|
|
|
/// Syncs the lighting global ambient color to match the PICA register
|
|
|
|
void SyncGlobalAmbient();
|
|
|
|
|
2016-04-22 16:52:02 +02:00
|
|
|
/// Syncs the specified light's specular 0 color to match the PICA register
|
|
|
|
void SyncLightSpecular0(int light_index);
|
|
|
|
|
|
|
|
/// Syncs the specified light's specular 1 color to match the PICA register
|
|
|
|
void SyncLightSpecular1(int light_index);
|
|
|
|
|
2015-11-12 23:33:21 +01:00
|
|
|
/// Syncs the specified light's diffuse color to match the PICA register
|
|
|
|
void SyncLightDiffuse(int light_index);
|
|
|
|
|
|
|
|
/// Syncs the specified light's ambient color to match the PICA register
|
|
|
|
void SyncLightAmbient(int light_index);
|
|
|
|
|
|
|
|
/// Syncs the specified light's position to match the PICA register
|
|
|
|
void SyncLightPosition(int light_index);
|
|
|
|
|
2017-05-25 19:15:12 +02:00
|
|
|
/// Syncs the specified spot light direcition to match the PICA register
|
|
|
|
void SyncLightSpotDirection(int light_index);
|
|
|
|
|
2016-05-23 22:53:53 +02:00
|
|
|
/// Syncs the specified light's distance attenuation bias to match the PICA register
|
|
|
|
void SyncLightDistanceAttenuationBias(int light_index);
|
|
|
|
|
|
|
|
/// Syncs the specified light's distance attenuation scale to match the PICA register
|
|
|
|
void SyncLightDistanceAttenuationScale(int light_index);
|
|
|
|
|
2018-04-11 14:47:02 +02:00
|
|
|
/// Syncs the shadow rendering bias to match the PICA register
|
|
|
|
void SyncShadowBias();
|
|
|
|
|
2019-02-03 02:39:51 +01:00
|
|
|
/// Syncs the shadow texture bias to match the PICA register
|
|
|
|
void SyncShadowTextureBias();
|
|
|
|
|
2018-05-19 11:12:51 +02:00
|
|
|
/// Syncs and uploads the lighting, fog and proctex LUTs
|
|
|
|
void SyncAndUploadLUTs();
|
|
|
|
|
2018-05-02 09:34:36 +02:00
|
|
|
/// Upload the uniform blocks to the uniform buffer object
|
2018-05-11 15:41:26 +02:00
|
|
|
void UploadUniforms(bool accelerate_draw, bool use_gs);
|
|
|
|
|
|
|
|
/// Generic draw function for DrawTriangles and AccelerateDrawBatch
|
2018-05-18 12:55:19 +02:00
|
|
|
bool Draw(bool accelerate, bool is_indexed);
|
2018-05-11 15:41:26 +02:00
|
|
|
|
|
|
|
/// Internal implementation for AccelerateDrawBatch
|
2018-05-18 12:55:19 +02:00
|
|
|
bool AccelerateDrawBatchInternal(bool is_indexed, bool use_gs);
|
2018-05-11 15:41:26 +02:00
|
|
|
|
|
|
|
struct VertexArrayInfo {
|
|
|
|
u32 vs_input_index_min;
|
|
|
|
u32 vs_input_index_max;
|
|
|
|
u32 vs_input_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Retrieve the range and the size of the input vertex
|
|
|
|
VertexArrayInfo AnalyzeVertexArray(bool is_indexed);
|
|
|
|
|
|
|
|
/// Setup vertex array for AccelerateDrawBatch
|
|
|
|
void SetupVertexArray(u8* array_ptr, GLintptr buffer_offset, GLuint vs_input_index_min,
|
|
|
|
GLuint vs_input_index_max);
|
|
|
|
|
|
|
|
/// Setup vertex shader for AccelerateDrawBatch
|
|
|
|
bool SetupVertexShader();
|
|
|
|
|
|
|
|
/// Setup geometry shader for AccelerateDrawBatch
|
|
|
|
bool SetupGeometryShader();
|
2018-05-02 09:34:36 +02:00
|
|
|
|
2018-07-31 12:51:42 +02:00
|
|
|
bool is_amd;
|
|
|
|
|
2016-04-17 00:57:57 +02:00
|
|
|
OpenGLState state;
|
2019-08-10 11:05:00 +02:00
|
|
|
GLuint default_texture;
|
2015-05-19 06:21:33 +02:00
|
|
|
|
|
|
|
RasterizerCacheOpenGL res_cache;
|
|
|
|
|
2018-08-12 02:20:19 +02:00
|
|
|
Frontend::EmuWindow& emu_window;
|
2018-08-24 15:18:46 +02:00
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
std::vector<HardwareVertex> vertex_batch;
|
|
|
|
|
2016-04-17 00:57:57 +02:00
|
|
|
bool shader_dirty;
|
2015-07-26 03:13:11 +02:00
|
|
|
|
2015-11-10 17:58:53 +01:00
|
|
|
struct {
|
|
|
|
UniformData data;
|
2018-05-19 14:47:25 +02:00
|
|
|
std::array<bool, Pica::LightingRegs::NumLightingSampler> lighting_lut_dirty;
|
|
|
|
bool lighting_lut_dirty_any;
|
2016-05-21 01:04:57 +02:00
|
|
|
bool fog_lut_dirty;
|
2017-05-05 14:25:04 +02:00
|
|
|
bool proctex_noise_lut_dirty;
|
|
|
|
bool proctex_color_map_dirty;
|
|
|
|
bool proctex_alpha_map_dirty;
|
|
|
|
bool proctex_lut_dirty;
|
|
|
|
bool proctex_diff_lut_dirty;
|
2015-11-10 17:58:53 +01:00
|
|
|
bool dirty;
|
2016-05-14 10:16:06 +02:00
|
|
|
} uniform_block_data = {};
|
2015-11-10 17:58:53 +01:00
|
|
|
|
2018-04-09 23:31:11 +02:00
|
|
|
std::unique_ptr<ShaderProgramManager> shader_program_manager;
|
|
|
|
|
2018-05-02 09:34:36 +02:00
|
|
|
// They shall be big enough for about one frame.
|
2019-08-10 10:51:24 +02:00
|
|
|
static constexpr std::size_t VERTEX_BUFFER_SIZE = 16 * 1024 * 1024;
|
2018-09-06 22:03:28 +02:00
|
|
|
static constexpr std::size_t INDEX_BUFFER_SIZE = 1 * 1024 * 1024;
|
|
|
|
static constexpr std::size_t UNIFORM_BUFFER_SIZE = 2 * 1024 * 1024;
|
|
|
|
static constexpr std::size_t TEXTURE_BUFFER_SIZE = 1 * 1024 * 1024;
|
2018-05-02 09:34:36 +02:00
|
|
|
|
2018-05-11 15:41:26 +02:00
|
|
|
OGLVertexArray sw_vao; // VAO for software shader draw
|
|
|
|
OGLVertexArray hw_vao; // VAO for hardware shader / accelerate draw
|
|
|
|
std::array<bool, 16> hw_vao_enabled_attributes{};
|
|
|
|
|
2016-04-17 00:57:57 +02:00
|
|
|
std::array<SamplerInfo, 3> texture_samplers;
|
2018-03-22 22:16:23 +01:00
|
|
|
OGLStreamBuffer vertex_buffer;
|
2018-05-02 09:34:36 +02:00
|
|
|
OGLStreamBuffer uniform_buffer;
|
2018-05-11 15:41:26 +02:00
|
|
|
OGLStreamBuffer index_buffer;
|
2018-05-19 12:54:50 +02:00
|
|
|
OGLStreamBuffer texture_buffer;
|
2015-05-19 06:21:33 +02:00
|
|
|
OGLFramebuffer framebuffer;
|
2018-05-02 09:34:36 +02:00
|
|
|
GLint uniform_buffer_alignment;
|
2018-09-06 22:03:28 +02:00
|
|
|
std::size_t uniform_size_aligned_vs;
|
|
|
|
std::size_t uniform_size_aligned_gs;
|
|
|
|
std::size_t uniform_size_aligned_fs;
|
2015-11-15 23:43:01 +01:00
|
|
|
|
2018-03-09 15:46:34 +01:00
|
|
|
SamplerInfo texture_cube_sampler;
|
|
|
|
|
2018-05-19 12:54:50 +02:00
|
|
|
OGLTexture texture_buffer_lut_rg;
|
|
|
|
OGLTexture texture_buffer_lut_rgba;
|
|
|
|
|
2017-06-16 13:00:15 +02:00
|
|
|
std::array<std::array<GLvec2, 256>, Pica::LightingRegs::NumLightingSampler> lighting_lut_data{};
|
2017-06-22 16:22:45 +02:00
|
|
|
std::array<GLvec2, 128> fog_lut_data{};
|
2017-05-05 14:25:04 +02:00
|
|
|
std::array<GLvec2, 128> proctex_noise_lut_data{};
|
|
|
|
std::array<GLvec2, 128> proctex_color_map_data{};
|
|
|
|
std::array<GLvec2, 128> proctex_alpha_map_data{};
|
|
|
|
std::array<GLvec4, 256> proctex_lut_data{};
|
|
|
|
std::array<GLvec4, 256> proctex_diff_lut_data{};
|
2018-04-11 14:47:02 +02:00
|
|
|
|
|
|
|
bool allow_shadow;
|
2015-05-19 06:21:33 +02:00
|
|
|
};
|
2018-11-17 08:29:10 +01:00
|
|
|
|
|
|
|
} // namespace OpenGL
|