1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "RenderAPI/BsDepthStencilState.h"
4#include "Managers/BsRenderStateManager.h"
5#include "RenderAPI/BsRenderAPI.h"
6#include "Private/RTTI/BsDepthStencilStateRTTI.h"
7#include "Error/BsException.h"
8#include "Resources/BsResources.h"
9
10namespace bs
11{
12 bool DEPTH_STENCIL_STATE_DESC::operator == (const DEPTH_STENCIL_STATE_DESC& rhs) const
13 {
14 return depthReadEnable == rhs.depthReadEnable &&
15 depthWriteEnable == rhs.depthWriteEnable &&
16 depthComparisonFunc == rhs.depthComparisonFunc &&
17 stencilEnable == rhs.stencilEnable &&
18 stencilReadMask == rhs.stencilReadMask &&
19 stencilWriteMask == rhs.stencilWriteMask &&
20 frontStencilFailOp == rhs.frontStencilFailOp &&
21 frontStencilZFailOp == rhs.frontStencilZFailOp &&
22 frontStencilPassOp == rhs.frontStencilPassOp &&
23 frontStencilComparisonFunc == rhs.frontStencilComparisonFunc &&
24 backStencilFailOp == rhs.backStencilFailOp &&
25 backStencilZFailOp == rhs.backStencilZFailOp &&
26 backStencilPassOp == rhs.backStencilPassOp &&
27 backStencilComparisonFunc == rhs.backStencilComparisonFunc;
28 }
29
30 DepthStencilProperties::DepthStencilProperties(const DEPTH_STENCIL_STATE_DESC& desc)
31 :mData(desc), mHash(DepthStencilState::generateHash(desc))
32 {
33
34 }
35
36 DepthStencilState::DepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc)
37 :mProperties(desc), mId(0)
38 {
39
40 }
41
42 SPtr<ct::DepthStencilState> DepthStencilState::getCore() const
43 {
44 return std::static_pointer_cast<ct::DepthStencilState>(mCoreSpecific);
45 }
46
47 SPtr<ct::CoreObject> DepthStencilState::createCore() const
48 {
49 SPtr<ct::DepthStencilState> core = ct::RenderStateManager::instance()._createDepthStencilState(mProperties.mData);
50 mId = core->getId(); // Accessing core from sim thread is okay here since core ID is immutable
51
52 return core;
53 }
54
55 const SPtr<DepthStencilState>& DepthStencilState::getDefault()
56 {
57 return RenderStateManager::instance().getDefaultDepthStencilState();
58 }
59
60 const DepthStencilProperties& DepthStencilState::getProperties() const
61 {
62 return mProperties;
63 }
64
65 SPtr<DepthStencilState> DepthStencilState::create(const DEPTH_STENCIL_STATE_DESC& desc)
66 {
67 return RenderStateManager::instance().createDepthStencilState(desc);
68 }
69
70 UINT64 DepthStencilState::generateHash(const DEPTH_STENCIL_STATE_DESC& desc)
71 {
72 size_t hash = 0;
73 bs_hash_combine(hash, desc.depthReadEnable);
74 bs_hash_combine(hash, desc.depthWriteEnable);
75 bs_hash_combine(hash, (UINT32)desc.depthComparisonFunc);
76 bs_hash_combine(hash, desc.stencilEnable);
77 bs_hash_combine(hash, desc.stencilReadMask);
78 bs_hash_combine(hash, desc.stencilWriteMask);
79 bs_hash_combine(hash, (UINT32)desc.frontStencilFailOp);
80 bs_hash_combine(hash, (UINT32)desc.frontStencilZFailOp);
81 bs_hash_combine(hash, (UINT32)desc.frontStencilPassOp);
82 bs_hash_combine(hash, (UINT32)desc.frontStencilComparisonFunc);
83 bs_hash_combine(hash, (UINT32)desc.backStencilFailOp);
84 bs_hash_combine(hash, (UINT32)desc.backStencilZFailOp);
85 bs_hash_combine(hash, (UINT32)desc.backStencilPassOp);
86 bs_hash_combine(hash, (UINT32)desc.backStencilComparisonFunc);
87
88 return (UINT64)hash;
89 }
90
91 /************************************************************************/
92 /* RTTI */
93 /************************************************************************/
94
95 RTTITypeBase* DepthStencilState::getRTTIStatic()
96 {
97 return DepthStencilStateRTTI::instance();
98 }
99
100 RTTITypeBase* DepthStencilState::getRTTI() const
101 {
102 return DepthStencilState::getRTTIStatic();
103 }
104
105 namespace ct
106 {
107 DepthStencilState::DepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc, UINT32 id)
108 : mProperties(desc), mId(id)
109 {
110
111 }
112
113 DepthStencilState::~DepthStencilState()
114 {
115
116 }
117
118 void DepthStencilState::initialize()
119 {
120 // Since we cache states it's possible this object was already initialized
121 // (i.e. multiple sim-states can share a single core-state)
122 if (isInitialized())
123 return;
124
125 createInternal();
126 CoreObject::initialize();
127 }
128
129 const DepthStencilProperties& DepthStencilState::getProperties() const
130 {
131 return mProperties;
132 }
133
134 SPtr<DepthStencilState> DepthStencilState::create(const DEPTH_STENCIL_STATE_DESC& desc)
135 {
136 return RenderStateManager::instance().createDepthStencilState(desc);
137 }
138
139 const SPtr<DepthStencilState>& DepthStencilState::getDefault()
140 {
141 return RenderStateManager::instance().getDefaultDepthStencilState();
142 }
143 }
144}