summaryrefslogtreecommitdiffstats
path: root/Graphics
diff options
context:
space:
mode:
authorassiduous <assiduous@diligentgraphics.com>2020-05-17 17:08:00 +0000
committerassiduous <assiduous@diligentgraphics.com>2020-05-17 17:08:00 +0000
commit6c0ed372bfb1f9094422c4a46f0c6baef65c4898 (patch)
treec831629824c2844bc96d387dbc054bce339da2c8 /Graphics
parentMathLib test: added matrix equality tests (to cover https://github.com/Dilige... (diff)
downloadDiligentCore-6c0ed372bfb1f9094422c4a46f0c6baef65c4898.tar.gz
DiligentCore-6c0ed372bfb1f9094422c4a46f0c6baef65c4898.zip
Unified shader type defines for all backends
Diffstat (limited to 'Graphics')
-rw-r--r--Graphics/GLSLTools/src/GLSLSourceBuilder.cpp48
-rw-r--r--Graphics/GLSLTools/src/SPIRVUtils.cpp19
-rw-r--r--Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp37
3 files changed, 81 insertions, 23 deletions
diff --git a/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp b/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp
index abb63d4f..e6824641 100644
--- a/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp
+++ b/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp
@@ -37,6 +37,38 @@
namespace Diligent
{
+const char* GetShaderTypeDefines(SHADER_TYPE Type)
+{
+ switch (Type)
+ {
+ case SHADER_TYPE_VERTEX:
+ return "#define VERTEX_SHADER 1\n";
+
+ case SHADER_TYPE_PIXEL:
+ return "#define FRAGMENT_SHADER 1\n"
+ "#define PIXEL_SHADER 1\n";
+
+ case SHADER_TYPE_GEOMETRY:
+ return "#define GEOMETRY_SHADER 1\n";
+
+ case SHADER_TYPE_HULL:
+ return "#define TESS_CONTROL_SHADER 1\n"
+ "#define HULL_SHADER 1\n";
+
+ case SHADER_TYPE_DOMAIN:
+ return "#define TESS_EVALUATION_SHADER 1\n"
+ "#define DOMAIN_SHADER 1\n";
+ break;
+
+ case SHADER_TYPE_COMPUTE:
+ return "#define COMPUTE_SHADER 1\n";
+
+ default:
+ UNEXPECTED("Unexpected shader type");
+ return nullptr;
+ }
+}
+
String BuildGLSLSourceString(const ShaderCreateInfo& CreationAttribs,
const DeviceCaps& deviceCaps,
TargetGLSLCompiler TargetCompiler,
@@ -252,20 +284,8 @@ String BuildGLSLSourceString(const ShaderCreateInfo& CreationAttribs,
"#define gl_InstanceID gl_InstanceIndex\n");
}
- const Char* ShaderTypeDefine = nullptr;
- switch (ShaderType)
- {
- // clang-format off
- case SHADER_TYPE_VERTEX: ShaderTypeDefine = "#define VERTEX_SHADER\n"; break;
- case SHADER_TYPE_PIXEL: ShaderTypeDefine = "#define FRAGMENT_SHADER\n"; break;
- case SHADER_TYPE_GEOMETRY: ShaderTypeDefine = "#define GEOMETRY_SHADER\n"; break;
- case SHADER_TYPE_HULL: ShaderTypeDefine = "#define TESS_CONTROL_SHADER\n"; break;
- case SHADER_TYPE_DOMAIN: ShaderTypeDefine = "#define TESS_EVALUATION_SHADER\n"; break;
- case SHADER_TYPE_COMPUTE: ShaderTypeDefine = "#define COMPUTE_SHADER\n"; break;
- default: UNEXPECTED("Shader type is not specified");
- // clang-format on
- }
- GLSLSource += ShaderTypeDefine;
+ if (const auto* ShaderTypeDefine = GetShaderTypeDefines(ShaderType))
+ GLSLSource += ShaderTypeDefine;
if (ExtraDefinitions != nullptr)
{
diff --git a/Graphics/GLSLTools/src/SPIRVUtils.cpp b/Graphics/GLSLTools/src/SPIRVUtils.cpp
index e5009884..4da444e4 100644
--- a/Graphics/GLSLTools/src/SPIRVUtils.cpp
+++ b/Graphics/GLSLTools/src/SPIRVUtils.cpp
@@ -44,14 +44,19 @@
#include "spirv-tools/optimizer.hpp"
+// clang-format off
static const char g_HLSLDefinitions[] =
- {
+{
#include "../../GraphicsEngineD3DBase/include/HLSLDefinitions_inc.fxh"
};
+// clang-format on
namespace Diligent
{
+// Implemented in GLSLSourceBuilder.cpp
+const char* GetShaderTypeDefines(SHADER_TYPE Type);
+
void InitializeGlslang()
{
glslang::InitializeProcess();
@@ -467,7 +472,10 @@ std::vector<unsigned int> HLSLtoSPIRV(const ShaderCreateInfo& Attribs, IDataBlob
SourceCodeLen = static_cast<int>(pFileData->GetSize());
}
- std::string Defines;
+ std::string Defines = g_HLSLDefinitions;
+ if (const auto* ShaderTypeDefine = GetShaderTypeDefines(Attribs.Desc.ShaderType))
+ Defines += ShaderTypeDefine;
+
if (Attribs.Macros != nullptr)
{
Defines = g_HLSLDefinitions;
@@ -482,12 +490,9 @@ std::vector<unsigned int> HLSLtoSPIRV(const ShaderCreateInfo& Attribs, IDataBlob
Defines += "\n";
++pMacro;
}
- Shader.setPreamble(Defines.c_str());
- }
- else
- {
- Shader.setPreamble(g_HLSLDefinitions);
}
+ Shader.setPreamble(Defines.c_str());
+
const char* ShaderStrings[] = {SourceCode};
const int ShaderStringLenghts[] = {SourceCodeLen};
const char* Names[] = {Attribs.FilePath != nullptr ? Attribs.FilePath : ""};
diff --git a/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp b/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp
index e880fe0f..3962a8e4 100644
--- a/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp
+++ b/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp
@@ -173,16 +173,49 @@ ShaderD3DBase::ShaderD3DBase(const ShaderCreateInfo& ShaderCI, const char* Shade
const D3D_SHADER_MACRO* pDefines = nullptr;
std::vector<D3D_SHADER_MACRO> D3DMacros;
+ switch (ShaderCI.Desc.ShaderType)
+ {
+ case SHADER_TYPE_VERTEX:
+ D3DMacros.push_back({"VERTEX_SHADER", "1"});
+ break;
+
+ case SHADER_TYPE_PIXEL:
+ D3DMacros.push_back({"FRAGMENT_SHADER", "1"});
+ D3DMacros.push_back({"PIXEL_SHADER", "1"});
+ break;
+
+ case SHADER_TYPE_GEOMETRY:
+ D3DMacros.push_back({"GEOMETRY_SHADER", "1"});
+ break;
+
+ case SHADER_TYPE_HULL:
+ D3DMacros.push_back({"TESS_CONTROL_SHADER", "1"});
+ D3DMacros.push_back({"HULL_SHADER", "1"});
+ break;
+
+ case SHADER_TYPE_DOMAIN:
+ D3DMacros.push_back({"TESS_EVALUATION_SHADER", "1"});
+ D3DMacros.push_back({"DOMAIN_SHADER", "1"});
+ break;
+
+ case SHADER_TYPE_COMPUTE:
+ D3DMacros.push_back({"COMPUTE_SHADER", "1"});
+ break;
+
+ default: UNEXPECTED("Unexpected shader type");
+ }
+
if (ShaderCI.Macros)
{
for (auto* pCurrMacro = ShaderCI.Macros; pCurrMacro->Name && pCurrMacro->Definition; ++pCurrMacro)
{
D3DMacros.push_back({pCurrMacro->Name, pCurrMacro->Definition});
}
- D3DMacros.push_back({nullptr, nullptr});
- pDefines = D3DMacros.data();
}
+ D3DMacros.push_back({nullptr, nullptr});
+ pDefines = D3DMacros.data();
+
DEV_CHECK_ERR(ShaderCI.EntryPoint != nullptr, "Entry point must not be null");
CComPtr<ID3DBlob> errors;
auto hr = CompileShader(ShaderSource.c_str(), ShaderCI.EntryPoint, pDefines, ShaderCI.pShaderSourceStreamFactory, strShaderProfile.c_str(), &m_pShaderByteCode, &errors);