From 10953495c1b7de412312b64370d99a2294dfe6a2 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 7 Apr 2018 04:51:42 -0400 Subject: gl_shader_gen: Add hashable setup/config structs. --- src/video_core/renderer_opengl/gl_shader_gen.h | 75 ++++++++++++++++---------- 1 file changed, 48 insertions(+), 27 deletions(-) (limited to 'src/video_core/renderer_opengl/gl_shader_gen.h') diff --git a/src/video_core/renderer_opengl/gl_shader_gen.h b/src/video_core/renderer_opengl/gl_shader_gen.h index 5101e7d30..925e66ee4 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.h +++ b/src/video_core/renderer_opengl/gl_shader_gen.h @@ -4,46 +4,67 @@ #pragma once -#include +#include #include #include +#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}; -struct MaxwellShaderConfigCommon { - explicit MaxwellShaderConfigCommon(){}; +using ProgramCode = std::array; + +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 MaxwellVSConfig : MaxwellShaderConfigCommon { - explicit MaxwellVSConfig() : MaxwellShaderConfigCommon() {} +struct MaxwellShaderConfigCommon { + void Init(ShaderSetup& setup) { + program_hash = setup.GetProgramCodeHash(); + } - bool operator==(const MaxwellVSConfig& o) const { - return std::memcmp(this, &o, sizeof(MaxwellVSConfig)) == 0; - }; + u64 program_hash; }; -struct MaxwellFSConfig : MaxwellShaderConfigCommon { - explicit MaxwellFSConfig() : MaxwellShaderConfigCommon() {} +struct MaxwellVSConfig : Common::HashableStruct { + explicit MaxwellVSConfig(ShaderSetup& setup) { + state.Init(setup); + } +}; - bool operator==(const MaxwellFSConfig& o) const { - return std::memcmp(this, &o, sizeof(MaxwellFSConfig)) == 0; - }; +struct MaxwellFSConfig : Common::HashableStruct { + explicit MaxwellFSConfig(ShaderSetup& setup) { + state.Init(setup); + } }; -std::string GenerateVertexShader(const MaxwellVSConfig& config); -std::string GenerateFragmentShader(const MaxwellFSConfig& config); +/** + * Generates the GLSL vertex shader program source code for the given VS program + * @returns String of the shader source code + */ +std::string 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 + */ +std::string GenerateFragmentShader(const ShaderSetup& setup, const MaxwellFSConfig& config); } // namespace GLShader @@ -52,14 +73,14 @@ namespace std { template <> struct hash { size_t operator()(const GLShader::MaxwellVSConfig& k) const { - return Common::ComputeHash64(&k, sizeof(GLShader::MaxwellVSConfig)); + return k.Hash(); } }; template <> struct hash { size_t operator()(const GLShader::MaxwellFSConfig& k) const { - return Common::ComputeHash64(&k, sizeof(GLShader::MaxwellFSConfig)); + return k.Hash(); } }; -- cgit v1.2.3