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#pragma once
4
5#include "BsCorePrerequisites.h"
6#include "CoreThread/BsCoreObject.h"
7
8namespace bs
9{
10 /** @addtogroup RenderAPI
11 * @{
12 */
13
14 /** Descriptor structure used for initializing a GPU pipeline state. */
15 struct PIPELINE_STATE_DESC
16 {
17 SPtr<BlendState> blendState;
18 SPtr<RasterizerState> rasterizerState;
19 SPtr<DepthStencilState> depthStencilState;
20
21 SPtr<GpuProgram> vertexProgram;
22 SPtr<GpuProgram> fragmentProgram;
23 SPtr<GpuProgram> geometryProgram;
24 SPtr<GpuProgram> hullProgram;
25 SPtr<GpuProgram> domainProgram;
26 };
27
28 /** @} */
29
30 namespace ct
31 {
32 /** @addtogroup RenderAPI-Internal
33 * @{
34 */
35
36 /** Descriptor structure used for initializing a GPU pipeline state. */
37 struct PIPELINE_STATE_DESC
38 {
39 SPtr<BlendState> blendState;
40 SPtr<RasterizerState> rasterizerState;
41 SPtr<DepthStencilState> depthStencilState;
42
43 SPtr<GpuProgram> vertexProgram;
44 SPtr<GpuProgram> fragmentProgram;
45 SPtr<GpuProgram> geometryProgram;
46 SPtr<GpuProgram> hullProgram;
47 SPtr<GpuProgram> domainProgram;
48 };
49
50 /** @} */
51 }
52
53 /** @addtogroup Implementation
54 * @{
55 */
56
57 /** Contains all data used by a GPU pipeline state, templated so it may contain both core and sim thread data. */
58 template<bool Core>
59 struct TGpuPipelineStateTypes
60 { };
61
62 template<>
63 struct TGpuPipelineStateTypes < false >
64 {
65 typedef GpuPipelineParamInfo GpuPipelineParamInfoType;
66 typedef PIPELINE_STATE_DESC StateDescType;
67 };
68
69 template<>
70 struct TGpuPipelineStateTypes < true >
71 {
72 typedef ct::GpuPipelineParamInfo GpuPipelineParamInfoType;
73 typedef ct::PIPELINE_STATE_DESC StateDescType;
74 };
75
76 /**
77 * Templated version of GraphicsPipelineState so it can be used for both core and non-core versions of the pipeline
78 * state.
79 */
80 template<bool Core>
81 class BS_CORE_EXPORT TGraphicsPipelineState
82 {
83 public:
84 using BlendStateType = SPtr<CoreVariantType<BlendState, Core>>;
85 using RasterizerStateType = SPtr<CoreVariantType<RasterizerState, Core>>;
86 using DepthStencilStateType = SPtr<CoreVariantType<DepthStencilState, Core>>;
87 using GpuProgramType = SPtr<CoreVariantType<GpuProgram, Core>>;
88 using StateDescType = typename TGpuPipelineStateTypes<Core>::StateDescType;
89 using GpuPipelineParamInfoType = typename TGpuPipelineStateTypes<Core>::GpuPipelineParamInfoType;
90
91 virtual ~TGraphicsPipelineState() = default;
92
93 bool hasVertexProgram() const { return mData.vertexProgram != nullptr; }
94 bool hasFragmentProgram() const { return mData.fragmentProgram != nullptr; }
95 bool hasGeometryProgram() const { return mData.geometryProgram != nullptr; }
96 bool hasHullProgram() const { return mData.hullProgram != nullptr; }
97 bool hasDomainProgram() const { return mData.domainProgram != nullptr; }
98
99 BlendStateType getBlendState() const { return mData.blendState; }
100 RasterizerStateType getRasterizerState() const { return mData.rasterizerState; }
101 DepthStencilStateType getDepthStencilState() const { return mData.depthStencilState; }
102
103 const GpuProgramType& getVertexProgram() const { return mData.vertexProgram; }
104 const GpuProgramType& getFragmentProgram() const { return mData.fragmentProgram; }
105 const GpuProgramType& getGeometryProgram() const { return mData.geometryProgram; }
106 const GpuProgramType& getHullProgram() const { return mData.hullProgram; }
107 const GpuProgramType& getDomainProgram() const { return mData.domainProgram; }
108
109 /** Returns an object containing meta-data for parameters of all GPU programs used in this pipeline state. */
110 const SPtr<GpuPipelineParamInfoType>& getParamInfo() const { return mParamInfo; }
111
112 protected:
113 TGraphicsPipelineState() = default;
114 TGraphicsPipelineState(const StateDescType& desc);
115
116 StateDescType mData;
117 SPtr<GpuPipelineParamInfoType> mParamInfo;
118 };
119
120 /**
121 * Templated version of ComputePipelineState so it can be used for both core and non-core versions of the pipeline
122 * state.
123 */
124 template<bool Core>
125 class BS_CORE_EXPORT TComputePipelineState
126 {
127 public:
128 using GpuProgramType = SPtr<CoreVariantType<GpuProgram, Core>>;
129 using GpuPipelineParamInfoType = typename TGpuPipelineStateTypes<Core>::GpuPipelineParamInfoType;
130
131 virtual ~TComputePipelineState() { }
132
133 const GpuProgramType& getProgram() const { return mProgram; }
134
135 /** Returns an object containing meta-data for parameters of the GPU program used in this pipeline state. */
136 const SPtr<GpuPipelineParamInfoType>& getParamInfo() const { return mParamInfo; }
137
138 protected:
139 TComputePipelineState();
140 TComputePipelineState(const GpuProgramType& program);
141
142 GpuProgramType mProgram;
143 SPtr<GpuPipelineParamInfoType> mParamInfo;
144 };
145
146 /** @} */
147
148 /** @addtogroup RenderAPI
149 * @{
150 */
151
152 /**
153 * Describes the state of the GPU pipeline that determines how are primitives rendered. It consists of programmable
154 * states (vertex, fragment, geometry, etc. GPU programs), as well as a set of fixed states (blend, rasterizer,
155 * depth-stencil). Once created the state is immutable, and can be bound to RenderAPI for rendering.
156 */
157 class BS_CORE_EXPORT GraphicsPipelineState : public CoreObject, public TGraphicsPipelineState<false>
158 {
159 public:
160 virtual ~GraphicsPipelineState() { }
161
162 /**
163 * Retrieves a core implementation of the pipeline object usable only from the core thread.
164 *
165 * @note Core thread only.
166 */
167 SPtr<ct::GraphicsPipelineState> getCore() const;
168
169 /** @copydoc RenderStateManager::createGraphicsPipelineState */
170 static SPtr<GraphicsPipelineState> create(const PIPELINE_STATE_DESC& desc);
171 protected:
172 friend class RenderStateManager;
173
174 GraphicsPipelineState(const PIPELINE_STATE_DESC& desc);
175
176 /** @copydoc CoreObject::createCore */
177 virtual SPtr<ct::CoreObject> createCore() const;
178 };
179
180 /**
181 * Describes the state of the GPU pipeline that determines how are compute programs executed. It consists of
182 * of a single programmable state (GPU program). Once created the state is immutable, and can be bound to RenderAPI for
183 * use.
184 */
185 class BS_CORE_EXPORT ComputePipelineState : public CoreObject, public TComputePipelineState<false>
186 {
187 public:
188 virtual ~ComputePipelineState() { }
189
190 /**
191 * Retrieves a core implementation of the pipeline object usable only from the core thread.
192 *
193 * @note Core thread only.
194 */
195 SPtr<ct::ComputePipelineState> getCore() const;
196
197 /** @copydoc RenderStateManager::createComputePipelineState */
198 static SPtr<ComputePipelineState> create(const SPtr<GpuProgram>& program);
199 protected:
200 friend class RenderStateManager;
201
202 ComputePipelineState(const SPtr<GpuProgram>& program);
203
204 /** @copydoc CoreObject::createCore */
205 virtual SPtr<ct::CoreObject> createCore() const;
206 };
207
208 /** @} */
209
210 namespace ct
211 {
212 /** @addtogroup RenderAPI-Internal
213 * @{
214 */
215
216 /** Core thread version of a bs::GraphicsPipelineState. */
217 class BS_CORE_EXPORT GraphicsPipelineState : public CoreObject, public TGraphicsPipelineState<true>
218 {
219 public:
220 GraphicsPipelineState(const PIPELINE_STATE_DESC& desc, GpuDeviceFlags deviceMask);
221 virtual ~GraphicsPipelineState() { }
222
223 /** @copydoc CoreObject::initialize() */
224 void initialize() override;
225
226 /** @copydoc RenderStateManager::createGraphicsPipelineState */
227 static SPtr<GraphicsPipelineState> create(const PIPELINE_STATE_DESC& desc,
228 GpuDeviceFlags deviceMask = GDF_DEFAULT);
229
230 protected:
231 GpuDeviceFlags mDeviceMask;
232 };
233
234 /** Core thread version of a bs::ComputePipelineState. */
235 class BS_CORE_EXPORT ComputePipelineState : public CoreObject, public TComputePipelineState<true>
236 {
237 public:
238 ComputePipelineState(const SPtr<GpuProgram>& program, GpuDeviceFlags deviceMask);
239 virtual ~ComputePipelineState() { }
240
241 /** @copydoc CoreObject::initialize() */
242 void initialize() override;
243
244 /** @copydoc RenderStateManager::createComputePipelineState */
245 static SPtr<ComputePipelineState> create(const SPtr<GpuProgram>& program,
246 GpuDeviceFlags deviceMask = GDF_DEFAULT);
247
248 protected:
249 GpuDeviceFlags mDeviceMask;
250 };
251
252 /** @} */
253 }
254}