Skip to content

Commit 0107542

Browse files
Added SourceLength to ShaderCreateInfo (close #282)
1 parent e9301ac commit 0107542

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

Graphics/GraphicsEngine/interface/Shader.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,22 @@ struct ShaderCreateInfo
240240
/// HLSL shaders need to be compiled against 4.0 profile or higher.
241241
const void* ByteCode DEFAULT_INITIALIZER(nullptr);
242242

243-
/// Size of the compiled shader bytecode
243+
union
244+
{
245+
/// Length of the source code, when Source is not null.
244246

245-
/// Byte code size (in bytes) must be provided if ByteCode is not null
246-
size_t ByteCodeSize DEFAULT_INITIALIZER(0);
247+
/// When Source is not null and is not a null-terminated string, this member
248+
/// should be used to specify the length of the source code.
249+
/// If SourceLength is zero, the source code string is assumed to be
250+
/// null-terminated.
251+
size_t SourceLength DEFAULT_INITIALIZER(0);
252+
253+
254+
/// Size of the compiled shader byte code, when ByteCode is not null.
255+
256+
/// Byte code size (in bytes) must not be zero if ByteCode is not null.
257+
size_t ByteCodeSize;
258+
};
247259

248260
/// Shader entry point
249261

Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ ShaderD3DBase::ShaderD3DBase(const ShaderCreateInfo& ShaderCI, const ShaderVersi
139139
if (ShaderCI.Source || ShaderCI.FilePath)
140140
{
141141
DEV_CHECK_ERR(ShaderCI.ByteCode == nullptr, "'ByteCode' must be null when shader is created from the source code or a file");
142-
DEV_CHECK_ERR(ShaderCI.ByteCodeSize == 0, "'ByteCodeSize' must be 0 when shader is created from the source code or a file");
143142
DEV_CHECK_ERR(ShaderCI.EntryPoint != nullptr, "Entry point must not be null");
144143

145144
bool UseDXC = false;

Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ ShaderGLImpl::ShaderGLImpl(IReferenceCounters* pRefCounters,
6060
// clang-format on
6161
{
6262
DEV_CHECK_ERR(ShaderCI.ByteCode == nullptr, "'ByteCode' must be null when shader is created from the source code or a file");
63-
DEV_CHECK_ERR(ShaderCI.ByteCodeSize == 0, "'ByteCodeSize' must be 0 when shader is created from the source code or a file");
6463
DEV_CHECK_ERR(ShaderCI.ShaderCompiler == SHADER_COMPILER_DEFAULT, "only default compiler is supported in OpenGL");
6564

6665
const auto& DeviceInfo = pDeviceGL->GetDeviceInfo();
@@ -93,9 +92,9 @@ ShaderGLImpl::ShaderGLImpl(IReferenceCounters* pRefCounters,
9392
}
9493

9594
// Read the source file directly and use it as is
96-
size_t SourceLen = 0;
95+
size_t SourceLen = ShaderCI.SourceLength;
9796
ShaderStrings[0] = ReadShaderSourceFile(ShaderCI.Source, ShaderCI.pShaderSourceStreamFactory, ShaderCI.FilePath, pSourceFileData, SourceLen);
98-
Lengths[0] = static_cast<GLint>(SourceLen);
97+
Lengths[0] = StaticCast<GLint>(SourceLen);
9998
}
10099
else
101100
{

Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ ShaderVkImpl::ShaderVkImpl(IReferenceCounters* pRefCounters,
6060
if (ShaderCI.Source != nullptr || ShaderCI.FilePath != nullptr)
6161
{
6262
DEV_CHECK_ERR(ShaderCI.ByteCode == nullptr, "'ByteCode' must be null when shader is created from source code or a file");
63-
DEV_CHECK_ERR(ShaderCI.ByteCodeSize == 0, "'ByteCodeSize' must be 0 when shader is created from source code or a file");
6463

6564
static constexpr char VulkanDefine[] =
6665
"#ifndef VULKAN\n"
@@ -110,7 +109,7 @@ ShaderVkImpl::ShaderVkImpl(IReferenceCounters* pRefCounters,
110109
RefCntAutoPtr<IDataBlob> pSourceFileData;
111110

112111
const char* ShaderSource = nullptr;
113-
size_t SourceLength = 0;
112+
size_t SourceLength = ShaderCI.SourceLength;
114113
const ShaderMacro* Macros = nullptr;
115114
if (ShaderCI.SourceLanguage == SHADER_SOURCE_LANGUAGE_GLSL_VERBATIM)
116115
{

Graphics/ShaderTools/src/GLSLUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ String BuildGLSLSourceString(const ShaderCreateInfo& ShaderCI,
272272
AppendShaderMacros(GLSLSource, ShaderCI.Macros);
273273

274274
RefCntAutoPtr<IDataBlob> pFileData;
275-
size_t SourceLen = 0;
275+
size_t SourceLen = ShaderCI.SourceLength;
276276

277277
const auto* ShaderSource = ReadShaderSourceFile(ShaderCI.Source, ShaderCI.pShaderSourceStreamFactory, ShaderCI.FilePath, pFileData, SourceLen);
278278

Graphics/ShaderTools/src/GLSLangUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ std::vector<unsigned int> HLSLtoSPIRV(const ShaderCreateInfo& ShaderCI,
359359
Shader.setEnvTargetHlslFunctionality1();
360360

361361
RefCntAutoPtr<IDataBlob> pFileData;
362-
size_t SourceCodeLen = 0;
362+
size_t SourceCodeLen = ShaderCI.SourceLength;
363363

364364
const char* SourceCode = ReadShaderSourceFile(ShaderCI.Source, ShaderCI.pShaderSourceStreamFactory, ShaderCI.FilePath, pFileData, SourceCodeLen);
365365

Graphics/ShaderTools/src/ShaderToolsCommon.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ const char* ReadShaderSourceFile(const char* SourceCode,
109109
RefCntAutoPtr<IDataBlob>& pFileData,
110110
size_t& SourceCodeLen) noexcept(false)
111111
{
112-
SourceCodeLen = 0;
113112
if (SourceCode != nullptr)
114113
{
115114
VERIFY(FilePath == nullptr, "FilePath must be null when SourceCode is not null");
116-
SourceCodeLen = strlen(SourceCode);
115+
if (SourceCodeLen == 0)
116+
SourceCodeLen = strlen(SourceCode);
117117
}
118118
else
119119
{
@@ -147,9 +147,11 @@ const char* ReadShaderSourceFile(const char* SourceCode,
147147

148148
void AppendShaderSourceCode(std::string& Source, const ShaderCreateInfo& ShaderCI) noexcept(false)
149149
{
150+
VERIFY_EXPR(ShaderCI.ByteCode == nullptr);
151+
150152
RefCntAutoPtr<IDataBlob> pFileData;
151153

152-
size_t SourceCodeLen = 0;
154+
size_t SourceCodeLen = ShaderCI.SourceLength;
153155

154156
const auto* SourceCode =
155157
ReadShaderSourceFile(ShaderCI.Source, ShaderCI.pShaderSourceStreamFactory,

0 commit comments

Comments
 (0)