-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store shaders once at the start of the program and let things look them up. Levels and sprites do this now. #134
- Loading branch information
Showing
35 changed files
with
924 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "CppUnitTest.h" | ||
|
||
#include <trview.graphics/PixelShader.h> | ||
|
||
using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||
|
||
namespace trview | ||
{ | ||
namespace graphics | ||
{ | ||
namespace tests | ||
{ | ||
TEST_CLASS(PixelShaderTests) | ||
{ | ||
// Tests that trying to create a pixel shader with no data throws. | ||
TEST_METHOD(EmptyData) | ||
{ | ||
Assert::ExpectException<std::exception>([]() { PixelShader{ nullptr, std::vector<uint8_t>() }; }); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "CppUnitTest.h" | ||
|
||
#include <trview.graphics/ShaderStorage.h> | ||
|
||
using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||
|
||
namespace trview | ||
{ | ||
namespace graphics | ||
{ | ||
namespace tests | ||
{ | ||
TEST_CLASS(ShaderStorageTests) | ||
{ | ||
private: | ||
class TestShader final : public IShader | ||
{ | ||
public: | ||
virtual ~TestShader() = default; | ||
|
||
virtual void apply(const CComPtr<ID3D11DeviceContext>& context) override | ||
{ | ||
} | ||
}; | ||
|
||
public: | ||
// Test that the shader is added to the store and can then be retrieved. | ||
TEST_METHOD(AddAndRetrieveShader) | ||
{ | ||
ShaderStorage storage; | ||
Assert::IsNull(storage.get("test")); | ||
storage.add("test", std::make_unique<TestShader>()); | ||
Assert::IsNotNull(storage.get("test")); | ||
} | ||
|
||
// Test that a null shader is rejected by the store. | ||
TEST_METHOD(NullShader) | ||
{ | ||
ShaderStorage storage; | ||
Assert::IsNull(storage.get("test")); | ||
Assert::ExpectException<std::exception>([&storage]() { storage.add("test", nullptr); }); | ||
Assert::IsNull(storage.get("test")); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "CppUnitTest.h" | ||
|
||
#include <trview.graphics/VertexShader.h> | ||
|
||
using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||
|
||
namespace trview | ||
{ | ||
namespace graphics | ||
{ | ||
namespace tests | ||
{ | ||
TEST_CLASS(VertexShaderTests) | ||
{ | ||
// Tests that trying to create a vertex shader with no data throws. | ||
TEST_METHOD(EmptyData) | ||
{ | ||
Assert::ExpectException<std::exception>([]() { VertexShader{ nullptr, std::vector<uint8_t>(), std::vector<D3D11_INPUT_ELEMENT_DESC>(1) }; }); | ||
} | ||
|
||
// Tests that trying to create a vertex shader with no input descriptions throws. | ||
TEST_METHOD(EmptyInputDescription) | ||
{ | ||
Assert::ExpectException<std::exception>([]() { VertexShader{ nullptr, std::vector<uint8_t>(1), std::vector<D3D11_INPUT_ELEMENT_DESC>() }; }); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|Win32"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|Win32"> | ||
<Configuration>Release</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Debug|x64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClCompile Include="PixelShaderTests.cpp" /> | ||
<ClCompile Include="ShaderStorageTests.cpp" /> | ||
<ClCompile Include="VertexShaderTests.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\trview.graphics\trview.graphics.vcxproj"> | ||
<Project>{3270fd29-edab-40be-8ca1-dabc5e261e4c}</Project> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<VCProjectVersion>15.0</VCProjectVersion> | ||
<ProjectGuid>{6E6B151D-DF48-4FE2-B7AF-2D07FB3451D2}</ProjectGuid> | ||
<Keyword>Win32Proj</Keyword> | ||
<RootNamespace>trviewgraphicstests</RootNamespace> | ||
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion> | ||
<ProjectSubType>NativeUnitTestProject</ProjectSubType> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | ||
<ConfigurationType>DynamicLibrary</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
<UseOfMfc>false</UseOfMfc> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>DynamicLibrary</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
<UseOfMfc>false</UseOfMfc> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>DynamicLibrary</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
<UseOfMfc>false</UseOfMfc> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>DynamicLibrary</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
<UseOfMfc>false</UseOfMfc> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"> | ||
</ImportGroup> | ||
<ImportGroup Label="Shared"> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<PrecompiledHeader>NotUsing</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;$(SolutionDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<UseFullPaths>true</UseFullPaths> | ||
<PrecompiledHeaderFile /> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Windows</SubSystem> | ||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<ClCompile> | ||
<PrecompiledHeader>NotUsing</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;$(SolutionDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||
<PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<UseFullPaths>true</UseFullPaths> | ||
<PrecompiledHeaderFile /> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Windows</SubSystem> | ||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader>NotUsing</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;$(SolutionDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||
<PreprocessorDefinitions>WIN32;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<UseFullPaths>true</UseFullPaths> | ||
<PrecompiledHeaderFile /> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Windows</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader>NotUsing</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;$(SolutionDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<UseFullPaths>true</UseFullPaths> | ||
<PrecompiledHeaderFile /> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Windows</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<ClCompile Include="ShaderStorageTests.cpp" /> | ||
<ClCompile Include="VertexShaderTests.cpp" /> | ||
<ClCompile Include="PixelShaderTests.cpp" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include <atlbase.h> | ||
#include <d3d11.h> | ||
|
||
namespace trview | ||
{ | ||
namespace graphics | ||
{ | ||
struct IShader | ||
{ | ||
virtual ~IShader() = 0; | ||
|
||
virtual void apply(const CComPtr<ID3D11DeviceContext>& context) = 0; | ||
}; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "IShaderStorage.h" | ||
|
||
namespace trview | ||
{ | ||
namespace graphics | ||
{ | ||
IShaderStorage::~IShaderStorage() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
namespace trview | ||
{ | ||
namespace graphics | ||
{ | ||
struct IShader; | ||
|
||
struct IShaderStorage | ||
{ | ||
virtual ~IShaderStorage() = 0; | ||
|
||
// Add the shader to the store. | ||
// name: The name of the shader. | ||
// shader: The shader. | ||
virtual void add(const std::string& name, std::unique_ptr<IShader> shader) = 0; | ||
|
||
// Get the shader with the specified name. | ||
// name: The name of the shader to find. | ||
// Returns: The shader or nullptr if the shader was not found. | ||
virtual IShader* get(const std::string& name) const = 0; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "PixelShader.h" | ||
|
||
namespace trview | ||
{ | ||
namespace graphics | ||
{ | ||
PixelShader::PixelShader(const CComPtr<ID3D11Device>& device, const std::vector<uint8_t>& data) | ||
{ | ||
if (data.empty()) | ||
{ | ||
throw std::exception("Data for PixelShader cannot be empty"); | ||
} | ||
|
||
device->CreatePixelShader(&data[0], data.size(), nullptr, &_pixel_shader); | ||
} | ||
|
||
void PixelShader::apply(const CComPtr<ID3D11DeviceContext>& context) | ||
{ | ||
context->PSSetShader(_pixel_shader, nullptr, 0); | ||
} | ||
} | ||
} |
Oops, something went wrong.