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/BsRasterizerState.h"
4#include "Managers/BsRenderStateManager.h"
5#include "RenderAPI/BsRenderAPI.h"
6#include "Private/RTTI/BsRasterizerStateRTTI.h"
7#include "Resources/BsResources.h"
8
9namespace bs
10{
11 bool RASTERIZER_STATE_DESC::operator == (const RASTERIZER_STATE_DESC& rhs) const
12 {
13 return polygonMode == rhs.polygonMode &&
14 cullMode == rhs.cullMode &&
15 depthBias == rhs.depthBias &&
16 depthBiasClamp == rhs.depthBiasClamp &&
17 slopeScaledDepthBias == rhs.slopeScaledDepthBias &&
18 depthClipEnable == rhs.depthClipEnable &&
19 scissorEnable == rhs.scissorEnable &&
20 multisampleEnable == rhs.multisampleEnable &&
21 antialiasedLineEnable == rhs.antialiasedLineEnable;
22 }
23
24 RasterizerProperties::RasterizerProperties(const RASTERIZER_STATE_DESC& desc)
25 :mData(desc), mHash(RasterizerState::generateHash(desc))
26 { }
27
28 RasterizerState::RasterizerState(const RASTERIZER_STATE_DESC& desc)
29 : mProperties(desc), mId(0)
30 {
31
32 }
33
34 RasterizerState::~RasterizerState()
35 {
36
37 }
38
39 SPtr<ct::RasterizerState> RasterizerState::getCore() const
40 {
41 return std::static_pointer_cast<ct::RasterizerState>(mCoreSpecific);
42 }
43
44 SPtr<ct::CoreObject> RasterizerState::createCore() const
45 {
46 SPtr<ct::RasterizerState> core = ct::RenderStateManager::instance()._createRasterizerState(mProperties.mData);
47 mId = core->getId(); // Accessing core from sim thread is okay here since core ID is immutable
48
49 return core;
50 }
51
52 const RasterizerProperties& RasterizerState::getProperties() const
53 {
54 return mProperties;
55 }
56
57 const SPtr<RasterizerState>& RasterizerState::getDefault()
58 {
59 return RenderStateManager::instance().getDefaultRasterizerState();
60 }
61
62 SPtr<RasterizerState> RasterizerState::create(const RASTERIZER_STATE_DESC& desc)
63 {
64 return RenderStateManager::instance().createRasterizerState(desc);
65 }
66
67 UINT64 RasterizerState::generateHash(const RASTERIZER_STATE_DESC& desc)
68 {
69 size_t hash = 0;
70 bs_hash_combine(hash, (UINT32)desc.polygonMode);
71 bs_hash_combine(hash, (UINT32)desc.cullMode);
72 bs_hash_combine(hash, desc.depthBias);
73 bs_hash_combine(hash, desc.depthBiasClamp);
74 bs_hash_combine(hash, desc.slopeScaledDepthBias);
75 bs_hash_combine(hash, desc.depthClipEnable);
76 bs_hash_combine(hash, desc.scissorEnable);
77 bs_hash_combine(hash, desc.multisampleEnable);
78 bs_hash_combine(hash, desc.antialiasedLineEnable);
79
80 return (UINT64)hash;
81 }
82
83 /************************************************************************/
84 /* RTTI */
85 /************************************************************************/
86
87 RTTITypeBase* RasterizerState::getRTTIStatic()
88 {
89 return RasterizerStateRTTI::instance();
90 }
91
92 RTTITypeBase* RasterizerState::getRTTI() const
93 {
94 return RasterizerState::getRTTIStatic();
95 }
96
97 namespace ct
98 {
99 RasterizerState::RasterizerState(const RASTERIZER_STATE_DESC& desc, UINT32 id)
100 : mProperties(desc), mId(id)
101 {
102
103 }
104
105 RasterizerState::~RasterizerState()
106 {
107
108 }
109
110 void RasterizerState::initialize()
111 {
112 // Since we cache states it's possible this object was already initialized
113 // (i.e. multiple sim-states can share a single core-state)
114 if (isInitialized())
115 return;
116
117 createInternal();
118 CoreObject::initialize();
119 }
120
121 const RasterizerProperties& RasterizerState::getProperties() const
122 {
123 return mProperties;
124 }
125
126 SPtr<RasterizerState> RasterizerState::create(const RASTERIZER_STATE_DESC& desc)
127 {
128 return RenderStateManager::instance().createRasterizerState(desc);
129 }
130
131 const SPtr<RasterizerState>& RasterizerState::getDefault()
132 {
133 return RenderStateManager::instance().getDefaultRasterizerState();
134 }
135 }
136}