| 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 "Resources/BsResource.h" |
| 7 | #include "String/BsStringID.h" |
| 8 | #include "Resources/BsResourceMetaData.h" |
| 9 | #include "Material/BsTechnique.h" |
| 10 | |
| 11 | namespace bs |
| 12 | { |
| 13 | /** @addtogroup Implementation |
| 14 | * @{ |
| 15 | */ |
| 16 | |
| 17 | class Shader; |
| 18 | |
| 19 | namespace ct |
| 20 | { |
| 21 | class Shader; |
| 22 | } |
| 23 | |
| 24 | /** Templated version of SubShader that can be used for both core and sim threads. */ |
| 25 | template<bool Core> |
| 26 | struct TSubShader |
| 27 | { |
| 28 | using TechniqueType = CoreVariantType<Technique, Core>; |
| 29 | using ShaderType = SPtr<CoreVariantType<Shader, Core>>; |
| 30 | |
| 31 | String name; |
| 32 | ShaderType shader; |
| 33 | }; |
| 34 | |
| 35 | /** Shared memebers between SHADER_DATA_PARAM_DESC and SHADER_OBJECT_PARAM_DESC */ |
| 36 | struct SHADER_PARAM_COMMON |
| 37 | { |
| 38 | SHADER_PARAM_COMMON() = default; |
| 39 | SHADER_PARAM_COMMON(String name, String gpuVariableName, StringID rendererSemantic = StringID::NONE) |
| 40 | : name(std::move(name)), gpuVariableName(gpuVariableName), rendererSemantic(rendererSemantic) |
| 41 | { } |
| 42 | |
| 43 | /** The name of the parameter. Name must be unique between all data and object parameters in a shader. */ |
| 44 | String name; |
| 45 | |
| 46 | /** Name of the GPU variable in the GpuProgram that the parameter corresponds with. */ |
| 47 | String gpuVariableName; |
| 48 | |
| 49 | /** |
| 50 | * Optional semantic that allows you to specify the use of this parameter in the renderer. The actual value of the |
| 51 | * semantic depends on the current Renderer and its supported list of semantics. Elements with renderer semantics |
| 52 | * should not be updated by the user, and will be updated by the renderer. These semantics will also be used to |
| 53 | * determine if a shader is compatible with a specific renderer or not. Value of 0 signifies the parameter is not |
| 54 | * used by the renderer. |
| 55 | */ |
| 56 | StringID rendererSemantic; |
| 57 | |
| 58 | /** Index of the default value inside the Shader. Should not be set externally by the user. */ |
| 59 | UINT32 defaultValueIdx = (UINT32)-1; |
| 60 | |
| 61 | /** Index to a set of optional attributes attached to the parameter. Should not be set externally by the user. */ |
| 62 | UINT32 attribIdx = (UINT32)-1; |
| 63 | }; |
| 64 | |
| 65 | /** @} */ |
| 66 | |
| 67 | /** @addtogroup Material |
| 68 | * @{ |
| 69 | */ |
| 70 | |
| 71 | /** |
| 72 | * Describes a single data (int, Vector2, etc.) shader parameter. |
| 73 | * |
| 74 | * @see Shader::addParameter(). |
| 75 | */ |
| 76 | struct SHADER_DATA_PARAM_DESC : SHADER_PARAM_COMMON |
| 77 | { |
| 78 | SHADER_DATA_PARAM_DESC() = default; |
| 79 | SHADER_DATA_PARAM_DESC(String name, String gpuVariableName, GpuParamDataType type, |
| 80 | StringID rendererSemantic = StringID::NONE, UINT32 arraySize = 1, UINT32 elementSize = 0) |
| 81 | :SHADER_PARAM_COMMON(std::move(name), std::move(gpuVariableName), rendererSemantic) |
| 82 | , type(type), arraySize(arraySize), elementSize(elementSize) |
| 83 | { } |
| 84 | |
| 85 | /** The type of the parameter, must be the same as the type in GpuProgram. */ |
| 86 | GpuParamDataType type = GPDT_FLOAT1; |
| 87 | |
| 88 | /** If the parameter is an array, the number of elements in the array. Size of 1 means its not an array. */ |
| 89 | UINT32 arraySize = 1; |
| 90 | |
| 91 | /** |
| 92 | * Size of an individual element in the array, in bytes. You only need to set this if you are setting variable |
| 93 | * length parameters, like structs. Otherwise the size is determined from the type. |
| 94 | */ |
| 95 | UINT32 elementSize = 0; |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * Describes a single object (texture, sampler state, etc.) shader parameter. |
| 100 | * |
| 101 | * @see Shader::addParameter(). |
| 102 | */ |
| 103 | struct SHADER_OBJECT_PARAM_DESC : SHADER_PARAM_COMMON |
| 104 | { |
| 105 | SHADER_OBJECT_PARAM_DESC() = default; |
| 106 | SHADER_OBJECT_PARAM_DESC(String name, String gpuVariableName, GpuParamObjectType type, |
| 107 | StringID rendererSemantic = StringID::NONE) |
| 108 | :SHADER_PARAM_COMMON(std::move(name), gpuVariableName, rendererSemantic), type(type) |
| 109 | { |
| 110 | gpuVariableNames.emplace_back(gpuVariableName); |
| 111 | } |
| 112 | |
| 113 | /** The type of the parameter, must be the same as the type in GpuProgram. */ |
| 114 | GpuParamObjectType type = GPOT_TEXTURE2D; |
| 115 | |
| 116 | /** Names of all GPU variables this shader parameter maps to. */ |
| 117 | Vector<String> gpuVariableNames; |
| 118 | }; |
| 119 | |
| 120 | /** Describes a shader parameter block. */ |
| 121 | struct SHADER_PARAM_BLOCK_DESC |
| 122 | { |
| 123 | String name; |
| 124 | bool shared; |
| 125 | StringID rendererSemantic; |
| 126 | GpuBufferUsage usage; |
| 127 | }; |
| 128 | |
| 129 | /** Available attribute types that can be assigned to Shader parameters. */ |
| 130 | enum class ShaderParamAttributeType |
| 131 | { |
| 132 | /** |
| 133 | * Selects a 4D vector to use for storing UV offset and size when rendering a subset of a larger texture (e.g. |
| 134 | * when attaching a SpriteTexture to the material parameter). The attribute value is a string naming the texture |
| 135 | * parameter that contains the texture whose subset the UV represents. |
| 136 | */ |
| 137 | SpriteUV, |
| 138 | |
| 139 | /** Specifies a human readable name of the shader parameter. */ |
| 140 | Name, |
| 141 | |
| 142 | /** Hides the parameter from the display in editor inspector. */ |
| 143 | HideInInspector |
| 144 | }; |
| 145 | |
| 146 | /** Optional attribute that can be applied to a shader parameter. */ |
| 147 | struct SHADER_PARAM_ATTRIBUTE |
| 148 | { |
| 149 | /** Type of the attribute. */ |
| 150 | ShaderParamAttributeType type = (ShaderParamAttributeType)0; |
| 151 | |
| 152 | /** Value of the parameter encoded as a string. */ |
| 153 | String value; |
| 154 | |
| 155 | /** Index of the next attribute in the linked list for this parameter. Should not be set externally by the user. */ |
| 156 | UINT32 nextParamIdx = (UINT32)-1; |
| 157 | }; |
| 158 | |
| 159 | /** Represents a single potential value of a shader variation parameter and optionally its name. */ |
| 160 | struct BS_SCRIPT_EXPORT(m:Renderer,pl:true) ShaderVariationParamValue |
| 161 | { |
| 162 | /** Optional human-readable name describing what this particular value represents. */ |
| 163 | String name; |
| 164 | |
| 165 | /** Integer value of the parameter. */ |
| 166 | INT32 value = 0; |
| 167 | }; |
| 168 | |
| 169 | /** Represents a single shader variation parameter and a set of all possible values. */ |
| 170 | struct BS_SCRIPT_EXPORT(m:Renderer,pl:true) ShaderVariationParamInfo |
| 171 | { |
| 172 | /** Optional human-readable name describing the variation parameter. */ |
| 173 | String name; |
| 174 | |
| 175 | /** BSL identifier for the parameter. */ |
| 176 | String identifier; |
| 177 | |
| 178 | /** True if the parameter is for internal use by the renderer, and false if its intended to be set by the user. */ |
| 179 | bool isInternal = true; |
| 180 | |
| 181 | /** A list of potential values this parameter can take on. */ |
| 182 | SmallVector<ShaderVariationParamValue, 4> values; |
| 183 | }; |
| 184 | |
| 185 | /** |
| 186 | * Sub-shader represents a set of techniques not used by the main shader, but rather a specialized set of techniques |
| 187 | * used by the renderer for a specific purpose. The renderer identifies these techniques by a unique name, and utilizes |
| 188 | * them when present, or uses the default built-in techniques otherwise. Note that sub-shader techniques need to follow |
| 189 | * a specific interface that can be utilized by the renderer, usually similar/identical to the default built-in |
| 190 | * technique. |
| 191 | */ |
| 192 | struct BS_CORE_EXPORT SubShader : TSubShader<false>, IReflectable |
| 193 | { |
| 194 | /************************************************************************/ |
| 195 | /* SERIALIZATION */ |
| 196 | /************************************************************************/ |
| 197 | public: |
| 198 | friend class SubShaderRTTI; |
| 199 | static RTTITypeBase* getRTTIStatic(); |
| 200 | RTTITypeBase* getRTTI() const override; |
| 201 | }; |
| 202 | |
| 203 | /** @} */ |
| 204 | |
| 205 | /** @addtogroup Implementation |
| 206 | * @{ |
| 207 | */ |
| 208 | |
| 209 | template<bool Core> struct TSubShaderType {}; |
| 210 | template<> struct TSubShaderType < false > { typedef SubShader Type; }; |
| 211 | template<> struct TSubShaderType < true > { typedef TSubShader<true> Type; }; |
| 212 | |
| 213 | /** Structure used for initializing a shader. */ |
| 214 | template<bool Core> |
| 215 | struct BS_CORE_EXPORT TSHADER_DESC |
| 216 | { |
| 217 | using TextureType = CoreVariantHandleType<Texture, Core>; |
| 218 | using SamplerStateType = SPtr<CoreVariantType<SamplerState, Core>>; |
| 219 | using TechniqueType = CoreVariantType<Technique, Core>; |
| 220 | using SubShaderType = typename TSubShaderType<Core>::Type ; |
| 221 | |
| 222 | TSHADER_DESC(); |
| 223 | |
| 224 | /** |
| 225 | * Registers a new data (int, Vector2, etc.) parameter you that you may then use via Material by providing the |
| 226 | * parameter name. All parameters internally map to variables defined in GPU programs. |
| 227 | * |
| 228 | * @param[in] paramDesc Structure describing the parameter to add. |
| 229 | * @param[in] defaultValue (optional) Pointer to the buffer containing the default value for this parameter |
| 230 | * (initial value that will be set when a material is initialized with this shader). |
| 231 | * The provided buffer must be of the correct size (depending on the element type |
| 232 | * and array size). |
| 233 | * |
| 234 | * @note If multiple parameters are given with the same name but different types behavior is undefined. |
| 235 | */ |
| 236 | void addParameter(SHADER_DATA_PARAM_DESC paramDesc, UINT8* defaultValue = nullptr); |
| 237 | |
| 238 | /** |
| 239 | * Registers a new object (texture, sampler state, etc.) parameter you that you may then use via Material by |
| 240 | * providing the parameter name. All parameters internally map to variables defined in GPU programs. Multiple GPU |
| 241 | * variables may be mapped to a single parameter in which case the first variable actually found in the program will |
| 242 | * be used while others will be ignored. |
| 243 | * |
| 244 | * @param[in] paramDesc Structure describing the parameter to add. |
| 245 | * |
| 246 | * @note |
| 247 | * If multiple parameters are given with the same name but different types behavior is undefined. You are allowed |
| 248 | * to call this method multiple times in order to map multiple GPU variable names to a single parameter, but the |
| 249 | * default value (if any) will only be recognized on the first call. Mapping multiple GPU variables to a single |
| 250 | * parameter is useful when you are defining a shader that supports techniques across different render systems |
| 251 | * where GPU variable names for the same parameters might differ. |
| 252 | */ |
| 253 | void addParameter(SHADER_OBJECT_PARAM_DESC paramDesc); |
| 254 | |
| 255 | /** |
| 256 | * @see SHADER_DESC::addParameter(SHADER_OBJECT_PARAM_DESC) |
| 257 | * |
| 258 | * @note |
| 259 | * Specialized version of addParameter that accepts a default sampler value that will be used for initializing the |
| 260 | * object parameter upon Material creation. Default sampler value is only valid if the object type is one of the |
| 261 | * sampler types. |
| 262 | */ |
| 263 | void addParameter(SHADER_OBJECT_PARAM_DESC paramDesc, const SamplerStateType& defaultValue); |
| 264 | |
| 265 | /** |
| 266 | * @see SHADER_DESC::addParameter(SHADER_OBJECT_PARAM_DESC) |
| 267 | * |
| 268 | * @note |
| 269 | * Specialized version of addParameter that accepts a default texture value that will be used for initializing the |
| 270 | * object parameter upon Material creation. Default texture value is only valid if the object type is one of the |
| 271 | * texture types. |
| 272 | */ |
| 273 | void addParameter(SHADER_OBJECT_PARAM_DESC paramDesc, const TextureType& defaultValue); |
| 274 | |
| 275 | /** |
| 276 | * Applies an attribute to the parameter with the specified name. |
| 277 | * |
| 278 | * @param[in] name Name of an object or data parameter to apply the attribute to. |
| 279 | * @param[in] attrib Structure describing the attribute to apply. |
| 280 | */ |
| 281 | void setParameterAttribute(const String& name, const SHADER_PARAM_ATTRIBUTE& attrib); |
| 282 | |
| 283 | /** |
| 284 | * Changes parameters of a parameter block with the specified name. |
| 285 | * |
| 286 | * @param[in] name Name of the parameter block. This should correspond with the name specified in |
| 287 | * the GPU program code. |
| 288 | * @param[in] shared If parameter block is marked as shared it will not be automatically created by |
| 289 | * the Material. You will need to create it elsewhere and then assign it manually. |
| 290 | * @param[in] usage Specified how often do we plan on modifying the buffer, which determines how is |
| 291 | * the buffer internally stored for best performance. |
| 292 | * @param[in] rendererSemantic (optional) Semantic that allows you to specify the use of this parameter block |
| 293 | * in the renderer. The actual value of the semantic depends on the current |
| 294 | * Renderer and its supported list of semantics. Elements with a renderer semantic |
| 295 | * will not have their parameter block automatically created (similar to "shared" |
| 296 | * argument), but instead a Renderer will create an assign it instead. Be aware |
| 297 | * that renderers have strict policies on what and how are parameters stored in the |
| 298 | * buffer and you will need to respect them. If you don't respect them your shader |
| 299 | * will be deemed incompatible and won't be used. Value of 0 signifies the parameter |
| 300 | * block is not used by the renderer. |
| 301 | */ |
| 302 | void setParamBlockAttribs(const String& name, bool shared, GpuBufferUsage usage, |
| 303 | StringID rendererSemantic = StringID::NONE); |
| 304 | |
| 305 | /** |
| 306 | * Sorting type to use when performing sort in the render queue. Default value is sort front to back which causes |
| 307 | * least overdraw and is preferable. Transparent objects need to be sorted back to front. You may also specify no |
| 308 | * sorting and the elements will be rendered in the order they were added to the render queue. |
| 309 | */ |
| 310 | QueueSortType queueSortType; |
| 311 | |
| 312 | /** |
| 313 | * Priority that allows you to control in what order are your shaders rendered. See QueuePriority for a list of |
| 314 | * initial values. Shaders with higher priority will be rendered before shaders with lower priority, and |
| 315 | * additionally render queue will only sort elements within the same priority group. |
| 316 | * |
| 317 | * @note |
| 318 | * This is useful when you want all your opaque objects to be rendered before you start drawing your transparent |
| 319 | * ones. Or to render your overlays after everything else. Values provided in QueuePriority are just for general |
| 320 | * guidance and feel free to increase them or decrease them for finer tuning. (for example QueuePriority::Opaque + |
| 321 | * 1). |
| 322 | */ |
| 323 | INT32 queuePriority; |
| 324 | |
| 325 | /** |
| 326 | * Enables or disables separable passes. When separable passes are disabled all shader passes will be executed in a |
| 327 | * sequence one after another. If it is disabled the renderer is free to mix and match passes from different |
| 328 | * objects to achieve best performance. (They will still be executed in sequence, but some other object may be |
| 329 | * rendered in-between passes) |
| 330 | * |
| 331 | * @note Shaders with transparency generally can't be separable, while opaque can. |
| 332 | */ |
| 333 | bool separablePasses; |
| 334 | |
| 335 | /** Flags that let the renderer know how should it interpret the shader. */ |
| 336 | ShaderFlags flags; |
| 337 | |
| 338 | /** Techniques to initialize the shader with. */ |
| 339 | Vector<SPtr<TechniqueType>> techniques; |
| 340 | |
| 341 | /** Optional set of sub-shaders to initialize the shader with. */ |
| 342 | Vector<SubShaderType> subShaders; |
| 343 | |
| 344 | /** |
| 345 | * Information about all variation parameters and their possible values. Each permutation of variation parameters |
| 346 | * represents a separate shader technique. |
| 347 | */ |
| 348 | Vector<ShaderVariationParamInfo> variationParams; |
| 349 | |
| 350 | Map<String, SHADER_DATA_PARAM_DESC> dataParams; |
| 351 | Map<String, SHADER_OBJECT_PARAM_DESC> textureParams; |
| 352 | Map<String, SHADER_OBJECT_PARAM_DESC> bufferParams; |
| 353 | Map<String, SHADER_OBJECT_PARAM_DESC> samplerParams; |
| 354 | Map<String, SHADER_PARAM_BLOCK_DESC> paramBlocks; |
| 355 | |
| 356 | Vector<UINT8> dataDefaultValues; |
| 357 | Vector<SamplerStateType> samplerDefaultValues; |
| 358 | Vector<TextureType> textureDefaultValues; |
| 359 | Vector<SHADER_PARAM_ATTRIBUTE> paramAttributes; |
| 360 | |
| 361 | private: |
| 362 | /** |
| 363 | * @copydoc addParameter(SHADER_OBJECT_PARAM_DESC) |
| 364 | * |
| 365 | * @note Common method shared by different addParameter overloads. |
| 366 | */ |
| 367 | void addParameterInternal(SHADER_OBJECT_PARAM_DESC paramDesc, UINT32 defaultValueIdx); |
| 368 | }; |
| 369 | |
| 370 | /** Templated version of Shader used for implementing both sim and core thread variants. */ |
| 371 | template<bool Core> |
| 372 | class BS_CORE_EXPORT TShader |
| 373 | { |
| 374 | public: |
| 375 | using TechniqueType = CoreVariantType<Technique, Core>; |
| 376 | using TextureType = typename TSHADER_DESC<Core>::TextureType; |
| 377 | using SamplerStateType = typename TSHADER_DESC<Core>::SamplerStateType; |
| 378 | using SubShaderType = typename TSubShaderType<Core>::Type; |
| 379 | |
| 380 | TShader(UINT32 id); |
| 381 | TShader(const String& name, const TSHADER_DESC<Core>& desc, UINT32 id); |
| 382 | virtual ~TShader(); |
| 383 | |
| 384 | /** Returns the total number of techniques in this shader. */ |
| 385 | UINT32 getNumTechniques() const { return (UINT32)mDesc.techniques.size(); } |
| 386 | |
| 387 | /** Returns the list of all supported techniques based on current render API and renderer. */ |
| 388 | Vector<SPtr<TechniqueType>> getCompatibleTechniques() const; |
| 389 | |
| 390 | /** |
| 391 | * Returns the list of all supported techniques based on current render API and renderer, and limits the techniques |
| 392 | * to only those implementing the specified variation. |
| 393 | * |
| 394 | * @param[in] variation Object containing variation parameters to compare to technique variation. |
| 395 | * @param[in] exact When true the technique variation needs to have the exact number of parameters with |
| 396 | * identical contents to the provided variation. When false, only the provided subset |
| 397 | * of parameters is used for comparison, while any extra parameters present in |
| 398 | * the technique are not compared. |
| 399 | */ |
| 400 | Vector<SPtr<TechniqueType>> getCompatibleTechniques(const ShaderVariation& variation, bool exact) const; |
| 401 | |
| 402 | /** Returns a list of all techniques in this shader. */ |
| 403 | const Vector<SPtr<TechniqueType>>& getTechniques() const { return mDesc.techniques; } |
| 404 | |
| 405 | /** Returns a list of all sub-shaders in this shader. */ |
| 406 | const Vector<SubShaderType>& getSubShaders() const { return mDesc.subShaders; } |
| 407 | |
| 408 | /** |
| 409 | * Returns the list of all variation parameters supported by this shader, possible values of each parameter and |
| 410 | * other meta-data. |
| 411 | */ |
| 412 | BS_SCRIPT_EXPORT(n:VariationParams,pr:getter) |
| 413 | const Vector<ShaderVariationParamInfo> getVariationParams() const { return mDesc.variationParams; } |
| 414 | |
| 415 | /** |
| 416 | * Returns currently active queue sort type. |
| 417 | * |
| 418 | * @see SHADER_DESC::queueSortType |
| 419 | */ |
| 420 | QueueSortType getQueueSortType() const { return mDesc.queueSortType; } |
| 421 | |
| 422 | /** |
| 423 | * Returns currently active queue priority. |
| 424 | * |
| 425 | * @see SHADER_DESC::queuePriority |
| 426 | */ |
| 427 | INT32 getQueuePriority() const { return mDesc.queuePriority; } |
| 428 | |
| 429 | /** |
| 430 | * Returns if separable passes are allowed. |
| 431 | * |
| 432 | * @see SHADER_DESC::separablePasses |
| 433 | */ |
| 434 | bool getAllowSeparablePasses() const { return mDesc.separablePasses; } |
| 435 | |
| 436 | /** |
| 437 | * Returns flags that control how the renderer interprets the shader. Actual interpretation of the flags depends on |
| 438 | * the active renderer. |
| 439 | */ |
| 440 | ShaderFlags getFlags() const { return mDesc.flags; } |
| 441 | |
| 442 | /** Returns type of the parameter with the specified name. Throws exception if the parameter doesn't exist. */ |
| 443 | GpuParamType getParamType(const String& name) const; |
| 444 | |
| 445 | /** |
| 446 | * Returns description for a data parameter with the specified name. Throws exception if the parameter doesn't exist. |
| 447 | */ |
| 448 | const SHADER_DATA_PARAM_DESC& getDataParamDesc(const String& name) const; |
| 449 | |
| 450 | /** |
| 451 | * Returns description for a texture parameter with the specified name. Throws exception if the parameter doesn't |
| 452 | * exist. |
| 453 | */ |
| 454 | const SHADER_OBJECT_PARAM_DESC& getTextureParamDesc(const String& name) const; |
| 455 | |
| 456 | /** |
| 457 | * Returns description for a sampler parameter with the specified name. Throws exception if the parameter doesn't |
| 458 | * exist. |
| 459 | */ |
| 460 | const SHADER_OBJECT_PARAM_DESC& getSamplerParamDesc(const String& name) const; |
| 461 | |
| 462 | /** |
| 463 | * Returns description for a buffer parameter with the specified name. Throws exception if the parameter doesn't |
| 464 | * exist. |
| 465 | */ |
| 466 | const SHADER_OBJECT_PARAM_DESC& getBufferParamDesc(const String& name) const; |
| 467 | |
| 468 | /** Checks if the parameter with the specified name exists, and is a data parameter. */ |
| 469 | bool hasDataParam(const String& name) const; |
| 470 | |
| 471 | /** Checks if the parameter with the specified name exists, and is a texture parameter. */ |
| 472 | bool hasTextureParam(const String& name) const; |
| 473 | |
| 474 | /** Checks if the parameter with the specified name exists, and is a sampler parameter. */ |
| 475 | bool hasSamplerParam(const String& name) const; |
| 476 | |
| 477 | /** Checks if the parameter with the specified name exists, and is a buffer parameter. */ |
| 478 | bool hasBufferParam(const String& name) const; |
| 479 | |
| 480 | /** Checks if the parameter block with the specified name exists. */ |
| 481 | bool hasParamBlock(const String& name) const; |
| 482 | |
| 483 | /** Returns a map of all data parameters in the shader. */ |
| 484 | const Map<String, SHADER_DATA_PARAM_DESC>& getDataParams() const { return mDesc.dataParams; } |
| 485 | |
| 486 | /** Returns a map of all texture parameters in the shader. */ |
| 487 | const Map<String, SHADER_OBJECT_PARAM_DESC>& getTextureParams() const { return mDesc.textureParams; } |
| 488 | |
| 489 | /** Returns a map of all buffer parameters in the shader. */ |
| 490 | const Map<String, SHADER_OBJECT_PARAM_DESC>& getBufferParams() const { return mDesc.bufferParams; } |
| 491 | |
| 492 | /** Returns a map of all sampler parameters in the shader. */ |
| 493 | const Map<String, SHADER_OBJECT_PARAM_DESC>& getSamplerParams() const { return mDesc.samplerParams; } |
| 494 | |
| 495 | /** Returns a map of all parameter blocks. */ |
| 496 | const Map<String, SHADER_PARAM_BLOCK_DESC>& getParamBlocks() const { return mDesc.paramBlocks; } |
| 497 | |
| 498 | /** Returns a list of all parameter attributes, as referenced by individual parameters. */ |
| 499 | const Vector<SHADER_PARAM_ATTRIBUTE>& getParamAttributes() const { return mDesc.paramAttributes; } |
| 500 | |
| 501 | /** |
| 502 | * Returns a default texture for a parameter that has the specified default value index (retrieved from the |
| 503 | * parameters descriptor). |
| 504 | */ |
| 505 | TextureType getDefaultTexture(UINT32 index) const; |
| 506 | |
| 507 | /** |
| 508 | * Returns a default sampler state for a parameter that has the specified default value index (retrieved from the |
| 509 | * parameters descriptor). |
| 510 | */ |
| 511 | SamplerStateType getDefaultSampler(UINT32 index) const; |
| 512 | |
| 513 | /** |
| 514 | * Returns a pointer to the internal buffer containing the default value for a data parameter that has the |
| 515 | * specified default value index (retrieved from the parameters descriptor). |
| 516 | */ |
| 517 | UINT8* getDefaultValue(UINT32 index) const; |
| 518 | |
| 519 | /** Returns the unique shader ID. */ |
| 520 | UINT32 getId() const { return mId; } |
| 521 | |
| 522 | protected: |
| 523 | String mName; |
| 524 | TSHADER_DESC<Core> mDesc; |
| 525 | UINT32 mId; |
| 526 | }; |
| 527 | |
| 528 | /** @} */ |
| 529 | |
| 530 | namespace ct |
| 531 | { |
| 532 | /** @addtogroup Material-Internal |
| 533 | * @{ |
| 534 | */ |
| 535 | |
| 536 | typedef TSHADER_DESC<true> SHADER_DESC; |
| 537 | |
| 538 | /** Core thread version of bs::SubShader. */ |
| 539 | typedef TSubShader<true> SubShader; |
| 540 | |
| 541 | /** @} */ |
| 542 | } |
| 543 | |
| 544 | /** @addtogroup Material |
| 545 | * @{ |
| 546 | */ |
| 547 | |
| 548 | typedef TSHADER_DESC<false> SHADER_DESC; |
| 549 | |
| 550 | /** |
| 551 | * @native |
| 552 | * Shader represents a collection of techniques that control object rendering. They are used in Material%s, which can be |
| 553 | * considered as instances of a Shader. Multiple materials may share the same shader but provide different parameters to |
| 554 | * it. |
| 555 | * |
| 556 | * Shader will always choose the first supported technique based on the current render system, render manager and other |
| 557 | * properties. So make sure to add most important techniques first so you make sure they are used if they are supported. |
| 558 | * @endnative |
| 559 | * |
| 560 | * @script |
| 561 | * Contains definitions of GPU programs used for rendering, as well as a set of global parameters to control those |
| 562 | * programs. |
| 563 | * @endscript |
| 564 | */ |
| 565 | class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Rendering) Shader : public Resource, public TShader<false> |
| 566 | { |
| 567 | public: |
| 568 | /** Retrieves an implementation of a shader usable only from the core thread. */ |
| 569 | SPtr<ct::Shader> getCore() const; |
| 570 | |
| 571 | /** |
| 572 | * Sets a list include file paths that are referenced by this shader. |
| 573 | * |
| 574 | * @note |
| 575 | * This is not used directly by the shader as includes are expected to be processed during GPU program and state |
| 576 | * creation, but it may be referenced by higher layers for various purposes. |
| 577 | */ |
| 578 | void setIncludeFiles(const Vector<String>& includes); |
| 579 | |
| 580 | /** Checks is the provided object type a sampler. */ |
| 581 | static bool isSampler(GpuParamObjectType type); |
| 582 | |
| 583 | /** Checks is the provided object type a texture. */ |
| 584 | static bool isTexture(GpuParamObjectType type); |
| 585 | |
| 586 | /** Checks is the provided object type a load/store (unordered read/write) texture. */ |
| 587 | static bool isLoadStoreTexture(GpuParamObjectType type); |
| 588 | |
| 589 | /** Checks is the provided object type a buffer. */ |
| 590 | static bool isBuffer(GpuParamObjectType type); |
| 591 | |
| 592 | /** |
| 593 | * Returns the size in bytes for a specific data type. |
| 594 | * |
| 595 | * @note Returns 0 for variable size types like structures. |
| 596 | */ |
| 597 | static UINT32 getDataParamSize(GpuParamDataType type); |
| 598 | |
| 599 | /** Creates a new shader resource using the provided descriptor and techniques. */ |
| 600 | static HShader create(const String& name, const SHADER_DESC& desc); |
| 601 | |
| 602 | /** Returns a shader object but doesn't initialize it. */ |
| 603 | static SPtr<Shader> createEmpty(); |
| 604 | |
| 605 | public: // ***** INTERNAL ****** |
| 606 | /** @name Internal |
| 607 | * @{ |
| 608 | */ |
| 609 | |
| 610 | /** |
| 611 | * Creates a new shader object using the provided descriptor and techniques. |
| 612 | * |
| 613 | * @note Internal method. Use create() for normal use. |
| 614 | */ |
| 615 | static SPtr<Shader> _createPtr(const String& name, const SHADER_DESC& desc); |
| 616 | |
| 617 | /** @} */ |
| 618 | |
| 619 | private: |
| 620 | Shader(const String& name, const SHADER_DESC& desc, UINT32 id); |
| 621 | |
| 622 | /** @copydoc CoreObject::getCoreDependencies */ |
| 623 | void getCoreDependencies(Vector<CoreObject*>& dependencies) override; |
| 624 | |
| 625 | /** @copydoc CoreObject::createCore */ |
| 626 | SPtr<ct::CoreObject> createCore() const override; |
| 627 | |
| 628 | /** Converts a sim thread version of the shader descriptor to a core thread version. */ |
| 629 | ct::SHADER_DESC convertDesc(const SHADER_DESC& desc) const; |
| 630 | |
| 631 | private: |
| 632 | /************************************************************************/ |
| 633 | /* RTTI */ |
| 634 | /************************************************************************/ |
| 635 | Shader(UINT32 id); |
| 636 | |
| 637 | public: |
| 638 | friend class ShaderRTTI; |
| 639 | static RTTITypeBase* getRTTIStatic(); |
| 640 | RTTITypeBase* getRTTI() const override; |
| 641 | }; |
| 642 | |
| 643 | /** @} */ |
| 644 | /** @addtogroup Material |
| 645 | * @{ |
| 646 | */ |
| 647 | |
| 648 | /** Shader specific resource meta-data containing information about referenced include files. */ |
| 649 | class BS_CORE_EXPORT ShaderMetaData : public ResourceMetaData |
| 650 | { |
| 651 | public: |
| 652 | Vector<String> includes; |
| 653 | |
| 654 | /************************************************************************/ |
| 655 | /* SERIALIZATION */ |
| 656 | /************************************************************************/ |
| 657 | public: |
| 658 | friend class ShaderMetaDataRTTI; |
| 659 | static RTTITypeBase* getRTTIStatic(); |
| 660 | RTTITypeBase* getRTTI() const override; |
| 661 | }; |
| 662 | |
| 663 | /** @} */ |
| 664 | |
| 665 | namespace ct |
| 666 | { |
| 667 | /** @addtogroup Material-Internal |
| 668 | * @{ |
| 669 | */ |
| 670 | |
| 671 | /** Core thread version of Shader. */ |
| 672 | class BS_CORE_EXPORT Shader : public CoreObject, public TShader<true> |
| 673 | { |
| 674 | public: |
| 675 | /** @copydoc bs::Shader::create */ |
| 676 | static SPtr<Shader> create(const String& name, const SHADER_DESC& desc); |
| 677 | |
| 678 | protected: |
| 679 | friend class bs::Shader; |
| 680 | |
| 681 | Shader(const String& name, const SHADER_DESC& desc, UINT32 id); |
| 682 | |
| 683 | static std::atomic<UINT32> mNextShaderId; |
| 684 | }; |
| 685 | |
| 686 | /** @} */ |
| 687 | } |
| 688 | } |