From 0e2ef6a889d15e64f8dac06536b6300884030e14 Mon Sep 17 00:00:00 2001 From: Areloch Date: Thu, 5 Oct 2017 17:04:51 -0500 Subject: [PATCH 1/9] Initial implementation of Custom Shader Features. --- .../source/materials/materialDefinition.cpp | 16 ++ Engine/source/materials/materialDefinition.h | 9 +- Engine/source/materials/processedMaterial.h | 2 + .../materials/processedShaderMaterial.cpp | 2 +- .../shaderGen/HLSL/customFeatureHLSL.cpp | 213 ++++++++++++++++++ .../source/shaderGen/HLSL/customFeatureHLSL.h | 68 ++++++ .../source/shaderGen/customShaderFeature.cpp | 141 ++++++++++++ Engine/source/shaderGen/customShaderFeature.h | 74 ++++++ Engine/source/shaderGen/shaderGen.cpp | 35 ++- Engine/source/shaderGen/shaderGen.h | 11 +- Engine/source/terrain/terrCellMaterial.cpp | 4 +- .../Full/game/art/shapes/cube/materials.cs | 29 ++- 12 files changed, 595 insertions(+), 9 deletions(-) create mode 100644 Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp create mode 100644 Engine/source/shaderGen/HLSL/customFeatureHLSL.h create mode 100644 Engine/source/shaderGen/customShaderFeature.cpp create mode 100644 Engine/source/shaderGen/customShaderFeature.h diff --git a/Engine/source/materials/materialDefinition.cpp b/Engine/source/materials/materialDefinition.cpp index d4ec8ed7d..deba2ff0d 100644 --- a/Engine/source/materials/materialDefinition.cpp +++ b/Engine/source/materials/materialDefinition.cpp @@ -483,6 +483,9 @@ void Material::initPersistFields() endGroup( "Behavioral" ); + addProtectedField("customShaderFeature", TypeRealString, NULL, &protectedSetCustomShaderFeature, &defaultProtectedGetFn, + "Do not modify, for internal use.", AbstractClassRep::FIELD_HideInInspectors); + Parent::initPersistFields(); } @@ -500,6 +503,19 @@ bool Material::writeField( StringTableEntry fieldname, const char *value ) return Parent::writeField( fieldname, value ); } +bool Material::protectedSetCustomShaderFeature(void *object, const char *index, const char *data) +{ + Material *material = static_cast< Material* >(object); + + CustomShaderFeatureData* customFeature; + if (!Sim::findObject(data, customFeature)) + return false; + + material->mCustomShaderFeatures.push_back(customFeature); + + return false; +} + bool Material::onAdd() { if (Parent::onAdd() == false) diff --git a/Engine/source/materials/materialDefinition.h b/Engine/source/materials/materialDefinition.h index be0e2ae71..b6bdba054 100644 --- a/Engine/source/materials/materialDefinition.h +++ b/Engine/source/materials/materialDefinition.h @@ -41,6 +41,9 @@ #include "console/dynamicTypes.h" #endif +#ifndef CUSTOMSHADERFEATURE_H +#include "shaderGen/customShaderFeature.h" +#endif class CubemapData; class SFXTrack; @@ -49,7 +52,7 @@ class FeatureSet; class FeatureType; class MaterialSoundProfile; class MaterialPhysicsProfile; - +class CustomShaderFeatureData; /// The basic material definition. class Material : public BaseMaterialDefinition @@ -343,6 +346,8 @@ public: F32 mDirectSoundOcclusion; ///< Amount of volume occlusion on direct sounds. F32 mReverbSoundOcclusion; ///< Amount of volume occlusion on reverb sounds. + Vector mCustomShaderFeatures; + ///@} String mMapTo; // map Material to this texture name @@ -380,6 +385,8 @@ public: virtual void inspectPostApply(); virtual bool writeField( StringTableEntry fieldname, const char *value ); + static bool protectedSetCustomShaderFeature(void *object, const char *index, const char *data); + // // ConsoleObject interface // diff --git a/Engine/source/materials/processedMaterial.h b/Engine/source/materials/processedMaterial.h index 3c3f97a88..38685eacb 100644 --- a/Engine/source/materials/processedMaterial.h +++ b/Engine/source/materials/processedMaterial.h @@ -81,6 +81,8 @@ public: MaterialFeatureData mFeatureData; + Vector mCustomShaderFeatureData; + bool mGlow; Material::BlendOp mBlendOp; diff --git a/Engine/source/materials/processedShaderMaterial.cpp b/Engine/source/materials/processedShaderMaterial.cpp index 4fb2acc94..2785dc429 100644 --- a/Engine/source/materials/processedShaderMaterial.cpp +++ b/Engine/source/materials/processedShaderMaterial.cpp @@ -654,7 +654,7 @@ bool ProcessedShaderMaterial::_addPass( ShaderRenderPassData &rpd, // Generate shader GFXShader::setLogging( true, true ); - rpd.shader = SHADERGEN->getShader( rpd.mFeatureData, mVertexFormat, &mUserMacros, samplers ); + rpd.shader = SHADERGEN->getShader( rpd.mFeatureData, mMaterial->mCustomShaderFeatures, mVertexFormat, &mUserMacros, samplers ); if( !rpd.shader ) return false; rpd.shaderHandles.init( rpd.shader ); diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp new file mode 100644 index 000000000..74c687db8 --- /dev/null +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp @@ -0,0 +1,213 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2012 GarageGames, LLC +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +//----------------------------------------------------------------------------- + +#include "customFeatureHLSL.h" +#include "shaderGen/shaderFeature.h" +#include "shaderGen/shaderOp.h" +#include "shaderGen/featureMgr.h" +//#include "materials/materialFeatureTypes.h" +//#include "gfx/gfxDevice.h" +//#include "materials/processedMaterial.h" + +//**************************************************************************** +// Accu Texture +//**************************************************************************** +void CustomFeatureHLSL::processVert(Vector &componentList, + const MaterialFeatureData &fd) +{ + /*MultiLine *meta = new MultiLine; + getOutTexCoord( "texCoord", + "float2", + false, + meta, + componentList ); + + getOutObjToTangentSpace( componentList, meta, fd ); + + output = meta;*/ + + if (mOwner->isMethod("processVertHLSL")) + Con::executef(mOwner, "processVertHLSL"); +} + +void CustomFeatureHLSL::processPix(Vector &componentList, + const MaterialFeatureData &fd) +{ + meta = new MultiLine; + + /*MultiLine *meta = new MultiLine; + + output = meta; + + // OUT.col + Var *color = (Var*) LangElement::find( "col1" ); + if (!color) + { + output = new GenOp(" //NULL COLOR!"); + return; + } + + // accu map + Var *accuMap = new Var; + accuMap->setType("SamplerState"); + + accuMap->setName( "accuMap" ); + accuMap->uniform = true; + accuMap->sampler = true; + accuMap->constNum = Var::getTexUnitNum(); // used as texture unit num here + + // accuColor var + Var *accuColor = new Var; + accuColor->setType( "float4" ); + accuColor->setName( "accuColor" ); + LangElement *colorAccuDecl = new DecOp( accuColor ); + + // plc (placement) + Var *accuPlc = new Var; + accuPlc->setType( "float4" ); + accuPlc->setName( "plc" ); + LangElement *plcAccu = new DecOp( accuPlc ); + + // accu constants + Var *accuScale = (Var*)LangElement::find( "accuScale" ); + if ( !accuScale ) + { + accuScale = new Var; + accuScale->setType( "float" ); + accuScale->setName( "accuScale" ); + accuScale->uniform = true; + accuScale->sampler = false; + accuScale->constSortPos = cspPotentialPrimitive; + } + Var *accuDirection = (Var*)LangElement::find( "accuDirection" ); + if ( !accuDirection ) + { + accuDirection = new Var; + accuDirection->setType( "float" ); + accuDirection->setName( "accuDirection" ); + accuDirection->uniform = true; + accuDirection->sampler = false; + accuDirection->constSortPos = cspPotentialPrimitive; + } + Var *accuStrength = (Var*)LangElement::find( "accuStrength" ); + if ( !accuStrength ) + { + accuStrength = new Var; + accuStrength->setType( "float" ); + accuStrength->setName( "accuStrength" ); + accuStrength->uniform = true; + accuStrength->sampler = false; + accuStrength->constSortPos = cspPotentialPrimitive; + } + Var *accuCoverage = (Var*)LangElement::find( "accuCoverage" ); + if ( !accuCoverage ) + { + accuCoverage = new Var; + accuCoverage->setType( "float" ); + accuCoverage->setName( "accuCoverage" ); + accuCoverage->uniform = true; + accuCoverage->sampler = false; + accuCoverage->constSortPos = cspPotentialPrimitive; + } + Var *accuSpecular = (Var*)LangElement::find( "accuSpecular" ); + if ( !accuSpecular ) + { + accuSpecular = new Var; + accuSpecular->setType( "float" ); + accuSpecular->setName( "accuSpecular" ); + accuSpecular->uniform = true; + accuSpecular->sampler = false; + accuSpecular->constSortPos = cspPotentialPrimitive; + } + + Var *inTex = getInTexCoord( "texCoord", "float2", componentList ); + Var *accuVec = getInTexCoord( "accuVec", "float3", componentList ); + Var *bumpNorm = (Var *)LangElement::find( "bumpSample" ); + if( bumpNorm == NULL ) + { + bumpNorm = (Var *)LangElement::find( "bumpNormal" ); + if (!bumpNorm) + return; + } + + // get the accu pixel color + + Var *accuMapTex = new Var; + accuMapTex->setType("Texture2D"); + accuMapTex->setName("accuMapTex"); + accuMapTex->uniform = true; + accuMapTex->texture = true; + accuMapTex->constNum = accuMap->constNum; + meta->addStatement(new GenOp(" @ = @.Sample(@, @ * @);\r\n", colorAccuDecl, accuMapTex, accuMap, inTex, accuScale)); + + // scale up normals + meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 0.5;\r\n", bumpNorm, bumpNorm ) ); + + // assign direction + meta->addStatement( new GenOp( " @.z *= @*2.0;\r\n", accuVec, accuDirection ) ); + + // saturate based on strength + meta->addStatement( new GenOp( " @ = saturate( dot( @.xyz, @.xyz * pow(@, 5) ) );\r\n", plcAccu, bumpNorm, accuVec, accuStrength ) ); + + // add coverage + meta->addStatement( new GenOp( " @.a += (2 * pow(@/2, 5)) - 0.5;\r\n", accuPlc, accuCoverage ) ); + + // clamp to a sensible value + meta->addStatement( new GenOp( " @.a = clamp(@.a, 0, 1);\r\n", accuPlc, accuPlc ) ); + + // light + Var *lightColor = (Var*) LangElement::find( "d_lightcolor" ); + if(lightColor != NULL) + meta->addStatement( new GenOp( " @ *= float4(@, 1.0);\r\n\r\n", accuColor, lightColor ) ); + + // lerp with current pixel - use the accu alpha as well + meta->addStatement( new GenOp( " @ = lerp( @, @, @.a * @.a);\r\n", color, color, accuColor, accuPlc, accuColor ) ); + + // the result should always be opaque + meta->addStatement( new GenOp( " @.a = 1.0;\r\n", color ) );*/ + if (mOwner->isMethod("processPixelHLSL")) + Con::executef(mOwner, "processPixelHLSL"); + + output = meta; +} + +void CustomFeatureHLSL::setTexData(Material::StageData &stageDat, + const MaterialFeatureData &fd, + RenderPassData &passData, + U32 &texIndex) +{ + //GFXTextureObject *tex = stageDat.getTex( MFT_AccuMap ); + //if ( tex ) + //{ + //passData.mSamplerNames[ texIndex ] = "AccuMap"; + //passData.mTexType[ texIndex++ ] = Material::AccuMap; + //} + + if (mOwner->isMethod("setTextureData")) + Con::executef(mOwner, "setTextureData"); +} + +void CustomFeatureHLSL::writeLine(String format, S32 argc, ConsoleValueRef *argv) +{ + //do the var/arg fetching here + meta->addStatement(new GenOp(format + "\n"/*, colorAccuDecl, accuMapTex, accuMap, inTex, accuScale*/)); +} \ No newline at end of file diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.h b/Engine/source/shaderGen/HLSL/customFeatureHLSL.h new file mode 100644 index 000000000..23d2b2ca4 --- /dev/null +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.h @@ -0,0 +1,68 @@ +#ifndef _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_ +#include "shaderGen/HLSL/shaderFeatureHLSL.h" +#endif +#ifndef _LANG_ELEMENT_H_ +#include "shaderGen/langElement.h" +#endif +#ifndef _GFXDEVICE_H_ +#include "gfx/gfxDevice.h" +#endif +#ifndef _FEATUREMGR_H_ +#include "shaderGen/featureMgr.h" +#endif +#ifndef _MATERIALFEATURETYPES_H_ +#include "materials/materialFeatureTypes.h" +#endif +#ifndef _MATERIALFEATUREDATA_H_ +#include "materials/materialFeatureData.h" +#endif + +#ifndef CUSTOMSHADERFEATURE_H +#include "shaderGen/customShaderFeature.h" +#endif + +class CustomShaderFeatureData; + +class CustomFeatureHLSL : public ShaderFeatureHLSL +{ + friend class CustomShaderFeatureData; + +public: + CustomShaderFeatureData* mOwner; + +protected: + MultiLine *meta; + +public: + + //**************************************************************************** + // Accu Texture + //**************************************************************************** + virtual void processVert(Vector &componentList, + const MaterialFeatureData &fd); + + virtual void processPix(Vector &componentList, + const MaterialFeatureData &fd); + + virtual Material::BlendOp getBlendOp() { return Material::LerpAlpha; } + + virtual Resources getResources(const MaterialFeatureData &fd) + { + Resources res; + res.numTex = 1; + res.numTexReg = 1; + return res; + } + + virtual void setTexData(Material::StageData &stageDat, + const MaterialFeatureData &fd, + RenderPassData &passData, + U32 &texIndex); + + virtual String getName() + { + return mOwner->getName(); + } + + void writeLine(String format, S32 argc, ConsoleValueRef *argv); +}; \ No newline at end of file diff --git a/Engine/source/shaderGen/customShaderFeature.cpp b/Engine/source/shaderGen/customShaderFeature.cpp new file mode 100644 index 000000000..2809cfa57 --- /dev/null +++ b/Engine/source/shaderGen/customShaderFeature.cpp @@ -0,0 +1,141 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2012 GarageGames, LLC +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +//----------------------------------------------------------------------------- + +#include "shadergen/CustomShaderFeature.h" +#include "shaderGen/HLSL/customFeatureHLSL.h" + +#include "math/mathIO.h" +#include "scene/sceneRenderState.h" +#include "core/stream/bitStream.h" +#include "materials/sceneData.h" +#include "gfx/gfxDebugEvent.h" +#include "gfx/gfxTransformSaver.h" +#include "renderInstance/renderPassManager.h" + + +IMPLEMENT_CONOBJECT(CustomShaderFeatureData); + +ConsoleDocClass(CustomShaderFeatureData, + "@brief An example scene object which renders using a callback.\n\n" + "This class implements a basic SceneObject that can exist in the world at a " + "3D position and render itself. Note that CustomShaderFeatureData handles its own " + "rendering by submitting itself as an ObjectRenderInst (see " + "renderInstance\renderPassmanager.h) along with a delegate for its render() " + "function. However, the preffered rendering method in the engine is to submit " + "a MeshRenderInst along with a Material, vertex buffer, primitive buffer, and " + "transform and allow the RenderMeshMgr handle the actual rendering. You can " + "see this implemented in RenderMeshExample.\n\n" + "See the C++ code for implementation details.\n\n" + "@ingroup Examples\n"); + +//----------------------------------------------------------------------------- +// Object setup and teardown +//----------------------------------------------------------------------------- +CustomShaderFeatureData::CustomShaderFeatureData() +{ +} + +CustomShaderFeatureData::~CustomShaderFeatureData() +{ +} + +//----------------------------------------------------------------------------- +// Object Editing +//----------------------------------------------------------------------------- +void CustomShaderFeatureData::initPersistFields() +{ + // SceneObject already handles exposing the transform + Parent::initPersistFields(); +} + +bool CustomShaderFeatureData::onAdd() +{ + if (!Parent::onAdd()) + return false; + + mFeatureHLSL = new CustomFeatureHLSL(); + mFeatureHLSL->mOwner = this; + + return true; +} + +void CustomShaderFeatureData::onRemove() +{ + Parent::onRemove(); +} + +//Shadergen setup functions +void CustomShaderFeatureData::writeLine(String format, S32 argc, ConsoleValueRef *argv) +{ + /*mOnObject = onObject; + mArgc = argc; + + mArgv = new ConsoleValueRef[argc]; + for (int i = 0; itype = ConsoleValue::TypeInternalString; + mArgv[i].value->init(); + if (argv) + { + mArgv[i].value->setStringValue((const char*)argv[i]); + } + }*/ + + mFeatureHLSL->writeLine(format, argc, argv); +} + +/*//Actual shader processing +void CustomShaderFeatureData::processVert(Vector &componentList, + const MaterialFeatureData &fd) +{ + mFeatureHLSL.processVert(componentList, fd); +} + +void CustomShaderFeatureData::processPix(Vector &componentList, + const MaterialFeatureData &fd) +{ + mFeatureHLSL.processPix(componentList, fd); + +} + +void CustomShaderFeatureData::setTexData(Material::StageData &stageDat, + const MaterialFeatureData &fd, + RenderPassData &passData, + U32 &texIndex) +{ + mFeatureHLSL.setTexData(stageDat, fd, passData, texIndex); +}*/ + +/*DefineEngineMethod(CustomShaderFeatureData, newVar, void, (String name, String type), ("", ""), "") +{ + object->newVar(name, type); +}*/ + +ConsoleMethod(CustomShaderFeatureData, writeLine, void, 3, 0, "( string format, string args... ) Dynamically call a method on an object.\n" + "@param method Name of method to call.\n" + "@param args Zero or more arguments for the method.\n" + "@return The result of the method call.") +{ + argv[1] = argv[2]; + object->writeLine(argv[1], argc - 1, argv + 1); +} \ No newline at end of file diff --git a/Engine/source/shaderGen/customShaderFeature.h b/Engine/source/shaderGen/customShaderFeature.h new file mode 100644 index 000000000..f40f55152 --- /dev/null +++ b/Engine/source/shaderGen/customShaderFeature.h @@ -0,0 +1,74 @@ +//----------------------------------------------------------------------------- +// Copyright (c) 2012 GarageGames, LLC +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +//----------------------------------------------------------------------------- +#pragma once +#ifndef CUSTOMSHADERFEATURE_H +#define CUSTOMSHADERFEATURE_H + +#ifndef _SIMOBJECT_H_ +#include "console/simObject.h" +#endif + +class CustomFeatureHLSL; + +class CustomShaderFeatureData : public SimObject +{ + typedef SimObject Parent; + +public: + CustomFeatureHLSL* mFeatureHLSL; + +public: + CustomShaderFeatureData(); + virtual ~CustomShaderFeatureData(); + + // Declare this object as a ConsoleObject so that we can + // instantiate it into the world and network it + DECLARE_CONOBJECT(CustomShaderFeatureData); + + //-------------------------------------------------------------------------- + // Object Editing + // Since there is always a server and a client object in Torque and we + // actually edit the server object we need to implement some basic + // networking functions + //-------------------------------------------------------------------------- + // Set up any fields that we want to be editable (like position) + static void initPersistFields(); + + // Handle when we are added to the scene and removed from the scene + bool onAdd(); + void onRemove(); + + //shadergen setup + void writeLine(String format, S32 argc, ConsoleValueRef *argv); + + //shader generation + /*void CustomShaderFeatureData::processVert(Vector &componentList, + const MaterialFeatureData &fd); + void CustomShaderFeatureData::processPix(Vector &componentList, + const MaterialFeatureData &fd); + void CustomShaderFeatureData::setTexData(Material::StageData &stageDat, + const MaterialFeatureData &fd, + RenderPassData &passData, + U32 &texIndex);*/ +}; + +#endif \ No newline at end of file diff --git a/Engine/source/shaderGen/shaderGen.cpp b/Engine/source/shaderGen/shaderGen.cpp index a182588d5..5ed514aae 100644 --- a/Engine/source/shaderGen/shaderGen.cpp +++ b/Engine/source/shaderGen/shaderGen.cpp @@ -31,6 +31,7 @@ #include "core/memVolume.h" #include "core/module.h" +#include "shaderGen/HLSL/customFeatureHLSL.h" MODULE_BEGIN( ShaderGen ) @@ -135,13 +136,16 @@ void ShaderGen::generateShader( const MaterialFeatureData &featureData, F32 *pixVersion, const GFXVertexFormat *vertexFormat, const char* cacheName, - Vector ¯os ) + Vector ¯os, + Vector &customFeatureData) { PROFILE_SCOPE( ShaderGen_GenerateShader ); mFeatureData = featureData; mVertexFormat = vertexFormat; + mCustomFeaturesData = customFeatureData; + _uninit(); _init(); @@ -320,6 +324,31 @@ void ShaderGen::_processPixFeatures( Vector ¯os, bool macros mOutput->addStatement( new GenOp( " \r\n" ) ); } } + + //Handle if we have any custom features + if (!mCustomFeaturesData.empty()) + { + for (U32 i = 0; i < mCustomFeaturesData.size(); ++i) + { + mCustomFeaturesData[i]->mFeatureHLSL->processPix(mComponents, mFeatureData); + + String line = String::ToString(" // %s\r\n", mCustomFeaturesData[i]->mFeatureHLSL->getName().c_str()); + mOutput->addStatement(new GenOp(line)); + + if (mCustomFeaturesData[i]->mFeatureHLSL->getOutput()) + mOutput->addStatement(mCustomFeaturesData[i]->mFeatureHLSL->getOutput()); + //ShaderFeatureHLSL feature = mCustomFeaturesData[i]->mHLSLFeature; + //feature->setProcessIndex(index); + + /*feature->processPixMacros(macros, mFeatureData); + + feature->setInstancingFormat(&mInstancingFormat); + feature->processPix(mComponents, mFeatureData);*/ + + mCustomFeaturesData[i]->mFeatureHLSL->reset(); + mOutput->addStatement(new GenOp(" \r\n")); + } + } ShaderConnector *connect = dynamic_cast( mComponents[C_CONNECTOR] ); connect->sortVars(); @@ -443,7 +472,7 @@ void ShaderGen::_printPixShader( Stream &stream ) mPrinter->printPixelShaderCloser(stream); } -GFXShader* ShaderGen::getShader( const MaterialFeatureData &featureData, const GFXVertexFormat *vertexFormat, const Vector *macros, const Vector &samplers ) +GFXShader* ShaderGen::getShader( const MaterialFeatureData &featureData, Vector &customFeatureData, const GFXVertexFormat *vertexFormat, const Vector *macros, const Vector &samplers ) { PROFILE_SCOPE( ShaderGen_GetShader ); @@ -483,7 +512,7 @@ GFXShader* ShaderGen::getShader( const MaterialFeatureData &featureData, const G shaderMacros.push_back( GFXShaderMacro( "TORQUE_SHADERGEN" ) ); if ( macros ) shaderMacros.merge( *macros ); - generateShader( featureData, vertFile, pixFile, &pixVersion, vertexFormat, cacheKey, shaderMacros ); + generateShader( featureData, vertFile, pixFile, &pixVersion, vertexFormat, cacheKey, shaderMacros, customFeatureData ); GFXShader *shader = GFX->createShader(); if (!shader->init(vertFile, pixFile, pixVersion, shaderMacros, samplers, &mInstancingFormat)) diff --git a/Engine/source/shaderGen/shaderGen.h b/Engine/source/shaderGen/shaderGen.h index 2ebcda4c2..829487602 100644 --- a/Engine/source/shaderGen/shaderGen.h +++ b/Engine/source/shaderGen/shaderGen.h @@ -47,6 +47,10 @@ #include "materials/materialFeatureData.h" #endif +#ifndef CUSTOMSHADERFEATURE_H +#include "shadergen/customShaderFeature.h" +#endif + /// Base class used by shaderGen to be API agnostic. Subclasses implement the various methods /// in an API specific way. class ShaderGenPrinter @@ -151,10 +155,11 @@ public: F32 *pixVersion, const GFXVertexFormat *vertexFormat, const char* cacheName, - Vector ¯os ); + Vector ¯os, + Vector &customFeatureData); // Returns a shader that implements the features listed by dat. - GFXShader* getShader( const MaterialFeatureData &dat, const GFXVertexFormat *vertexFormat, const Vector *macros, const Vector &samplers ); + GFXShader* getShader( const MaterialFeatureData &dat, Vector &customFeatureData, const GFXVertexFormat *vertexFormat, const Vector *macros, const Vector &samplers ); // This will delete all of the procedural shaders that we have. Used to regenerate shaders when // the ShaderFeatures have changed (due to lighting system change, or new plugin) @@ -176,6 +181,8 @@ protected: Vector< ShaderComponent *> mComponents; + Vector< CustomShaderFeatureData* > mCustomFeaturesData; + AutoPtr mPrinter; AutoPtr mComponentFactory; diff --git a/Engine/source/terrain/terrCellMaterial.cpp b/Engine/source/terrain/terrCellMaterial.cpp index cd1f123d0..2adb9f3dd 100644 --- a/Engine/source/terrain/terrCellMaterial.cpp +++ b/Engine/source/terrain/terrCellMaterial.cpp @@ -460,6 +460,8 @@ bool TerrainCellMaterial::_createPass( Vector *materials, featureData.features = features; featureData.materialFeatures = features; + Vector customFeatures; + // Check to see how many vertex shader output // registers we're gonna need. U32 numTex = 0; @@ -502,7 +504,7 @@ bool TerrainCellMaterial::_createPass( Vector *materials, const bool logErrors = matCount == 1; GFXShader::setLogging( logErrors, true ); - pass->shader = SHADERGEN->getShader( featureData, getGFXVertexFormat(), NULL, mSamplerNames ); + pass->shader = SHADERGEN->getShader( featureData, customFeatures, getGFXVertexFormat(), NULL, mSamplerNames ); } // If we got a shader then we can continue. diff --git a/Templates/Full/game/art/shapes/cube/materials.cs b/Templates/Full/game/art/shapes/cube/materials.cs index bf5e3c940..108e41ad7 100644 --- a/Templates/Full/game/art/shapes/cube/materials.cs +++ b/Templates/Full/game/art/shapes/cube/materials.cs @@ -21,7 +21,7 @@ //----------------------------------------------------------------------------- //--- cube.dae MATERIALS BEGIN --- -singleton Material(cube_GridMaterial) +/*singleton Material(cube_GridMaterial) { mapTo = "GridMaterial"; @@ -38,7 +38,34 @@ singleton Material(cube_GridMaterial) doubleSided = false; translucent = false; translucentBlendOp = "None"; +};*/ + +singleton CustomShaderFeatureData(FlatColorFeature) +{ + +}; + +singleton Material(cube_GridMaterial) +{ + mapTo = "GridMaterial"; + + CustomShaderFeature[0] = FlatColorFeature; }; //--- cube.dae MATERIALS END --- +//Voodoo! +function FlatColorFeature::processVert(%this) +{ + +} + +function FlatColorFeature::processPixelHLSL(%this) +{ + %this.writeLine(" float bobsyeruncle = 15.915;"); +} + +function FlatColorFeature::setTextureResources(%this) +{ + +} From 1dca9958a11e4cd85c3944cdb5e565facff24ccf Mon Sep 17 00:00:00 2001 From: Areloch Date: Fri, 6 Oct 2017 16:49:50 -0500 Subject: [PATCH 2/9] Updates --- .../shaderGen/HLSL/customFeatureHLSL.cpp | 63 ++++++++++++++++++- .../source/shaderGen/HLSL/customFeatureHLSL.h | 1 + .../source/shaderGen/customShaderFeature.cpp | 14 +++-- Engine/source/shaderGen/customShaderFeature.h | 1 + 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp index 74c687db8..98888f3e6 100644 --- a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp @@ -206,8 +206,69 @@ void CustomFeatureHLSL::setTexData(Material::StageData &stageDat, Con::executef(mOwner, "setTextureData"); } +void CustomFeatureHLSL::addVariable(String name, String type, String defaultValue) +{ + //do the var/arg fetching here + Var *newVar = (Var*)LangElement::find(name.c_str()); + if (!newVar) + { + newVar = new Var(name, type); + LangElement *newVarDecl = new DecOp(newVar); + + if (!defaultValue.isEmpty()) + { + char declareStatement[128]; + dSprintf(declareStatement, 128, " @ = %s;\n", defaultValue.c_str()); + + meta->addStatement(new GenOp(declareStatement, newVarDecl)); + } + else + { + meta->addStatement(new GenOp(" @;\n", newVarDecl)); + } + } +} + void CustomFeatureHLSL::writeLine(String format, S32 argc, ConsoleValueRef *argv) { //do the var/arg fetching here - meta->addStatement(new GenOp(format + "\n"/*, colorAccuDecl, accuMapTex, accuMap, inTex, accuScale*/)); + Vector varList; + + for (U32 i = 0; i < argc; i++) + { + String varName = argv[i].getStringValue(); + Var *newVar = (Var*)LangElement::find(varName.c_str()); + if (!newVar) + { + //couldn't find that variable, bail out + Con::errorf("CustomShaderFeature::writeLine: unable to find variable %s, meaning it was not declared before being used!", argv[i].getStringValue()); + return; + } + + varList.push_back(newVar); + } + + //not happy about it, but do a trampoline here to pass along the args + + switch (varList.size()) + { + case 0: + meta->addStatement(new GenOp(format + "\n")); + break; + case 1: + meta->addStatement(new GenOp(format + "\n", varList[0])); + break; + case 2: + meta->addStatement(new GenOp(format + "\n", varList[0], varList[1])); + break; + case 3: + meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2])); + break; + case 4: + meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2], varList[3])); + break; + case 5: + meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2], varList[3], varList[4])); + break; + } } \ No newline at end of file diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.h b/Engine/source/shaderGen/HLSL/customFeatureHLSL.h index 23d2b2ca4..834f4f72a 100644 --- a/Engine/source/shaderGen/HLSL/customFeatureHLSL.h +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.h @@ -64,5 +64,6 @@ public: return mOwner->getName(); } + void addVariable(String name, String type, String defaultValue); void writeLine(String format, S32 argc, ConsoleValueRef *argv); }; \ No newline at end of file diff --git a/Engine/source/shaderGen/customShaderFeature.cpp b/Engine/source/shaderGen/customShaderFeature.cpp index 2809cfa57..3f6f66c10 100644 --- a/Engine/source/shaderGen/customShaderFeature.cpp +++ b/Engine/source/shaderGen/customShaderFeature.cpp @@ -84,6 +84,11 @@ void CustomShaderFeatureData::onRemove() } //Shadergen setup functions +void CustomShaderFeatureData::addVariable(String name, String type, String defaultValue) +{ + mFeatureHLSL->addVariable(name, type, defaultValue); +} + void CustomShaderFeatureData::writeLine(String format, S32 argc, ConsoleValueRef *argv) { /*mOnObject = onObject; @@ -126,16 +131,15 @@ void CustomShaderFeatureData::setTexData(Material::StageData &stageDat, mFeatureHLSL.setTexData(stageDat, fd, passData, texIndex); }*/ -/*DefineEngineMethod(CustomShaderFeatureData, newVar, void, (String name, String type), ("", ""), "") +DefineEngineMethod(CustomShaderFeatureData, addVariable, void, (String name, String type, String defaultValue), ("", "", ""), "") { - object->newVar(name, type); -}*/ + object->addVariable(name, type, defaultValue); +} ConsoleMethod(CustomShaderFeatureData, writeLine, void, 3, 0, "( string format, string args... ) Dynamically call a method on an object.\n" "@param method Name of method to call.\n" "@param args Zero or more arguments for the method.\n" "@return The result of the method call.") { - argv[1] = argv[2]; - object->writeLine(argv[1], argc - 1, argv + 1); + object->writeLine(argv[2], argc - 3, argv + 3); } \ No newline at end of file diff --git a/Engine/source/shaderGen/customShaderFeature.h b/Engine/source/shaderGen/customShaderFeature.h index f40f55152..312728df1 100644 --- a/Engine/source/shaderGen/customShaderFeature.h +++ b/Engine/source/shaderGen/customShaderFeature.h @@ -58,6 +58,7 @@ public: void onRemove(); //shadergen setup + void addVariable(String name, String type, String defaultValue); void writeLine(String format, S32 argc, ConsoleValueRef *argv); //shader generation From a28914e695f5d576d549907960d45869b4d07c44 Mon Sep 17 00:00:00 2001 From: Areloch Date: Sat, 7 Oct 2017 14:36:58 -0500 Subject: [PATCH 3/9] begun adding uniform hooks --- .../source/materials/materialDefinition.cpp | 25 +++++++++++ Engine/source/materials/materialDefinition.h | 1 + Engine/source/ts/tsRenderState.h | 41 +++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/Engine/source/materials/materialDefinition.cpp b/Engine/source/materials/materialDefinition.cpp index deba2ff0d..dbb668603 100644 --- a/Engine/source/materials/materialDefinition.cpp +++ b/Engine/source/materials/materialDefinition.cpp @@ -485,6 +485,8 @@ void Material::initPersistFields() addProtectedField("customShaderFeature", TypeRealString, NULL, &protectedSetCustomShaderFeature, &defaultProtectedGetFn, "Do not modify, for internal use.", AbstractClassRep::FIELD_HideInInspectors); + addProtectedField("CustomShaderFeatureUniforms", TypeRealString, NULL, &protectedSetCustomShaderFeatureUniforms, &defaultProtectedGetFn, + "Do not modify, for internal use.", AbstractClassRep::FIELD_HideInInspectors); Parent::initPersistFields(); } @@ -516,6 +518,29 @@ bool Material::protectedSetCustomShaderFeature(void *object, const char *index, return false; } +bool Material::protectedSetCustomShaderFeatureUniforms(void *object, const char *index, const char *data) +{ + Material *material = static_cast< Material* >(object); + + //CustomShaderFeatureData* customFeature; + //if (!Sim::findObject(data, customFeature)) + // return false; + + //material->mCustomShaderFeatures.push_back(customFeature); + if (index != NULL) + { + char featureName[256] = { 0 }; + U32 id = 0; + dSscanf(index, "%s_%i", featureName, id); + + String uniformName = data; + + bool tmp = true; + } + + return false; +} + bool Material::onAdd() { if (Parent::onAdd() == false) diff --git a/Engine/source/materials/materialDefinition.h b/Engine/source/materials/materialDefinition.h index b6bdba054..3b993cb0a 100644 --- a/Engine/source/materials/materialDefinition.h +++ b/Engine/source/materials/materialDefinition.h @@ -386,6 +386,7 @@ public: virtual bool writeField( StringTableEntry fieldname, const char *value ); static bool protectedSetCustomShaderFeature(void *object, const char *index, const char *data); + static bool protectedSetCustomShaderFeatureUniforms(void *object, const char *index, const char *data); // // ConsoleObject interface diff --git a/Engine/source/ts/tsRenderState.h b/Engine/source/ts/tsRenderState.h index fcb765185..224d081ba 100644 --- a/Engine/source/ts/tsRenderState.h +++ b/Engine/source/ts/tsRenderState.h @@ -37,6 +37,41 @@ class Frustum; class LightQuery; class TSShape; +struct CustomShaderBindingData +{ + enum UniformType + { + Float = 0, + Float2, + Float3, + Float4, + Texture2D, + Texture3D, + Cubemap, + Matrix2x2, + Matrix2x3, + Matrix2x4, + Matrix3x2, + Matrix3x3, + Matrix3x4, + Matrix4x2, + Matrix4x3, + Matrix4x4 + }; + + String targetedUniformName; + + //ShaderConstHandles shaderConstHandle; + + UniformType type; + + void* data; //for numeric data + + //Image stuff + GFXTexHandle texture; + GFXSamplerStateDesc samplerState; +}; + /// A simple class for passing render state through the pre-render pipeline. /// /// @section TSRenderState_intro Introduction @@ -115,10 +150,10 @@ protected: /// Count of matrices in the mNodeTransforms list U32 mNodeTransformCount; + //Custom Shader data + Vector mCustomShaderData; + public: - - - TSRenderState(); TSRenderState( const TSRenderState &state ); From 6f8e4cb3ee9519f31b0e2655d752a46251debab8 Mon Sep 17 00:00:00 2001 From: Areloch Date: Mon, 9 Oct 2017 17:15:57 -0500 Subject: [PATCH 4/9] Added support for uniforms, textures and samplers. --- .../shaderGen/HLSL/customFeatureHLSL.cpp | 128 +++++++++++++++++- .../source/shaderGen/HLSL/customFeatureHLSL.h | 39 ++++++ .../source/shaderGen/customShaderFeature.cpp | 30 ++++ Engine/source/shaderGen/customShaderFeature.h | 4 + Engine/source/shaderGen/langElement.cpp | 1 + Engine/source/shaderGen/shaderGen.cpp | 25 ++++ .../Full/game/art/shapes/cube/materials.cs | 17 ++- 7 files changed, 235 insertions(+), 9 deletions(-) diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp index 98888f3e6..effe43468 100644 --- a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp @@ -206,25 +206,104 @@ void CustomFeatureHLSL::setTexData(Material::StageData &stageDat, Con::executef(mOwner, "setTextureData"); } +void CustomFeatureHLSL::addUniform(String name, String type, String defaultValue, U32 arraySize) +{ + //do the var/arg fetching here + Var *newVar = (Var*)LangElement::find(name.c_str()); + if (!newVar) + { + VarHolder newVarHolder(name, type, ""); + newVarHolder.arraySize = arraySize; + newVarHolder.sampler = false; + newVarHolder.uniform = true; + newVarHolder.constSortPos = cspPrimitive; + + mVars.push_back(newVarHolder); + } +} + +void CustomFeatureHLSL::addSampler(String name, String type, U32 arraySize) +{ + //do the var/arg fetching here + Var *newVar = (Var*)LangElement::find(name.c_str()); + if (!newVar) + { + //As far as I know, it's always SamplerState regardless of the texture's type + VarHolder newVarHolder(name, "SamplerState", ""); + newVarHolder.arraySize = arraySize; + newVarHolder.sampler = true; + newVarHolder.uniform = true; + newVarHolder.constNum = Var::getTexUnitNum(); // used as texture unit num here + + mVars.push_back(newVarHolder); + } +} + +void CustomFeatureHLSL::addTexture(String name, String type, String samplerState, U32 arraySize) +{ + //do the var/arg fetching here + Var *newVar = (Var*)LangElement::find(name.c_str()); + if (!newVar) + { + //go find our sampler state var + U32 constNum = 0; + + Var *samplerStateVar = (Var*)LangElement::find(samplerState.c_str()); + if (!samplerStateVar) + { + //check our holder vars + bool foundHolder = false; + for (U32 v = 0; v < mVars.size(); v++) + { + if (mVars[v].varName == samplerState) + { + constNum = mVars[v].constNum; + foundHolder = true; + break; + } + } + + if (!foundHolder) + { + Con::errorf("CustomShaderFeature::addTexture: Unable to find texture's sampler state!"); + return; + } + } + else + { + constNum = samplerStateVar->constNum; + } + + VarHolder newVarHolder(name, type, ""); + newVarHolder.arraySize = arraySize; + newVarHolder.texture = true; + newVarHolder.uniform = true; + newVarHolder.constNum = constNum; // used as texture unit num here + + mVars.push_back(newVarHolder); + } +} + void CustomFeatureHLSL::addVariable(String name, String type, String defaultValue) { //do the var/arg fetching here Var *newVar = (Var*)LangElement::find(name.c_str()); if (!newVar) { - newVar = new Var(name, type); - LangElement *newVarDecl = new DecOp(newVar); - if (!defaultValue.isEmpty()) { char declareStatement[128]; dSprintf(declareStatement, 128, " @ = %s;\n", defaultValue.c_str()); + newVar = new Var(name, type); + LangElement *newVarDecl = new DecOp(newVar); meta->addStatement(new GenOp(declareStatement, newVarDecl)); } else { - meta->addStatement(new GenOp(" @;\n", newVarDecl)); + VarHolder newVarHolder(name, type, defaultValue); + + mVars.push_back(newVarHolder); } } } @@ -233,6 +312,7 @@ void CustomFeatureHLSL::writeLine(String format, S32 argc, ConsoleValueRef *argv { //do the var/arg fetching here Vector varList; + bool declarationStatement = false; for (U32 i = 0; i < argc; i++) { @@ -240,9 +320,43 @@ void CustomFeatureHLSL::writeLine(String format, S32 argc, ConsoleValueRef *argv Var *newVar = (Var*)LangElement::find(varName.c_str()); if (!newVar) { - //couldn't find that variable, bail out - Con::errorf("CustomShaderFeature::writeLine: unable to find variable %s, meaning it was not declared before being used!", argv[i].getStringValue()); - return; + //ok, check our existing var holders, see if we just haven't utilized it yet + for (U32 v = 0; v < mVars.size(); v++) + { + if (mVars[v].varName == varName) + { + Var* newDeclVar = new Var(mVars[v].varName, mVars[v].type); + + newDeclVar->arraySize = mVars[v].arraySize; + newDeclVar->uniform = mVars[v].uniform; + newDeclVar->sampler = mVars[v].sampler; + newDeclVar->texture = mVars[v].texture; + newDeclVar->constNum = mVars[v].constNum; + newDeclVar->constSortPos = mVars[v].constSortPos; + + if (!newDeclVar->uniform) + { + LangElement *newVarDecl = new DecOp(newDeclVar); + newVar = (Var*)newVarDecl; + + declarationStatement = true; + } + else + { + newVar = newDeclVar; + } + + mVars.erase(v); + break; + } + } + + if (!newVar) + { + //couldn't find that variable, bail out + Con::errorf("CustomShaderFeature::writeLine: unable to find variable %s, meaning it was not declared before being used!", argv[i].getStringValue()); + return; + } } varList.push_back(newVar); diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.h b/Engine/source/shaderGen/HLSL/customFeatureHLSL.h index 834f4f72a..d270242a1 100644 --- a/Engine/source/shaderGen/HLSL/customFeatureHLSL.h +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.h @@ -27,6 +27,42 @@ class CustomFeatureHLSL : public ShaderFeatureHLSL { friend class CustomShaderFeatureData; + struct VarHolder + { + String varName; + String defaultValue; + String type; + bool uniform; + bool sampler; + bool texture; + U32 constNum; + ConstantSortPosition constSortPos; + U32 arraySize; + + VarHolder() : + varName(""), + type(""), + defaultValue(""), + uniform(false), + sampler(false), + texture(false), + constNum(0), + arraySize(0), + constSortPos(cspUninit) + { + } + + VarHolder(String _varName,String _type, String _defaultValue) : + uniform(false), sampler(false), texture(false), constNum(0), arraySize(0), constSortPos(cspUninit) + { + varName = _varName; + type = _type; + defaultValue = _defaultValue; + } + }; + + Vector mVars; + public: CustomShaderFeatureData* mOwner; @@ -64,6 +100,9 @@ public: return mOwner->getName(); } + void addUniform(String name, String type, String defaultValue, U32 arraySize = 0); void addVariable(String name, String type, String defaultValue); + void addSampler(String name, String type, U32 arraySize = 0); + void addTexture(String name, String type, String samplerState, U32 arraySize); void writeLine(String format, S32 argc, ConsoleValueRef *argv); }; \ No newline at end of file diff --git a/Engine/source/shaderGen/customShaderFeature.cpp b/Engine/source/shaderGen/customShaderFeature.cpp index 3f6f66c10..91f80b2db 100644 --- a/Engine/source/shaderGen/customShaderFeature.cpp +++ b/Engine/source/shaderGen/customShaderFeature.cpp @@ -89,6 +89,21 @@ void CustomShaderFeatureData::addVariable(String name, String type, String defau mFeatureHLSL->addVariable(name, type, defaultValue); } +void CustomShaderFeatureData::addUniform(String name, String type, String defaultValue, U32 arraySize) +{ + mFeatureHLSL->addUniform(name, type, defaultValue, arraySize); +} + +void CustomShaderFeatureData::addSampler(String name, String type, U32 arraySize) +{ + mFeatureHLSL->addSampler(name, type, arraySize); +} + +void CustomShaderFeatureData::addTexture(String name, String type, String samplerState, U32 arraySize) +{ + mFeatureHLSL->addTexture(name, type, samplerState, arraySize); +} + void CustomShaderFeatureData::writeLine(String format, S32 argc, ConsoleValueRef *argv) { /*mOnObject = onObject; @@ -136,6 +151,21 @@ DefineEngineMethod(CustomShaderFeatureData, addVariable, void, (String name, Str object->addVariable(name, type, defaultValue); } +DefineEngineMethod(CustomShaderFeatureData, addUniform, void, (String name, String type, String defaultValue, U32 arraySize), ("", "", "", 0), "") +{ + object->addUniform(name, type, defaultValue, arraySize); +} + +DefineEngineMethod(CustomShaderFeatureData, addSampler, void, (String name, U32 arraySize), ("", 0), "") +{ + object->addSampler(name, "", arraySize); +} + +DefineEngineMethod(CustomShaderFeatureData, addTexture, void, (String name, String type, String samplerState, U32 arraySize), ("", "", 0), "") +{ + object->addTexture(name, type, samplerState, arraySize); +} + ConsoleMethod(CustomShaderFeatureData, writeLine, void, 3, 0, "( string format, string args... ) Dynamically call a method on an object.\n" "@param method Name of method to call.\n" "@param args Zero or more arguments for the method.\n" diff --git a/Engine/source/shaderGen/customShaderFeature.h b/Engine/source/shaderGen/customShaderFeature.h index 312728df1..32b7d284d 100644 --- a/Engine/source/shaderGen/customShaderFeature.h +++ b/Engine/source/shaderGen/customShaderFeature.h @@ -59,6 +59,10 @@ public: //shadergen setup void addVariable(String name, String type, String defaultValue); + void addUniform(String name, String type, String defaultValue, U32 arraySize); + void addSampler(String name, String type, U32 arraySize); + void addTexture(String name, String type, String samplerState, U32 arraySize); + void writeLine(String format, S32 argc, ConsoleValueRef *argv); //shader generation diff --git a/Engine/source/shaderGen/langElement.cpp b/Engine/source/shaderGen/langElement.cpp index 59e9a3bde..57648e02c 100644 --- a/Engine/source/shaderGen/langElement.cpp +++ b/Engine/source/shaderGen/langElement.cpp @@ -112,6 +112,7 @@ Var::Var( const char *inName, const char *inType ) sampler = false; texCoordNum = 0; constSortPos = cspUninit; + constNum = 0; arraySize = 1; texture = false; rank = 0; diff --git a/Engine/source/shaderGen/shaderGen.cpp b/Engine/source/shaderGen/shaderGen.cpp index 5ed514aae..fc2e9d2b0 100644 --- a/Engine/source/shaderGen/shaderGen.cpp +++ b/Engine/source/shaderGen/shaderGen.cpp @@ -285,6 +285,31 @@ void ShaderGen::_processVertFeatures( Vector ¯os, bool macro } } + //Handle if we have any custom features + if (!mCustomFeaturesData.empty()) + { + for (U32 i = 0; i < mCustomFeaturesData.size(); ++i) + { + mCustomFeaturesData[i]->mFeatureHLSL->processVert(mComponents, mFeatureData); + + String line = String::ToString(" // %s\r\n", mCustomFeaturesData[i]->mFeatureHLSL->getName().c_str()); + mOutput->addStatement(new GenOp(line)); + + if (mCustomFeaturesData[i]->mFeatureHLSL->getOutput()) + mOutput->addStatement(mCustomFeaturesData[i]->mFeatureHLSL->getOutput()); + //ShaderFeatureHLSL feature = mCustomFeaturesData[i]->mHLSLFeature; + //feature->setProcessIndex(index); + + /*feature->processPixMacros(macros, mFeatureData); + + feature->setInstancingFormat(&mInstancingFormat); + feature->processPix(mComponents, mFeatureData);*/ + + mCustomFeaturesData[i]->mFeatureHLSL->reset(); + mOutput->addStatement(new GenOp(" \r\n")); + } + } + ShaderConnector *connect = dynamic_cast( mComponents[C_CONNECTOR] ); connect->sortVars(); } diff --git a/Templates/Full/game/art/shapes/cube/materials.cs b/Templates/Full/game/art/shapes/cube/materials.cs index 108e41ad7..345a7c4b8 100644 --- a/Templates/Full/game/art/shapes/cube/materials.cs +++ b/Templates/Full/game/art/shapes/cube/materials.cs @@ -50,19 +50,32 @@ singleton Material(cube_GridMaterial) mapTo = "GridMaterial"; CustomShaderFeature[0] = FlatColorFeature; + CustomShaderFeatureUniforms[FlatColorFeature,0] = "TestFloat"; }; //--- cube.dae MATERIALS END --- //Voodoo! -function FlatColorFeature::processVert(%this) +function FlatColorFeature::processVertHLSL(%this) { } function FlatColorFeature::processPixelHLSL(%this) { - %this.writeLine(" float bobsyeruncle = 15.915;"); + %this.addUniform("strudel", "float2"); + %this.addSampler("strudelMap"); + %this.addTexture("strudelTex", "Texture2D", "strudelMap"); + + %this.addVariable("bobsyeruncle", "float", 15.915); + %this.addVariable("chimmychanga", "float"); + + %this.writeLine(" @ = @ * 2;", "chimmychanga", "bobsyeruncle"); + %this.writeLine(" @ *= @.x;", "bobsyeruncle", "strudel"); + %this.writeLine(" @ *= @.y;", "chimmychanga", "strudel"); + + %this.addVariable("sprangle", "float4"); + %this.writeLine(" @ = @.Sample(@,@);", "sprangle", "strudelTex", "strudelMap", "strudel"); } function FlatColorFeature::setTextureResources(%this) From ccdc93fc2518e0b68e0468240f406b00722389fa Mon Sep 17 00:00:00 2001 From: Areloch Date: Tue, 10 Oct 2017 15:54:54 -0500 Subject: [PATCH 5/9] Implement of the shader consts binding. --- Engine/source/T3D/tsStatic.cpp | 9 ++- Engine/source/T3D/tsStatic.h | 2 + .../materials/processedShaderMaterial.cpp | 34 +++++++++++ .../materials/processedShaderMaterial.h | 16 +++++ Engine/source/materials/sceneData.h | 4 +- .../renderInstance/renderBinManager.cpp | 1 + .../source/renderInstance/renderPassManager.h | 5 ++ Engine/source/scene/sceneObject.h | 4 +- .../shaderGen/HLSL/customFeatureHLSL.cpp | 6 ++ Engine/source/shaderGen/customShaderFeature.h | 2 + Engine/source/ts/tsMesh.cpp | 2 + Engine/source/ts/tsRenderState.h | 59 +++++++++++++++++-- 12 files changed, 137 insertions(+), 7 deletions(-) diff --git a/Engine/source/T3D/tsStatic.cpp b/Engine/source/T3D/tsStatic.cpp index 84b3b0797..9b0f934b1 100644 --- a/Engine/source/T3D/tsStatic.cpp +++ b/Engine/source/T3D/tsStatic.cpp @@ -305,7 +305,9 @@ bool TSStatic::onAdd() mCubeReflector.unregisterReflector(); if ( reflectorDesc ) - mCubeReflector.registerReflector( this, reflectorDesc ); + mCubeReflector.registerReflector( this, reflectorDesc ); + + strudelCSB = new CustomShaderBindingData(); } _updateShouldTick(); @@ -627,6 +629,11 @@ void TSStatic::prepRenderImage( SceneRenderState* state ) // Acculumation rdata.setAccuTex(mAccuTex); + //Various arbitrary shader render bits to add + strudelCSB->setFloat("strudel", 0.25); + + rdata.addCustomShaderBinding(strudelCSB); + // If we have submesh culling enabled then prepare // the object space frustum to pass to the shape. Frustum culler; diff --git a/Engine/source/T3D/tsStatic.h b/Engine/source/T3D/tsStatic.h index db1ff138c..04938650f 100644 --- a/Engine/source/T3D/tsStatic.h +++ b/Engine/source/T3D/tsStatic.h @@ -236,6 +236,8 @@ public: const Vector& getLOSDetails() const { return mLOSDetails; } + CustomShaderBindingData* strudelCSB; + }; typedef TSStatic::MeshType TSMeshType; diff --git a/Engine/source/materials/processedShaderMaterial.cpp b/Engine/source/materials/processedShaderMaterial.cpp index 2785dc429..2fff8e99b 100644 --- a/Engine/source/materials/processedShaderMaterial.cpp +++ b/Engine/source/materials/processedShaderMaterial.cpp @@ -41,6 +41,8 @@ #include "gfx/util/screenspace.h" #include "math/util/matrixSet.h" +#include "ts/tsRenderState.h" + // We need to include customMaterialDefinition for ShaderConstHandles::init #include "materials/customMaterialDefinition.h" @@ -118,6 +120,21 @@ void ShaderConstHandles::init( GFXShader *shader, CustomMaterial* mat /*=NULL*/ mMatInfoFlagsSC = shader->getShaderConstHandle(ShaderGenVars::matInfoFlags); } +void CustomFeatureShaderConstHandles::init(GFXShader *shader, Vector customFeatureData) +{ + for (U32 f = 0; f < customFeatureData.size(); ++f) + { + for (U32 i = 0; i < customFeatureData[f]->mAddedShaderConstants.size(); ++i) + { + handleData newSC; + newSC.handle = shader->getShaderConstHandle(customFeatureData[f]->mAddedShaderConstants[i]); + newSC.handleName = customFeatureData[f]->mAddedShaderConstants[i]; + + mHandles.push_back(newSC); + } + } +} + /// /// ShaderRenderPassData /// @@ -659,6 +676,9 @@ bool ProcessedShaderMaterial::_addPass( ShaderRenderPassData &rpd, return false; rpd.shaderHandles.init( rpd.shader ); + //Store our customShaderFeature handles + rpd.customFeatureShaderHandles.init(rpd.shader, mMaterial->mCustomShaderFeatures); + // If a pass glows, we glow if( rpd.mGlow ) mHasGlow = true; @@ -1297,6 +1317,20 @@ void ProcessedShaderMaterial::setSceneInfo(SceneRenderState * state, const Scene for ( U32 i=0; i < rpd->featureShaderHandles.size(); i++ ) rpd->featureShaderHandles[i]->setConsts( state, sgData, shaderConsts ); + for (U32 i = 0; i < sgData.customShaderData.size(); i++) + { + //roll through and try setting our data! + for (U32 h = 0; h < rpd->customFeatureShaderHandles.mHandles.size(); ++h) + { + if (rpd->customFeatureShaderHandles.mHandles[h].handleName == sgData.customShaderData[i]->getHandleName()) + { + if(sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float) + shaderConsts->setSafe(rpd->customFeatureShaderHandles.mHandles[h].handle, sgData.customShaderData[i]->getFloat()); + break; + } + } + } + LIGHTMGR->setLightInfo( this, mMaterial, sgData, state, pass, shaderConsts ); } diff --git a/Engine/source/materials/processedShaderMaterial.h b/Engine/source/materials/processedShaderMaterial.h index ee0781b95..f67204cc9 100644 --- a/Engine/source/materials/processedShaderMaterial.h +++ b/Engine/source/materials/processedShaderMaterial.h @@ -97,6 +97,21 @@ public: GFXShaderConstHandle* mNodeTransforms; void init( GFXShader* shader, CustomMaterial* mat = NULL ); + +}; + +class CustomFeatureShaderConstHandles +{ +public: + struct handleData + { + StringTableEntry handleName; + GFXShaderConstHandle* handle; + }; + + Vector mHandles; + + void init(GFXShader *shader, Vector customFeatureData); }; class ShaderRenderPassData : public RenderPassData @@ -109,6 +124,7 @@ public: GFXShaderRef shader; ShaderConstHandles shaderHandles; + CustomFeatureShaderConstHandles customFeatureShaderHandles; Vector featureShaderHandles; virtual void reset(); diff --git a/Engine/source/materials/sceneData.h b/Engine/source/materials/sceneData.h index 99fc8ef52..eb7bc689b 100644 --- a/Engine/source/materials/sceneData.h +++ b/Engine/source/materials/sceneData.h @@ -34,7 +34,7 @@ class GFXTexHandle; class GFXCubemap; - +class CustomShaderBindingData; struct SceneData { @@ -92,6 +92,8 @@ struct SceneData /// features. void *materialHint; + Vector customShaderData; + /// Constructor. SceneData() { diff --git a/Engine/source/renderInstance/renderBinManager.cpp b/Engine/source/renderInstance/renderBinManager.cpp index 16f559d5a..6a808c722 100644 --- a/Engine/source/renderInstance/renderBinManager.cpp +++ b/Engine/source/renderInstance/renderBinManager.cpp @@ -172,6 +172,7 @@ void RenderBinManager::setupSGData( MeshRenderInst *ri, SceneData &data ) data.lightmap = ri->lightmap; data.visibility = ri->visibility; data.materialHint = ri->materialHint; + data.customShaderData = ri->mCustomShaderData; } DefineEngineMethod( RenderBinManager, getBinType, const char*, (),, diff --git a/Engine/source/renderInstance/renderPassManager.h b/Engine/source/renderInstance/renderPassManager.h index 98080365b..218dec995 100644 --- a/Engine/source/renderInstance/renderPassManager.h +++ b/Engine/source/renderInstance/renderPassManager.h @@ -49,6 +49,8 @@ struct RenderInst; class MatrixSet; class GFXPrimitiveBufferHandle; +class CustomShaderBindingData; + /// A RenderInstType hash value. typedef U32 RenderInstTypeHash; @@ -382,6 +384,9 @@ struct MeshRenderInst : public RenderInst const char *objectName; #endif + //Custom Shader data + Vector mCustomShaderData; + void clear(); }; diff --git a/Engine/source/scene/sceneObject.h b/Engine/source/scene/sceneObject.h index 3985d372c..4a448842e 100644 --- a/Engine/source/scene/sceneObject.h +++ b/Engine/source/scene/sceneObject.h @@ -54,7 +54,9 @@ #ifndef _GFXDEVICE_H_ #include "gfx/gfxDevice.h" #endif - +#ifndef _TSRENDERDATA_H_ +#include "ts/tsRenderState.h" +#endif class SceneManager; class SceneRenderState; diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp index effe43468..49014f292 100644 --- a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp @@ -219,6 +219,8 @@ void CustomFeatureHLSL::addUniform(String name, String type, String defaultValue newVarHolder.constSortPos = cspPrimitive; mVars.push_back(newVarHolder); + + mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str())); } } @@ -236,6 +238,8 @@ void CustomFeatureHLSL::addSampler(String name, String type, U32 arraySize) newVarHolder.constNum = Var::getTexUnitNum(); // used as texture unit num here mVars.push_back(newVarHolder); + + mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str())); } } @@ -281,6 +285,8 @@ void CustomFeatureHLSL::addTexture(String name, String type, String samplerState newVarHolder.constNum = constNum; // used as texture unit num here mVars.push_back(newVarHolder); + + mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str())); } } diff --git a/Engine/source/shaderGen/customShaderFeature.h b/Engine/source/shaderGen/customShaderFeature.h index 32b7d284d..4007b1945 100644 --- a/Engine/source/shaderGen/customShaderFeature.h +++ b/Engine/source/shaderGen/customShaderFeature.h @@ -36,6 +36,8 @@ class CustomShaderFeatureData : public SimObject public: CustomFeatureHLSL* mFeatureHLSL; + Vector mAddedShaderConstants; + public: CustomShaderFeatureData(); virtual ~CustomShaderFeatureData(); diff --git a/Engine/source/ts/tsMesh.cpp b/Engine/source/ts/tsMesh.cpp index b52a50b96..80390e775 100644 --- a/Engine/source/ts/tsMesh.cpp +++ b/Engine/source/ts/tsMesh.cpp @@ -207,6 +207,8 @@ void TSMesh::innerRender( TSMaterialList *materials, const TSRenderState &rdata, coreRI->materialHint = rdata.getMaterialHint(); + coreRI->mCustomShaderData = rdata.getCustomShaderBinding(); + coreRI->visibility = meshVisibility; coreRI->cubemap = rdata.getCubemap(); diff --git a/Engine/source/ts/tsRenderState.h b/Engine/source/ts/tsRenderState.h index 224d081ba..db81a205c 100644 --- a/Engine/source/ts/tsRenderState.h +++ b/Engine/source/ts/tsRenderState.h @@ -39,6 +39,7 @@ class TSShape; struct CustomShaderBindingData { +public: enum UniformType { Float = 0, @@ -58,18 +59,59 @@ struct CustomShaderBindingData Matrix4x3, Matrix4x4 }; - - String targetedUniformName; +private: + StringTableEntry targetedUniformName; //ShaderConstHandles shaderConstHandle; UniformType type; - void* data; //for numeric data + F32 mFloat; + Point2F mFloat2; + Point3F mFloat3; + Point4F mFloat4; //Image stuff GFXTexHandle texture; GFXSamplerStateDesc samplerState; + +public: + void setFloat(StringTableEntry shaderConstName, F32 f) + { + targetedUniformName = shaderConstName; + mFloat = f; + type = Float; + } + F32 getFloat() { return mFloat; } + + void setFloat2(StringTableEntry shaderConstName, Point2F f) + { + targetedUniformName = shaderConstName; + mFloat2 = f; + type = Float2; + } + + void setFloat3(StringTableEntry shaderConstName, Point3F f) + { + targetedUniformName = shaderConstName; + mFloat3 = f; + type = Float3; + } + + void setFloat4(StringTableEntry shaderConstName, Point4F f) + { + targetedUniformName = shaderConstName; + mFloat4 = f; + type = Float4; + } + + StringTableEntry getHandleName() { + return targetedUniformName; + } + + UniformType getType() { + return type; + } }; /// A simple class for passing render state through the pre-render pipeline. @@ -151,7 +193,7 @@ protected: U32 mNodeTransformCount; //Custom Shader data - Vector mCustomShaderData; + Vector mCustomShaderData; public: TSRenderState(); @@ -200,6 +242,15 @@ public: void setAccuTex( GFXTextureObject* query ) { mAccuTex = query; } GFXTextureObject* getAccuTex() const { return mAccuTex; } + void addCustomShaderBinding(CustomShaderBindingData* data) + { + mCustomShaderData.push_back(data); + } + Vector getCustomShaderBinding() const + { + return mCustomShaderData; + } + ///@ see mNodeTransforms, mNodeTransformCount void setNodeTransforms(MatrixF *list, U32 count) { mNodeTransforms = list; mNodeTransformCount = count; } void getNodeTransforms(MatrixF **list, U32 *count) const { *list = mNodeTransforms; *count = mNodeTransformCount; } From d674ac5817de810f12e310b70cfc28e4df87af35 Mon Sep 17 00:00:00 2001 From: Areloch Date: Tue, 17 Oct 2017 08:48:31 -0500 Subject: [PATCH 6/9] Working on shader const binding --- Engine/source/T3D/tsStatic.cpp | 14 ++++-- Engine/source/T3D/tsStatic.h | 4 +- .../materials/processedShaderMaterial.cpp | 8 ++++ .../renderInstance/renderBinManager.cpp | 45 +++++++++++-------- .../source/renderInstance/renderPassManager.h | 2 +- .../shaderGen/HLSL/customFeatureHLSL.cpp | 22 ++++++++- .../source/shaderGen/HLSL/customFeatureHLSL.h | 4 ++ .../source/shaderGen/customShaderFeature.cpp | 10 +++++ Engine/source/shaderGen/customShaderFeature.h | 2 + Engine/source/ts/tsRenderState.cpp | 3 +- Engine/source/ts/tsRenderState.h | 15 ++++--- 11 files changed, 96 insertions(+), 33 deletions(-) diff --git a/Engine/source/T3D/tsStatic.cpp b/Engine/source/T3D/tsStatic.cpp index 9b0f934b1..ed0a97d5e 100644 --- a/Engine/source/T3D/tsStatic.cpp +++ b/Engine/source/T3D/tsStatic.cpp @@ -124,6 +124,8 @@ TSStatic::TSStatic() mCollisionType = CollisionMesh; mDecalType = CollisionMesh; + + mOverrideColor = LinearColorF::BLACK; } TSStatic::~TSStatic() @@ -187,6 +189,9 @@ void TSStatic::initPersistFields() addField( "originSort", TypeBool, Offset( mUseOriginSort, TSStatic ), "Enables translucent sorting of the TSStatic by its origin instead of the bounds." ); + addField("overrideColor", TypeColorF, Offset(mOverrideColor, TSStatic), + "@brief The skin applied to the shape.\n\n"); + endGroup("Rendering"); addGroup( "Reflection" ); @@ -306,8 +311,6 @@ bool TSStatic::onAdd() if ( reflectorDesc ) mCubeReflector.registerReflector( this, reflectorDesc ); - - strudelCSB = new CustomShaderBindingData(); } _updateShouldTick(); @@ -630,7 +633,8 @@ void TSStatic::prepRenderImage( SceneRenderState* state ) rdata.setAccuTex(mAccuTex); //Various arbitrary shader render bits to add - strudelCSB->setFloat("strudel", 0.25); + CustomShaderBindingData strudelCSB; + strudelCSB.setFloat4(StringTable->insert("overrideColor"), mOverrideColor); rdata.addCustomShaderBinding(strudelCSB); @@ -800,6 +804,8 @@ U32 TSStatic::packUpdate(NetConnection *con, U32 mask, BitStream *stream) { stream->writeRangedU32( reflectorDesc->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast ); } + + stream->write(mOverrideColor); return retMask; } @@ -887,6 +893,8 @@ void TSStatic::unpackUpdate(NetConnection *con, BitStream *stream) cubeDescId = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast ); } + stream->read(&mOverrideColor); + if ( isProperlyAdded() ) _updateShouldTick(); } diff --git a/Engine/source/T3D/tsStatic.h b/Engine/source/T3D/tsStatic.h index 04938650f..356bd5cd4 100644 --- a/Engine/source/T3D/tsStatic.h +++ b/Engine/source/T3D/tsStatic.h @@ -191,6 +191,8 @@ protected: PhysicsBody *mPhysicsRep; + LinearColorF mOverrideColor; + // Debug stuff F32 mRenderNormalScalar; S32 mForceDetail; @@ -236,8 +238,6 @@ public: const Vector& getLOSDetails() const { return mLOSDetails; } - CustomShaderBindingData* strudelCSB; - }; typedef TSStatic::MeshType TSMeshType; diff --git a/Engine/source/materials/processedShaderMaterial.cpp b/Engine/source/materials/processedShaderMaterial.cpp index 2fff8e99b..4c1a2f7e9 100644 --- a/Engine/source/materials/processedShaderMaterial.cpp +++ b/Engine/source/materials/processedShaderMaterial.cpp @@ -1322,10 +1322,18 @@ void ProcessedShaderMaterial::setSceneInfo(SceneRenderState * state, const Scene //roll through and try setting our data! for (U32 h = 0; h < rpd->customFeatureShaderHandles.mHandles.size(); ++h) { + StringTableEntry handleName = sgData.customShaderData[i]->getHandleName(); + StringTableEntry rpdHandleName = rpd->customFeatureShaderHandles.mHandles[h].handleName; if (rpd->customFeatureShaderHandles.mHandles[h].handleName == sgData.customShaderData[i]->getHandleName()) { if(sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float) shaderConsts->setSafe(rpd->customFeatureShaderHandles.mHandles[h].handle, sgData.customShaderData[i]->getFloat()); + else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float2) + shaderConsts->setSafe(rpd->customFeatureShaderHandles.mHandles[h].handle, sgData.customShaderData[i]->getFloat2()); + else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float3) + shaderConsts->setSafe(rpd->customFeatureShaderHandles.mHandles[h].handle, sgData.customShaderData[i]->getFloat3()); + else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float4) + shaderConsts->setSafe(rpd->customFeatureShaderHandles.mHandles[h].handle, sgData.customShaderData[i]->getFloat4()); break; } } diff --git a/Engine/source/renderInstance/renderBinManager.cpp b/Engine/source/renderInstance/renderBinManager.cpp index 6a808c722..19c32633b 100644 --- a/Engine/source/renderInstance/renderBinManager.cpp +++ b/Engine/source/renderInstance/renderBinManager.cpp @@ -151,28 +151,35 @@ S32 FN_CDECL RenderBinManager::cmpKeyFunc(const void* p1, const void* p2) return ( test1 == 0 ) ? S32(mse1->key2) - S32(mse2->key2) : test1; } -void RenderBinManager::setupSGData( MeshRenderInst *ri, SceneData &data ) +void RenderBinManager::setupSGData(MeshRenderInst *ri, SceneData &data) { - PROFILE_SCOPE( RenderBinManager_setupSGData ); + PROFILE_SCOPE(RenderBinManager_setupSGData); - // NOTE: We do not reset or clear the scene state - // here as the caller has initialized non-RI members - // himself and we must preserve them. - // - // It also saves a bunch of CPU as this is called for - // every MeshRenderInst in every pass. + // NOTE: We do not reset or clear the scene state + // here as the caller has initialized non-RI members + // himself and we must preserve them. + // + // It also saves a bunch of CPU as this is called for + // every MeshRenderInst in every pass. - dMemcpy( data.lights, ri->lights, sizeof( data.lights ) ); - data.objTrans = ri->objectToWorld; - data.backBuffTex = ri->backBuffTex; - data.cubemap = ri->cubemap; - data.miscTex = ri->miscTex; - data.reflectTex = ri->reflectTex; - data.accuTex = ri->accuTex; - data.lightmap = ri->lightmap; - data.visibility = ri->visibility; - data.materialHint = ri->materialHint; - data.customShaderData = ri->mCustomShaderData; + dMemcpy(data.lights, ri->lights, sizeof(data.lights)); + data.objTrans = ri->objectToWorld; + data.backBuffTex = ri->backBuffTex; + data.cubemap = ri->cubemap; + data.miscTex = ri->miscTex; + data.reflectTex = ri->reflectTex; + data.accuTex = ri->accuTex; + data.lightmap = ri->lightmap; + data.visibility = ri->visibility; + data.materialHint = ri->materialHint; + + data.customShaderData.clear(); + for (U32 i = 0; i < ri->mCustomShaderData.size(); i++) + { + data.customShaderData.push_back(&ri->mCustomShaderData[i]); + } + + bool bl = true; } DefineEngineMethod( RenderBinManager, getBinType, const char*, (),, diff --git a/Engine/source/renderInstance/renderPassManager.h b/Engine/source/renderInstance/renderPassManager.h index 218dec995..bbaba4fe2 100644 --- a/Engine/source/renderInstance/renderPassManager.h +++ b/Engine/source/renderInstance/renderPassManager.h @@ -385,7 +385,7 @@ struct MeshRenderInst : public RenderInst #endif //Custom Shader data - Vector mCustomShaderData; + Vector mCustomShaderData; void clear(); }; diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp index 49014f292..d5f15e1b4 100644 --- a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp @@ -45,14 +45,22 @@ void CustomFeatureHLSL::processVert(Vector &componentList, output = meta;*/ + meta = new MultiLine; + + mFeatureData = fd; + if (mOwner->isMethod("processVertHLSL")) Con::executef(mOwner, "processVertHLSL"); + + output = meta; } void CustomFeatureHLSL::processPix(Vector &componentList, const MaterialFeatureData &fd) { meta = new MultiLine; + + mFeatureData = fd; /*MultiLine *meta = new MultiLine; @@ -216,7 +224,7 @@ void CustomFeatureHLSL::addUniform(String name, String type, String defaultValue newVarHolder.arraySize = arraySize; newVarHolder.sampler = false; newVarHolder.uniform = true; - newVarHolder.constSortPos = cspPrimitive; + newVarHolder.constSortPos = cspPotentialPrimitive; mVars.push_back(newVarHolder); @@ -391,4 +399,16 @@ void CustomFeatureHLSL::writeLine(String format, S32 argc, ConsoleValueRef *argv meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2], varList[3], varList[4])); break; } +} + +bool CustomFeatureHLSL::hasFeature(String name) +{ + for (U32 i = 0; i < mFeatureData.materialFeatures.getCount(); i++) + { + String featureName = mFeatureData.materialFeatures.getAt(i).getName(); + if (name == featureName) + return true; + } + + return false; } \ No newline at end of file diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.h b/Engine/source/shaderGen/HLSL/customFeatureHLSL.h index d270242a1..216cc1464 100644 --- a/Engine/source/shaderGen/HLSL/customFeatureHLSL.h +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.h @@ -66,6 +66,8 @@ class CustomFeatureHLSL : public ShaderFeatureHLSL public: CustomShaderFeatureData* mOwner; + MaterialFeatureData mFeatureData; + protected: MultiLine *meta; @@ -100,6 +102,8 @@ public: return mOwner->getName(); } + bool hasFeature(String name); + void addUniform(String name, String type, String defaultValue, U32 arraySize = 0); void addVariable(String name, String type, String defaultValue); void addSampler(String name, String type, U32 arraySize = 0); diff --git a/Engine/source/shaderGen/customShaderFeature.cpp b/Engine/source/shaderGen/customShaderFeature.cpp index 91f80b2db..aeb4ea753 100644 --- a/Engine/source/shaderGen/customShaderFeature.cpp +++ b/Engine/source/shaderGen/customShaderFeature.cpp @@ -104,6 +104,11 @@ void CustomShaderFeatureData::addTexture(String name, String type, String sample mFeatureHLSL->addTexture(name, type, samplerState, arraySize); } +bool CustomShaderFeatureData::hasFeature(String name) +{ + return mFeatureHLSL->hasFeature(name); +} + void CustomShaderFeatureData::writeLine(String format, S32 argc, ConsoleValueRef *argv) { /*mOnObject = onObject; @@ -172,4 +177,9 @@ ConsoleMethod(CustomShaderFeatureData, writeLine, void, 3, 0, "( string format, "@return The result of the method call.") { object->writeLine(argv[2], argc - 3, argv + 3); +} + +DefineEngineMethod(CustomShaderFeatureData, hasFeature, bool, (String name), (""), "") +{ + return object->hasFeature(name); } \ No newline at end of file diff --git a/Engine/source/shaderGen/customShaderFeature.h b/Engine/source/shaderGen/customShaderFeature.h index 4007b1945..ed830dfbb 100644 --- a/Engine/source/shaderGen/customShaderFeature.h +++ b/Engine/source/shaderGen/customShaderFeature.h @@ -65,6 +65,8 @@ public: void addSampler(String name, String type, U32 arraySize); void addTexture(String name, String type, String samplerState, U32 arraySize); + bool hasFeature(String name); + void writeLine(String format, S32 argc, ConsoleValueRef *argv); //shader generation diff --git a/Engine/source/ts/tsRenderState.cpp b/Engine/source/ts/tsRenderState.cpp index 0c3458af7..4c0ce6b93 100644 --- a/Engine/source/ts/tsRenderState.cpp +++ b/Engine/source/ts/tsRenderState.cpp @@ -52,6 +52,7 @@ TSRenderState::TSRenderState( const TSRenderState &state ) mLightQuery( state.mLightQuery ), mAccuTex( state.mAccuTex ), mNodeTransforms( state.mNodeTransforms ), - mNodeTransformCount( state.mNodeTransformCount ) + mNodeTransformCount( state.mNodeTransformCount ), + mCustomShaderData( state.mCustomShaderData ) { } diff --git a/Engine/source/ts/tsRenderState.h b/Engine/source/ts/tsRenderState.h index db81a205c..b9dd65eeb 100644 --- a/Engine/source/ts/tsRenderState.h +++ b/Engine/source/ts/tsRenderState.h @@ -90,21 +90,24 @@ public: mFloat2 = f; type = Float2; } - + Point2F getFloat2() { return mFloat2; } + void setFloat3(StringTableEntry shaderConstName, Point3F f) { targetedUniformName = shaderConstName; mFloat3 = f; type = Float3; } - + Point3F getFloat3() { return mFloat3; } + void setFloat4(StringTableEntry shaderConstName, Point4F f) { targetedUniformName = shaderConstName; mFloat4 = f; type = Float4; } - + Point4F getFloat4() { return mFloat4; } + StringTableEntry getHandleName() { return targetedUniformName; } @@ -193,7 +196,7 @@ protected: U32 mNodeTransformCount; //Custom Shader data - Vector mCustomShaderData; + Vector mCustomShaderData; public: TSRenderState(); @@ -242,11 +245,11 @@ public: void setAccuTex( GFXTextureObject* query ) { mAccuTex = query; } GFXTextureObject* getAccuTex() const { return mAccuTex; } - void addCustomShaderBinding(CustomShaderBindingData* data) + void addCustomShaderBinding(CustomShaderBindingData data) { mCustomShaderData.push_back(data); } - Vector getCustomShaderBinding() const + Vector getCustomShaderBinding() const { return mCustomShaderData; } From 86eb678f42a7583f9cbcfa3408114dd6999c5a2a Mon Sep 17 00:00:00 2001 From: Areloch Date: Thu, 19 Oct 2017 15:34:57 -0500 Subject: [PATCH 7/9] Handler binding on a scene level working --- .../materials/processedCustomMaterial.cpp | 2 +- .../materials/processedShaderMaterial.cpp | 83 ++++++++++++------- .../materials/processedShaderMaterial.h | 24 ++---- .../renderInstance/renderDeferredMgr.cpp | 4 + 4 files changed, 66 insertions(+), 47 deletions(-) diff --git a/Engine/source/materials/processedCustomMaterial.cpp b/Engine/source/materials/processedCustomMaterial.cpp index 365676d44..38a574099 100644 --- a/Engine/source/materials/processedCustomMaterial.cpp +++ b/Engine/source/materials/processedCustomMaterial.cpp @@ -245,7 +245,7 @@ bool ProcessedCustomMaterial::init( const FeatureSet &features, return false; } - rpd->shaderHandles.init( rpd->shader, mCustomMaterial ); + rpd->shaderHandles.init( rpd->shader, mCustomMaterial->mCustomShaderFeatures, mCustomMaterial ); _initMaterialParameters(); mDefaultParameters = allocMaterialParameters(); setMaterialParameters( mDefaultParameters, 0 ); diff --git a/Engine/source/materials/processedShaderMaterial.cpp b/Engine/source/materials/processedShaderMaterial.cpp index 4c1a2f7e9..2ad9d59b1 100644 --- a/Engine/source/materials/processedShaderMaterial.cpp +++ b/Engine/source/materials/processedShaderMaterial.cpp @@ -52,7 +52,7 @@ /// /// ShaderConstHandles /// -void ShaderConstHandles::init( GFXShader *shader, CustomMaterial* mat /*=NULL*/ ) +void ShaderConstHandles::init( GFXShader *shader, Vector customFeatureData, CustomMaterial* mat /*=NULL*/) { mDiffuseColorSC = shader->getShaderConstHandle("$diffuseMaterialColor"); mTexMatSC = shader->getShaderConstHandle(ShaderGenVars::texMat); @@ -118,21 +118,19 @@ void ShaderConstHandles::init( GFXShader *shader, CustomMaterial* mat /*=NULL*/ // Deferred Shading mMatInfoFlagsSC = shader->getShaderConstHandle(ShaderGenVars::matInfoFlags); -} -void CustomFeatureShaderConstHandles::init(GFXShader *shader, Vector customFeatureData) -{ - for (U32 f = 0; f < customFeatureData.size(); ++f) - { - for (U32 i = 0; i < customFeatureData[f]->mAddedShaderConstants.size(); ++i) - { - handleData newSC; - newSC.handle = shader->getShaderConstHandle(customFeatureData[f]->mAddedShaderConstants[i]); - newSC.handleName = customFeatureData[f]->mAddedShaderConstants[i]; + //custom features + for (U32 f = 0; f < customFeatureData.size(); ++f) + { + for (U32 i = 0; i < customFeatureData[f]->mAddedShaderConstants.size(); ++i) + { + customHandleData newSC; + newSC.handle = shader->getShaderConstHandle(String("$") + String(customFeatureData[f]->mAddedShaderConstants[i])); + newSC.handleName = customFeatureData[f]->mAddedShaderConstants[i]; - mHandles.push_back(newSC); - } - } + mCustomHandles.push_back(newSC); + } + } } /// @@ -674,10 +672,7 @@ bool ProcessedShaderMaterial::_addPass( ShaderRenderPassData &rpd, rpd.shader = SHADERGEN->getShader( rpd.mFeatureData, mMaterial->mCustomShaderFeatures, mVertexFormat, &mUserMacros, samplers ); if( !rpd.shader ) return false; - rpd.shaderHandles.init( rpd.shader ); - - //Store our customShaderFeature handles - rpd.customFeatureShaderHandles.init(rpd.shader, mMaterial->mCustomShaderFeatures); + rpd.shaderHandles.init( rpd.shader, mMaterial->mCustomShaderFeatures); // If a pass glows, we glow if( rpd.mGlow ) @@ -1216,6 +1211,31 @@ void ProcessedShaderMaterial::_setShaderConstants(SceneRenderState * state, cons shaderConsts->set( handles->mAccuCoverageSC, mMaterial->mAccuCoverage[stageNum] ); if( handles->mAccuSpecularSC->isValid() ) shaderConsts->set( handles->mAccuSpecularSC, mMaterial->mAccuSpecular[stageNum] ); + + /*for (U32 i = 0; i < sgData.customShaderData.size(); i++) + { + //roll through and try setting our data! + for (U32 h = 0; h < handles->mCustomHandles.size(); ++h) + { + StringTableEntry handleName = sgData.customShaderData[i]->getHandleName(); + StringTableEntry rpdHandleName = handles->mCustomHandles[h].handleName; + if (handles->mCustomHandles[h].handleName == sgData.customShaderData[i]->getHandleName()) + { + if (handles->mCustomHandles[h].handle->isValid()) + { + if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, sgData.customShaderData[i]->getFloat()); + else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float2) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, sgData.customShaderData[i]->getFloat2()); + else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float3) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, sgData.customShaderData[i]->getFloat3()); + else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float4) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, sgData.customShaderData[i]->getFloat4()); + break; + } + } + } + }*/ } bool ProcessedShaderMaterial::_hasCubemap(U32 pass) @@ -1320,21 +1340,24 @@ void ProcessedShaderMaterial::setSceneInfo(SceneRenderState * state, const Scene for (U32 i = 0; i < sgData.customShaderData.size(); i++) { //roll through and try setting our data! - for (U32 h = 0; h < rpd->customFeatureShaderHandles.mHandles.size(); ++h) + for (U32 h = 0; h < handles->mCustomHandles.size(); ++h) { StringTableEntry handleName = sgData.customShaderData[i]->getHandleName(); - StringTableEntry rpdHandleName = rpd->customFeatureShaderHandles.mHandles[h].handleName; - if (rpd->customFeatureShaderHandles.mHandles[h].handleName == sgData.customShaderData[i]->getHandleName()) + StringTableEntry rpdHandleName = handles->mCustomHandles[h].handleName; + if (handles->mCustomHandles[h].handleName == sgData.customShaderData[i]->getHandleName()) { - if(sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float) - shaderConsts->setSafe(rpd->customFeatureShaderHandles.mHandles[h].handle, sgData.customShaderData[i]->getFloat()); - else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float2) - shaderConsts->setSafe(rpd->customFeatureShaderHandles.mHandles[h].handle, sgData.customShaderData[i]->getFloat2()); - else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float3) - shaderConsts->setSafe(rpd->customFeatureShaderHandles.mHandles[h].handle, sgData.customShaderData[i]->getFloat3()); - else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float4) - shaderConsts->setSafe(rpd->customFeatureShaderHandles.mHandles[h].handle, sgData.customShaderData[i]->getFloat4()); - break; + if (handles->mCustomHandles[h].handle->isValid()) + { + if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, sgData.customShaderData[i]->getFloat()); + else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float2) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, sgData.customShaderData[i]->getFloat2()); + else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float3) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, sgData.customShaderData[i]->getFloat3()); + else if (sgData.customShaderData[i]->getType() == CustomShaderBindingData::Float4) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, sgData.customShaderData[i]->getFloat4()); + break; + } } } } diff --git a/Engine/source/materials/processedShaderMaterial.h b/Engine/source/materials/processedShaderMaterial.h index f67204cc9..149dcfaed 100644 --- a/Engine/source/materials/processedShaderMaterial.h +++ b/Engine/source/materials/processedShaderMaterial.h @@ -96,24 +96,17 @@ public: GFXShaderConstHandle* mNodeTransforms; - void init( GFXShader* shader, CustomMaterial* mat = NULL ); + struct customHandleData + { + StringTableEntry handleName; + GFXShaderConstHandle* handle; + }; + Vector mCustomHandles; + + void init( GFXShader* shader, Vector customFeatureData, CustomMaterial* mat = NULL); }; -class CustomFeatureShaderConstHandles -{ -public: - struct handleData - { - StringTableEntry handleName; - GFXShaderConstHandle* handle; - }; - - Vector mHandles; - - void init(GFXShader *shader, Vector customFeatureData); -}; - class ShaderRenderPassData : public RenderPassData { typedef RenderPassData Parent; @@ -124,7 +117,6 @@ public: GFXShaderRef shader; ShaderConstHandles shaderHandles; - CustomFeatureShaderConstHandles customFeatureShaderHandles; Vector featureShaderHandles; virtual void reset(); diff --git a/Engine/source/renderInstance/renderDeferredMgr.cpp b/Engine/source/renderInstance/renderDeferredMgr.cpp index 434fd585a..b03a0997c 100644 --- a/Engine/source/renderInstance/renderDeferredMgr.cpp +++ b/Engine/source/renderInstance/renderDeferredMgr.cpp @@ -431,6 +431,10 @@ void RenderDeferredMgr::render( SceneRenderState *state ) mat->setNodeTransforms(passRI->mNodeTransforms, passRI->mNodeTransformCount); } + //-JR + //push along any overriden fields that are instance-specific as well + //mat->setCustomShaderHandles() + // If we're instanced then don't render yet. if ( mat->isInstanced() ) { From 524e5aad2d096f1095038c8caf3c97927250de1b Mon Sep 17 00:00:00 2001 From: Areloch Date: Thu, 26 Oct 2017 17:06:27 -0500 Subject: [PATCH 8/9] asdasd --- Engine/source/materials/baseMatInstance.h | 11 ++- .../materials/customShaderBindingData.h | 89 +++++++++++++++++++ Engine/source/materials/matInstance.cpp | 6 ++ Engine/source/materials/matInstance.h | 1 + Engine/source/materials/processedFFMaterial.h | 1 + Engine/source/materials/processedMaterial.h | 8 +- .../materials/processedShaderMaterial.cpp | 44 ++++++++- .../materials/processedShaderMaterial.h | 4 + .../renderInstance/renderDeferredMgr.cpp | 7 +- .../source/renderInstance/renderGlowMgr.cpp | 7 ++ .../source/renderInstance/renderMeshMgr.cpp | 7 ++ .../renderInstance/renderTranslucentMgr.cpp | 7 ++ Engine/source/ts/tsRenderState.h | 84 +---------------- 13 files changed, 191 insertions(+), 85 deletions(-) create mode 100644 Engine/source/materials/customShaderBindingData.h diff --git a/Engine/source/materials/baseMatInstance.h b/Engine/source/materials/baseMatInstance.h index 6a2bee0a1..4409f8372 100644 --- a/Engine/source/materials/baseMatInstance.h +++ b/Engine/source/materials/baseMatInstance.h @@ -49,6 +49,13 @@ #ifndef _MATSTATEHINT_H_ #include "materials/matStateHint.h" #endif +#ifndef _GFXDEVICE_H_ +#include "gfx/gfxDevice.h" +#endif + +#ifndef CUSTOMSHADERBINDINGDATA_H +#include "materials/customShaderBindingData.h" +#endif struct RenderPassData; class GFXVertexBufferHandleBase; @@ -60,7 +67,6 @@ class GFXVertexFormat; class MatrixSet; class ProcessedMaterial; - /// class BaseMatInstance { @@ -155,6 +161,9 @@ public: /// Sets node transforms for the current stage. Used for hardware skinning. virtual void setNodeTransforms( const MatrixF *address, const U32 numTransforms ) = 0; + /// Sets custom shader data + virtual void setCustomShaderData(Vector &shaderData) = 0; + /// This initializes various material scene state settings and /// should be called after setupPass() within the pass loop. /// @see setupPass diff --git a/Engine/source/materials/customShaderBindingData.h b/Engine/source/materials/customShaderBindingData.h new file mode 100644 index 000000000..52dad2e9e --- /dev/null +++ b/Engine/source/materials/customShaderBindingData.h @@ -0,0 +1,89 @@ +#pragma once + +#ifndef CUSTOMSHADERBINDINGDATA_H +#define CUSTOMSHADERBINDINGDATA_H +#ifndef _GFXDEVICE_H_ +#include "gfx/gfxDevice.h" +#endif + +struct CustomShaderBindingData +{ +public: + enum UniformType + { + Float = 0, + Float2, + Float3, + Float4, + Texture2D, + Texture3D, + Cubemap, + Matrix2x2, + Matrix2x3, + Matrix2x4, + Matrix3x2, + Matrix3x3, + Matrix3x4, + Matrix4x2, + Matrix4x3, + Matrix4x4 + }; +private: + StringTableEntry targetedUniformName; + + //ShaderConstHandles shaderConstHandle; + + UniformType type; + + F32 mFloat; + Point2F mFloat2; + Point3F mFloat3; + Point4F mFloat4; + + //Image stuff + GFXTexHandle texture; + GFXSamplerStateDesc samplerState; + +public: + void setFloat(StringTableEntry shaderConstName, F32 f) + { + targetedUniformName = shaderConstName; + mFloat = f; + type = Float; + } + F32 getFloat() { return mFloat; } + + void setFloat2(StringTableEntry shaderConstName, Point2F f) + { + targetedUniformName = shaderConstName; + mFloat2 = f; + type = Float2; + } + Point2F getFloat2() { return mFloat2; } + + void setFloat3(StringTableEntry shaderConstName, Point3F f) + { + targetedUniformName = shaderConstName; + mFloat3 = f; + type = Float3; + } + Point3F getFloat3() { return mFloat3; } + + void setFloat4(StringTableEntry shaderConstName, Point4F f) + { + targetedUniformName = shaderConstName; + mFloat4 = f; + type = Float4; + } + Point4F getFloat4() { return mFloat4; } + + StringTableEntry getHandleName() { + return targetedUniformName; + } + + UniformType getType() { + return type; + } +}; + +#endif \ No newline at end of file diff --git a/Engine/source/materials/matInstance.cpp b/Engine/source/materials/matInstance.cpp index 33b6357c9..8d563e514 100644 --- a/Engine/source/materials/matInstance.cpp +++ b/Engine/source/materials/matInstance.cpp @@ -472,6 +472,12 @@ void MatInstance::setNodeTransforms(const MatrixF *address, const U32 numTransfo mProcessedMaterial->setNodeTransforms(address, numTransforms, getCurPass()); } +void MatInstance::setCustomShaderData(Vector &shaderData) +{ + PROFILE_SCOPE(MatInstance_setCustomShaderData); + mProcessedMaterial->setCustomShaderData(shaderData, getCurPass()); +} + void MatInstance::setSceneInfo(SceneRenderState * state, const SceneData& sgData) { PROFILE_SCOPE(MatInstance_setSceneInfo); diff --git a/Engine/source/materials/matInstance.h b/Engine/source/materials/matInstance.h index 11178a42c..a89445243 100644 --- a/Engine/source/materials/matInstance.h +++ b/Engine/source/materials/matInstance.h @@ -66,6 +66,7 @@ public: virtual bool setupPass(SceneRenderState *, const SceneData &sgData ); virtual void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state); virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms); + virtual void setCustomShaderData(Vector &shaderData); virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData); virtual void setTextureStages(SceneRenderState * state, const SceneData &sgData ); virtual void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer); diff --git a/Engine/source/materials/processedFFMaterial.h b/Engine/source/materials/processedFFMaterial.h index c65175381..3ba72594c 100644 --- a/Engine/source/materials/processedFFMaterial.h +++ b/Engine/source/materials/processedFFMaterial.h @@ -54,6 +54,7 @@ public: virtual void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state, const U32 pass); virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms, const U32 pass) {;} + virtual void setCustomShaderData(Vector &shaderData, const U32 pass) {;} //-JR virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData, U32 pass); diff --git a/Engine/source/materials/processedMaterial.h b/Engine/source/materials/processedMaterial.h index 38685eacb..f6f3fdcec 100644 --- a/Engine/source/materials/processedMaterial.h +++ b/Engine/source/materials/processedMaterial.h @@ -39,6 +39,10 @@ #include "materials/matStateHint.h" #endif +#ifndef CUSTOMSHADERBINDINGDATA_H +#include "materials/customShaderBindingData.h" +#endif + class ShaderFeature; class MaterialParameters; class MaterialParameterHandle; @@ -47,7 +51,6 @@ class GFXVertexBufferHandleBase; class GFXPrimitiveBufferHandle; class MatrixSet; - /// This contains the common data needed to render a pass. struct RenderPassData { @@ -146,6 +149,9 @@ public: /// Sets the node transforms for HW Skinning virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms, const U32 pass) = 0; + + /// Sets any custom shader data + virtual void setCustomShaderData(Vector &shaderData, const U32 pass) = 0; /// Sets the scene info like lights for the given pass. virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData, U32 pass) = 0; diff --git a/Engine/source/materials/processedShaderMaterial.cpp b/Engine/source/materials/processedShaderMaterial.cpp index 2ad9d59b1..56561f089 100644 --- a/Engine/source/materials/processedShaderMaterial.cpp +++ b/Engine/source/materials/processedShaderMaterial.cpp @@ -1300,6 +1300,46 @@ void ProcessedShaderMaterial::setNodeTransforms(const MatrixF *transforms, const } } +void ProcessedShaderMaterial::setCustomShaderData(Vector &shaderData, const U32 pass) +{ + PROFILE_SCOPE(ProcessedShaderMaterial_setCustomShaderData); + + GFXShaderConstBuffer* shaderConsts = _getShaderConstBuffer(pass); + ShaderConstHandles* handles = _getShaderConstHandles(pass); + + for (U32 i = 0; i < shaderData.size(); i++) + { + for (U32 h = 0; h < handles->mCustomHandles.size(); ++h) + { + StringTableEntry handleName = shaderData[i].getHandleName(); + bool tmp = true; + } + //roll through and try setting our data! + for (U32 h = 0; h < handles->mCustomHandles.size(); ++h) + { + StringTableEntry handleName = shaderData[i].getHandleName(); + StringTableEntry rpdHandleName = handles->mCustomHandles[h].handleName; + if (handles->mCustomHandles[h].handleName == shaderData[i].getHandleName()) + { + if (handles->mCustomHandles[h].handle->isValid()) + { + CustomShaderBindingData::UniformType type = shaderData[i].getType(); + + if (type == CustomShaderBindingData::Float) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, shaderData[i].getFloat()); + else if (type == CustomShaderBindingData::Float2) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, shaderData[i].getFloat2()); + else if (type == CustomShaderBindingData::Float3) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, shaderData[i].getFloat3()); + else if (type == CustomShaderBindingData::Float4) + shaderConsts->setSafe(handles->mCustomHandles[h].handle, shaderData[i].getFloat4()); + break; + } + } + } + } +} + void ProcessedShaderMaterial::setSceneInfo(SceneRenderState * state, const SceneData& sgData, U32 pass) { PROFILE_SCOPE( ProcessedShaderMaterial_setSceneInfo ); @@ -1337,7 +1377,7 @@ void ProcessedShaderMaterial::setSceneInfo(SceneRenderState * state, const Scene for ( U32 i=0; i < rpd->featureShaderHandles.size(); i++ ) rpd->featureShaderHandles[i]->setConsts( state, sgData, shaderConsts ); - for (U32 i = 0; i < sgData.customShaderData.size(); i++) + /*for (U32 i = 0; i < sgData.customShaderData.size(); i++) { //roll through and try setting our data! for (U32 h = 0; h < handles->mCustomHandles.size(); ++h) @@ -1360,7 +1400,7 @@ void ProcessedShaderMaterial::setSceneInfo(SceneRenderState * state, const Scene } } } - } + }*/ LIGHTMGR->setLightInfo( this, mMaterial, sgData, state, pass, shaderConsts ); } diff --git a/Engine/source/materials/processedShaderMaterial.h b/Engine/source/materials/processedShaderMaterial.h index 149dcfaed..8a45ddfc7 100644 --- a/Engine/source/materials/processedShaderMaterial.h +++ b/Engine/source/materials/processedShaderMaterial.h @@ -29,6 +29,9 @@ #ifndef _GFXSHADER_H_ #include "gfx/gfxShader.h" #endif +#ifndef CUSTOMSHADERBINDINGDATA_H +#include "materials/customShaderBindingData.h" +#endif class GenericConstBufferLayout; class ShaderData; @@ -140,6 +143,7 @@ public: virtual void setTextureStages(SceneRenderState *, const SceneData &sgData, U32 pass ); virtual void setTransforms(const MatrixSet &matrixSet, SceneRenderState *state, const U32 pass); virtual void setNodeTransforms(const MatrixF *address, const U32 numTransforms, const U32 pass); + virtual void setCustomShaderData(Vector &shaderData, const U32 pass); virtual void setSceneInfo(SceneRenderState *, const SceneData& sgData, U32 pass); virtual void setBuffers(GFXVertexBufferHandleBase* vertBuffer, GFXPrimitiveBufferHandle* primBuffer); virtual bool stepInstance(); diff --git a/Engine/source/renderInstance/renderDeferredMgr.cpp b/Engine/source/renderInstance/renderDeferredMgr.cpp index b03a0997c..2da8b8cd6 100644 --- a/Engine/source/renderInstance/renderDeferredMgr.cpp +++ b/Engine/source/renderInstance/renderDeferredMgr.cpp @@ -51,6 +51,8 @@ #include "materials/shaderData.h" #include "gfx/sim/cubemapData.h" +#include "materials/customShaderBindingData.h" + const MatInstanceHookType DeferredMatInstanceHook::Type( "Deferred" ); const String RenderDeferredMgr::BufferName("deferred"); const RenderInstType RenderDeferredMgr::RIT_Deferred("Deferred"); @@ -433,7 +435,10 @@ void RenderDeferredMgr::render( SceneRenderState *state ) //-JR //push along any overriden fields that are instance-specific as well - //mat->setCustomShaderHandles() + if (passRI->mCustomShaderData.size() > 0) + { + mat->setCustomShaderData(passRI->mCustomShaderData); + } // If we're instanced then don't render yet. if ( mat->isInstanced() ) diff --git a/Engine/source/renderInstance/renderGlowMgr.cpp b/Engine/source/renderInstance/renderGlowMgr.cpp index 1ce49149d..dd2d2f29d 100644 --- a/Engine/source/renderInstance/renderGlowMgr.cpp +++ b/Engine/source/renderInstance/renderGlowMgr.cpp @@ -251,6 +251,13 @@ void RenderGlowMgr::render( SceneRenderState *state ) glowMat->setNodeTransforms(passRI->mNodeTransforms, passRI->mNodeTransformCount); } + //-JR + //push along any overriden fields that are instance-specific as well + if (passRI->mCustomShaderData.size() > 0) + { + mat->setCustomShaderData(passRI->mCustomShaderData); + } + glowMat->setSceneInfo(state, sgData); glowMat->setBuffers(passRI->vertBuff, passRI->primBuff); diff --git a/Engine/source/renderInstance/renderMeshMgr.cpp b/Engine/source/renderInstance/renderMeshMgr.cpp index c9de7c229..adaee396f 100644 --- a/Engine/source/renderInstance/renderMeshMgr.cpp +++ b/Engine/source/renderInstance/renderMeshMgr.cpp @@ -182,6 +182,13 @@ void RenderMeshMgr::render(SceneRenderState * state) mat->setNodeTransforms(passRI->mNodeTransforms, passRI->mNodeTransformCount); } + //-JR + //push along any overriden fields that are instance-specific as well + if (passRI->mCustomShaderData.size() > 0) + { + mat->setCustomShaderData(passRI->mCustomShaderData); + } + setupSGData( passRI, sgData ); mat->setSceneInfo( state, sgData ); diff --git a/Engine/source/renderInstance/renderTranslucentMgr.cpp b/Engine/source/renderInstance/renderTranslucentMgr.cpp index e0c3e6bb4..74dbdd154 100644 --- a/Engine/source/renderInstance/renderTranslucentMgr.cpp +++ b/Engine/source/renderInstance/renderTranslucentMgr.cpp @@ -249,6 +249,13 @@ void RenderTranslucentMgr::render( SceneRenderState *state ) mat->setNodeTransforms(passRI->mNodeTransforms, passRI->mNodeTransformCount); } + //-JR + //push along any overriden fields that are instance-specific as well + if (passRI->mCustomShaderData.size() > 0) + { + mat->setCustomShaderData(passRI->mCustomShaderData); + } + // If we're instanced then don't render yet. if ( mat->isInstanced() ) { diff --git a/Engine/source/ts/tsRenderState.h b/Engine/source/ts/tsRenderState.h index b9dd65eeb..eda8d5b3a 100644 --- a/Engine/source/ts/tsRenderState.h +++ b/Engine/source/ts/tsRenderState.h @@ -31,92 +31,16 @@ #include "gfx/gfxDevice.h" #endif +#ifndef _BASEMATINSTANCE_H_ +#include "materials/baseMatInstance.h" +#endif + class SceneRenderState; class GFXCubemap; class Frustum; class LightQuery; class TSShape; -struct CustomShaderBindingData -{ -public: - enum UniformType - { - Float = 0, - Float2, - Float3, - Float4, - Texture2D, - Texture3D, - Cubemap, - Matrix2x2, - Matrix2x3, - Matrix2x4, - Matrix3x2, - Matrix3x3, - Matrix3x4, - Matrix4x2, - Matrix4x3, - Matrix4x4 - }; -private: - StringTableEntry targetedUniformName; - - //ShaderConstHandles shaderConstHandle; - - UniformType type; - - F32 mFloat; - Point2F mFloat2; - Point3F mFloat3; - Point4F mFloat4; - - //Image stuff - GFXTexHandle texture; - GFXSamplerStateDesc samplerState; - -public: - void setFloat(StringTableEntry shaderConstName, F32 f) - { - targetedUniformName = shaderConstName; - mFloat = f; - type = Float; - } - F32 getFloat() { return mFloat; } - - void setFloat2(StringTableEntry shaderConstName, Point2F f) - { - targetedUniformName = shaderConstName; - mFloat2 = f; - type = Float2; - } - Point2F getFloat2() { return mFloat2; } - - void setFloat3(StringTableEntry shaderConstName, Point3F f) - { - targetedUniformName = shaderConstName; - mFloat3 = f; - type = Float3; - } - Point3F getFloat3() { return mFloat3; } - - void setFloat4(StringTableEntry shaderConstName, Point4F f) - { - targetedUniformName = shaderConstName; - mFloat4 = f; - type = Float4; - } - Point4F getFloat4() { return mFloat4; } - - StringTableEntry getHandleName() { - return targetedUniformName; - } - - UniformType getType() { - return type; - } -}; - /// A simple class for passing render state through the pre-render pipeline. /// /// @section TSRenderState_intro Introduction From a3a877eaf2d91baf38d04843deb727c86db348c9 Mon Sep 17 00:00:00 2001 From: Areloch Date: Tue, 16 Jan 2018 14:24:21 -0600 Subject: [PATCH 9/9] Initial setup for shader connectors --- .../shaderGen/HLSL/customFeatureHLSL.cpp | 54 +++++++++++++++++++ .../source/shaderGen/HLSL/customFeatureHLSL.h | 11 ++++ .../source/shaderGen/customShaderFeature.cpp | 5 ++ Engine/source/shaderGen/customShaderFeature.h | 1 + 4 files changed, 71 insertions(+) diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp index d5f15e1b4..409d280a3 100644 --- a/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.cpp @@ -48,6 +48,9 @@ void CustomFeatureHLSL::processVert(Vector &componentList, meta = new MultiLine; mFeatureData = fd; + mComponentList = componentList; + + mOutputState = VertexOutput; if (mOwner->isMethod("processVertHLSL")) Con::executef(mOwner, "processVertHLSL"); @@ -61,6 +64,9 @@ void CustomFeatureHLSL::processPix(Vector &componentList, meta = new MultiLine; mFeatureData = fd; + mComponentList = componentList; + + mOutputState = PixelOutput; /*MultiLine *meta = new MultiLine; @@ -322,6 +328,54 @@ void CustomFeatureHLSL::addVariable(String name, String type, String defaultValu } } +void CustomFeatureHLSL::addConnector(String name, String elementName, String type) +{ + // grab connector texcoord register + ShaderConnector *connectComp = dynamic_cast(mComponentList[C_CONNECTOR]); + + //Get element + S32 element = -1; + + if (elementName == String("RT_POSITION")) + element = RT_POSITION; + else if (elementName == String("RT_NORMAL")) + element = RT_NORMAL; + else if (elementName == String("RT_BINORMAL")) + element = RT_BINORMAL; + else if (elementName == String("RT_TANGENT")) + element = RT_TANGENT; + else if (elementName == String("RT_TANGENTW")) + element = RT_TANGENTW; + else if (elementName == String("RT_COLOR")) + element = RT_COLOR; + else if (elementName == String("RT_TEXCOORD")) + element = RT_TEXCOORD; + else if (elementName == String("RT_VPOS")) + element = RT_VPOS; + else if (elementName == String("RT_SVPOSITION")) + element = RT_SVPOSITION; + else if (elementName == String("RT_BLENDINDICES")) + element = RT_BLENDINDICES; + else if (elementName == String("RT_BLENDWEIGHT")) + element = RT_BLENDWEIGHT; + + if (element == -1) + { + Con::errorf("CustomShaderFeatureHLSL::addConnector - Invalid element type %s", elementName.c_str()); + return; + } + + Var *connector = connectComp->getElement((RegisterType)element); + connector->setName(name); + + if (mOutputState == VertexOutput) + connector->setStructName("OUT"); + else if(mOutputState == PixelOutput) + connector->setStructName("IN"); + + connector->setType(type); +} + void CustomFeatureHLSL::writeLine(String format, S32 argc, ConsoleValueRef *argv) { //do the var/arg fetching here diff --git a/Engine/source/shaderGen/HLSL/customFeatureHLSL.h b/Engine/source/shaderGen/HLSL/customFeatureHLSL.h index 216cc1464..e36e682bd 100644 --- a/Engine/source/shaderGen/HLSL/customFeatureHLSL.h +++ b/Engine/source/shaderGen/HLSL/customFeatureHLSL.h @@ -63,9 +63,19 @@ class CustomFeatureHLSL : public ShaderFeatureHLSL Vector mVars; + enum outputState + { + NoOutput, + VertexOutput, + PixelOutput + }; + + outputState mOutputState; + public: CustomShaderFeatureData* mOwner; + Vector mComponentList; MaterialFeatureData mFeatureData; protected: @@ -108,5 +118,6 @@ public: void addVariable(String name, String type, String defaultValue); void addSampler(String name, String type, U32 arraySize = 0); void addTexture(String name, String type, String samplerState, U32 arraySize); + void addConnector(String name, String elementName, String type); void writeLine(String format, S32 argc, ConsoleValueRef *argv); }; \ No newline at end of file diff --git a/Engine/source/shaderGen/customShaderFeature.cpp b/Engine/source/shaderGen/customShaderFeature.cpp index aeb4ea753..51d4e805e 100644 --- a/Engine/source/shaderGen/customShaderFeature.cpp +++ b/Engine/source/shaderGen/customShaderFeature.cpp @@ -104,6 +104,11 @@ void CustomShaderFeatureData::addTexture(String name, String type, String sample mFeatureHLSL->addTexture(name, type, samplerState, arraySize); } +void CustomShaderFeatureData::addConnector(String name, String elementName, String type) +{ + mFeatureHLSL->addConnector(name, elementName, type); +} + bool CustomShaderFeatureData::hasFeature(String name) { return mFeatureHLSL->hasFeature(name); diff --git a/Engine/source/shaderGen/customShaderFeature.h b/Engine/source/shaderGen/customShaderFeature.h index ed830dfbb..d134fff5d 100644 --- a/Engine/source/shaderGen/customShaderFeature.h +++ b/Engine/source/shaderGen/customShaderFeature.h @@ -64,6 +64,7 @@ public: void addUniform(String name, String type, String defaultValue, U32 arraySize); void addSampler(String name, String type, U32 arraySize); void addTexture(String name, String type, String samplerState, U32 arraySize); + void addConnector(String name, String elementName, String type); bool hasFeature(String name);