summaryrefslogtreecommitdiffstats
path: root/Graphics
diff options
context:
space:
mode:
authorEgor Yusov <egor.yusov@gmail.com>2019-10-12 05:35:10 +0000
committerEgor Yusov <egor.yusov@gmail.com>2019-10-12 05:35:10 +0000
commit5ccaa04e65415f7b892f998e75683df1f444bf3a (patch)
tree1852382a6cb0e1aaa15db86e1cf4c0bed4a56045 /Graphics
parentHLSL->GLSL converter: added bindings for image uniforms (diff)
downloadDiligentCore-5ccaa04e65415f7b892f998e75683df1f444bf3a.tar.gz
DiligentCore-5ccaa04e65415f7b892f998e75683df1f444bf3a.zip
HLSL->GLSL conerter: allowed providing null HLSL source code pointer in which case the source will be loaded from the input stream factory
Diffstat (limited to 'Graphics')
-rw-r--r--Graphics/HLSL2GLSLConverterLib/include/HLSL2GLSLConverterImpl.h63
-rw-r--r--Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp33
2 files changed, 92 insertions, 4 deletions
diff --git a/Graphics/HLSL2GLSLConverterLib/include/HLSL2GLSLConverterImpl.h b/Graphics/HLSL2GLSLConverterLib/include/HLSL2GLSLConverterImpl.h
index 6da944b1..7dfd0c19 100644
--- a/Graphics/HLSL2GLSLConverterLib/include/HLSL2GLSLConverterImpl.h
+++ b/Graphics/HLSL2GLSLConverterLib/include/HLSL2GLSLConverterImpl.h
@@ -83,25 +83,70 @@ namespace Diligent
namespace Diligent
{
+ /// HLSL to GLSL shader source code converter implementation
class HLSL2GLSLConverterImpl
{
public:
static const HLSL2GLSLConverterImpl& GetInstance();
+
+ /// Conversion attributes
struct ConversionAttribs
{
+ /// Shader source input stream factory. Used to load shader source when HLSLSource is null as
+ /// well as to load shader includes.
IShaderSourceInputStreamFactory* pSourceStreamFactory = nullptr;
+
+ /// Optional pointer to a conversion stream.
IHLSL2GLSLConversionStream** ppConversionStream = nullptr;
+
+ /// HLSL source code. Can be null, in which case the source code will be loaded from the
+ /// input stream factory (that must not be null in this case) using InputFileName.
const Char* HLSLSource = nullptr;
+
+ /// Number of symbols in HLSLSource string. Ignored if HLSLSource is null.
size_t NumSymbols = 0;
+
+ /// Shader entry point.
const Char* EntryPoint = nullptr;
+
+ /// Shader type. See Diligent::SHADER_TYPE.
SHADER_TYPE ShaderType = SHADER_TYPE_UNKNOWN;
+
+ /// Whether to include GLSL definitions supporting HLSL->GLSL conversion.
bool IncludeDefinitions = false;
+
+ /// Input file name. If HLSLSource is not null, this name will only be used for
+ /// information purposes. If HLSLSource is null, the name will be used to load
+ /// shader source from the input stream factory.
const Char* InputFileName = nullptr;
+
+ /// Combined texture sampler suffix.
const Char* SamplerSuffix = "_sampler";
+
+ /// Whether to use in-out location qualifiers.
+ /// This requires separate shader objects extension:
+ /// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_separate_shader_objects.txt
bool UseInOutLocationQualifiers = true;
};
+ /// Converts HLSL source to GLSL
+
+ /// \param [in] Attribs - Conversion attributes.
+ /// \return Converted GLSL source code.
String Convert(ConversionAttribs& Attribs)const;
+
+ /// Creates a conversion stream
+
+ /// \param [in] InputFileName - Input file name. If HLSLSource is null, this name will be
+ /// used to load shader source code from the input stream factory.
+ /// If HLSLSource is not null, the name will only be used for information
+ /// purposes.
+ /// \param [in] pSourceStreamFactory - Input stream factory that is used to load shader includes as well
+ /// as to load the shader source code if HLSLSource is null.
+ /// \param [in] HLSLSource - HLSL source code. If this parameter is null, the source will be loaded from
+ /// the input stream factory using InputFileName.
+ /// \param [in] NumSymbols - Number of symbols in the HLSLSource string
+ /// \prarm [out] ppStream - Memory address where pointer to the created stream will be written
void CreateStream(const Char* InputFileName,
IShaderSourceInputStreamFactory* pSourceStreamFactory,
const Char* HLSLSource,
@@ -213,7 +258,23 @@ namespace Diligent
class ConversionStream : public ObjectBase<IHLSL2GLSLConversionStream>
{
public:
- typedef ObjectBase<IHLSL2GLSLConversionStream> TBase;
+ using TBase = ObjectBase<IHLSL2GLSLConversionStream>;
+
+ /// Conversion stream constructor.
+
+ /// \param [in] pRefCounters - Pointer to a reference counters object
+ /// \param [in] Converter - Reference to HLSL2GLSLConverterImpl class instance
+ /// \param [in] InputFileName - Input file name. If HLSLSource is null, this name will be
+ /// used to load shader source code from the input stream factory.
+ /// If HLSLSource is not null, the name will only be used for information
+ /// purposes.
+ /// \param [in] pInputStreamFactory - Input stream factory that is used to load shader includes as well
+ /// as to load the shader source code if HLSLSource is null.
+ /// \param [in] HLSLSource - HLSL source code. If this parameter is null, the source will be loaded from
+ /// the input stream factory using InputFileName.
+ /// \param [in] NumSymbols - Number of symbols in the HLSLSource string
+ /// \param [in] bPreserveTokens - Whether to preserve original tokens. This must be set to true if the stream
+ /// will be used for multiple conversions.
ConversionStream(IReferenceCounters* pRefCounters,
const HLSL2GLSLConverterImpl& Converter,
const char* InputFileName,
diff --git a/Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp b/Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp
index 346cfbcf..db4fe2ae 100644
--- a/Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp
+++ b/Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp
@@ -4407,6 +4407,26 @@ HLSL2GLSLConverterImpl::ConversionStream::ConversionStream(IReferenceCounters*
m_Converter (Converter),
m_InputFileName (InputFileName != nullptr ? InputFileName : "<Unknown>")
{
+ RefCntAutoPtr<IDataBlob> pFileData;
+ if (HLSLSource == nullptr)
+ {
+ if (InputFileName == nullptr)
+ LOG_ERROR_AND_THROW("Input file name must not be null when HLSL source code is not provided");
+
+ if (pInputStreamFactory == nullptr)
+ LOG_ERROR_AND_THROW("Input stream factory must not be null when HLSL source code is not provided");
+
+ RefCntAutoPtr<IFileStream> pSourceStream;
+ pInputStreamFactory->CreateInputStream(InputFileName, &pSourceStream);
+ if (pSourceStream == nullptr)
+ LOG_ERROR_AND_THROW("Failed to open shader source file ", InputFileName);
+
+ pFileData = MakeNewRCObj<DataBlobImpl>()(0);
+ pSourceStream->Read(pFileData);
+ HLSLSource = reinterpret_cast<char*>(pFileData->GetDataPtr());
+ NumSymbols = pFileData->GetSize();
+ }
+
String Source(HLSLSource, NumSymbols);
InsertIncludes( Source, pInputStreamFactory );
@@ -4419,8 +4439,15 @@ String HLSL2GLSLConverterImpl::Convert(ConversionAttribs& Attribs)const
{
if (Attribs.ppConversionStream == nullptr)
{
- ConversionStream Stream(nullptr, *this, Attribs.InputFileName, Attribs.pSourceStreamFactory, Attribs.HLSLSource, Attribs.NumSymbols, false);
- return Stream.Convert(Attribs.EntryPoint, Attribs.ShaderType, Attribs.IncludeDefinitions, Attribs.SamplerSuffix, Attribs.UseInOutLocationQualifiers);
+ try
+ {
+ ConversionStream Stream(nullptr, *this, Attribs.InputFileName, Attribs.pSourceStreamFactory, Attribs.HLSLSource, Attribs.NumSymbols, false);
+ return Stream.Convert(Attribs.EntryPoint, Attribs.ShaderType, Attribs.IncludeDefinitions, Attribs.SamplerSuffix, Attribs.UseInOutLocationQualifiers);
+ }
+ catch(std::runtime_error&)
+ {
+ return "";
+ }
}
else
{
@@ -4428,7 +4455,7 @@ String HLSL2GLSLConverterImpl::Convert(ConversionAttribs& Attribs)const
if (*Attribs.ppConversionStream != nullptr)
{
pStream = ValidatedCast<ConversionStream>(*Attribs.ppConversionStream);
- const auto &FileNameFromStream = pStream->GetInputFileName();
+ const auto& FileNameFromStream = pStream->GetInputFileName();
if (FileNameFromStream != Attribs.InputFileName)
{
LOG_WARNING_MESSAGE("Input stream was initialized for input file \"", FileNameFromStream, "\" that does not match the name of the file to be converted \"", Attribs.InputFileName, "\". New stream will be created");