Skip to content

Commit

Permalink
Add shader variables that point to object properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dfranx committed Apr 2, 2020
1 parent 4c6c043 commit caa3971
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 125 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[v1.3.2]
+ add texture properties
+ add shader variables that point to shader properties
+ add "Changelog" popup which shows up on startup after an update
+ add "Show tips on startup" option
+ add "This will unassign Shortcut.ID" popup when changing shortcuts
+ fix ScreenQuad position and size on other RTs
+ fix a bug that caused SHADERed to crash when clicking on shader variable function list combobox

[v1.3.1]
+ add confirmation dialog when deleting a shader pass item
Expand Down
4 changes: 3 additions & 1 deletion src/SHADERed/GUIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ namespace ed {

m_checkChangelog();

FunctionVariableManager::Instance().Initialize(&objects->Pipeline);

m_data->Renderer.Pause(Settings::Instance().Preview.PausedOnStartup);
}
GUIManager::~GUIManager()
Expand Down Expand Up @@ -515,7 +517,7 @@ namespace ed {

// update audio textures
m_data->Objects.Update(delta);
FunctionVariableManager::ClearVariableList();
FunctionVariableManager::Instance().ClearVariableList();

// update editor & workspace font
if (((CodeEditorUI*)Get(ViewID::Code))->NeedsFontUpdate() || ((m_cachedFont != settings.General.Font || m_cachedFontSize != settings.General.FontSize) && strcmp(settings.General.Font, "null") != 0) || m_fontNeedsUpdate) {
Expand Down
253 changes: 145 additions & 108 deletions src/SHADERed/Objects/FunctionVariableManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,135 +5,74 @@
#include <glm/gtc/type_ptr.hpp>

namespace ed {
int FunctionVariableManager::CurrentIndex = 0;
std::vector<ed::ShaderVariable*> FunctionVariableManager::VariableList = std::vector<ed::ShaderVariable*>();

size_t FunctionVariableManager::GetArgumentCount(ed::FunctionShaderVariable func)
FunctionVariableManager::FunctionVariableManager()
{
switch (func) {
case FunctionShaderVariable::MatrixIdentity: return 0;
case FunctionShaderVariable::MatrixLookAtLH: return 9;
case FunctionShaderVariable::MatrixLookToLH: return 9;
case FunctionShaderVariable::MatrixOrthographicLH: return 4;
case FunctionShaderVariable::MatrixPerspectiveFovLH: return 4;
case FunctionShaderVariable::MatrixPerspectiveLH: return 4;
case FunctionShaderVariable::MatrixRotationAxis: return 4;
case FunctionShaderVariable::MatrixRotationNormal: return 4;
case FunctionShaderVariable::MatrixRotationRollPitchYaw: return 3;
case FunctionShaderVariable::MatrixRotationX: return 1;
case FunctionShaderVariable::MatrixRotationY: return 1;
case FunctionShaderVariable::MatrixRotationZ: return 1;
case FunctionShaderVariable::MatrixScaling: return 3;
case FunctionShaderVariable::MatrixTranslation: return 3;
case FunctionShaderVariable::ScalarCos: return 1;
case FunctionShaderVariable::ScalarSin: return 1;
case FunctionShaderVariable::Pointer: return 1;
case FunctionShaderVariable::CameraSnapshot: return 1;
case FunctionShaderVariable::VectorNormalize: return 4;
}
return 0;
m_currentIndex = 0;
}
void FunctionVariableManager::AllocateArgumentSpace(ed::ShaderVariable* var, ed::FunctionShaderVariable func)
{
size_t args = GetArgumentCount(func) * sizeof(float);
if (func == FunctionShaderVariable::PluginFunction)
args = var->PluginFuncData.Owner->GetVariableFunctionArgSpaceSize(var->PluginFuncData.Name, (plugin::VariableType)var->GetType());
var->Function = func;

if (func == ed::FunctionShaderVariable::Pointer || func == ed::FunctionShaderVariable::CameraSnapshot)
args = sizeof(char) * VARIABLE_NAME_LENGTH;

if (var->Arguments != nullptr) {
free(var->Arguments);
var->Arguments = (char*)calloc(args, 1);

// set default values
switch (func) {
case FunctionShaderVariable::MatrixLookAtLH:
*LoadFloat(var->Arguments, 5) = 1;
*LoadFloat(var->Arguments, 7) = 1;
break;
case FunctionShaderVariable::MatrixLookToLH:
*LoadFloat(var->Arguments, 5) = 1;
*LoadFloat(var->Arguments, 7) = 1;
break;
case FunctionShaderVariable::MatrixPerspectiveLH:
case FunctionShaderVariable::MatrixOrthographicLH:
*LoadFloat(var->Arguments, 0) = 800;
*LoadFloat(var->Arguments, 1) = 600;
*LoadFloat(var->Arguments, 2) = 0.1f;
*LoadFloat(var->Arguments, 3) = 100.0f;
break;
case FunctionShaderVariable::MatrixPerspectiveFovLH:
*LoadFloat(var->Arguments, 0) = glm::half_pi<float>();
*LoadFloat(var->Arguments, 1) = 800 / 600.0f;
*LoadFloat(var->Arguments, 2) = 0.1f;
*LoadFloat(var->Arguments, 3) = 100.0f;
break;
case FunctionShaderVariable::MatrixRotationAxis:
*LoadFloat(var->Arguments, 0) = 1;
break;
case FunctionShaderVariable::MatrixRotationNormal:
*LoadFloat(var->Arguments, 0) = 1;
break;
case FunctionShaderVariable::VectorNormalize:
*LoadFloat(var->Arguments, 0) = 1;
break;
case FunctionShaderVariable::Pointer:
memset(var->Arguments, 0, VARIABLE_NAME_LENGTH * sizeof(char));
break;
case FunctionShaderVariable::CameraSnapshot:
memset(var->Arguments, 0, VARIABLE_NAME_LENGTH * sizeof(char));
break;
}
} else
var->Arguments = (char*)calloc(args, 1);
}
bool FunctionVariableManager::HasValidReturnType(ShaderVariable::ValueType ret, ed::FunctionShaderVariable func)
void FunctionVariableManager::Initialize(PipelineManager* pipe)
{
if (func >= FunctionShaderVariable::MatrixIdentity && func <= FunctionShaderVariable::MatrixTranslation)
return ret == ShaderVariable::ValueType::Float4x4;
else if (func >= FunctionShaderVariable::ScalarCos && func <= FunctionShaderVariable::ScalarSin)
return ret == ShaderVariable::ValueType::Float1;
else if (func == FunctionShaderVariable::VectorNormalize)
return ret > ShaderVariable::ValueType::Float1 && ret <= ShaderVariable::ValueType::Float4;
else if (func == FunctionShaderVariable::CameraSnapshot)
return ret == ShaderVariable::ValueType::Float4x4;
else if (func == FunctionShaderVariable::Pointer)
return true;
return false;
m_pipeline = pipe;
m_currentIndex = 0;
}

void FunctionVariableManager::AddToList(ed::ShaderVariable* var)
{
int& curIndex = FunctionVariableManager::CurrentIndex;
std::vector<ed::ShaderVariable*>& varList = FunctionVariableManager::VariableList;
for (int i = 0; i < curIndex; i++)
if (varList[i] == var)
for (int i = 0; i < m_currentIndex; i++)
if (VariableList[i] == var)
return; // already exists
if (curIndex % 20 == 0)
varList.resize(curIndex + 20);
varList[curIndex] = var;
curIndex++;
if (m_currentIndex % 20 == 0)
VariableList.resize(m_currentIndex + 20);
VariableList[m_currentIndex] = var;
m_currentIndex++;
}
void FunctionVariableManager::Update(ed::ShaderVariable* var)
{
if (var->Function == FunctionShaderVariable::None)
return;

if (var->Function == FunctionShaderVariable::Pointer) {
std::vector<ed::ShaderVariable*>& varList = FunctionVariableManager::VariableList;
for (int i = 0; i < varList.size(); i++) {
if (varList[i] == nullptr)
for (int i = 0; i < VariableList.size(); i++) {
if (VariableList[i] == nullptr)
continue;

if (strcmp(varList[i]->Name, var->Arguments) == 0) {
memcpy(var->Data, varList[i]->Data, ShaderVariable::GetSize(var->GetType()));
if (strcmp(VariableList[i]->Name, var->Arguments) == 0) {
memcpy(var->Data, VariableList[i]->Data, ShaderVariable::GetSize(var->GetType()));
break;
}
}
} else if (var->Function == FunctionShaderVariable::CameraSnapshot) {
glm::mat4 camVal = CameraSnapshots::Get(var->Arguments);
memcpy(var->Data, glm::value_ptr(camVal), sizeof(glm::mat4));
} else if (var->Function == FunctionShaderVariable::ObjectProperty) {
ed::PipelineItem* item = m_pipeline->Get(var->Arguments);
char* propName = var->Arguments + PIPELINE_ITEM_NAME_LENGTH;
glm::vec4 dataVal(0.0f);

if (item->Type == PipelineItem::ItemType::Geometry) {
pipe::GeometryItem* geom = (pipe::GeometryItem*)item->Data;

if (strcmp(propName, "Position") == 0)
dataVal = glm::vec4(geom->Position, 1.0f);
else if (strcmp(propName, "Scale") == 0)
dataVal = glm::vec4(geom->Scale, 0.0f);
else if (strcmp(propName, "Rotation") == 0)
dataVal = glm::vec4(geom->Rotation, 0.0f);
} else if (item->Type == PipelineItem::ItemType::Model) {
pipe::Model* mdl = (pipe::Model*)item->Data;

if (strcmp(propName, "Position") == 0)
dataVal = glm::vec4(mdl->Position, 1.0f);
else if (strcmp(propName, "Scale") == 0)
dataVal = glm::vec4(mdl->Scale, 0.0f);
else if (strcmp(propName, "Rotation") == 0)
dataVal = glm::vec4(mdl->Rotation, 0.0f);
}

if (var->GetType() == ShaderVariable::ValueType::Float3)
memcpy(var->Data, glm::value_ptr(dataVal), sizeof(glm::vec3));
else if (var->GetType() == ShaderVariable::ValueType::Float4)
memcpy(var->Data, glm::value_ptr(dataVal), sizeof(glm::vec4));
} else if (var->Function >= FunctionShaderVariable::MatrixIdentity && var->Function <= FunctionShaderVariable::MatrixTranslation) {
glm::mat4 matrix;

Expand Down Expand Up @@ -264,8 +203,106 @@ namespace ed {
}
void FunctionVariableManager::ClearVariableList()
{
FunctionVariableManager::VariableList.clear();
FunctionVariableManager::CurrentIndex = 0;
VariableList.clear();
m_currentIndex = 0;
}

void FunctionVariableManager::AllocateArgumentSpace(ed::ShaderVariable* var, ed::FunctionShaderVariable func)
{
size_t args = GetArgumentCount(func) * sizeof(float);
if (func == FunctionShaderVariable::PluginFunction)
args = var->PluginFuncData.Owner->GetVariableFunctionArgSpaceSize(var->PluginFuncData.Name, (plugin::VariableType)var->GetType());
var->Function = func;

if (func == ed::FunctionShaderVariable::Pointer || func == ed::FunctionShaderVariable::CameraSnapshot || func == ed::FunctionShaderVariable::ObjectProperty)
args = sizeof(char) * VARIABLE_NAME_LENGTH;

if (var->Arguments != nullptr) {
free(var->Arguments);
var->Arguments = (char*)calloc(args, 1);

// set default values
switch (func) {
case FunctionShaderVariable::MatrixLookAtLH:
*LoadFloat(var->Arguments, 5) = 1;
*LoadFloat(var->Arguments, 7) = 1;
break;
case FunctionShaderVariable::MatrixLookToLH:
*LoadFloat(var->Arguments, 5) = 1;
*LoadFloat(var->Arguments, 7) = 1;
break;
case FunctionShaderVariable::MatrixPerspectiveLH:
case FunctionShaderVariable::MatrixOrthographicLH:
*LoadFloat(var->Arguments, 0) = 800;
*LoadFloat(var->Arguments, 1) = 600;
*LoadFloat(var->Arguments, 2) = 0.1f;
*LoadFloat(var->Arguments, 3) = 100.0f;
break;
case FunctionShaderVariable::MatrixPerspectiveFovLH:
*LoadFloat(var->Arguments, 0) = glm::half_pi<float>();
*LoadFloat(var->Arguments, 1) = 800 / 600.0f;
*LoadFloat(var->Arguments, 2) = 0.1f;
*LoadFloat(var->Arguments, 3) = 100.0f;
break;
case FunctionShaderVariable::MatrixRotationAxis:
*LoadFloat(var->Arguments, 0) = 1;
break;
case FunctionShaderVariable::MatrixRotationNormal:
*LoadFloat(var->Arguments, 0) = 1;
break;
case FunctionShaderVariable::VectorNormalize:
*LoadFloat(var->Arguments, 0) = 1;
break;
case FunctionShaderVariable::CameraSnapshot:
case FunctionShaderVariable::ObjectProperty:
case FunctionShaderVariable::Pointer:
memset(var->Arguments, 0, VARIABLE_NAME_LENGTH * sizeof(char));
break;
}
} else
var->Arguments = (char*)calloc(args, 1);
}
size_t FunctionVariableManager::GetArgumentCount(ed::FunctionShaderVariable func)
{
switch (func) {
case FunctionShaderVariable::MatrixIdentity: return 0;
case FunctionShaderVariable::MatrixLookAtLH: return 9;
case FunctionShaderVariable::MatrixLookToLH: return 9;
case FunctionShaderVariable::MatrixOrthographicLH: return 4;
case FunctionShaderVariable::MatrixPerspectiveFovLH: return 4;
case FunctionShaderVariable::MatrixPerspectiveLH: return 4;
case FunctionShaderVariable::MatrixRotationAxis: return 4;
case FunctionShaderVariable::MatrixRotationNormal: return 4;
case FunctionShaderVariable::MatrixRotationRollPitchYaw: return 3;
case FunctionShaderVariable::MatrixRotationX: return 1;
case FunctionShaderVariable::MatrixRotationY: return 1;
case FunctionShaderVariable::MatrixRotationZ: return 1;
case FunctionShaderVariable::MatrixScaling: return 3;
case FunctionShaderVariable::MatrixTranslation: return 3;
case FunctionShaderVariable::ScalarCos: return 1;
case FunctionShaderVariable::ScalarSin: return 1;
case FunctionShaderVariable::Pointer: return 1;
case FunctionShaderVariable::CameraSnapshot: return 1;
case FunctionShaderVariable::ObjectProperty: return 2;
case FunctionShaderVariable::VectorNormalize: return 4;
}
return 0;
}
bool FunctionVariableManager::HasValidReturnType(ShaderVariable::ValueType ret, ed::FunctionShaderVariable func)
{
if (func >= FunctionShaderVariable::MatrixIdentity && func <= FunctionShaderVariable::MatrixTranslation)
return ret == ShaderVariable::ValueType::Float4x4;
else if (func >= FunctionShaderVariable::ScalarCos && func <= FunctionShaderVariable::ScalarSin)
return ret == ShaderVariable::ValueType::Float1;
else if (func == FunctionShaderVariable::VectorNormalize)
return ret > ShaderVariable::ValueType::Float1 && ret <= ShaderVariable::ValueType::Float4;
else if (func == FunctionShaderVariable::CameraSnapshot)
return ret == ShaderVariable::ValueType::Float4x4;
else if (func == FunctionShaderVariable::Pointer)
return true;
else if (func == FunctionShaderVariable::ObjectProperty)
return ret == ShaderVariable::ValueType::Float4 || ret == ShaderVariable::ValueType::Float3;
return false;
}
float* FunctionVariableManager::LoadFloat(char* data, int index)
{
Expand Down
27 changes: 21 additions & 6 deletions src/SHADERed/Objects/FunctionVariableManager.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
#pragma once
#include <SHADERed/Objects/ShaderVariable.h>
#include <SHADERed/Objects/PipelineManager.h>
#include <vector>

namespace ed {
class FunctionVariableManager {
public:
static size_t GetArgumentCount(ed::FunctionShaderVariable func);
FunctionVariableManager();

void Initialize(PipelineManager* pipe);

void AddToList(ed::ShaderVariable* var);
void Update(ed::ShaderVariable* var);

static void AllocateArgumentSpace(ed::ShaderVariable* var, ed::FunctionShaderVariable func);
static size_t GetArgumentCount(ed::FunctionShaderVariable func);
static bool HasValidReturnType(ShaderVariable::ValueType ret, ed::FunctionShaderVariable func);
static void AddToList(ed::ShaderVariable* var);
static void Update(ed::ShaderVariable* var);
static float* LoadFloat(char* data, int index);

static void ClearVariableList();
void ClearVariableList();

std::vector<ed::ShaderVariable*> VariableList;

static inline FunctionVariableManager& Instance()
{
static FunctionVariableManager ret;
return ret;
}

static int CurrentIndex;
static std::vector<ed::ShaderVariable*> VariableList;
private:
int m_currentIndex;
PipelineManager* m_pipeline;
};
}
1 change: 1 addition & 0 deletions src/SHADERed/Objects/Names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const char* FUNCTION_NAMES[] = {
"None",
"Pointer",
"CameraSnapshot",
"ObjectProperty",
"MatrixIdentity",
"MatrixLookAtLH",
"MatrixLookToLH",
Expand Down
2 changes: 1 addition & 1 deletion src/SHADERed/Objects/Names.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern const char* TOPOLOGY_ITEM_NAMES[10];
extern const char* SYSTEM_VARIABLE_NAMES[20];
extern const char* VARIABLE_TYPE_NAMES[15];
extern const char* VARIABLE_TYPE_NAMES_GLSL[15];
extern const char* FUNCTION_NAMES[23];
extern const char* FUNCTION_NAMES[22];
extern const char* GEOMETRY_NAMES[7];
extern const char* PIPELINE_ITEM_NAMES[7];
extern const char* BLEND_NAMES[20];
Expand Down
Loading

0 comments on commit caa3971

Please sign in to comment.