Groundwork for other shaders

Adds the ground work for geometry shaders
Expands shaderData and gfxShader to allow for more shader types

note: when building a GFXShader in source you have to call setShaderStageFile with the shaderStage and the filepath for that stage.

Once we add compute shaders this will become more apparent as compute shaders are a stage of their own and do not require vertex and pixel files whereas other shaders sometimes do.
This commit is contained in:
marauder2k7 2024-03-06 13:26:39 +00:00
parent 949f788a0a
commit 808e2f4200
7 changed files with 220 additions and 161 deletions

View file

@ -59,13 +59,18 @@ bool GFXShader::init( const Torque::Path &vertFile,
} }
#endif #endif
bool GFXShader::init( const Torque::Path &vertFile, bool GFXShader::init( F32 pixVersion,
const Torque::Path &pixFile,
F32 pixVersion,
const Vector<GFXShaderMacro> &macros, const Vector<GFXShaderMacro> &macros,
const Vector<String> &samplerNames, const Vector<String> &samplerNames,
GFXVertexFormat *instanceFormat) GFXVertexFormat *instanceFormat)
{ {
// early out.
if (mVertexFile.isEmpty() && mPixelFile.isEmpty() && mGeometryFile.isEmpty())
{
Con::errorf("Shader files empty, please call setShaderStageFile from shaderData");
return false;
}
// Take care of instancing // Take care of instancing
if (instanceFormat) if (instanceFormat)
{ {
@ -74,8 +79,6 @@ bool GFXShader::init( const Torque::Path &vertFile,
} }
// Store the inputs for use in reloading. // Store the inputs for use in reloading.
mVertexFile = vertFile;
mPixelFile = pixFile;
mPixVersion = pixVersion; mPixVersion = pixVersion;
mMacros = macros; mMacros = macros;
mSamplerNamesOrdered = samplerNames; mSamplerNamesOrdered = samplerNames;
@ -91,8 +94,12 @@ bool GFXShader::init( const Torque::Path &vertFile,
_updateDesc(); _updateDesc();
// Add file change notifications for reloads. // Add file change notifications for reloads.
Torque::FS::AddChangeNotification( mVertexFile, this, &GFXShader::_onFileChanged ); if(!mVertexFile.isEmpty())
Torque::FS::AddChangeNotification( mPixelFile, this, &GFXShader::_onFileChanged ); Torque::FS::AddChangeNotification( mVertexFile, this, &GFXShader::_onFileChanged );
if(!mPixelFile.isEmpty())
Torque::FS::AddChangeNotification( mPixelFile, this, &GFXShader::_onFileChanged );
if(!mGeometryFile.isEmpty())
Torque::FS::AddChangeNotification( mGeometryFile, this, &GFXShader::_onFileChanged);
return true; return true;
} }
@ -161,6 +168,24 @@ bool GFXShader::removeGlobalMacro( const String &name )
return false; return false;
} }
void GFXShader::setShaderStageFile(const GFXShaderStage stage, const Torque::Path& filePath)
{
switch (stage)
{
case GFXShaderStage::VERTEX_SHADER:
mVertexFile = filePath;
break;
case GFXShaderStage::PIXEL_SHADER:
mPixelFile = filePath;
break;
case GFXShaderStage::GEOMETRY_SHADER:
mGeometryFile = filePath;
break;
default:
break;
}
}
void GFXShader::_unlinkBuffer( GFXShaderConstBuffer *buf ) void GFXShader::_unlinkBuffer( GFXShaderConstBuffer *buf )
{ {
Vector<GFXShaderConstBuffer*>::iterator iter = mActiveBuffers.begin(); Vector<GFXShaderConstBuffer*>::iterator iter = mActiveBuffers.begin();

View file

@ -249,6 +249,9 @@ protected:
/// The pixel shader file. /// The pixel shader file.
Torque::Path mPixelFile; Torque::Path mPixelFile;
// the geometry shader file.
Torque::Path mGeometryFile;
/// The macros to be passed to the shader. /// The macros to be passed to the shader.
Vector<GFXShaderMacro> mMacros; Vector<GFXShaderMacro> mMacros;
@ -322,9 +325,7 @@ public:
#endif #endif
/// ///
bool init( const Torque::Path &vertFile, bool init( F32 pixVersion,
const Torque::Path &pixFile,
F32 pixVersion,
const Vector<GFXShaderMacro> &macros, const Vector<GFXShaderMacro> &macros,
const Vector<String> &samplerNames, const Vector<String> &samplerNames,
GFXVertexFormat *instanceFormat = NULL ); GFXVertexFormat *instanceFormat = NULL );
@ -364,6 +365,8 @@ public:
/// the shader disassembly. /// the shader disassembly.
virtual bool getDisassembly( String &outStr ) const { return false; } virtual bool getDisassembly( String &outStr ) const { return false; }
void setShaderStageFile(const GFXShaderStage stage, const Torque::Path& filePath);
/// Returns the vertex shader file path. /// Returns the vertex shader file path.
const String& getVertexShaderFile() const { return mVertexFile.getFullPath(); } const String& getVertexShaderFile() const { return mVertexFile.getFullPath(); }

View file

@ -134,7 +134,7 @@ void GuiShaderEditor::onPreRender()
setUpdate(); setUpdate();
} }
void GuiShaderEditor::drawThickLine(const Point2I& pt1, const Point2I& pt2, U32 thickness = 2, ColorI col1 = ColorI(255, 255, 255), ColorI col2 = ColorI(255, 255, 255)) void GuiShaderEditor::drawThickLine(const Point2I& pt1, const Point2I& pt2, U32 thickness, ColorI col1, ColorI col2)
{ {
Point2F dir = Point2F(pt2.x - pt1.x, pt2.y - pt1.y); Point2F dir = Point2F(pt2.x - pt1.x, pt2.y - pt1.y);
if (dir == Point2F::Zero) if (dir == Point2F::Zero)
@ -292,7 +292,7 @@ void GuiShaderEditor::renderConnections(Point2I offset, const RectI& updateRect)
start += Point2I(mNodeSize / 2, mNodeSize / 2); start += Point2I(mNodeSize / 2, mNodeSize / 2);
end += Point2I(mNodeSize / 2, mNodeSize / 2); end += Point2I(mNodeSize / 2, mNodeSize / 2);
drawThickLine(start, end); drawThickLine(start, end, mNodeSize/3);
} }
// Restore the clip rect to what it was at the start // Restore the clip rect to what it was at the start
@ -328,7 +328,7 @@ void GuiShaderEditor::onRender(Point2I offset, const RectI& updateRect)
RectI sockActive(start, Point2I(mNodeSize, mNodeSize)); RectI sockActive(start, Point2I(mNodeSize, mNodeSize));
start += Point2I(mNodeSize / 2, mNodeSize / 2); start += Point2I(mNodeSize / 2, mNodeSize / 2);
drawThickLine(start, mLastMousePos + offset); drawThickLine(start, mLastMousePos + offset, mNodeSize/3);
// draw socket overlay over the top of the line. // draw socket overlay over the top of the line.
sockActive.inset(1, 1); sockActive.inset(1, 1);

View file

@ -117,7 +117,7 @@ public:
virtual void onRemove() override; virtual void onRemove() override;
virtual void onPreRender() override; virtual void onPreRender() override;
void drawThickLine(const Point2I& pt1, const Point2I& pt2, U32 thickness, ColorI col1, ColorI col2); void drawThickLine(const Point2I& pt1, const Point2I& pt2, U32 thickness = 2, ColorI col1 = ColorI(255, 255, 255), ColorI col2 = ColorI(255, 255, 255));
virtual void onRender(Point2I offset, const RectI& updateRect) override; virtual void onRender(Point2I offset, const RectI& updateRect) override;
// interaction // interaction

View file

@ -48,10 +48,12 @@ ConsoleDocClass( ShaderData,
"// Used for the procedural clould system\n" "// Used for the procedural clould system\n"
"singleton ShaderData( CloudLayerShader )\n" "singleton ShaderData( CloudLayerShader )\n"
"{\n" "{\n"
" DXVertexShaderFile = $Core::CommonShaderPath @ \"/cloudLayerV.hlsl\";\n" " DXVertexShaderFile = $Core::CommonShaderPath @ \"/cloudLayerV.hlsl\";\n"
" DXPixelShaderFile = $Core::CommonShaderPath @ \"/cloudLayerP.hlsl\";\n" " DXPixelShaderFile = $Core::CommonShaderPath @ \"/cloudLayerP.hlsl\";\n"
" OGLVertexShaderFile = $Core::CommonShaderPath @ \"/gl/cloudLayerV.glsl\";\n" " DXGeometryShaderFile = $Core::CommonShaderPath @ \"/cloudLayerG.hlsl\";\n"
" OGLPixelShaderFile = $Core::CommonShaderPath @ \"/gl/cloudLayerP.glsl\";\n" " OGLVertexShaderFile = $Core::CommonShaderPath @ \"/gl/cloudLayerV.glsl\";\n"
" OGLPixelShaderFile = $Core::CommonShaderPath @ \"/gl/cloudLayerP.glsl\";\n"
" OGLGeometryShaderFile = $Core::CommonShaderPath @ \"/gl/cloudLayerG.glsl\";\n"
" pixVersion = 2.0;\n" " pixVersion = 2.0;\n"
"};\n" "};\n"
"@endtsexample\n\n" "@endtsexample\n\n"
@ -67,70 +69,87 @@ ShaderData::ShaderData()
for( int i = 0; i < NumTextures; ++i) for( int i = 0; i < NumTextures; ++i)
mRTParams[i] = false; mRTParams[i] = false;
mDXVertexShaderName = StringTable->EmptyString();
mDXPixelShaderName = StringTable->EmptyString();
mDXGeometryShaderName = StringTable->EmptyString();
mOGLVertexShaderName = StringTable->EmptyString();
mOGLPixelShaderName = StringTable->EmptyString();
mOGLGeometryShaderName = StringTable->EmptyString();
} }
void ShaderData::initPersistFields() void ShaderData::initPersistFields()
{ {
docsURL; docsURL;
addField("DXVertexShaderFile", TypeStringFilename, Offset(mDXVertexShaderName, ShaderData), addField("DXVertexShaderFile", TypeStringFilename, Offset(mDXVertexShaderName, ShaderData),
"@brief %Path to the DirectX vertex shader file to use for this ShaderData.\n\n" "@brief %Path to the DirectX vertex shader file to use for this ShaderData.\n\n"
"It must contain only one program and no pixel shader, just the vertex shader." "It must contain only one program and no pixel shader, just the vertex shader."
"It can be either an HLSL or assembly level shader. HLSL's must have a " "It can be either an HLSL or assembly level shader. HLSL's must have a "
"filename extension of .hlsl, otherwise its assumed to be an assembly file."); "filename extension of .hlsl, otherwise its assumed to be an assembly file.");
addField("DXPixelShaderFile", TypeStringFilename, Offset(mDXPixelShaderName, ShaderData), addField("DXPixelShaderFile", TypeStringFilename, Offset(mDXPixelShaderName, ShaderData),
"@brief %Path to the DirectX pixel shader file to use for this ShaderData.\n\n" "@brief %Path to the DirectX pixel shader file to use for this ShaderData.\n\n"
"It must contain only one program and no vertex shader, just the pixel " "It must contain only one program and no vertex shader, just the pixel "
"shader. It can be either an HLSL or assembly level shader. HLSL's " "shader. It can be either an HLSL or assembly level shader. HLSL's "
"must have a filename extension of .hlsl, otherwise its assumed to be an assembly file."); "must have a filename extension of .hlsl, otherwise its assumed to be an assembly file.");
addField("OGLVertexShaderFile", TypeStringFilename, Offset(mOGLVertexShaderName, ShaderData), addField("DXGeometryShaderFile", TypeStringFilename, Offset(mDXGeometryShaderName, ShaderData),
"@brief %Path to an OpenGL vertex shader file to use for this ShaderData.\n\n" "@brief %Path to the DirectX geometry shader file to use for this ShaderData.\n\n"
"It must contain only one program and no pixel shader, just the vertex shader."); "It can be either an HLSL or assembly level shader. HLSL's must have a "
"filename extension of .hlsl, otherwise its assumed to be an assembly file.");
addField("OGLPixelShaderFile", TypeStringFilename, Offset(mOGLPixelShaderName, ShaderData), addField("OGLVertexShaderFile", TypeStringFilename, Offset(mOGLVertexShaderName, ShaderData),
"@brief %Path to an OpenGL pixel shader file to use for this ShaderData.\n\n" "@brief %Path to an OpenGL vertex shader file to use for this ShaderData.\n\n"
"It must contain only one program and no vertex shader, just the pixel " "It must contain only one program and no pixel shader, just the vertex shader.");
"shader.");
addField("useDevicePixVersion", TypeBool, Offset(mUseDevicePixVersion, ShaderData), addField("OGLPixelShaderFile", TypeStringFilename, Offset(mOGLPixelShaderName, ShaderData),
"@brief If true, the maximum pixel shader version offered by the graphics card will be used.\n\n" "@brief %Path to an OpenGL pixel shader file to use for this ShaderData.\n\n"
"Otherwise, the script-defined pixel shader version will be used.\n\n"); "It must contain only one program and no vertex shader, just the pixel "
"shader.");
addField("pixVersion", TypeF32, Offset(mPixVersion, ShaderData), addField("OGLGeometryShaderFile", TypeStringFilename, Offset(mOGLGeometryShaderName, ShaderData),
"@brief Indicates target level the shader should be compiled.\n\n" "@brief %Path to the OpenGL Geometry shader file to use for this ShaderData.\n\n");
"Valid numbers at the time of this writing are 1.1, 1.4, 2.0, and 3.0. "
"The shader will not run properly if the hardware does not support the "
"level of shader compiled.");
addField("defines", TypeRealString, Offset(mDefines, ShaderData), addField("useDevicePixVersion", TypeBool, Offset(mUseDevicePixVersion, ShaderData),
"@brief String of case-sensitive defines passed to the shader compiler.\n\n" "@brief If true, the maximum pixel shader version offered by the graphics card will be used.\n\n"
"Otherwise, the script-defined pixel shader version will be used.\n\n");
addField("pixVersion", TypeF32, Offset(mPixVersion, ShaderData),
"@brief Indicates target level the shader should be compiled.\n\n"
"Valid numbers at the time of this writing are 1.1, 1.4, 2.0, and 3.0. "
"The shader will not run properly if the hardware does not support the "
"level of shader compiled.");
addField("defines", TypeRealString, Offset(mDefines, ShaderData),
"@brief String of case-sensitive defines passed to the shader compiler.\n\n"
"The string should be delimited by a semicolon, tab, or newline character." "The string should be delimited by a semicolon, tab, or newline character."
"@tsexample\n" "@tsexample\n"
"singleton ShaderData( FlashShader )\n" "singleton ShaderData( FlashShader )\n"
"{\n" "{\n"
"DXVertexShaderFile = $shaderGen::cachePath @ \"/postFx/flashV.hlsl\";\n" "DXVertexShaderFile = $shaderGen::cachePath @ \"/postFx/flashV.hlsl\";\n"
"DXPixelShaderFile = $shaderGen::cachePath @ \"/postFx/flashP.hlsl\";\n\n" "DXPixelShaderFile = $shaderGen::cachePath @ \"/postFx/flashP.hlsl\";\n\n"
" //Define setting the color of WHITE_COLOR.\n" "DXGeometryShaderFile = $shaderGen::cachePath @ \"/postFx/flashG.hlsl\";\n\n"
"defines = \"WHITE_COLOR=float4(1.0,1.0,1.0,0.0)\";\n\n" " //Define setting the color of WHITE_COLOR.\n"
"pixVersion = 2.0\n" "defines = \"WHITE_COLOR=float4(1.0,1.0,1.0,0.0)\";\n\n"
"}\n" "pixVersion = 2.0\n"
"}\n"
"@endtsexample\n\n" "@endtsexample\n\n"
); );
addField("samplerNames", TypeRealString, Offset(mSamplerNames, ShaderData), NumTextures, addField("samplerNames", TypeRealString, Offset(mSamplerNames, ShaderData), NumTextures,
"@brief Indicates names of samplers present in shader. Order is important.\n\n" "@brief Indicates names of samplers present in shader. Order is important.\n\n"
"Order of sampler names are used to assert correct sampler register/location" "Order of sampler names are used to assert correct sampler register/location"
"Other objects (GFXStateBlockData, PostEffect...) use index number to link samplers." "Other objects (GFXStateBlockData, PostEffect...) use index number to link samplers."
); );
addField("rtParams", TypeBool, Offset(mRTParams, ShaderData), NumTextures, ""); addField("rtParams", TypeBool, Offset(mRTParams, ShaderData), NumTextures, "");
Parent::initPersistFields(); Parent::initPersistFields();
// Make sure we get activation signals. // Make sure we get activation signals.
LightManager::smActivateSignal.notify( &ShaderData::_onLMActivate ); LightManager::smActivateSignal.notify(&ShaderData::_onLMActivate);
} }
bool ShaderData::onAdd() bool ShaderData::onAdd()
@ -237,9 +256,13 @@ GFXShader* ShaderData::_createShader( const Vector<GFXShaderMacro> &macros )
{ {
case Direct3D11: case Direct3D11:
{ {
success = shader->init( mDXVertexShaderName, if (mDXVertexShaderName != String::EmptyString)
mDXPixelShaderName, shader->setShaderStageFile(GFXShaderStage::VERTEX_SHADER, mDXVertexShaderName);
pixver, if (mDXPixelShaderName != String::EmptyString)
shader->setShaderStageFile(GFXShaderStage::PIXEL_SHADER, mDXPixelShaderName);
if (mDXGeometryShaderName != String::EmptyString)
shader->setShaderStageFile(GFXShaderStage::GEOMETRY_SHADER, mDXGeometryShaderName);
success = shader->init( pixver,
macros, macros,
samplers); samplers);
break; break;
@ -247,9 +270,14 @@ GFXShader* ShaderData::_createShader( const Vector<GFXShaderMacro> &macros )
case OpenGL: case OpenGL:
{ {
success = shader->init( mOGLVertexShaderName, if(mOGLVertexShaderName != String::EmptyString)
mOGLPixelShaderName, shader->setShaderStageFile(GFXShaderStage::VERTEX_SHADER, mOGLVertexShaderName);
pixver, if (mOGLPixelShaderName != String::EmptyString)
shader->setShaderStageFile(GFXShaderStage::PIXEL_SHADER, mOGLPixelShaderName);
if (mOGLGeometryShaderName != String::EmptyString)
shader->setShaderStageFile(GFXShaderStage::GEOMETRY_SHADER, mOGLGeometryShaderName);
success = shader->init( pixver,
macros, macros,
samplers); samplers);
break; break;

View file

@ -47,7 +47,7 @@ protected:
/// ///
static Vector<ShaderData*> smAllShaderData; static Vector<ShaderData*> smAllShaderData;
typedef HashTable<String,GFXShaderRef> ShaderCache; typedef HashTable<String, GFXShaderRef> ShaderCache;
ShaderCache mShaders; ShaderCache mShaders;
@ -56,12 +56,12 @@ protected:
F32 mPixVersion; F32 mPixVersion;
StringTableEntry mDXVertexShaderName; StringTableEntry mDXVertexShaderName;
StringTableEntry mDXPixelShaderName; StringTableEntry mDXPixelShaderName;
StringTableEntry mDXGeometryShaderName;
StringTableEntry mOGLVertexShaderName; StringTableEntry mOGLVertexShaderName;
StringTableEntry mOGLPixelShaderName; StringTableEntry mOGLPixelShaderName;
StringTableEntry mOGLGeometryShaderName;
/// A semicolon, tab, or newline delimited string of case /// A semicolon, tab, or newline delimited string of case
/// sensitive defines that are passed to the shader compiler. /// sensitive defines that are passed to the shader compiler.
@ -82,14 +82,14 @@ protected:
/// Helper for converting an array of macros /// Helper for converting an array of macros
/// into a formatted string. /// into a formatted string.
void _stringizeMacros( const Vector<GFXShaderMacro> &macros, void _stringizeMacros(const Vector<GFXShaderMacro>& macros,
String *outString ); String* outString);
/// Creates a new shader returning NULL on error. /// Creates a new shader returning NULL on error.
GFXShader* _createShader( const Vector<GFXShaderMacro> &macros ); GFXShader* _createShader(const Vector<GFXShaderMacro>& macros);
/// @see LightManager::smActivateSignal /// @see LightManager::smActivateSignal
static void _onLMActivate( const char *lm, bool activate ); static void _onLMActivate(const char* lm, bool activate);
enum enum
{ {
@ -99,21 +99,21 @@ protected:
String mSamplerNames[NumTextures]; String mSamplerNames[NumTextures];
bool mRTParams[NumTextures]; bool mRTParams[NumTextures];
bool _checkDefinition(GFXShader *shader); bool _checkDefinition(GFXShader* shader);
public: public:
void setSamplerName(const String &name, int idx) { mSamplerNames[idx] = name; } void setSamplerName(const String& name, int idx) { mSamplerNames[idx] = name; }
String getSamplerName(int idx) const { return mSamplerNames[idx]; } String getSamplerName(int idx) const { return mSamplerNames[idx]; }
bool hasSamplerDef(const String &samplerName, int &pos) const; bool hasSamplerDef(const String& samplerName, int& pos) const;
bool hasRTParamsDef(const int pos) const { return mRTParams[pos]; } bool hasRTParamsDef(const int pos) const { return mRTParams[pos]; }
ShaderData(); ShaderData();
/// Returns an initialized shader instance or NULL /// Returns an initialized shader instance or NULL
/// if the shader failed to be created. /// if the shader failed to be created.
GFXShader* getShader( const Vector<GFXShaderMacro> &macros = Vector<GFXShaderMacro>() ); GFXShader* getShader(const Vector<GFXShaderMacro>& macros = Vector<GFXShaderMacro>());
/// Forces a reinitialization of all the instanced shaders. /// Forces a reinitialization of all the instanced shaders.
void reloadShaders(); void reloadShaders();

View file

@ -493,7 +493,10 @@ GFXShader* ShaderGen::getShader( const MaterialFeatureData &featureData, const G
generateShader( featureData, vertFile, pixFile, &pixVersion, vertexFormat, cacheKey, shaderMacros ); generateShader( featureData, vertFile, pixFile, &pixVersion, vertexFormat, cacheKey, shaderMacros );
GFXShader *shader = GFX->createShader(); GFXShader *shader = GFX->createShader();
if (!shader->init(vertFile, pixFile, pixVersion, shaderMacros, samplers, &mInstancingFormat)) shader->setShaderStageFile(GFXShaderStage::VERTEX_SHADER, vertFile);
shader->setShaderStageFile(GFXShaderStage::PIXEL_SHADER, pixFile);
if (!shader->init(pixVersion, shaderMacros, samplers, &mInstancingFormat))
{ {
delete shader; delete shader;
return NULL; return NULL;