Commit c1f64ca7 authored by Administrator's avatar Administrator
Browse files

shared some bindables among same shape

VertexShader
PixelShader
IndexBuffer
InputLayout
Topology
parent 57d362d4
...@@ -23,15 +23,6 @@ Box::Box(Graphics& gfx, float position[][3], float color[][3], Logger& logger) ...@@ -23,15 +23,6 @@ Box::Box(Graphics& gfx, float position[][3], float color[][3], Logger& logger)
} }
} }
// Assign color values
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 3; j++)
{
this->color[i][j] = color[i][j];
}
}
/************************************ /************************************
* 2.3 DefineVertexBuffer * 2.3 DefineVertexBuffer
************************************/ ************************************/
...@@ -49,32 +40,14 @@ Box::Box(Graphics& gfx, float position[][3], float color[][3], Logger& logger) ...@@ -49,32 +40,14 @@ Box::Box(Graphics& gfx, float position[][3], float color[][3], Logger& logger)
AddBind(std::make_unique<VertexBuffer>(gfx, vertices, logger)); AddBind(std::make_unique<VertexBuffer>(gfx, vertices, logger));
/************************************ // Assign color values
* 2.7 DefineVertexShader for (int i = 0; i < 6; i++)
************************************/
auto pVertexShader = std::make_unique<VertexShader>(gfx, L"VertexShader.cso");
auto pvsbc = pVertexShader->GetBytecode();
AddBind(std::move(pVertexShader));
/************************************
* 2.8 DefinePixelShader
************************************/
AddBind(std::make_unique<PixelShader>(gfx, L"PixelShader.cso"));
/************************************
* 2.4 DefineIndexBuffer
************************************/
const std::vector<unsigned short> indices =
{ {
0,2,1, 2,3,1, for (int j = 0; j < 3; j++)
1,3,5, 3,7,5, {
2,6,3, 3,6,7, this->color[i][j] = color[i][j];
4,5,7, 4,7,6, }
0,4,2, 2,4,6, }
0,1,4, 1,5,4
};
AddIndexBuffer(std::make_unique<IndexBuffer>(gfx, indices, logger));
/************************************ /************************************
* 2.6 DefineColorConstBuffer(PixelConstBuf) * 2.6 DefineColorConstBuffer(PixelConstBuf)
...@@ -99,25 +72,63 @@ Box::Box(Graphics& gfx, float position[][3], float color[][3], Logger& logger) ...@@ -99,25 +72,63 @@ Box::Box(Graphics& gfx, float position[][3], float color[][3], Logger& logger)
AddBind(std::make_unique<PixelConstantBuffer<StructPixelConstBuf>>(gfx, cb2)); AddBind(std::make_unique<PixelConstantBuffer<StructPixelConstBuf>>(gfx, cb2));
/************************************
* 2.9 DefineInputLayout
************************************/
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied =
{
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
};
AddBind(std::make_unique<InputLayout>(gfx, ied, pvsbc));
/************************************
* 2.10 DefinePrimitiveTopology
************************************/
AddBind(std::make_unique<Topology>(gfx, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST));
/************************************ /************************************
* 2.5 DefineTransConstBuffer * 2.5 DefineTransConstBuffer
************************************/ ************************************/
AddBind(std::make_unique<TransformCbuf>(gfx, *this)); AddBind(std::make_unique<TransformCbuf>(gfx, *this));
if (!IsStaticInitialized())
{
/************************************
* 2.7 DefineVertexShader
************************************/
auto pVertexShader = std::make_unique<VertexShader>(gfx, L"VertexShader.cso");
auto pvsbc = pVertexShader->GetBytecode();
//AddBind(std::move(pVertexShader));
AddStaticBind(std::move(pVertexShader));
/************************************
* 2.8 DefinePixelShader
************************************/
//AddBind(std::make_unique<PixelShader>(gfx, L"PixelShader.cso"));
AddStaticBind(std::make_unique<PixelShader>(gfx, L"PixelShader.cso"));
/************************************
* 2.4 DefineIndexBuffer
************************************/
const std::vector<unsigned short> indices =
{
0,2,1, 2,3,1,
1,3,5, 3,7,5,
2,6,3, 3,6,7,
4,5,7, 4,7,6,
0,4,2, 2,4,6,
0,1,4, 1,5,4
};
//AddIndexBuffer(std::make_unique<IndexBuffer>(gfx, indices, logger));
AddStaticIndexBuffer(std::make_unique<IndexBuffer>(gfx, indices, logger));
/************************************
* 2.9 DefineInputLayout
************************************/
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied =
{
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
};
//AddBind(std::make_unique<InputLayout>(gfx, ied, pvsbc));
AddStaticBind(std::make_unique<InputLayout>(gfx, ied, pvsbc));
/************************************
* 2.10 DefinePrimitiveTopology
************************************/
//AddBind(std::make_unique<Topology>(gfx, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST));
AddStaticBind(std::make_unique<Topology>(gfx, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST));
}
else
{
SetIndexFromStatic();
}
} }
void Box::Update(float dt) noexcept void Box::Update(float dt) noexcept
......
#pragma once #pragma once
#include "Drawable.h" #include "DrawableBase.h"
#include "Logger.h" #include "Logger.h"
class Box : public Drawable class Box : public DrawableBase<Box>
{ {
public: public:
Box(Graphics& gfx, float position[][3], float color[][3], Logger& logger); Box(Graphics& gfx, float position[][3], float color[][3], Logger& logger);
......
...@@ -18,6 +18,10 @@ void Drawable::ExecuteBind(Graphics& gfx, Logger& logger) noexcept ...@@ -18,6 +18,10 @@ void Drawable::ExecuteBind(Graphics& gfx, Logger& logger) noexcept
{ {
b->Bind(gfx, logger); b->Bind(gfx, logger);
} }
for (auto& b : GetStaticBinds())
{
b->Bind(gfx, logger);
}
}; };
void Drawable::AddBind(std::unique_ptr<Bindable> bind) noexcept void Drawable::AddBind(std::unique_ptr<Bindable> bind) noexcept
......
...@@ -11,6 +11,8 @@ class Bindable; ...@@ -11,6 +11,8 @@ class Bindable;
class Drawable class Drawable
{ {
template<class T>
friend class DrawableBase; // Enable DrawableBase to access member of Drawable
public: public:
Drawable() = default; Drawable() = default;
Drawable(const Drawable&) = delete; Drawable(const Drawable&) = delete;
...@@ -30,6 +32,7 @@ public: ...@@ -30,6 +32,7 @@ public:
private: private:
const IndexBuffer* pIndexBuffer = nullptr; const IndexBuffer* pIndexBuffer = nullptr;
std::vector<std::unique_ptr<Bindable>> binds; std::vector<std::unique_ptr<Bindable>> binds;
virtual const std::vector<std::unique_ptr<Bindable>>& GetStaticBinds() const noexcept = 0;
protected: protected:
// for camera // for camera
DirectX::XMVECTOR upDirection; DirectX::XMVECTOR upDirection;
......
#pragma once
#include "Drawable.h"
#include "IndexBuffer.h"
template<class T>
class DrawableBase : public Drawable
{
public:
bool IsStaticInitialized() const noexcept
{
return !staticBinds.empty();
}
void AddStaticBind(std::unique_ptr<Bindable> bind) noexcept
{
assert("*Must* use AddIndexBuffer to bind index buffer" && typeid(*bind) != typeid(IndexBuffer));
staticBinds.push_back(std::move(bind));
}
void AddStaticIndexBuffer(std::unique_ptr<IndexBuffer> ibuf) noexcept
{
assert("Attempting to add index buffer a second time" && pIndexBuffer == nullptr);
pIndexBuffer = ibuf.get();
staticBinds.push_back(std::move(ibuf));
}
void SetIndexFromStatic() noexcept
{
assert("Attempting to add index buffer a second time" && pIndexBuffer == nullptr);
for (const auto& b : staticBinds)
{
if (const auto p = dynamic_cast<IndexBuffer*>(b.get()))
{
pIndexBuffer = p;
return;
}
}
assert("Failed to find index buffer in static binds" && pIndexBuffer != nullptr);
}
private:
const std::vector<std::unique_ptr<Bindable>>& GetStaticBinds() const noexcept override
{
return staticBinds;
}
private:
static std::vector<std::unique_ptr<Bindable>> staticBinds;
};
template<class T>
std::vector<std::unique_ptr<Bindable>> DrawableBase<T>::staticBinds;
...@@ -131,6 +131,7 @@ ...@@ -131,6 +131,7 @@
<ClCompile Include="Box.cpp" /> <ClCompile Include="Box.cpp" />
<ClCompile Include="ConstantBuffer.h" /> <ClCompile Include="ConstantBuffer.h" />
<ClCompile Include="Drawable.cpp" /> <ClCompile Include="Drawable.cpp" />
<ClCompile Include="DrawableBase.cpp" />
<ClCompile Include="Graphics.cpp" /> <ClCompile Include="Graphics.cpp" />
<ClCompile Include="Ground.cpp" /> <ClCompile Include="Ground.cpp" />
<ClCompile Include="IndexBuffer.cpp" /> <ClCompile Include="IndexBuffer.cpp" />
...@@ -170,6 +171,7 @@ ...@@ -170,6 +171,7 @@
<ClInclude Include="Bindable.h" /> <ClInclude Include="Bindable.h" />
<ClInclude Include="Box.h" /> <ClInclude Include="Box.h" />
<ClInclude Include="Drawable.h" /> <ClInclude Include="Drawable.h" />
<ClInclude Include="DrawableBase.h" />
<ClInclude Include="Graphics.h" /> <ClInclude Include="Graphics.h" />
<ClInclude Include="Ground.h" /> <ClInclude Include="Ground.h" />
<ClInclude Include="IndexBuffer.h" /> <ClInclude Include="IndexBuffer.h" />
......
...@@ -96,6 +96,9 @@ ...@@ -96,6 +96,9 @@
<ClCompile Include="Ground.cpp"> <ClCompile Include="Ground.cpp">
<Filter>Source Files\Scenes</Filter> <Filter>Source Files\Scenes</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="DrawableBase.cpp">
<Filter>Source Files\Drawables</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<FxCompile Include="PixelShader.hlsl"> <FxCompile Include="PixelShader.hlsl">
...@@ -157,5 +160,8 @@ ...@@ -157,5 +160,8 @@
<ClInclude Include="Ground.h"> <ClInclude Include="Ground.h">
<Filter>Header Files\Scenes</Filter> <Filter>Header Files\Scenes</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="DrawableBase.h">
<Filter>Header Files\Drawables</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment