Skip to content

Commit 64f9f3c

Browse files
Fixed shader source language detection for shaders unpacked from state cache in OpenGL
1 parent ce3c521 commit 64f9f3c

File tree

9 files changed

+250
-32
lines changed

9 files changed

+250
-32
lines changed

Graphics/GraphicsEngineOpenGL/include/ShaderGLImpl.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ShaderGLImpl final : public ShaderBase<EngineGLImplTraits>
7979
}
8080

8181
private:
82-
const SHADER_SOURCE_LANGUAGE m_SourceLanguage;
82+
SHADER_SOURCE_LANGUAGE m_SourceLanguage = SHADER_SOURCE_LANGUAGE_DEFAULT;
8383
std::string m_GLSLSourceString;
8484
GLObjectWrappers::GLShaderObj m_GLShaderObj;
8585
std::shared_ptr<const ShaderResourcesGL> m_pShaderResources;

Graphics/GraphicsEngineOpenGL/src/RenderDeviceGLImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ IMPLEMENT_QUERY_INTERFACE(RenderDeviceGLImpl, IID_RenderDeviceGL, TRenderDeviceB
279279

280280
void RenderDeviceGLImpl::InitTexRegionRender()
281281
{
282-
m_pTexRegionRender.reset(new TexRegionRender(this));
282+
m_pTexRegionRender = std::make_unique<TexRegionRender>(this);
283283
}
284284

285285
void RenderDeviceGLImpl::CreateBuffer(const BufferDesc& BuffDesc, const BufferData* pBuffData, IBuffer** ppBuffer, bool bIsDeviceInternal)

Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ ShaderGLImpl::ShaderGLImpl(IReferenceCounters* pRefCounters,
8181
// Read the source file directly and use it as is
8282
SourceData = ReadShaderSourceFile(ShaderCI);
8383
m_GLSLSourceString = std::string{SourceData.Source, SourceData.SourceLength};
84+
85+
auto SourceLang = ParseShaderSourceLanguageDefinition(m_GLSLSourceString);
86+
if (SourceLang != SHADER_SOURCE_LANGUAGE_DEFAULT)
87+
m_SourceLanguage = SourceLang;
8488
}
8589
else
8690
{
@@ -91,6 +95,8 @@ ShaderGLImpl::ShaderGLImpl(IReferenceCounters* pRefCounters,
9195
m_GLSLSourceString = BuildGLSLSourceString(
9296
ShaderCI, DeviceInfo, AdapterInfo, TargetGLSLCompiler::driver,
9397
(DeviceInfo.NDC.MinZ >= 0 ? NDCDefine : nullptr));
98+
99+
AppendShaderSourceLanguageDefinition(m_GLSLSourceString, ShaderCI.SourceLanguage);
94100
}
95101

96102
if (pDeviceGL == nullptr)
@@ -179,7 +185,7 @@ ShaderGLImpl::ShaderGLImpl(IReferenceCounters* pRefCounters,
179185
VERIFY_EXPR(pImmediateCtx);
180186
auto& GLState = pImmediateCtx->GetContextState();
181187

182-
std::unique_ptr<ShaderResourcesGL> pResources{new ShaderResourcesGL{}};
188+
auto pResources = std::make_unique<ShaderResourcesGL>();
183189
pResources->LoadUniforms(m_Desc.ShaderType,
184190
m_SourceLanguage == SHADER_SOURCE_LANGUAGE_HLSL ?
185191
PIPELINE_RESOURCE_FLAG_NONE : // Reflect samplers as separate for consistency with other backends

Graphics/ShaderTools/include/ShaderToolsCommon.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ void AppendShaderMacros(std::string& Source, const ShaderMacro* Macros);
7070
void AppendShaderTypeDefinitions(std::string& Source, SHADER_TYPE Type);
7171

7272

73+
/// Appends a special comment that contains the shader source language definition.
74+
/// For example, for HLSL:
75+
///
76+
/// /*$SHADER_SOURCE_LANGUAGE=1*/
77+
void AppendShaderSourceLanguageDefinition(std::string& Source, SHADER_SOURCE_LANGUAGE Language);
78+
79+
80+
/// Parses the shader source language definition comment and returns the result.
81+
/// If the comment is not present or can't be parsed, returns SHADER_SOURCE_LANGUAGE_DEFAULT.
82+
SHADER_SOURCE_LANGUAGE ParseShaderSourceLanguageDefinition(const std::string& Source);
83+
84+
7385
struct ShaderSourceFileData
7486
{
7587
RefCntAutoPtr<IDataBlob> pFileData;

Graphics/ShaderTools/src/ShaderToolsCommon.cpp

+96-14
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ namespace Diligent
4040
namespace
4141
{
4242

43-
const ShaderMacro VSMacros[] = {{"VERTEX_SHADER", "1"}, {}};
44-
const ShaderMacro PSMacros[] = {{"FRAGMENT_SHADER", "1"}, {"PIXEL_SHADER", "1"}, {}};
45-
const ShaderMacro GSMacros[] = {{"GEOMETRY_SHADER", "1"}, {}};
46-
const ShaderMacro HSMacros[] = {{"TESS_CONTROL_SHADER", "1"}, {"HULL_SHADER", "1"}, {}};
47-
const ShaderMacro DSMacros[] = {{"TESS_EVALUATION_SHADER", "1"}, {"DOMAIN_SHADER", "1"}, {}};
48-
const ShaderMacro CSMacros[] = {{"COMPUTE_SHADER", "1"}, {}};
49-
const ShaderMacro ASMacros[] = {{"TASK_SHADER", "1"}, {"AMPLIFICATION_SHADER", "1"}, {}};
50-
const ShaderMacro MSMacros[] = {{"MESH_SHADER", "1"}, {}};
51-
const ShaderMacro RGMacros[] = {{"RAY_GEN_SHADER", "1"}, {}};
52-
const ShaderMacro RMMacros[] = {{"RAY_MISS_SHADER", "1"}, {}};
53-
const ShaderMacro RCHMacros[] = {{"RAY_CLOSEST_HIT_SHADER", "1"}, {}};
54-
const ShaderMacro RAHMacros[] = {{"RAY_ANY_HIT_SHADER", "1"}, {}};
55-
const ShaderMacro RIMacros[] = {{"RAY_INTERSECTION_SHADER", "1"}, {}};
56-
const ShaderMacro RCMacros[] = {{"RAY_CALLABLE_SHADER", "1"}, {}};
43+
constexpr ShaderMacro VSMacros[] = {{"VERTEX_SHADER", "1"}, {}};
44+
constexpr ShaderMacro PSMacros[] = {{"FRAGMENT_SHADER", "1"}, {"PIXEL_SHADER", "1"}, {}};
45+
constexpr ShaderMacro GSMacros[] = {{"GEOMETRY_SHADER", "1"}, {}};
46+
constexpr ShaderMacro HSMacros[] = {{"TESS_CONTROL_SHADER", "1"}, {"HULL_SHADER", "1"}, {}};
47+
constexpr ShaderMacro DSMacros[] = {{"TESS_EVALUATION_SHADER", "1"}, {"DOMAIN_SHADER", "1"}, {}};
48+
constexpr ShaderMacro CSMacros[] = {{"COMPUTE_SHADER", "1"}, {}};
49+
constexpr ShaderMacro ASMacros[] = {{"TASK_SHADER", "1"}, {"AMPLIFICATION_SHADER", "1"}, {}};
50+
constexpr ShaderMacro MSMacros[] = {{"MESH_SHADER", "1"}, {}};
51+
constexpr ShaderMacro RGMacros[] = {{"RAY_GEN_SHADER", "1"}, {}};
52+
constexpr ShaderMacro RMMacros[] = {{"RAY_MISS_SHADER", "1"}, {}};
53+
constexpr ShaderMacro RCHMacros[] = {{"RAY_CLOSEST_HIT_SHADER", "1"}, {}};
54+
constexpr ShaderMacro RAHMacros[] = {{"RAY_ANY_HIT_SHADER", "1"}, {}};
55+
constexpr ShaderMacro RIMacros[] = {{"RAY_INTERSECTION_SHADER", "1"}, {}};
56+
constexpr ShaderMacro RCMacros[] = {{"RAY_CALLABLE_SHADER", "1"}, {}};
5757

5858
} // namespace
5959

@@ -108,6 +108,88 @@ void AppendShaderTypeDefinitions(std::string& Source, SHADER_TYPE Type)
108108
}
109109

110110

111+
static const std::string ShaderSourceLanguageKey = "$SHADER_SOURCE_LANGUAGE";
112+
113+
void AppendShaderSourceLanguageDefinition(std::string& Source, SHADER_SOURCE_LANGUAGE Language)
114+
{
115+
Source += "/*";
116+
Source += ShaderSourceLanguageKey;
117+
Source += '=';
118+
Source += std::to_string(static_cast<Uint32>(Language));
119+
Source += "*/";
120+
}
121+
122+
SHADER_SOURCE_LANGUAGE ParseShaderSourceLanguageDefinition(const std::string& Source)
123+
{
124+
// /*$SHADER_SOURCE_LANGUAGE=1*/
125+
// ^
126+
auto it = Source.end();
127+
if (it == Source.begin())
128+
return SHADER_SOURCE_LANGUAGE_DEFAULT;
129+
130+
--it;
131+
// /*$SHADER_SOURCE_LANGUAGE=1*/
132+
// ^
133+
if (it == Source.begin() || *it != '/')
134+
return SHADER_SOURCE_LANGUAGE_DEFAULT;
135+
136+
--it;
137+
// /*$SHADER_SOURCE_LANGUAGE=1*/
138+
// ^
139+
if (it == Source.begin() || *it != '*')
140+
return SHADER_SOURCE_LANGUAGE_DEFAULT;
141+
142+
--it;
143+
while (true)
144+
{
145+
if (it == Source.begin())
146+
return SHADER_SOURCE_LANGUAGE_DEFAULT;
147+
148+
if (*it == '*')
149+
{
150+
// /*$SHADER_SOURCE_LANGUAGE=1*/
151+
// ^
152+
--it;
153+
if (*it == '/')
154+
break;
155+
}
156+
else
157+
{
158+
--it;
159+
}
160+
}
161+
162+
// /*$SHADER_SOURCE_LANGUAGE=1*/
163+
// ^
164+
const auto KeyPos = Source.find(ShaderSourceLanguageKey, it - Source.begin() + 2);
165+
if (KeyPos == std::string::npos)
166+
return SHADER_SOURCE_LANGUAGE_DEFAULT;
167+
168+
it = Source.begin() + KeyPos + ShaderSourceLanguageKey.length();
169+
while (it != Source.end() && *it == ' ')
170+
++it;
171+
172+
// /*$SHADER_SOURCE_LANGUAGE=1*/
173+
// ^
174+
if (it == Source.end() || *it != '=')
175+
return SHADER_SOURCE_LANGUAGE_DEFAULT;
176+
177+
++it;
178+
while (it != Source.end() && *it == ' ')
179+
++it;
180+
181+
if (it == Source.end() || !IsNum(*it))
182+
return SHADER_SOURCE_LANGUAGE_DEFAULT;
183+
184+
Uint32 Lang = 0;
185+
for (; it != Source.end() && IsNum(*it); ++it)
186+
Lang = Lang * 10 + (*it - '0');
187+
188+
return Lang >= 0 && Lang < SHADER_SOURCE_LANGUAGE_COUNT ?
189+
static_cast<SHADER_SOURCE_LANGUAGE>(Lang) :
190+
SHADER_SOURCE_LANGUAGE_DEFAULT;
191+
}
192+
111193
ShaderSourceFileData ReadShaderSourceFile(const char* SourceCode,
112194
size_t SourceLength,
113195
IShaderSourceInputStreamFactory* pShaderSourceStreamFactory,
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#include "GraphicsCommon.h"
22

3+
Texture2D g_Tex2D;
4+
SamplerState g_Tex2D_sampler;
5+
36
float4 main(in PSInput PSIn) : SV_Target
47
{
5-
return float4(PSIn.Color.rgb, 1.0);
8+
float2 UV = float2(0.5, 0.5);
9+
return float4(PSIn.Color.rgb, 1.0) * g_Tex2D.Sample(g_Tex2D_sampler, UV.xy);
610
// NB: no new line at the end of file!
711
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "GraphicsCommon.h"
22

3+
Texture2D g_Tex2D;
4+
SamplerState g_Tex2D_sampler;
5+
36
float4 main(in PSInput PSIn) : SV_Target
47
{
5-
return float4(PSIn.Color.bgr, 1.0);
8+
float2 UV = float2(0.5, 0.5);
9+
return float4(PSIn.Color.bgr, 1.0) * g_Tex2D.Sample(g_Tex2D_sampler, UV.xy);
610
}

0 commit comments

Comments
 (0)