Use the Blob in object, not pointer
It reduces the chance of memory leak.
This commit is contained in:
Родитель
a8580b8166
Коммит
e82b303f9a
|
@ -12,7 +12,7 @@ foreach(target
|
|||
"spirv-tools-pkg-config" "spirv-tools-shared-pkg-config"
|
||||
"spirv-tools-build-version" "spirv-tools-header-DebugInfo"
|
||||
"SPIRV-Tools-link" "SPIRV-Tools-shared"
|
||||
"spirv-tools-header-OpenCLDebugInfo100" "spirv-tools-vimsyntax" "spv-tools-cldi100" "spv-tools-debuginfo" "spv-tools-spv-amd-gs"
|
||||
"spirv-tools-header-OpenCLDebugInfo100" "spirv-tools-vimsyntax" "spv-tools-cldi100" "spv-tools-clspvreflection" "spv-tools-debuginfo" "spv-tools-spv-amd-gs"
|
||||
"spv-tools-spv-amd-sb" "spv-tools-spv-amd-sevp" "spv-tools-spv-amd-stm")
|
||||
get_target_property(vsFolder ${target} FOLDER)
|
||||
if(NOT vsFolder)
|
||||
|
|
|
@ -101,15 +101,26 @@ namespace ShaderConductor
|
|||
class SC_API Blob
|
||||
{
|
||||
public:
|
||||
virtual ~Blob();
|
||||
Blob() noexcept;
|
||||
Blob(const void* data, uint32_t size);
|
||||
Blob(const Blob& other);
|
||||
Blob(Blob&& other) noexcept;
|
||||
~Blob() noexcept;
|
||||
|
||||
virtual const void* Data() const = 0;
|
||||
virtual uint32_t Size() const = 0;
|
||||
Blob& operator=(const Blob& other);
|
||||
Blob& operator=(Blob&& other) noexcept;
|
||||
|
||||
void Reset();
|
||||
void Reset(const void* data, uint32_t size);
|
||||
|
||||
const void* Data() const noexcept;
|
||||
uint32_t Size() const noexcept;
|
||||
|
||||
private:
|
||||
class BlobImpl;
|
||||
BlobImpl* m_impl = nullptr;
|
||||
};
|
||||
|
||||
SC_API Blob* CreateBlob(const void* data, uint32_t size);
|
||||
SC_API void DestroyBlob(Blob* blob);
|
||||
|
||||
class SC_API Compiler
|
||||
{
|
||||
public:
|
||||
|
@ -153,7 +164,7 @@ namespace ShaderConductor
|
|||
ShaderStage stage;
|
||||
const MacroDefine* defines;
|
||||
uint32_t numDefines;
|
||||
std::function<Blob*(const char* includeName)> loadIncludeCallback;
|
||||
std::function<Blob(const char* includeName)> loadIncludeCallback;
|
||||
};
|
||||
|
||||
struct Options
|
||||
|
@ -181,7 +192,7 @@ namespace ShaderConductor
|
|||
|
||||
struct ReflectionDesc
|
||||
{
|
||||
char* name; // Name of the resource
|
||||
char name[256]; // Name of the resource
|
||||
ShaderResourceType type; // Type of resource (e.g. texture, cbuffer, etc.)
|
||||
uint32_t bufferBindPoint; // Buffer's starting bind point
|
||||
uint32_t bindPoint; // Starting bind point
|
||||
|
@ -190,17 +201,17 @@ namespace ShaderConductor
|
|||
|
||||
struct ReflectionResultDesc
|
||||
{
|
||||
Blob* descs = nullptr; // The underneath type is ReflectionDesc
|
||||
Blob descs; // The underneath type is ReflectionDesc
|
||||
uint32_t descCount = 0;
|
||||
uint32_t instructionCount = 0;
|
||||
};
|
||||
|
||||
struct ResultDesc
|
||||
{
|
||||
Blob* target;
|
||||
Blob target;
|
||||
bool isText;
|
||||
|
||||
Blob* errorWarningMsg;
|
||||
Blob errorWarningMsg;
|
||||
bool hasError;
|
||||
|
||||
ReflectionResultDesc reflection;
|
||||
|
@ -216,7 +227,7 @@ namespace ShaderConductor
|
|||
struct ModuleDesc
|
||||
{
|
||||
const char* name;
|
||||
Blob* target;
|
||||
Blob target;
|
||||
};
|
||||
|
||||
struct LinkDesc
|
||||
|
@ -237,8 +248,6 @@ namespace ShaderConductor
|
|||
// Currently only Dxil on Windows supports linking
|
||||
static bool LinkSupport();
|
||||
static ResultDesc Link(const LinkDesc& modules, const Options& options, const TargetDesc& target);
|
||||
|
||||
static void DestroyResultDesc(const ResultDesc& result);
|
||||
};
|
||||
} // namespace ShaderConductor
|
||||
|
||||
|
|
|
@ -201,7 +201,7 @@ namespace
|
|||
class ScIncludeHandler : public IDxcIncludeHandler
|
||||
{
|
||||
public:
|
||||
explicit ScIncludeHandler(std::function<Blob*(const char* includeName)> loadCallback) : m_loadCallback(std::move(loadCallback))
|
||||
explicit ScIncludeHandler(std::function<Blob(const char* includeName)> loadCallback) : m_loadCallback(std::move(loadCallback))
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -218,12 +218,10 @@ namespace
|
|||
return E_FAIL;
|
||||
}
|
||||
|
||||
auto blobDeleter = [](Blob* blob) { DestroyBlob(blob); };
|
||||
|
||||
std::unique_ptr<Blob, decltype(blobDeleter)> source(nullptr, blobDeleter);
|
||||
Blob source;
|
||||
try
|
||||
{
|
||||
source.reset(m_loadCallback(utf8FileName.c_str()));
|
||||
source = m_loadCallback(utf8FileName.c_str());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
|
@ -231,7 +229,7 @@ namespace
|
|||
}
|
||||
|
||||
*includeSource = nullptr;
|
||||
return Dxcompiler::Instance().Library()->CreateBlobWithEncodingOnHeapCopy(source->Data(), source->Size(), CP_UTF8,
|
||||
return Dxcompiler::Instance().Library()->CreateBlobWithEncodingOnHeapCopy(source.Data(), source.Size(), CP_UTF8,
|
||||
reinterpret_cast<IDxcBlobEncoding**>(includeSource));
|
||||
}
|
||||
|
||||
|
@ -273,12 +271,12 @@ namespace
|
|||
}
|
||||
|
||||
private:
|
||||
std::function<Blob*(const char* includeName)> m_loadCallback;
|
||||
std::function<Blob(const char* includeName)> m_loadCallback;
|
||||
|
||||
std::atomic<ULONG> m_ref = 0;
|
||||
};
|
||||
|
||||
Blob* DefaultLoadCallback(const char* includeName)
|
||||
Blob DefaultLoadCallback(const char* includeName)
|
||||
{
|
||||
std::vector<char> ret;
|
||||
std::ifstream includeFile(includeName, std::ios_base::in);
|
||||
|
@ -294,45 +292,22 @@ namespace
|
|||
{
|
||||
throw std::runtime_error(std::string("COULDN'T load included file ") + includeName + ".");
|
||||
}
|
||||
return CreateBlob(ret.data(), static_cast<uint32_t>(ret.size()));
|
||||
return Blob(ret.data(), static_cast<uint32_t>(ret.size()));
|
||||
}
|
||||
|
||||
class ScBlob : public Blob
|
||||
{
|
||||
public:
|
||||
ScBlob(const void* data, uint32_t size)
|
||||
: data_(reinterpret_cast<const uint8_t*>(data), reinterpret_cast<const uint8_t*>(data) + size)
|
||||
{
|
||||
}
|
||||
|
||||
const void* Data() const override
|
||||
{
|
||||
return data_.data();
|
||||
}
|
||||
|
||||
uint32_t Size() const override
|
||||
{
|
||||
return static_cast<uint32_t>(data_.size());
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<uint8_t> data_;
|
||||
};
|
||||
|
||||
void AppendError(Compiler::ResultDesc& result, const std::string& msg)
|
||||
{
|
||||
std::string errorMSg;
|
||||
if (result.errorWarningMsg != nullptr)
|
||||
if (result.errorWarningMsg.Size() != 0)
|
||||
{
|
||||
errorMSg.assign(reinterpret_cast<const char*>(result.errorWarningMsg->Data()), result.errorWarningMsg->Size());
|
||||
errorMSg.assign(reinterpret_cast<const char*>(result.errorWarningMsg.Data()), result.errorWarningMsg.Size());
|
||||
}
|
||||
if (!errorMSg.empty())
|
||||
{
|
||||
errorMSg += "\n";
|
||||
}
|
||||
errorMSg += msg;
|
||||
DestroyBlob(result.errorWarningMsg);
|
||||
result.errorWarningMsg = CreateBlob(errorMSg.data(), static_cast<uint32_t>(errorMSg.size()));
|
||||
result.errorWarningMsg.Reset(errorMSg.data(), static_cast<uint32_t>(errorMSg.size()));
|
||||
result.hasError = true;
|
||||
}
|
||||
|
||||
|
@ -345,8 +320,7 @@ namespace
|
|||
|
||||
uint32_t dxilPartIndex = ~0u;
|
||||
IFT(containReflection->FindFirstPartKind(hlsl::DFCC_DXIL, &dxilPartIndex));
|
||||
HRESULT result =
|
||||
containReflection->GetPartReflection(dxilPartIndex, __uuidof(T), reinterpret_cast<void**>(&outReflection));
|
||||
HRESULT result = containReflection->GetPartReflection(dxilPartIndex, __uuidof(T), reinterpret_cast<void**>(&outReflection));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -382,8 +356,8 @@ namespace
|
|||
D3D12_SHADER_VARIABLE_DESC variableDesc;
|
||||
variable->GetDesc(&variableDesc);
|
||||
|
||||
reflectionDesc.name = new char[strlen(variableDesc.Name) + 1];
|
||||
std::strncpy(reflectionDesc.name, variableDesc.Name, strlen(variableDesc.Name) + 1);
|
||||
std::strncpy(reflectionDesc.name, variableDesc.Name,
|
||||
std::min(std::strlen(variableDesc.Name) + 1, sizeof(reflectionDesc.name)));
|
||||
|
||||
reflectionDesc.type = ShaderResourceType::Parameter;
|
||||
reflectionDesc.bufferBindPoint = bindDesc.BindPoint;
|
||||
|
@ -393,8 +367,8 @@ namespace
|
|||
}
|
||||
else
|
||||
{
|
||||
reflectionDesc.name = new char[strlen(bufferDesc.Name) + 1];
|
||||
std::strncpy(reflectionDesc.name, bufferDesc.Name, strlen(bufferDesc.Name) + 1);
|
||||
std::strncpy(reflectionDesc.name, bufferDesc.Name,
|
||||
std::min(std::strlen(bufferDesc.Name) + 1, sizeof(reflectionDesc.name)));
|
||||
|
||||
reflectionDesc.type = ShaderResourceType::ConstantBuffer;
|
||||
reflectionDesc.bufferBindPoint = bindDesc.BindPoint;
|
||||
|
@ -433,8 +407,7 @@ namespace
|
|||
break;
|
||||
}
|
||||
|
||||
reflectionDesc.name = new char[strlen(bindDesc.Name) + 1];
|
||||
std::strncpy(reflectionDesc.name, bindDesc.Name, strlen(bindDesc.Name) + 1);
|
||||
std::strncpy(reflectionDesc.name, bindDesc.Name, std::min(std::strlen(bindDesc.Name) + 1, sizeof(reflectionDesc.name)));
|
||||
|
||||
reflectionDesc.bufferBindPoint = 0;
|
||||
reflectionDesc.bindPoint = bindDesc.BindPoint;
|
||||
|
@ -444,12 +417,8 @@ namespace
|
|||
vecReflectionDescs.push_back(reflectionDesc);
|
||||
}
|
||||
|
||||
result.descs = nullptr;
|
||||
result.descCount = static_cast<uint32_t>(vecReflectionDescs.size());
|
||||
if (result.descCount > 0)
|
||||
{
|
||||
result.descs = CreateBlob(vecReflectionDescs.data(), sizeof(Compiler::ReflectionDesc) * result.descCount);
|
||||
}
|
||||
result.descs.Reset(vecReflectionDescs.data(), sizeof(Compiler::ReflectionDesc) * result.descCount);
|
||||
result.instructionCount = shaderDesc.InstructionCount;
|
||||
}
|
||||
#endif
|
||||
|
@ -500,17 +469,14 @@ namespace
|
|||
HRESULT status;
|
||||
IFT(dxcResult->GetStatus(&status));
|
||||
|
||||
result.target = nullptr;
|
||||
result.errorWarningMsg = nullptr;
|
||||
result.target.Reset();
|
||||
result.errorWarningMsg.Reset();
|
||||
|
||||
CComPtr<IDxcBlobEncoding> errors;
|
||||
IFT(dxcResult->GetErrorBuffer(&errors));
|
||||
if (errors != nullptr)
|
||||
{
|
||||
if (errors->GetBufferSize() > 0)
|
||||
{
|
||||
result.errorWarningMsg = CreateBlob(errors->GetBufferPointer(), static_cast<uint32_t>(errors->GetBufferSize()));
|
||||
}
|
||||
result.errorWarningMsg.Reset(errors->GetBufferPointer(), static_cast<uint32_t>(errors->GetBufferSize()));
|
||||
errors = nullptr;
|
||||
}
|
||||
|
||||
|
@ -522,7 +488,7 @@ namespace
|
|||
dxcResult = nullptr;
|
||||
if (program != nullptr)
|
||||
{
|
||||
result.target = CreateBlob(program->GetBufferPointer(), static_cast<uint32_t>(program->GetBufferSize()));
|
||||
result.target.Reset(program->GetBufferPointer(), static_cast<uint32_t>(program->GetBufferSize()));
|
||||
result.hasError = false;
|
||||
}
|
||||
|
||||
|
@ -592,8 +558,8 @@ namespace
|
|||
}
|
||||
|
||||
CComPtr<IDxcBlobEncoding> sourceBlob;
|
||||
IFT(Dxcompiler::Instance().Library()->CreateBlobWithEncodingOnHeapCopy(source.source, static_cast<UINT32>(strlen(source.source)),
|
||||
CP_UTF8, &sourceBlob));
|
||||
IFT(Dxcompiler::Instance().Library()->CreateBlobWithEncodingOnHeapCopy(
|
||||
source.source, static_cast<UINT32>(std::strlen(source.source)), CP_UTF8, &sourceBlob));
|
||||
IFTARG(sourceBlob->GetBufferSize() >= 4);
|
||||
|
||||
std::wstring shaderNameUtf16;
|
||||
|
@ -717,11 +683,10 @@ namespace
|
|||
const Compiler::TargetDesc& target)
|
||||
{
|
||||
assert((target.language != ShadingLanguage::Dxil) && (target.language != ShadingLanguage::SpirV));
|
||||
assert((binaryResult.target->Size() & (sizeof(uint32_t) - 1)) == 0);
|
||||
assert((binaryResult.target.Size() & (sizeof(uint32_t) - 1)) == 0);
|
||||
|
||||
Compiler::ResultDesc ret;
|
||||
|
||||
ret.target = nullptr;
|
||||
ret.errorWarningMsg = binaryResult.errorWarningMsg;
|
||||
ret.isText = true;
|
||||
|
||||
|
@ -731,8 +696,8 @@ namespace
|
|||
intVersion = std::stoi(target.version);
|
||||
}
|
||||
|
||||
const uint32_t* spirvIr = reinterpret_cast<const uint32_t*>(binaryResult.target->Data());
|
||||
const size_t spirvSize = binaryResult.target->Size() / sizeof(uint32_t);
|
||||
const uint32_t* spirvIr = reinterpret_cast<const uint32_t*>(binaryResult.target.Data());
|
||||
const size_t spirvSize = binaryResult.target.Size() / sizeof(uint32_t);
|
||||
|
||||
std::unique_ptr<spirv_cross::CompilerGLSL> compiler;
|
||||
bool combinedImageSamplers = false;
|
||||
|
@ -954,21 +919,17 @@ namespace
|
|||
try
|
||||
{
|
||||
const std::string targetStr = compiler->compile();
|
||||
ret.target = CreateBlob(targetStr.data(), static_cast<uint32_t>(targetStr.size()));
|
||||
ret.target.Reset(targetStr.data(), static_cast<uint32_t>(targetStr.size()));
|
||||
ret.hasError = false;
|
||||
if (binaryResult.reflection.descCount > 0)
|
||||
{
|
||||
ret.reflection.descs =
|
||||
CreateBlob(binaryResult.reflection.descs->Data(), sizeof(Compiler::ReflectionDesc) * binaryResult.reflection.descCount);
|
||||
ret.reflection.descCount = binaryResult.reflection.descCount;
|
||||
ret.reflection.instructionCount = binaryResult.reflection.instructionCount;
|
||||
}
|
||||
ret.reflection.descs.Reset(binaryResult.reflection.descs.Data(),
|
||||
sizeof(Compiler::ReflectionDesc) * binaryResult.reflection.descCount);
|
||||
ret.reflection.descCount = binaryResult.reflection.descCount;
|
||||
ret.reflection.instructionCount = binaryResult.reflection.instructionCount;
|
||||
}
|
||||
catch (spirv_cross::CompilerError& error)
|
||||
{
|
||||
const char* errorMsg = error.what();
|
||||
DestroyBlob(ret.errorWarningMsg);
|
||||
ret.errorWarningMsg = CreateBlob(errorMsg, static_cast<uint32_t>(strlen(errorMsg)));
|
||||
ret.errorWarningMsg.Reset(errorMsg, static_cast<uint32_t>(std::strlen(errorMsg)));
|
||||
ret.hasError = true;
|
||||
}
|
||||
|
||||
|
@ -1014,18 +975,95 @@ namespace
|
|||
|
||||
namespace ShaderConductor
|
||||
{
|
||||
Blob::~Blob() = default;
|
||||
|
||||
Blob* CreateBlob(const void* data, uint32_t size)
|
||||
class Blob::BlobImpl
|
||||
{
|
||||
return new ScBlob(data, size);
|
||||
public:
|
||||
BlobImpl(const void* data, uint32_t size) noexcept
|
||||
: m_data(reinterpret_cast<const uint8_t*>(data), reinterpret_cast<const uint8_t*>(data) + size)
|
||||
{
|
||||
}
|
||||
|
||||
const void* Data() const noexcept
|
||||
{
|
||||
return m_data.data();
|
||||
}
|
||||
|
||||
uint32_t Size() const noexcept
|
||||
{
|
||||
return static_cast<uint32_t>(m_data.size());
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<uint8_t> m_data;
|
||||
};
|
||||
|
||||
Blob::Blob() noexcept = default;
|
||||
|
||||
Blob::Blob(const void* data, uint32_t size)
|
||||
{
|
||||
this->Reset(data, size);
|
||||
}
|
||||
|
||||
void DestroyBlob(Blob* blob)
|
||||
Blob::Blob(const Blob& other)
|
||||
{
|
||||
delete blob;
|
||||
this->Reset(other.Data(), other.Size());
|
||||
}
|
||||
|
||||
Blob::Blob(Blob&& other) noexcept : m_impl(std::move(other.m_impl))
|
||||
{
|
||||
other.m_impl = nullptr;
|
||||
}
|
||||
|
||||
Blob::~Blob() noexcept
|
||||
{
|
||||
delete m_impl;
|
||||
}
|
||||
|
||||
Blob& Blob::operator=(const Blob& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
this->Reset(other.Data(), other.Size());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Blob& Blob::operator=(Blob&& other) noexcept
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
m_impl = std::move(other.m_impl);
|
||||
other.m_impl = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Blob::Reset()
|
||||
{
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
}
|
||||
|
||||
void Blob::Reset(const void* data, uint32_t size)
|
||||
{
|
||||
this->Reset();
|
||||
if ((data != nullptr) && (size > 0))
|
||||
{
|
||||
m_impl = new BlobImpl(data, size);
|
||||
}
|
||||
}
|
||||
|
||||
const void* Blob::Data() const noexcept
|
||||
{
|
||||
return m_impl ? m_impl->Data() : nullptr;
|
||||
}
|
||||
|
||||
uint32_t Blob::Size() const noexcept
|
||||
{
|
||||
return m_impl ? m_impl->Size() : 0;
|
||||
}
|
||||
|
||||
|
||||
Compiler::ResultDesc Compiler::Compile(const SourceDesc& source, const Options& options, const TargetDesc& target)
|
||||
{
|
||||
ResultDesc result;
|
||||
|
@ -1037,7 +1075,7 @@ namespace ShaderConductor
|
|||
ResultDesc* results)
|
||||
{
|
||||
SourceDesc sourceOverride = source;
|
||||
if (!sourceOverride.entryPoint || (strlen(sourceOverride.entryPoint) == 0))
|
||||
if (!sourceOverride.entryPoint || (std::strlen(sourceOverride.entryPoint) == 0))
|
||||
{
|
||||
sourceOverride.entryPoint = "main";
|
||||
}
|
||||
|
@ -1102,32 +1140,8 @@ namespace ShaderConductor
|
|||
binaryResult = spirvBinaryResult;
|
||||
}
|
||||
|
||||
if (binaryResult.target)
|
||||
{
|
||||
binaryResult.target = CreateBlob(binaryResult.target->Data(), binaryResult.target->Size());
|
||||
}
|
||||
if (binaryResult.errorWarningMsg)
|
||||
{
|
||||
binaryResult.errorWarningMsg = CreateBlob(binaryResult.errorWarningMsg->Data(), binaryResult.errorWarningMsg->Size());
|
||||
}
|
||||
results[i] = ConvertBinary(binaryResult, sourceOverride, targets[i]);
|
||||
}
|
||||
|
||||
if (hasDxil)
|
||||
{
|
||||
DestroyBlob(dxilBinaryResult.target);
|
||||
DestroyBlob(dxilBinaryResult.errorWarningMsg);
|
||||
}
|
||||
if (hasDxilModule)
|
||||
{
|
||||
DestroyBlob(dxilModuleBinaryResult.target);
|
||||
DestroyBlob(dxilModuleBinaryResult.errorWarningMsg);
|
||||
}
|
||||
if (hasSpirV)
|
||||
{
|
||||
DestroyBlob(spirvBinaryResult.target);
|
||||
DestroyBlob(spirvBinaryResult.errorWarningMsg);
|
||||
}
|
||||
}
|
||||
|
||||
Compiler::ResultDesc Compiler::Disassemble(const DisassembleDesc& source)
|
||||
|
@ -1136,9 +1150,7 @@ namespace ShaderConductor
|
|||
|
||||
Compiler::ResultDesc ret;
|
||||
|
||||
ret.target = nullptr;
|
||||
ret.isText = true;
|
||||
ret.errorWarningMsg = nullptr;
|
||||
|
||||
if (source.language == ShadingLanguage::SpirV)
|
||||
{
|
||||
|
@ -1155,14 +1167,14 @@ namespace ShaderConductor
|
|||
|
||||
if (error)
|
||||
{
|
||||
ret.errorWarningMsg = CreateBlob(diagnostic->error, static_cast<uint32_t>(strlen(diagnostic->error)));
|
||||
ret.errorWarningMsg.Reset(diagnostic->error, static_cast<uint32_t>(std::strlen(diagnostic->error)));
|
||||
ret.hasError = true;
|
||||
spvDiagnosticDestroy(diagnostic);
|
||||
}
|
||||
else
|
||||
{
|
||||
const std::string disassemble = text->str;
|
||||
ret.target = CreateBlob(disassemble.data(), static_cast<uint32_t>(disassemble.size()));
|
||||
ret.target.Reset(disassemble.data(), static_cast<uint32_t>(disassemble.size()));
|
||||
ret.hasError = false;
|
||||
}
|
||||
|
||||
|
@ -1178,7 +1190,7 @@ namespace ShaderConductor
|
|||
if (disassembly != nullptr)
|
||||
{
|
||||
// Remove the tailing \0
|
||||
ret.target = CreateBlob(disassembly->GetBufferPointer(), static_cast<uint32_t>(disassembly->GetBufferSize() - 1));
|
||||
ret.target.Reset(disassembly->GetBufferPointer(), static_cast<uint32_t>(disassembly->GetBufferSize() - 1));
|
||||
ret.hasError = false;
|
||||
}
|
||||
else
|
||||
|
@ -1209,7 +1221,7 @@ namespace ShaderConductor
|
|||
{
|
||||
IFTARG(modules.modules[i] != nullptr);
|
||||
|
||||
IFT(library->CreateBlobWithEncodingOnHeapCopy(modules.modules[i]->target->Data(), modules.modules[i]->target->Size(), CP_UTF8,
|
||||
IFT(library->CreateBlobWithEncodingOnHeapCopy(modules.modules[i]->target.Data(), modules.modules[i]->target.Size(), CP_UTF8,
|
||||
&moduleBlobs[i]));
|
||||
IFTARG(moduleBlobs[i]->GetBufferSize() >= 4);
|
||||
|
||||
|
@ -1234,19 +1246,6 @@ namespace ShaderConductor
|
|||
source.stage = modules.stage;
|
||||
return ConvertBinary(binaryResult, source, target);
|
||||
}
|
||||
|
||||
void Compiler::DestroyResultDesc(const ResultDesc& result)
|
||||
{
|
||||
if (result.reflection.descs)
|
||||
{
|
||||
const ReflectionDesc* reflectionDescs = reinterpret_cast<const ReflectionDesc*>(result.reflection.descs->Data());
|
||||
for (uint32_t i = 0; i < result.reflection.descCount; ++i)
|
||||
{
|
||||
delete[] reflectionDescs[i].name;
|
||||
}
|
||||
DestroyBlob(result.reflection.descs);
|
||||
}
|
||||
}
|
||||
} // namespace ShaderConductor
|
||||
|
||||
#ifdef _WIN32
|
||||
|
|
|
@ -92,7 +92,7 @@ namespace
|
|||
if (expectSuccessFlags[i])
|
||||
{
|
||||
EXPECT_FALSE(result.hasError);
|
||||
EXPECT_EQ(result.errorWarningMsg, nullptr);
|
||||
EXPECT_EQ(result.errorWarningMsg.Size(), 0U);
|
||||
EXPECT_TRUE(result.isText);
|
||||
|
||||
std::string compareName = name;
|
||||
|
@ -102,18 +102,14 @@ namespace
|
|||
}
|
||||
compareName += "." + extMap[static_cast<uint32_t>(targets[i].language)];
|
||||
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(result.target->Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + result.target->Size()), result.isText, compareName);
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(result.target.Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + result.target.Size()), result.isText, compareName);
|
||||
}
|
||||
else
|
||||
{
|
||||
EXPECT_TRUE(result.hasError);
|
||||
EXPECT_EQ(result.target, nullptr);
|
||||
EXPECT_EQ(result.target.Size(), 0U);
|
||||
}
|
||||
|
||||
DestroyBlob(result.errorWarningMsg);
|
||||
DestroyBlob(result.target);
|
||||
Compiler::DestroyResultDesc(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,10 +123,7 @@ namespace
|
|||
EXPECT_FALSE(result.hasError);
|
||||
EXPECT_FALSE(result.isText);
|
||||
|
||||
DestroyBlob(result.errorWarningMsg);
|
||||
Compiler::DestroyResultDesc(result);
|
||||
|
||||
return {moduleName, result.target};
|
||||
return {moduleName, std::move(result.target)};
|
||||
}
|
||||
|
||||
class TestBase : public testing::Test
|
||||
|
@ -521,15 +514,11 @@ namespace
|
|||
Compiler::Compile({source.c_str(), fileName.c_str(), "main", ShaderStage::PixelShader}, {}, {ShadingLanguage::Glsl, "30"});
|
||||
|
||||
EXPECT_FALSE(result.hasError);
|
||||
EXPECT_EQ(result.errorWarningMsg, nullptr);
|
||||
EXPECT_EQ(result.errorWarningMsg.Size(), 0U);
|
||||
EXPECT_TRUE(result.isText);
|
||||
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(result.target->Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + result.target->Size()), result.isText, "IncludeExist.glsl");
|
||||
|
||||
DestroyBlob(result.errorWarningMsg);
|
||||
DestroyBlob(result.target);
|
||||
Compiler::DestroyResultDesc(result);
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(result.target.Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + result.target.Size()), result.isText, "IncludeExist.glsl");
|
||||
}
|
||||
|
||||
TEST(IncludeTest, IncludeNotExist)
|
||||
|
@ -543,12 +532,8 @@ namespace
|
|||
Compiler::Compile({source.c_str(), fileName.c_str(), "main", ShaderStage::PixelShader}, {}, {ShadingLanguage::Glsl, "30"});
|
||||
|
||||
EXPECT_TRUE(result.hasError);
|
||||
const char* errorStr = reinterpret_cast<const char*>(result.errorWarningMsg->Data());
|
||||
EXPECT_GE(std::string(errorStr, errorStr + result.errorWarningMsg->Size()).find("fatal error: 'Header.hlsli' file not found"), 0U);
|
||||
|
||||
DestroyBlob(result.errorWarningMsg);
|
||||
DestroyBlob(result.target);
|
||||
Compiler::DestroyResultDesc(result);
|
||||
const char* errorStr = reinterpret_cast<const char*>(result.errorWarningMsg.Data());
|
||||
EXPECT_GE(std::string(errorStr, errorStr + result.errorWarningMsg.Size()).find("fatal error: 'Header.hlsli' file not found"), 0U);
|
||||
}
|
||||
|
||||
TEST(IncludeTest, IncludeEmptyFile)
|
||||
|
@ -562,15 +547,11 @@ namespace
|
|||
Compiler::Compile({source.c_str(), fileName.c_str(), "main", ShaderStage::PixelShader}, {}, {ShadingLanguage::Glsl, "30"});
|
||||
|
||||
EXPECT_FALSE(result.hasError);
|
||||
EXPECT_EQ(result.errorWarningMsg, nullptr);
|
||||
EXPECT_EQ(result.errorWarningMsg.Size(), 0U);
|
||||
EXPECT_TRUE(result.isText);
|
||||
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(result.target->Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + result.target->Size()), result.isText, "IncludeEmptyHeader.glsl");
|
||||
|
||||
DestroyBlob(result.errorWarningMsg);
|
||||
DestroyBlob(result.target);
|
||||
Compiler::DestroyResultDesc(result);
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(result.target.Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + result.target.Size()), result.isText, "IncludeEmptyHeader.glsl");
|
||||
}
|
||||
|
||||
TEST(HalfDataTypeTest, DotHalf)
|
||||
|
@ -590,12 +571,8 @@ namespace
|
|||
EXPECT_FALSE(result.hasError);
|
||||
EXPECT_TRUE(result.isText);
|
||||
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(result.target->Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + result.target->Size()), result.isText, "DotHalfPS.glsl");
|
||||
|
||||
DestroyBlob(result.errorWarningMsg);
|
||||
DestroyBlob(result.target);
|
||||
Compiler::DestroyResultDesc(result);
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(result.target.Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + result.target.Size()), result.isText, "DotHalfPS.glsl");
|
||||
}
|
||||
|
||||
TEST(HalfDataTypeTest, HalfOutParam)
|
||||
|
@ -615,12 +592,8 @@ namespace
|
|||
EXPECT_FALSE(result.hasError);
|
||||
EXPECT_TRUE(result.isText);
|
||||
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(result.target->Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + result.target->Size()), result.isText, "HalfOutParamPS.glsl");
|
||||
|
||||
DestroyBlob(result.errorWarningMsg);
|
||||
DestroyBlob(result.target);
|
||||
Compiler::DestroyResultDesc(result);
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(result.target.Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + result.target.Size()), result.isText, "HalfOutParamPS.glsl");
|
||||
}
|
||||
|
||||
TEST(LinkTest, LinkDxil)
|
||||
|
@ -658,27 +631,14 @@ namespace
|
|||
EXPECT_FALSE(linkedResult.isText);
|
||||
|
||||
Compiler::DisassembleDesc disasmDesc;
|
||||
disasmDesc.binary = reinterpret_cast<const uint8_t*>(linkedResult.target->Data());
|
||||
disasmDesc.binarySize = linkedResult.target->Size();
|
||||
disasmDesc.binary = reinterpret_cast<const uint8_t*>(linkedResult.target.Data());
|
||||
disasmDesc.binarySize = linkedResult.target.Size();
|
||||
disasmDesc.language = ShadingLanguage::Dxil;
|
||||
const auto disasmResult = Compiler::Disassemble(disasmDesc);
|
||||
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(disasmResult.target->Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + disasmResult.target->Size()), disasmResult.isText,
|
||||
const uint8_t* target_ptr = reinterpret_cast<const uint8_t*>(disasmResult.target.Data());
|
||||
CompareWithExpected(std::vector<uint8_t>(target_ptr, target_ptr + disasmResult.target.Size()), disasmResult.isText,
|
||||
expectedNames[i]);
|
||||
|
||||
DestroyBlob(linkedResult.errorWarningMsg);
|
||||
DestroyBlob(linkedResult.target);
|
||||
Compiler::DestroyResultDesc(linkedResult);
|
||||
|
||||
DestroyBlob(disasmResult.errorWarningMsg);
|
||||
DestroyBlob(disasmResult.target);
|
||||
Compiler::DestroyResultDesc(disasmResult);
|
||||
}
|
||||
|
||||
for (auto& mod : dxilModules)
|
||||
{
|
||||
DestroyBlob(mod.target);
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
|
|
@ -213,13 +213,13 @@ int main(int argc, char** argv)
|
|||
{
|
||||
const auto result = Compiler::Compile(sourceDesc, {}, targetDesc);
|
||||
|
||||
if (result.errorWarningMsg != nullptr)
|
||||
if (result.errorWarningMsg.Size() > 0)
|
||||
{
|
||||
const char* msg = reinterpret_cast<const char*>(result.errorWarningMsg->Data());
|
||||
const char* msg = reinterpret_cast<const char*>(result.errorWarningMsg.Data());
|
||||
std::cerr << "Error or warning from shader compiler: " << std::endl
|
||||
<< std::string(msg, msg + result.errorWarningMsg->Size()) << std::endl;
|
||||
<< std::string(msg, msg + result.errorWarningMsg.Size()) << std::endl;
|
||||
}
|
||||
if (result.target != nullptr)
|
||||
if (result.target.Size() > 0)
|
||||
{
|
||||
std::ofstream outputFile(outputName, std::ios_base::binary);
|
||||
if (!outputFile)
|
||||
|
@ -228,14 +228,10 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
outputFile.write(reinterpret_cast<const char*>(result.target->Data()), result.target->Size());
|
||||
outputFile.write(reinterpret_cast<const char*>(result.target.Data()), result.target.Size());
|
||||
|
||||
std::cout << "The compiled file is saved to " << outputName << std::endl;
|
||||
}
|
||||
|
||||
DestroyBlob(result.errorWarningMsg);
|
||||
DestroyBlob(result.target);
|
||||
Compiler::DestroyResultDesc(result);
|
||||
}
|
||||
catch (std::exception& ex)
|
||||
{
|
||||
|
|
|
@ -59,13 +59,13 @@ void Compile(SourceDescription* source, OptionsDescription* optionsDesc, TargetD
|
|||
{
|
||||
const auto translation = Compiler::Compile(sourceDesc, options, targetDesc);
|
||||
|
||||
if (translation.errorWarningMsg != nullptr)
|
||||
if (translation.errorWarningMsg.Size() > 0)
|
||||
{
|
||||
result->errorWarningMsg = reinterpret_cast<ShaderConductorBlob*>(translation.errorWarningMsg);
|
||||
result->errorWarningMsg = CreateShaderConductorBlob(translation.errorWarningMsg.Data(), translation.errorWarningMsg.Size());
|
||||
}
|
||||
if (translation.target != nullptr)
|
||||
if (translation.target.Size() > 0)
|
||||
{
|
||||
result->target = reinterpret_cast<ShaderConductorBlob*>(translation.target);
|
||||
result->target = CreateShaderConductorBlob(translation.target.Data(), translation.target.Size());
|
||||
}
|
||||
|
||||
result->hasError = translation.hasError;
|
||||
|
@ -88,13 +88,14 @@ void Disassemble(DisassembleDescription* source, ResultDescription* result)
|
|||
|
||||
const auto disassembleResult = Compiler::Disassemble(disassembleSource);
|
||||
|
||||
if (disassembleResult.errorWarningMsg != nullptr)
|
||||
if (disassembleResult.errorWarningMsg.Size() > 0)
|
||||
{
|
||||
result->errorWarningMsg = reinterpret_cast<ShaderConductorBlob*>(disassembleResult.errorWarningMsg);
|
||||
result->errorWarningMsg =
|
||||
CreateShaderConductorBlob(disassembleResult.errorWarningMsg.Data(), disassembleResult.errorWarningMsg.Size());
|
||||
}
|
||||
if (disassembleResult.target != nullptr)
|
||||
if (disassembleResult.target.Size() > 0)
|
||||
{
|
||||
result->target = reinterpret_cast<ShaderConductorBlob*>(disassembleResult.target);
|
||||
result->target = CreateShaderConductorBlob(disassembleResult.target.Data(), disassembleResult.target.Size());
|
||||
}
|
||||
|
||||
result->hasError = disassembleResult.hasError;
|
||||
|
@ -103,12 +104,12 @@ void Disassemble(DisassembleDescription* source, ResultDescription* result)
|
|||
|
||||
ShaderConductorBlob* CreateShaderConductorBlob(const void* data, int size)
|
||||
{
|
||||
return reinterpret_cast<ShaderConductorBlob*>(ShaderConductor::CreateBlob(data, size));
|
||||
return reinterpret_cast<ShaderConductorBlob*>(new ShaderConductor::Blob(data, size));
|
||||
}
|
||||
|
||||
void DestroyShaderConductorBlob(ShaderConductorBlob* blob)
|
||||
{
|
||||
DestroyBlob(reinterpret_cast<Blob*>(blob));
|
||||
delete reinterpret_cast<Blob*>(blob);
|
||||
}
|
||||
|
||||
const void* GetShaderConductorBlobData(ShaderConductorBlob* blob)
|
||||
|
|
Загрузка…
Ссылка в новой задаче