Skip to content

Commit

Permalink
Re-use shaders (#135)
Browse files Browse the repository at this point in the history
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
chreden authored Apr 25, 2018
1 parent 66aad24 commit 0db3839
Show file tree
Hide file tree
Showing 35 changed files with 924 additions and 98 deletions.
6 changes: 3 additions & 3 deletions trview.common/FileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace trview
{
// Load the contents of a file.
std::vector<char> load_file(std::wstring filename)
std::vector<uint8_t> load_file(std::wstring filename)
{
std::ifstream ifs (filename, std::ios::binary | std::ios::ate);
std::streampos length = ifs.tellg(); // already at end of file
Expand All @@ -16,10 +16,10 @@ namespace trview
else if (length == (std::streampos)-1)
throw new std::runtime_error("-1 returned from tellg(), read error likely");

std::vector<char> data (length);
std::vector<uint8_t> data (length);

ifs.seekg(0, std::ios::beg);
ifs.read((char*) &data[0], length);
ifs.read(reinterpret_cast<char*>(&data[0]), length);

return data;
}
Expand Down
2 changes: 1 addition & 1 deletion trview.common/FileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
namespace trview
{
// Load the contents of a file.
std::vector<char> load_file(std::wstring filename);
std::vector<uint8_t> load_file(std::wstring filename);
}
23 changes: 23 additions & 0 deletions trview.graphics.tests/PixelShaderTests.cpp
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>() }; });
}
};
}
}
}
47 changes: 47 additions & 0 deletions trview.graphics.tests/ShaderStorageTests.cpp
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"));
}
};
}
}
}
29 changes: 29 additions & 0 deletions trview.graphics.tests/VertexShaderTests.cpp
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>() }; });
}
};
}
}
}
171 changes: 171 additions & 0 deletions trview.graphics.tests/trview.graphics.tests.vcxproj
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>
8 changes: 8 additions & 0 deletions trview.graphics.tests/trview.graphics.tests.vcxproj.filters
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>
18 changes: 18 additions & 0 deletions trview.graphics/IShader.h
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;
};
}
}

11 changes: 11 additions & 0 deletions trview.graphics/IShaderStorage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "IShaderStorage.h"

namespace trview
{
namespace graphics
{
IShaderStorage::~IShaderStorage()
{
}
}
}
27 changes: 27 additions & 0 deletions trview.graphics/IShaderStorage.h
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;
};
}
}
22 changes: 22 additions & 0 deletions trview.graphics/PixelShader.cpp
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);
}
}
}
Loading

0 comments on commit 0db3839

Please sign in to comment.