|
| 1 | +/* |
| 2 | + * Copyright 2025 Diligent Graphics LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * In no event and under no legal theory, whether in tort (including negligence), |
| 17 | + * contract, or otherwise, unless required by applicable law (such as deliberate |
| 18 | + * and grossly negligent acts) or agreed to in writing, shall any Contributor be |
| 19 | + * liable for any damages, including any direct, indirect, special, incidental, |
| 20 | + * or consequential damages of any character arising as a result of this License or |
| 21 | + * out of the use or inability to use the software (including but not limited to damages |
| 22 | + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and |
| 23 | + * all other commercial damages or losses), even if such Contributor has been advised |
| 24 | + * of the possibility of such damages. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "GPUTestingEnvironment.hpp" |
| 28 | + |
| 29 | +#include "ObjectBase.hpp" |
| 30 | +#include "MemoryFileStream.hpp" |
| 31 | +#include "ProxyDataBlob.hpp" |
| 32 | + |
| 33 | +#include "gtest/gtest.h" |
| 34 | + |
| 35 | +using namespace Diligent; |
| 36 | +using namespace Diligent::Testing; |
| 37 | + |
| 38 | +namespace |
| 39 | +{ |
| 40 | + |
| 41 | +constexpr char g_PS[] = R"( |
| 42 | +Texture2D g_Texture; |
| 43 | +SamplerState g_Texture_sampler; |
| 44 | +
|
| 45 | +float4 main(in float2 UV : TEXCOORD) : SV_Target |
| 46 | +{ |
| 47 | + return g_Texture.Sample(g_Texture_sampler, UV); |
| 48 | +} |
| 49 | +)"; |
| 50 | + |
| 51 | +TEST(ShaderCreationTest, FromSource) |
| 52 | +{ |
| 53 | + GPUTestingEnvironment* pEnv = GPUTestingEnvironment::GetInstance(); |
| 54 | + IRenderDevice* pDevice = pEnv->GetDevice(); |
| 55 | + |
| 56 | + ShaderCreateInfo ShaderCI; |
| 57 | + ShaderCI.Desc = {"ShaderCreationTest.FromSource", SHADER_TYPE_PIXEL, true}; |
| 58 | + ShaderCI.Source = g_PS; |
| 59 | + ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL; |
| 60 | + |
| 61 | + { |
| 62 | + RefCntAutoPtr<IShader> pShader; |
| 63 | + pDevice->CreateShader(ShaderCI, &pShader); |
| 64 | + EXPECT_NE(pShader, nullptr); |
| 65 | + } |
| 66 | + |
| 67 | + { |
| 68 | + std::string Source = g_PS; |
| 69 | + Source += "invalid syntax"; |
| 70 | + |
| 71 | + ShaderCI.Source = Source.c_str(); |
| 72 | + ShaderCI.SourceLength = sizeof(g_PS) - 1; |
| 73 | + |
| 74 | + RefCntAutoPtr<IShader> pShader; |
| 75 | + pDevice->CreateShader(ShaderCI, &pShader); |
| 76 | + EXPECT_NE(pShader, nullptr); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +static std::vector<uint8_t> CompilePS(IRenderDevice* pDevice) |
| 81 | +{ |
| 82 | + ShaderCreateInfo ShaderCI; |
| 83 | + ShaderCI.Desc = {"ShaderCreationTest.FromSource", SHADER_TYPE_PIXEL, true}; |
| 84 | + ShaderCI.Source = g_PS; |
| 85 | + ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL; |
| 86 | + |
| 87 | + std::vector<uint8_t> Bytecode; |
| 88 | + RefCntAutoPtr<IShader> pShader; |
| 89 | + pDevice->CreateShader(ShaderCI, &pShader); |
| 90 | + if (pShader) |
| 91 | + { |
| 92 | + const void* pBytecode = nullptr; |
| 93 | + Uint64 Size = 0; |
| 94 | + pShader->GetBytecode(&pBytecode, Size); |
| 95 | + |
| 96 | + Bytecode.resize(static_cast<size_t>(Size)); |
| 97 | + memcpy(Bytecode.data(), pBytecode, static_cast<size_t>(Size)); |
| 98 | + } |
| 99 | + |
| 100 | + return Bytecode; |
| 101 | +} |
| 102 | + |
| 103 | +TEST(ShaderCreationTest, FromBytecode) |
| 104 | +{ |
| 105 | + GPUTestingEnvironment* pEnv = GPUTestingEnvironment::GetInstance(); |
| 106 | + IRenderDevice* pDevice = pEnv->GetDevice(); |
| 107 | + const RenderDeviceInfo& DeviceInfo = pDevice->GetDeviceInfo(); |
| 108 | + |
| 109 | + if (!(DeviceInfo.IsD3DDevice() || DeviceInfo.IsVulkanDevice())) |
| 110 | + { |
| 111 | + GTEST_SKIP() << "Creating shader from bytecode is not supported on this device type"; |
| 112 | + } |
| 113 | + |
| 114 | + std::vector<uint8_t> Bytecode = CompilePS(pDevice); |
| 115 | + ASSERT_FALSE(Bytecode.empty()) << "Failed to compile shader"; |
| 116 | + |
| 117 | + { |
| 118 | + ShaderCreateInfo ShaderCI; |
| 119 | + ShaderCI.Desc = {"ShaderCreationTest.FromBytecode - Src", SHADER_TYPE_PIXEL, true}; |
| 120 | + ShaderCI.ByteCode = Bytecode.data(); |
| 121 | + ShaderCI.ByteCodeSize = Bytecode.size(); |
| 122 | + |
| 123 | + RefCntAutoPtr<IShader> pShader; |
| 124 | + pDevice->CreateShader(ShaderCI, &pShader); |
| 125 | + EXPECT_NE(pShader, nullptr); |
| 126 | + } |
| 127 | + |
| 128 | + { |
| 129 | + class TestShaderSourceFactoryImpl : public ObjectBase<IShaderSourceInputStreamFactory> |
| 130 | + { |
| 131 | + public: |
| 132 | + static RefCntAutoPtr<IShaderSourceInputStreamFactory> Create(const std::vector<uint8_t>& Bytecode) |
| 133 | + { |
| 134 | + return RefCntAutoPtr<IShaderSourceInputStreamFactory>{MakeNewRCObj<TestShaderSourceFactoryImpl>()(Bytecode)}; |
| 135 | + } |
| 136 | + |
| 137 | + TestShaderSourceFactoryImpl(IReferenceCounters* pRefCounters, const std::vector<uint8_t>& Bytecode) : |
| 138 | + ObjectBase<IShaderSourceInputStreamFactory>(pRefCounters), |
| 139 | + m_Bytecode{Bytecode} |
| 140 | + { |
| 141 | + } |
| 142 | + |
| 143 | + virtual void DILIGENT_CALL_TYPE CreateInputStream(const Char* Name, IFileStream** ppStream) override final |
| 144 | + { |
| 145 | + CreateInputStream2(Name, CREATE_SHADER_SOURCE_INPUT_STREAM_FLAG_NONE, ppStream); |
| 146 | + } |
| 147 | + |
| 148 | + virtual void DILIGENT_CALL_TYPE CreateInputStream2(const Char* Name, |
| 149 | + CREATE_SHADER_SOURCE_INPUT_STREAM_FLAGS Flags, |
| 150 | + IFileStream** ppStream) override final |
| 151 | + { |
| 152 | + if (strcmp(Name, "ps.bc") != 0) |
| 153 | + return; |
| 154 | + |
| 155 | + RefCntAutoPtr<IDataBlob> pDataBlob = ProxyDataBlob::Create(m_Bytecode.data(), m_Bytecode.size()); |
| 156 | + RefCntAutoPtr<MemoryFileStream> pMemStream{MakeNewRCObj<MemoryFileStream>()(pDataBlob)}; |
| 157 | + |
| 158 | + pMemStream->QueryInterface(IID_FileStream, reinterpret_cast<IObject**>(ppStream)); |
| 159 | + } |
| 160 | + |
| 161 | + private: |
| 162 | + const std::vector<uint8_t>& m_Bytecode; |
| 163 | + }; |
| 164 | + |
| 165 | + RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceFactory = TestShaderSourceFactoryImpl::Create(Bytecode); |
| 166 | + |
| 167 | + ShaderCreateInfo ShaderCI; |
| 168 | + ShaderCI.Desc = {"ShaderCreationTest.FromBytecode", SHADER_TYPE_PIXEL, true}; |
| 169 | + ShaderCI.pShaderSourceStreamFactory = pShaderSourceFactory; |
| 170 | + ShaderCI.FilePath = "ps.bc"; |
| 171 | + ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_BYTECODE; |
| 172 | + |
| 173 | + RefCntAutoPtr<IShader> pShader; |
| 174 | + pDevice->CreateShader(ShaderCI, &pShader); |
| 175 | + EXPECT_NE(pShader, nullptr); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +} // namespace |
0 commit comments