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/BsSamplerState.h"
4#include "Private/RTTI/BsSamplerStateRTTI.h"
5#include "Managers/BsRenderStateManager.h"
6
7namespace bs
8{
9 bool SAMPLER_STATE_DESC::operator == (const SAMPLER_STATE_DESC& rhs) const
10 {
11 return addressMode == rhs.addressMode &&
12 minFilter == rhs.minFilter &&
13 magFilter == rhs.magFilter &&
14 mipFilter == rhs.mipFilter &&
15 maxAniso == rhs.maxAniso &&
16 mipmapBias == rhs.mipmapBias &&
17 mipMin == rhs.mipMin &&
18 mipMax == rhs.mipMax &&
19 borderColor == rhs.borderColor &&
20 comparisonFunc == rhs.comparisonFunc;
21 }
22
23 SamplerProperties::SamplerProperties(const SAMPLER_STATE_DESC& desc)
24 :mData(desc), mHash(SamplerState::generateHash(desc))
25 { }
26
27 FilterOptions SamplerProperties::getTextureFiltering(FilterType ft) const
28 {
29 switch (ft)
30 {
31 case FT_MIN:
32 return mData.minFilter;
33 case FT_MAG:
34 return mData.magFilter;
35 case FT_MIP:
36 return mData.mipFilter;
37 }
38
39 return mData.minFilter;
40 }
41
42 const Color& SamplerProperties::getBorderColor() const
43 {
44 return mData.borderColor;
45 }
46
47 SamplerState::SamplerState(const SAMPLER_STATE_DESC& desc)
48 :mProperties(desc)
49 {
50
51 }
52
53 SPtr<ct::SamplerState> SamplerState::getCore() const
54 {
55 return std::static_pointer_cast<ct::SamplerState>(mCoreSpecific);
56 }
57
58 SPtr<ct::CoreObject> SamplerState::createCore() const
59 {
60 return ct::RenderStateManager::instance()._createSamplerState(mProperties.mData);
61 }
62
63 SPtr<SamplerState> SamplerState::create(const SAMPLER_STATE_DESC& desc)
64 {
65 return RenderStateManager::instance().createSamplerState(desc);
66 }
67
68 const SPtr<SamplerState>& SamplerState::getDefault()
69 {
70 return RenderStateManager::instance().getDefaultSamplerState();
71 }
72
73 UINT64 SamplerState::generateHash(const SAMPLER_STATE_DESC& desc)
74 {
75 size_t hash = 0;
76 bs_hash_combine(hash, (UINT32)desc.addressMode.u);
77 bs_hash_combine(hash, (UINT32)desc.addressMode.v);
78 bs_hash_combine(hash, (UINT32)desc.addressMode.w);
79 bs_hash_combine(hash, (UINT32)desc.minFilter);
80 bs_hash_combine(hash, (UINT32)desc.magFilter);
81 bs_hash_combine(hash, (UINT32)desc.mipFilter);
82 bs_hash_combine(hash, desc.maxAniso);
83 bs_hash_combine(hash, desc.mipmapBias);
84 bs_hash_combine(hash, desc.mipMin);
85 bs_hash_combine(hash, desc.mipMax);
86 bs_hash_combine(hash, desc.borderColor);
87 bs_hash_combine(hash, (UINT32)desc.comparisonFunc);
88
89 return (UINT64)hash;
90 }
91
92 const SamplerProperties& SamplerState::getProperties() const
93 {
94 return mProperties;
95 }
96
97 /************************************************************************/
98 /* RTTI */
99 /************************************************************************/
100
101 RTTITypeBase* SamplerState::getRTTIStatic()
102 {
103 return SamplerStateRTTI::instance();
104 }
105
106 RTTITypeBase* SamplerState::getRTTI() const
107 {
108 return SamplerState::getRTTIStatic();
109 }
110
111 namespace ct
112 {
113
114 SamplerState::SamplerState(const SAMPLER_STATE_DESC& desc, GpuDeviceFlags deviceMask)
115 :mProperties(desc)
116 {
117
118 }
119
120 SamplerState::~SamplerState()
121 {
122 RenderStateManager::instance().notifySamplerStateDestroyed(mProperties.mData);
123 }
124
125 void SamplerState::initialize()
126 {
127 // Since we cache states it's possible this object was already initialized
128 // (i.e. multiple sim-states can share a single core-state)
129 if (isInitialized())
130 return;
131
132 createInternal();
133 CoreObject::initialize();
134 }
135
136 const SamplerProperties& SamplerState::getProperties() const
137 {
138 return mProperties;
139 }
140
141 SPtr<SamplerState> SamplerState::create(const SAMPLER_STATE_DESC& desc, GpuDeviceFlags deviceMask)
142 {
143 return RenderStateManager::instance().createSamplerState(desc, deviceMask);
144 }
145
146 const SPtr<SamplerState>& SamplerState::getDefault()
147 {
148 return RenderStateManager::instance().getDefaultSamplerState();
149 }
150
151 }
152}
153