From cc57914bbfb115136f694fd21c0a7ecc4d79318f Mon Sep 17 00:00:00 2001 From: Rachael Alexanderson Date: Tue, 1 Feb 2022 05:32:16 -0500 Subject: [PATCH] - fully implement uniform cvars --- source/common/console/c_cvars.cpp | 22 ++++++++ source/common/console/c_cvars.h | 8 +++ source/core/r_data/gldefs.cpp | 90 ++++++++++++++++++++++++++++++- 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/source/common/console/c_cvars.cpp b/source/common/console/c_cvars.cpp index 6510d561e..b9d8a3efc 100644 --- a/source/common/console/c_cvars.cpp +++ b/source/common/console/c_cvars.cpp @@ -145,6 +145,28 @@ FBaseCVar::~FBaseCVar () } } +void FBaseCVar::SetCallback(void *callback) +{ + m_Callback = reinterpret_cast(callback); + m_UseCallback = true; +} + +void FBaseCVar::ClearCallback() +{ + m_Callback = nullptr; + m_UseCallback = false; +} + +void FBaseCVar::SetExtraDataPointer(void *pointer) +{ + m_ExtraDataPointer = pointer; +} + +void* FBaseCVar::GetExtraDataPointer() +{ + return m_ExtraDataPointer; +} + const char *FBaseCVar::GetHumanString(int precision) const { return GetGenericRep(CVAR_String).String; diff --git a/source/common/console/c_cvars.h b/source/common/console/c_cvars.h index 7d5201a1d..fd65f14b5 100644 --- a/source/common/console/c_cvars.h +++ b/source/common/console/c_cvars.h @@ -184,6 +184,12 @@ public: ToggleMessages[1] = on; } + void SetCallback(void *callback); + void ClearCallback(); + + void SetExtraDataPointer(void *pointer); + + void* GetExtraDataPointer(); protected: virtual void DoSet (UCVarValue value, ECVarType type) = 0; @@ -213,6 +219,8 @@ private: static bool m_UseCallback; static bool m_DoNoSet; + void *m_ExtraDataPointer; + // These need to go away! friend FString C_GetMassCVarString (uint32_t filter, bool compact); friend void C_SerializeCVars(FSerializer& arc, const char* label, uint32_t filter); diff --git a/source/core/r_data/gldefs.cpp b/source/core/r_data/gldefs.cpp index 4e334b128..aacb25a0e 100644 --- a/source/core/r_data/gldefs.cpp +++ b/source/core/r_data/gldefs.cpp @@ -60,6 +60,46 @@ extern int AttenuationIsSet; #endif +struct ExtraUniformCVARData +{ + FString Shader; + FString Uniform; + double* vec4 = nullptr; +}; + +static void do_uniform_set(float value, ExtraUniformCVARData* data) +{ + if (!(data->vec4)) + { + for (unsigned int i = 0; i < PostProcessShaders.Size(); i++) + { + PostProcessShader& shader = PostProcessShaders[i]; + if (strcmp(shader.Name, data->Shader) == 0) + { + data->vec4 = shader.Uniforms[data->Uniform].Values; + } + } + } + double* vec4 = data->vec4; + if (vec4) + { + vec4[0] = value; + vec4[1] = 0.0; + vec4[2] = 0.0; + vec4[3] = 1.0; + } +} + +void uniform_callback_int(FIntCVar &self) +{ + do_uniform_set ((float)self, (ExtraUniformCVARData*)self.GetExtraDataPointer()); +} + +void uniform_callback_float(FFloatCVar &self) +{ + do_uniform_set ((float)self, (ExtraUniformCVARData*)self.GetExtraDataPointer()); +} + //----------------------------------------------------------------------------- // // ParseVavoomSkybox @@ -1457,8 +1497,10 @@ class GLDefsParser sc.MustGetString(); shaderdesc.Name = sc.String; } - else if (sc.Compare("uniform")) + else if (sc.Compare("uniform") || sc.Compare("cvar_uniform")) { + bool is_cvar = sc.Compare("cvar_uniform"); + sc.MustGetString(); FString uniformType = sc.String; uniformType.ToLower(); @@ -1479,8 +1521,54 @@ class GLDefsParser else sc.ScriptError("Unrecognized uniform type '%s'", sc.String); + auto strUniformType = sc.String; + if (parsedType != PostProcessUniformType::Undefined) shaderdesc.Uniforms[uniformName].Type = parsedType; + + if (is_cvar) + { + if (!shaderdesc.Name.GetChars()) + sc.ScriptError("Shader must have a name to use cvar uniforms"); + + ECVarType cvartype = CVAR_Dummy; + int cvarflags = CVAR_MOD|CVAR_ARCHIVE|CVAR_VIRTUAL; + FBaseCVar *cvar; + void* callback = NULL; + FString cvarname; + switch (parsedType) + { + case PostProcessUniformType::Int: + cvartype = CVAR_Int; + callback = uniform_callback_int; + break; + case PostProcessUniformType::Float: + cvartype = CVAR_Float; + callback = uniform_callback_float; + break; + default: + sc.ScriptError("'%s' not supported for CVAR uniforms!", strUniformType); + break; + } + sc.MustGetString(); + cvarname = sc.String; + cvar = C_CreateCVar(cvarname, cvartype, cvarflags); + + UCVarValue val; + sc.MustGetNumber(); + + val.Float = sc.Number; + shaderdesc.Uniforms[uniformName].Values[0] = sc.Number; + + cvar->SetGenericRepDefault(val, CVAR_Float); + + if (callback) + cvar->SetCallback(callback); + ExtraUniformCVARData* extra = new ExtraUniformCVARData; + extra->Shader = shaderdesc.Name.GetChars(); + extra->Uniform = uniformName.GetChars(); + cvar->SetExtraDataPointer(extra); + } } else if (sc.Compare("texture")) {