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
|
|
|
|
|
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>
|
2015-06-21 14:40:28 +02:00
|
|
|
#include <vector>
|
2015-07-26 03:13:11 +02:00
|
|
|
#include <unordered_map>
|
2015-06-21 14:40:28 +02:00
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2015-10-10 04:46:47 +02:00
|
|
|
#include "common/hash.h"
|
2015-06-21 14:40:28 +02:00
|
|
|
|
2015-10-06 02:52:04 +02:00
|
|
|
#include "video_core/pica.h"
|
2015-12-07 04:06:12 +01:00
|
|
|
#include "video_core/rasterizer_interface.h"
|
2015-09-11 13:20:02 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_rasterizer_cache.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_state.h"
|
2015-07-22 01:09:11 +02:00
|
|
|
#include "video_core/shader/shader_interpreter.h"
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2015-10-07 00:34:50 +02:00
|
|
|
/**
|
|
|
|
* This struct contains all state used to generate the GLSL shader program that emulates the current
|
|
|
|
* Pica register configuration. This struct is used as a cache key for generated GLSL shader
|
|
|
|
* programs. The functions in gl_shader_gen.cpp should retrieve state from this struct only, not by
|
|
|
|
* directly accessing Pica registers. This should reduce the risk of bugs in shader generation where
|
|
|
|
* Pica state is not being captured in the shader cache key, thereby resulting in (what should be)
|
|
|
|
* two separate shaders sharing the same key.
|
|
|
|
*/
|
2015-10-10 04:46:47 +02:00
|
|
|
struct PicaShaderConfig {
|
|
|
|
/// Construct a PicaShaderConfig with the current Pica register configuration.
|
|
|
|
static PicaShaderConfig CurrentConfig() {
|
|
|
|
PicaShaderConfig res;
|
2015-10-06 02:52:04 +02:00
|
|
|
const auto& regs = Pica::g_state.regs;
|
|
|
|
|
2015-10-10 04:46:47 +02:00
|
|
|
res.alpha_test_func = regs.output_merger.alpha_test.enable ?
|
2015-10-06 02:52:04 +02:00
|
|
|
regs.output_merger.alpha_test.func.Value() : Pica::Regs::CompareFunc::Always;
|
|
|
|
|
2015-10-07 00:34:50 +02:00
|
|
|
// Copy relevant TevStageConfig fields only. We're doing this manually (instead of calling
|
|
|
|
// the GetTevStages() function) because BitField explicitly disables copies.
|
|
|
|
|
2015-10-10 04:46:47 +02:00
|
|
|
res.tev_stages[0].sources_raw = regs.tev_stage0.sources_raw;
|
|
|
|
res.tev_stages[1].sources_raw = regs.tev_stage1.sources_raw;
|
|
|
|
res.tev_stages[2].sources_raw = regs.tev_stage2.sources_raw;
|
|
|
|
res.tev_stages[3].sources_raw = regs.tev_stage3.sources_raw;
|
|
|
|
res.tev_stages[4].sources_raw = regs.tev_stage4.sources_raw;
|
|
|
|
res.tev_stages[5].sources_raw = regs.tev_stage5.sources_raw;
|
|
|
|
|
|
|
|
res.tev_stages[0].modifiers_raw = regs.tev_stage0.modifiers_raw;
|
|
|
|
res.tev_stages[1].modifiers_raw = regs.tev_stage1.modifiers_raw;
|
|
|
|
res.tev_stages[2].modifiers_raw = regs.tev_stage2.modifiers_raw;
|
|
|
|
res.tev_stages[3].modifiers_raw = regs.tev_stage3.modifiers_raw;
|
|
|
|
res.tev_stages[4].modifiers_raw = regs.tev_stage4.modifiers_raw;
|
|
|
|
res.tev_stages[5].modifiers_raw = regs.tev_stage5.modifiers_raw;
|
|
|
|
|
|
|
|
res.tev_stages[0].ops_raw = regs.tev_stage0.ops_raw;
|
|
|
|
res.tev_stages[1].ops_raw = regs.tev_stage1.ops_raw;
|
|
|
|
res.tev_stages[2].ops_raw = regs.tev_stage2.ops_raw;
|
|
|
|
res.tev_stages[3].ops_raw = regs.tev_stage3.ops_raw;
|
|
|
|
res.tev_stages[4].ops_raw = regs.tev_stage4.ops_raw;
|
|
|
|
res.tev_stages[5].ops_raw = regs.tev_stage5.ops_raw;
|
|
|
|
|
|
|
|
res.tev_stages[0].scales_raw = regs.tev_stage0.scales_raw;
|
|
|
|
res.tev_stages[1].scales_raw = regs.tev_stage1.scales_raw;
|
|
|
|
res.tev_stages[2].scales_raw = regs.tev_stage2.scales_raw;
|
|
|
|
res.tev_stages[3].scales_raw = regs.tev_stage3.scales_raw;
|
|
|
|
res.tev_stages[4].scales_raw = regs.tev_stage4.scales_raw;
|
|
|
|
res.tev_stages[5].scales_raw = regs.tev_stage5.scales_raw;
|
|
|
|
|
|
|
|
res.combiner_buffer_input =
|
2015-10-06 02:52:04 +02:00
|
|
|
regs.tev_combiner_buffer_input.update_mask_rgb.Value() |
|
|
|
|
regs.tev_combiner_buffer_input.update_mask_a.Value() << 4;
|
|
|
|
|
2015-10-10 04:46:47 +02:00
|
|
|
return res;
|
2015-10-06 02:52:04 +02:00
|
|
|
}
|
2015-07-26 03:13:11 +02:00
|
|
|
|
2015-10-10 04:46:47 +02:00
|
|
|
bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const {
|
|
|
|
return (stage_index < 4) && (combiner_buffer_input & (1 << stage_index));
|
2015-10-06 04:33:47 +02:00
|
|
|
}
|
|
|
|
|
2015-10-10 04:46:47 +02:00
|
|
|
bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const {
|
|
|
|
return (stage_index < 4) && ((combiner_buffer_input >> 4) & (1 << stage_index));
|
2015-07-26 03:13:11 +02:00
|
|
|
}
|
2015-10-10 04:46:47 +02:00
|
|
|
|
|
|
|
bool operator ==(const PicaShaderConfig& o) const {
|
|
|
|
return std::memcmp(this, &o, sizeof(PicaShaderConfig)) == 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
Pica::Regs::CompareFunc alpha_test_func;
|
|
|
|
std::array<Pica::Regs::TevStageConfig, 6> tev_stages = {};
|
|
|
|
u8 combiner_buffer_input;
|
2015-07-26 03:13:11 +02:00
|
|
|
};
|
|
|
|
|
2015-10-10 04:46:47 +02:00
|
|
|
namespace std {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct hash<PicaShaderConfig> {
|
2015-10-13 04:23:26 +02:00
|
|
|
size_t operator()(const PicaShaderConfig& k) const {
|
2015-10-10 04:46:47 +02:00
|
|
|
return Common::ComputeHash64(&k, sizeof(PicaShaderConfig));
|
2015-07-26 03:13:11 +02:00
|
|
|
}
|
|
|
|
};
|
2015-10-06 04:33:47 +02:00
|
|
|
|
|
|
|
} // namespace std
|
2015-07-26 03:13:11 +02:00
|
|
|
|
2015-12-07 04:06:12 +01:00
|
|
|
class RasterizerOpenGL : public VideoCore::RasterizerInterface {
|
2015-05-19 06:21:33 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
RasterizerOpenGL();
|
|
|
|
~RasterizerOpenGL() override;
|
|
|
|
|
|
|
|
void InitObjects() override;
|
|
|
|
void Reset() override;
|
2015-07-22 01:04:05 +02:00
|
|
|
void AddTriangle(const Pica::Shader::OutputVertex& v0,
|
|
|
|
const Pica::Shader::OutputVertex& v1,
|
|
|
|
const Pica::Shader::OutputVertex& v2) override;
|
2015-05-19 06:21:33 +02:00
|
|
|
void DrawTriangles() override;
|
2015-12-07 02:56:45 +01:00
|
|
|
void FlushFramebuffer() override;
|
2015-05-19 06:21:33 +02:00
|
|
|
void NotifyPicaRegisterChanged(u32 id) override;
|
2015-12-07 02:56:45 +01:00
|
|
|
void FlushRegion(PAddr addr, u32 size) override;
|
|
|
|
void InvalidateRegion(PAddr addr, u32 size) override;
|
2015-05-19 06:21:33 +02:00
|
|
|
|
2015-10-10 01:32:38 +02:00
|
|
|
/// OpenGL shader generated for a given Pica register state
|
|
|
|
struct PicaShader {
|
|
|
|
/// OpenGL shader resource
|
2015-07-26 03:13:11 +02:00
|
|
|
OGLShader shader;
|
|
|
|
};
|
|
|
|
|
2015-10-10 01:32:38 +02:00
|
|
|
private:
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
/// Structure used for storing information about color textures
|
|
|
|
struct TextureInfo {
|
|
|
|
OGLTexture texture;
|
|
|
|
GLsizei width;
|
|
|
|
GLsizei height;
|
|
|
|
Pica::Regs::ColorFormat format;
|
|
|
|
GLenum gl_format;
|
|
|
|
GLenum gl_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Structure used for storing information about depth textures
|
|
|
|
struct DepthTextureInfo {
|
|
|
|
OGLTexture texture;
|
|
|
|
GLsizei width;
|
|
|
|
GLsizei height;
|
|
|
|
Pica::Regs::DepthFormat format;
|
|
|
|
GLenum gl_format;
|
|
|
|
GLenum gl_type;
|
|
|
|
};
|
|
|
|
|
2015-08-30 15:05:56 +02:00
|
|
|
struct SamplerInfo {
|
|
|
|
using TextureConfig = Pica::Regs::TextureConfig;
|
|
|
|
|
|
|
|
OGLSampler sampler;
|
|
|
|
|
|
|
|
/// Creates the sampler object, initializing its state so that it's in sync with the SamplerInfo struct.
|
|
|
|
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;
|
|
|
|
TextureConfig::WrapMode wrap_s;
|
|
|
|
TextureConfig::WrapMode wrap_t;
|
|
|
|
u32 border_color;
|
|
|
|
};
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
/// Structure that the hardware rendered vertices are composed of
|
|
|
|
struct HardwareVertex {
|
2015-07-22 01:04:05 +02:00
|
|
|
HardwareVertex(const Pica::Shader::OutputVertex& v) {
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
GLfloat position[4];
|
|
|
|
GLfloat color[4];
|
|
|
|
GLfloat tex_coord0[2];
|
|
|
|
GLfloat tex_coord1[2];
|
|
|
|
GLfloat tex_coord2[2];
|
|
|
|
};
|
|
|
|
|
2015-11-10 17:58:53 +01:00
|
|
|
/// Uniform structure for the Uniform Buffer Object, all members must be 16-byte aligned
|
|
|
|
struct UniformData {
|
|
|
|
// A vec4 color for each of the six tev stages
|
|
|
|
std::array<GLfloat, 4> const_color[6];
|
|
|
|
std::array<GLfloat, 4> tev_combiner_buffer_color;
|
|
|
|
GLint alphatest_ref;
|
2016-01-04 00:46:54 +01:00
|
|
|
GLfloat depth_offset;
|
|
|
|
INSERT_PADDING_BYTES(8);
|
2015-11-10 17:58:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(UniformData) == 0x80, "The size of the UniformData structure has changed, update the structure in the shader");
|
|
|
|
static_assert(sizeof(UniformData) < 16000, "UniformData structure must be less than 16kb as per the OpenGL spec");
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
/// Reconfigure the OpenGL color texture to use the given format and dimensions
|
|
|
|
void ReconfigureColorTexture(TextureInfo& texture, Pica::Regs::ColorFormat format, u32 width, u32 height);
|
|
|
|
|
|
|
|
/// Reconfigure the OpenGL depth texture to use the given format and dimensions
|
|
|
|
void ReconfigureDepthTexture(DepthTextureInfo& texture, Pica::Regs::DepthFormat format, u32 width, u32 height);
|
|
|
|
|
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 state and contents of the OpenGL framebuffer to match the current PICA framebuffer
|
|
|
|
void SyncFramebuffer();
|
|
|
|
|
|
|
|
/// Syncs the cull mode to match the PICA register
|
|
|
|
void SyncCullMode();
|
|
|
|
|
2016-01-04 00:46:54 +01:00
|
|
|
/// Syncs the depth scale and offset to match the PICA registers
|
|
|
|
void SyncDepthModifiers();
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
/// 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();
|
|
|
|
|
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();
|
|
|
|
|
2015-07-26 03:13:11 +02:00
|
|
|
/// Syncs the TEV constant color to match the PICA register
|
|
|
|
void SyncTevConstColor(int tev_index, const Pica::Regs::TevStageConfig& tev_stage);
|
2015-05-19 06:21:33 +02:00
|
|
|
|
|
|
|
/// Syncs the TEV combiner color buffer to match the PICA register
|
|
|
|
void SyncCombinerColor();
|
|
|
|
|
|
|
|
/// Syncs the remaining OpenGL drawing state to match the current PICA state
|
|
|
|
void SyncDrawState();
|
|
|
|
|
|
|
|
/// Copies the 3DS color framebuffer into the OpenGL color framebuffer texture
|
|
|
|
void ReloadColorBuffer();
|
|
|
|
|
|
|
|
/// Copies the 3DS depth framebuffer into the OpenGL depth framebuffer texture
|
|
|
|
void ReloadDepthBuffer();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the current OpenGL color framebuffer to the current PICA framebuffer in 3DS memory
|
|
|
|
* Loads the OpenGL framebuffer textures into temporary buffers
|
|
|
|
* Then copies into the 3DS framebuffer using proper Morton order
|
|
|
|
*/
|
|
|
|
void CommitColorBuffer();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the current OpenGL depth framebuffer to the current PICA framebuffer in 3DS memory
|
|
|
|
* Loads the OpenGL framebuffer textures into temporary buffers
|
|
|
|
* Then copies into the 3DS framebuffer using proper Morton order
|
|
|
|
*/
|
|
|
|
void CommitDepthBuffer();
|
|
|
|
|
|
|
|
RasterizerCacheOpenGL res_cache;
|
|
|
|
|
|
|
|
std::vector<HardwareVertex> vertex_batch;
|
|
|
|
|
|
|
|
OpenGLState state;
|
|
|
|
|
2016-02-03 21:52:34 +01:00
|
|
|
PAddr cached_fb_color_addr;
|
|
|
|
PAddr cached_fb_depth_addr;
|
2015-05-19 06:21:33 +02:00
|
|
|
|
|
|
|
// Hardware rasterizer
|
2015-08-30 15:05:56 +02:00
|
|
|
std::array<SamplerInfo, 3> texture_samplers;
|
2015-05-19 06:21:33 +02:00
|
|
|
TextureInfo fb_color_texture;
|
|
|
|
DepthTextureInfo fb_depth_texture;
|
2015-07-26 03:13:11 +02:00
|
|
|
|
2015-10-10 04:46:47 +02:00
|
|
|
std::unordered_map<PicaShaderConfig, std::unique_ptr<PicaShader>> shader_cache;
|
|
|
|
const PicaShader* current_shader = nullptr;
|
2015-07-26 03:13:11 +02:00
|
|
|
|
2015-11-10 17:58:53 +01:00
|
|
|
struct {
|
|
|
|
UniformData data;
|
|
|
|
bool dirty;
|
|
|
|
} uniform_block_data;
|
|
|
|
|
2015-05-19 06:21:33 +02:00
|
|
|
OGLVertexArray vertex_array;
|
|
|
|
OGLBuffer vertex_buffer;
|
2015-11-10 17:58:53 +01:00
|
|
|
OGLBuffer uniform_buffer;
|
2015-05-19 06:21:33 +02:00
|
|
|
OGLFramebuffer framebuffer;
|
|
|
|
};
|