diff options
Diffstat (limited to 'src/video_core/renderer_opengl/gl_shader_gen.h')
-rw-r--r-- | src/video_core/renderer_opengl/gl_shader_gen.h | 119 |
1 files changed, 93 insertions, 26 deletions
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.h b/src/video_core/renderer_opengl/gl_shader_gen.h index 5101e7d30..458032b5c 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.h +++ b/src/video_core/renderer_opengl/gl_shader_gen.h @@ -4,46 +4,113 @@ #pragma once -#include <cstring> +#include <array> #include <string> #include <type_traits> +#include <utility> +#include <vector> +#include "common/common_types.h" #include "common/hash.h" namespace GLShader { -enum Attributes { - ATTRIBUTE_POSITION, - ATTRIBUTE_COLOR, - ATTRIBUTE_TEXCOORD0, - ATTRIBUTE_TEXCOORD1, - ATTRIBUTE_TEXCOORD2, - ATTRIBUTE_TEXCOORD0_W, - ATTRIBUTE_NORMQUAT, - ATTRIBUTE_VIEW, +constexpr size_t MAX_PROGRAM_CODE_LENGTH{0x1000}; + +using ProgramCode = std::array<u64, MAX_PROGRAM_CODE_LENGTH>; + +class ConstBufferEntry { + using Maxwell = Tegra::Engines::Maxwell3D::Regs; + +public: + void MarkAsUsed(unsigned index, unsigned offset, Maxwell::ShaderStage stage) { + is_used = true; + this->index = index; + this->stage = stage; + max_offset = std::max(max_offset, offset); + } + + bool IsUsed() const { + return is_used; + } + + unsigned GetIndex() const { + return index; + } + + unsigned GetSize() const { + return max_offset + 1; + } + + std::string GetName() const { + return BufferBaseNames[static_cast<size_t>(stage)] + std::to_string(index); + } + +private: + static constexpr std::array<const char*, Maxwell::MaxShaderStage> BufferBaseNames = { + "buffer_vs_c", "buffer_tessc_c", "buffer_tesse_c", "buffer_gs_c", "buffer_fs_c", + }; + + bool is_used{}; + unsigned index{}; + unsigned max_offset{}; + Maxwell::ShaderStage stage; }; -struct MaxwellShaderConfigCommon { - explicit MaxwellShaderConfigCommon(){}; +struct ShaderEntries { + std::vector<ConstBufferEntry> const_buffer_entries; }; -struct MaxwellVSConfig : MaxwellShaderConfigCommon { - explicit MaxwellVSConfig() : MaxwellShaderConfigCommon() {} +using ProgramResult = std::pair<std::string, ShaderEntries>; - bool operator==(const MaxwellVSConfig& o) const { - return std::memcmp(this, &o, sizeof(MaxwellVSConfig)) == 0; - }; +struct ShaderSetup { + ShaderSetup(ProgramCode&& program_code) : program_code(std::move(program_code)) {} + + ProgramCode program_code; + bool program_code_hash_dirty = true; + + u64 GetProgramCodeHash() { + if (program_code_hash_dirty) { + program_code_hash = Common::ComputeHash64(&program_code, sizeof(program_code)); + program_code_hash_dirty = false; + } + return program_code_hash; + } + +private: + u64 program_code_hash{}; }; -struct MaxwellFSConfig : MaxwellShaderConfigCommon { - explicit MaxwellFSConfig() : MaxwellShaderConfigCommon() {} +struct MaxwellShaderConfigCommon { + void Init(ShaderSetup& setup) { + program_hash = setup.GetProgramCodeHash(); + } - bool operator==(const MaxwellFSConfig& o) const { - return std::memcmp(this, &o, sizeof(MaxwellFSConfig)) == 0; - }; + u64 program_hash; }; -std::string GenerateVertexShader(const MaxwellVSConfig& config); -std::string GenerateFragmentShader(const MaxwellFSConfig& config); +struct MaxwellVSConfig : Common::HashableStruct<MaxwellShaderConfigCommon> { + explicit MaxwellVSConfig(ShaderSetup& setup) { + state.Init(setup); + } +}; + +struct MaxwellFSConfig : Common::HashableStruct<MaxwellShaderConfigCommon> { + explicit MaxwellFSConfig(ShaderSetup& setup) { + state.Init(setup); + } +}; + +/** + * Generates the GLSL vertex shader program source code for the given VS program + * @returns String of the shader source code + */ +ProgramResult GenerateVertexShader(const ShaderSetup& setup, const MaxwellVSConfig& config); + +/** + * Generates the GLSL fragment shader program source code for the given FS program + * @returns String of the shader source code + */ +ProgramResult GenerateFragmentShader(const ShaderSetup& setup, const MaxwellFSConfig& config); } // namespace GLShader @@ -52,14 +119,14 @@ namespace std { template <> struct hash<GLShader::MaxwellVSConfig> { size_t operator()(const GLShader::MaxwellVSConfig& k) const { - return Common::ComputeHash64(&k, sizeof(GLShader::MaxwellVSConfig)); + return k.Hash(); } }; template <> struct hash<GLShader::MaxwellFSConfig> { size_t operator()(const GLShader::MaxwellFSConfig& k) const { - return Common::ComputeHash64(&k, sizeof(GLShader::MaxwellFSConfig)); + return k.Hash(); } }; |