| 1 | /*
|
| 2 | * Reflection.h
|
| 3 | *
|
| 4 | * This file is part of the XShaderCompiler project (Copyright (c) 2014-2017 by Lukas Hermanns)
|
| 5 | * See "LICENSE.txt" for license information.
|
| 6 | */
|
| 7 |
|
| 8 | #ifndef XSC_REFLECTION_H
|
| 9 | #define XSC_REFLECTION_H
|
| 10 |
|
| 11 |
|
| 12 | #include "Export.h"
|
| 13 | #include <limits>
|
| 14 | #include <string>
|
| 15 | #include <map>
|
| 16 | #include <vector>
|
| 17 | #include <ostream>
|
| 18 |
|
| 19 |
|
| 20 | namespace Xsc
|
| 21 | {
|
| 22 |
|
| 23 | //! Shader code reflection namespace
|
| 24 | namespace Reflection
|
| 25 | {
|
| 26 |
|
| 27 |
|
| 28 | /* ===== Public enumerations ===== */
|
| 29 |
|
| 30 | //! Sampler filter enumeration (D3D11_FILTER).
|
| 31 | enum class Filter
|
| 32 | {
|
| 33 | MinMagMipPoint = 0,
|
| 34 | MinMagPointMipLinear = 0x1,
|
| 35 | MinPointMagLinearMipPoint = 0x4,
|
| 36 | MinPointMagMipLinear = 0x5,
|
| 37 | MinLinearMagMipPoint = 0x10,
|
| 38 | MinLinearMagPointMipLinear = 0x11,
|
| 39 | MinMagLinearMipPoint = 0x14,
|
| 40 | MinMagMipLinear = 0x15,
|
| 41 | Anisotropic = 0x55,
|
| 42 | ComparisonMinMagMipPoint = 0x80,
|
| 43 | ComparisonMinMagPointMipLinear = 0x81,
|
| 44 | ComparisonMinPointMagLinearMipPoint = 0x84,
|
| 45 | ComparisonMinPointMagMipLinear = 0x85,
|
| 46 | ComparisonMinLinearMagMipPoint = 0x90,
|
| 47 | ComparisonMinLinearMagPointMipLinear = 0x91,
|
| 48 | ComparisonMinMagLinearMipPoint = 0x94,
|
| 49 | ComparisonMinMagMipLinear = 0x95,
|
| 50 | ComparisonAnisotropic = 0xd5,
|
| 51 | MinimumMinMagMipPoint = 0x100,
|
| 52 | MinimumMinMagPointMipLinear = 0x101,
|
| 53 | MinimumMinPointMagLinearMipPoint = 0x104,
|
| 54 | MinimumMinPointMagMipLinear = 0x105,
|
| 55 | MinimumMinLinearMagMipPoint = 0x110,
|
| 56 | MinimumMinLinearMagPointMipLinear = 0x111,
|
| 57 | MinimumMinMagLinearMipPoint = 0x114,
|
| 58 | MinimumMinMagMipLinear = 0x115,
|
| 59 | MinimumAnisotropic = 0x155,
|
| 60 | MaximumMinMagMipPoint = 0x180,
|
| 61 | MaximumMinMagPointMipLinear = 0x181,
|
| 62 | MaximumMinPointMagLinearMipPoint = 0x184,
|
| 63 | MaximumMinPointMagMipLinear = 0x185,
|
| 64 | MaximumMinLinearMagMipPoint = 0x190,
|
| 65 | MaximumMinLinearMagPointMipLinear = 0x191,
|
| 66 | MaximumMinMagLinearMipPoint = 0x194,
|
| 67 | MaximumMinMagMipLinear = 0x195,
|
| 68 | MaximumAnisotropic = 0x1d5,
|
| 69 | };
|
| 70 |
|
| 71 | //! Texture address mode enumeration (D3D11_TEXTURE_ADDRESS_MODE).
|
| 72 | enum class TextureAddressMode
|
| 73 | {
|
| 74 | Wrap = 1,
|
| 75 | Mirror = 2,
|
| 76 | Clamp = 3,
|
| 77 | Border = 4,
|
| 78 | MirrorOnce = 5,
|
| 79 | };
|
| 80 |
|
| 81 | //! Sample comparison function enumeration (D3D11_COMPARISON_FUNC).
|
| 82 | enum class ComparisonFunc
|
| 83 | {
|
| 84 | Never = 1,
|
| 85 | Less = 2,
|
| 86 | Equal = 3,
|
| 87 | LessEqual = 4,
|
| 88 | Greater = 5,
|
| 89 | NotEqual = 6,
|
| 90 | GreaterEqual = 7,
|
| 91 | Always = 8,
|
| 92 | };
|
| 93 |
|
| 94 |
|
| 95 | /* ===== Public structures ===== */
|
| 96 |
|
| 97 | /**
|
| 98 | \brief Static sampler state descriptor structure (D3D11_SAMPLER_DESC).
|
| 99 | \remarks All members and enumerations have the same values like the one in the "D3D11_SAMPLER_DESC" structure respectively.
|
| 100 | Thus, they can all be statically casted from and to the original D3D11 values.
|
| 101 | \see https://msdn.microsoft.com/en-us/library/windows/desktop/ff476207(v=vs.85).aspx
|
| 102 | */
|
| 103 | struct SamplerState
|
| 104 | {
|
| 105 | Filter filter = Filter::MinMagMipLinear;
|
| 106 | TextureAddressMode addressU = TextureAddressMode::Wrap;
|
| 107 | TextureAddressMode addressV = TextureAddressMode::Wrap;
|
| 108 | TextureAddressMode addressW = TextureAddressMode::Wrap;
|
| 109 | float mipLODBias = 0.0f;
|
| 110 | unsigned int maxAnisotropy = 1u;
|
| 111 | ComparisonFunc comparisonFunc = ComparisonFunc::Always;
|
| 112 | float borderColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
|
| 113 | float minLOD = -std::numeric_limits<float>::max();
|
| 114 | float maxLOD = std::numeric_limits<float>::max();
|
| 115 |
|
| 116 | // BEGIN BANSHEE CHANGES
|
| 117 | bool isNonDefault = false;
|
| 118 | std::string alias;
|
| 119 | // END BANSHEE CHANGES
|
| 120 | };
|
| 121 |
|
| 122 | //! Binding slot of textures, constant buffers, and fragment targets.
|
| 123 | struct BindingSlot
|
| 124 | {
|
| 125 | //! Identifier of the binding point.
|
| 126 | std::string ident;
|
| 127 |
|
| 128 | //! Zero based binding point or location. If this is -1, the location has not been set.
|
| 129 | int location;
|
| 130 | };
|
| 131 |
|
| 132 | // BEGIN BANSHEE CHANGES
|
| 133 |
|
| 134 | enum class VariableType
|
| 135 | {
|
| 136 | Buffer,
|
| 137 | UniformBuffer,
|
| 138 | Sampler,
|
| 139 | Variable,
|
| 140 | Struct
|
| 141 | };
|
| 142 |
|
| 143 | enum class BufferType
|
| 144 | {
|
| 145 | Undefined,
|
| 146 |
|
| 147 | Buffer,
|
| 148 | StructuredBuffer,
|
| 149 | ByteAddressBuffer,
|
| 150 |
|
| 151 | RWBuffer,
|
| 152 | RWStructuredBuffer,
|
| 153 | RWByteAddressBuffer,
|
| 154 | AppendStructuredBuffer,
|
| 155 | ConsumeStructuredBuffer,
|
| 156 |
|
| 157 | RWTexture1D,
|
| 158 | RWTexture1DArray,
|
| 159 | RWTexture2D,
|
| 160 | RWTexture2DArray,
|
| 161 | RWTexture3D,
|
| 162 |
|
| 163 | Texture1D,
|
| 164 | Texture1DArray,
|
| 165 | Texture2D,
|
| 166 | Texture2DArray,
|
| 167 | Texture3D,
|
| 168 | TextureCube,
|
| 169 | TextureCubeArray,
|
| 170 | Texture2DMS,
|
| 171 | Texture2DMSArray,
|
| 172 | };
|
| 173 |
|
| 174 | enum class DataType
|
| 175 | {
|
| 176 | Undefined,
|
| 177 |
|
| 178 | // String types,
|
| 179 | String,
|
| 180 |
|
| 181 | // Scalar types
|
| 182 | Bool,
|
| 183 | Int,
|
| 184 | UInt,
|
| 185 | Half,
|
| 186 | Float,
|
| 187 | Double,
|
| 188 |
|
| 189 | // Vector types
|
| 190 | Bool2,
|
| 191 | Bool3,
|
| 192 | Bool4,
|
| 193 | Int2,
|
| 194 | Int3,
|
| 195 | Int4,
|
| 196 | UInt2,
|
| 197 | UInt3,
|
| 198 | UInt4,
|
| 199 | Half2,
|
| 200 | Half3,
|
| 201 | Half4,
|
| 202 | Float2,
|
| 203 | Float3,
|
| 204 | Float4,
|
| 205 | Double2,
|
| 206 | Double3,
|
| 207 | Double4,
|
| 208 |
|
| 209 | // Matrix types
|
| 210 | Bool2x2,
|
| 211 | Bool2x3,
|
| 212 | Bool2x4,
|
| 213 | Bool3x2,
|
| 214 | Bool3x3,
|
| 215 | Bool3x4,
|
| 216 | Bool4x2,
|
| 217 | Bool4x3,
|
| 218 | Bool4x4,
|
| 219 | Int2x2,
|
| 220 | Int2x3,
|
| 221 | Int2x4,
|
| 222 | Int3x2,
|
| 223 | Int3x3,
|
| 224 | Int3x4,
|
| 225 | Int4x2,
|
| 226 | Int4x3,
|
| 227 | Int4x4,
|
| 228 | UInt2x2,
|
| 229 | UInt2x3,
|
| 230 | UInt2x4,
|
| 231 | UInt3x2,
|
| 232 | UInt3x3,
|
| 233 | UInt3x4,
|
| 234 | UInt4x2,
|
| 235 | UInt4x3,
|
| 236 | UInt4x4,
|
| 237 | Half2x2,
|
| 238 | Half2x3,
|
| 239 | Half2x4,
|
| 240 | Half3x2,
|
| 241 | Half3x3,
|
| 242 | Half3x4,
|
| 243 | Half4x2,
|
| 244 | Half4x3,
|
| 245 | Half4x4,
|
| 246 | Float2x2,
|
| 247 | Float2x3,
|
| 248 | Float2x4,
|
| 249 | Float3x2,
|
| 250 | Float3x3,
|
| 251 | Float3x4,
|
| 252 | Float4x2,
|
| 253 | Float4x3,
|
| 254 | Float4x4,
|
| 255 | Double2x2,
|
| 256 | Double2x3,
|
| 257 | Double2x4,
|
| 258 | Double3x2,
|
| 259 | Double3x3,
|
| 260 | Double3x4,
|
| 261 | Double4x2,
|
| 262 | Double4x3,
|
| 263 | Double4x4,
|
| 264 | };
|
| 265 |
|
| 266 | enum class VarType
|
| 267 | {
|
| 268 | Undefined,
|
| 269 | Void,
|
| 270 |
|
| 271 | // Scalar types
|
| 272 | Bool,
|
| 273 | Int,
|
| 274 | UInt,
|
| 275 | Half,
|
| 276 | Float,
|
| 277 | Double,
|
| 278 |
|
| 279 | // Vector types
|
| 280 | Bool2,
|
| 281 | Bool3,
|
| 282 | Bool4,
|
| 283 | Int2,
|
| 284 | Int3,
|
| 285 | Int4,
|
| 286 | UInt2,
|
| 287 | UInt3,
|
| 288 | UInt4,
|
| 289 | Half2,
|
| 290 | Half3,
|
| 291 | Half4,
|
| 292 | Float2,
|
| 293 | Float3,
|
| 294 | Float4,
|
| 295 | Double2,
|
| 296 | Double3,
|
| 297 | Double4,
|
| 298 |
|
| 299 | // Matrix types
|
| 300 | Bool2x2,
|
| 301 | Bool2x3,
|
| 302 | Bool2x4,
|
| 303 | Bool3x2,
|
| 304 | Bool3x3,
|
| 305 | Bool3x4,
|
| 306 | Bool4x2,
|
| 307 | Bool4x3,
|
| 308 | Bool4x4,
|
| 309 | Int2x2,
|
| 310 | Int2x3,
|
| 311 | Int2x4,
|
| 312 | Int3x2,
|
| 313 | Int3x3,
|
| 314 | Int3x4,
|
| 315 | Int4x2,
|
| 316 | Int4x3,
|
| 317 | Int4x4,
|
| 318 | UInt2x2,
|
| 319 | UInt2x3,
|
| 320 | UInt2x4,
|
| 321 | UInt3x2,
|
| 322 | UInt3x3,
|
| 323 | UInt3x4,
|
| 324 | UInt4x2,
|
| 325 | UInt4x3,
|
| 326 | UInt4x4,
|
| 327 | Half2x2,
|
| 328 | Half2x3,
|
| 329 | Half2x4,
|
| 330 | Half3x2,
|
| 331 | Half3x3,
|
| 332 | Half3x4,
|
| 333 | Half4x2,
|
| 334 | Half4x3,
|
| 335 | Half4x4,
|
| 336 | Float2x2,
|
| 337 | Float2x3,
|
| 338 | Float2x4,
|
| 339 | Float3x2,
|
| 340 | Float3x3,
|
| 341 | Float3x4,
|
| 342 | Float4x2,
|
| 343 | Float4x3,
|
| 344 | Float4x4,
|
| 345 | Double2x2,
|
| 346 | Double2x3,
|
| 347 | Double2x4,
|
| 348 | Double3x2,
|
| 349 | Double3x3,
|
| 350 | Double3x4,
|
| 351 | Double4x2,
|
| 352 | Double4x3,
|
| 353 | Double4x4,
|
| 354 | };
|
| 355 |
|
| 356 | union DefaultValue
|
| 357 | {
|
| 358 | bool boolean;
|
| 359 | float real;
|
| 360 | int integer;
|
| 361 | int imatrix[4];
|
| 362 | float matrix[16];
|
| 363 | int handle;
|
| 364 | };
|
| 365 |
|
| 366 | //! Information about a variable declaration
|
| 367 | struct Variable
|
| 368 | {
|
| 369 | //! Identifier of the element.
|
| 370 | std::string ident;
|
| 371 |
|
| 372 | //! Data type of the element.
|
| 373 | VariableType type = VariableType::Variable;
|
| 374 |
|
| 375 | //! Determines actual type of the element. Contents depend on "type".
|
| 376 | int baseType = 0;
|
| 377 |
|
| 378 | //! Number of elements in the array, if the variable is an array. Flattened if multi-dimensional array.
|
| 379 | int arraySize = 1;
|
| 380 | };
|
| 381 |
|
| 382 | //! A single element in a constant buffer or an opaque type
|
| 383 | struct Uniform : Variable
|
| 384 | {
|
| 385 | enum Flags
|
| 386 | {
|
| 387 | None = 0,
|
| 388 |
|
| 389 | Internal = 1 << 0,
|
| 390 | Color = 1 << 1,
|
| 391 | HideInInspector = 1 << 2
|
| 392 | };
|
| 393 |
|
| 394 | //! Index of the uniform block this uniform belongs to. -1 if none.
|
| 395 | int uniformBlock = -1;
|
| 396 |
|
| 397 | //! Index into the default value array. -1 if no default value.
|
| 398 | int defaultValue = -1;
|
| 399 |
|
| 400 | //! Flags further defining the uniform.
|
| 401 | int flags = None;
|
| 402 |
|
| 403 | //! In case the parameter is used as a destination for sprite animation UVs, identifier of the texture its animating
|
| 404 | std::string spriteUVRef;
|
| 405 |
|
| 406 | //! Optional readable name of the uniform, for display in GUI.
|
| 407 | std::string readableName;
|
| 408 | };
|
| 409 |
|
| 410 | //! Information about a struct type
|
| 411 | struct Struct
|
| 412 | {
|
| 413 | std::string name;
|
| 414 | std::vector<Variable> members;
|
| 415 | };
|
| 416 |
|
| 417 | //! Single parameter in a function.
|
| 418 | struct Parameter
|
| 419 | {
|
| 420 | enum Flags
|
| 421 | {
|
| 422 | In = 1 << 0,
|
| 423 | Out = 1 << 1
|
| 424 | };
|
| 425 |
|
| 426 | VarType type;
|
| 427 | std::string ident;
|
| 428 | int flags;
|
| 429 | };
|
| 430 |
|
| 431 | //! A single function defined in the program
|
| 432 | struct Function
|
| 433 | {
|
| 434 | //! Name of the function
|
| 435 | std::string ident;
|
| 436 |
|
| 437 | //! Return value of the function
|
| 438 | VarType returnValue;
|
| 439 |
|
| 440 | //! List of all function parameters
|
| 441 | std::vector<Parameter> parameters;
|
| 442 | };
|
| 443 |
|
| 444 | // END BANSHEE CHANGES
|
| 445 |
|
| 446 | //! Number of threads within each work group of a compute shader.
|
| 447 | struct NumThreads
|
| 448 | {
|
| 449 | //! Number of shader compute threads in X dimension.
|
| 450 | int x = 0;
|
| 451 |
|
| 452 | //! Number of shader compute threads in Y dimension.
|
| 453 | int y = 0;
|
| 454 |
|
| 455 | //! Number of shader compute threads in Z dimension.
|
| 456 | int z = 0;
|
| 457 | };
|
| 458 |
|
| 459 | //! Structure for shader output statistics (e.g. texture/buffer binding points).
|
| 460 | struct ReflectionData
|
| 461 | {
|
| 462 | //! All defined macros after pre-processing.
|
| 463 | std::vector<std::string> macros;
|
| 464 |
|
| 465 | //! Texture bindings.
|
| 466 | std::vector<BindingSlot> textures;
|
| 467 |
|
| 468 | //! Storage buffer bindings.
|
| 469 | std::vector<BindingSlot> storageBuffers;
|
| 470 |
|
| 471 | //! Constant buffer bindings.
|
| 472 | std::vector<BindingSlot> constantBuffers;
|
| 473 |
|
| 474 | //! Shader input attributes.
|
| 475 | std::vector<BindingSlot> inputAttributes;
|
| 476 |
|
| 477 | //! Shader output attributes.
|
| 478 | std::vector<BindingSlot> outputAttributes;
|
| 479 |
|
| 480 | //! Static sampler states (identifier, states).
|
| 481 | std::map<std::string, SamplerState> samplerStates;
|
| 482 |
|
| 483 | //! 'numthreads' attribute of a compute shader.
|
| 484 | NumThreads numThreads;
|
| 485 |
|
| 486 | // BEGIN BANSHEE CHANGES
|
| 487 | std::vector<Uniform> uniforms;
|
| 488 | std::vector<Struct> structs;
|
| 489 | std::vector<DefaultValue> defaultValues;
|
| 490 |
|
| 491 | std::vector<Function> functions;
|
| 492 | // END BANSHEE CHANGES
|
| 493 | };
|
| 494 |
|
| 495 |
|
| 496 | } // /namespace Reflection
|
| 497 |
|
| 498 |
|
| 499 | /* ===== Public functions ===== */
|
| 500 |
|
| 501 | //! Returns the string representation of the specified 'SamplerState::Filter' type.
|
| 502 | XSC_EXPORT std::string ToString(const Reflection::Filter t);
|
| 503 |
|
| 504 | //! Returns the string representation of the specified 'SamplerState::TextureAddressMode' type.
|
| 505 | XSC_EXPORT std::string ToString(const Reflection::TextureAddressMode t);
|
| 506 |
|
| 507 | //! Returns the string representation of the specified 'SamplerState::ComparisonFunc' type.
|
| 508 | XSC_EXPORT std::string ToString(const Reflection::ComparisonFunc t);
|
| 509 |
|
| 510 | //! Prints the reflection data into the output stream in a human readable format.
|
| 511 | XSC_EXPORT void PrintReflection(std::ostream& stream, const Reflection::ReflectionData& reflectionData);
|
| 512 |
|
| 513 |
|
| 514 | } // /namespace Xsc
|
| 515 |
|
| 516 |
|
| 517 | #endif
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 | // ================================================================================ |