| 1 |
/* |
| 2 |
* Copyright 2016-2020 Robert Konrad |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "spirv_hlsl.hpp" |
| 18 |
#include "GLSL.std.450.h" |
| 19 |
#include <algorithm> |
| 20 |
#include <assert.h> |
| 21 |
|
| 22 |
using namespace spv; |
| 23 |
using namespace SPIRV_CROSS_NAMESPACE; |
| 24 |
using namespace std; |
| 25 |
|
| 26 |
enum class ImageFormatNormalizedState |
| 27 |
{ |
| 28 |
None = 0, |
| 29 |
Unorm = 1, |
| 30 |
Snorm = 2 |
| 31 |
}; |
| 32 |
|
| 33 |
static ImageFormatNormalizedState image_format_to_normalized_state(ImageFormat fmt) |
| 34 |
{ |
| 35 |
switch (fmt) |
| 36 |
{ |
| 37 |
case ImageFormatR8: |
| 38 |
case ImageFormatR16: |
| 39 |
case ImageFormatRg8: |
| 40 |
case ImageFormatRg16: |
| 41 |
case ImageFormatRgba8: |
| 42 |
case ImageFormatRgba16: |
| 43 |
case ImageFormatRgb10A2: |
| 44 |
return ImageFormatNormalizedState::Unorm; |
| 45 |
|
| 46 |
case ImageFormatR8Snorm: |
| 47 |
case ImageFormatR16Snorm: |
| 48 |
case ImageFormatRg8Snorm: |
| 49 |
case ImageFormatRg16Snorm: |
| 50 |
case ImageFormatRgba8Snorm: |
| 51 |
case ImageFormatRgba16Snorm: |
| 52 |
return ImageFormatNormalizedState::Snorm; |
| 53 |
|
| 54 |
default: |
| 55 |
break; |
| 56 |
} |
| 57 |
|
| 58 |
return ImageFormatNormalizedState::None; |
| 59 |
} |
| 60 |
|
| 61 |
static unsigned image_format_to_components(ImageFormat fmt) |
| 62 |
{ |
| 63 |
switch (fmt) |
| 64 |
{ |
| 65 |
case ImageFormatR8: |
| 66 |
case ImageFormatR16: |
| 67 |
case ImageFormatR8Snorm: |
| 68 |
case ImageFormatR16Snorm: |
| 69 |
case ImageFormatR16f: |
| 70 |
case ImageFormatR32f: |
| 71 |
case ImageFormatR8i: |
| 72 |
case ImageFormatR16i: |
| 73 |
case ImageFormatR32i: |
| 74 |
case ImageFormatR8ui: |
| 75 |
case ImageFormatR16ui: |
| 76 |
case ImageFormatR32ui: |
| 77 |
return 1; |
| 78 |
|
| 79 |
case ImageFormatRg8: |
| 80 |
case ImageFormatRg16: |
| 81 |
case ImageFormatRg8Snorm: |
| 82 |
case ImageFormatRg16Snorm: |
| 83 |
case ImageFormatRg16f: |
| 84 |
case ImageFormatRg32f: |
| 85 |
case ImageFormatRg8i: |
| 86 |
case ImageFormatRg16i: |
| 87 |
case ImageFormatRg32i: |
| 88 |
case ImageFormatRg8ui: |
| 89 |
case ImageFormatRg16ui: |
| 90 |
case ImageFormatRg32ui: |
| 91 |
return 2; |
| 92 |
|
| 93 |
case ImageFormatR11fG11fB10f: |
| 94 |
return 3; |
| 95 |
|
| 96 |
case ImageFormatRgba8: |
| 97 |
case ImageFormatRgba16: |
| 98 |
case ImageFormatRgb10A2: |
| 99 |
case ImageFormatRgba8Snorm: |
| 100 |
case ImageFormatRgba16Snorm: |
| 101 |
case ImageFormatRgba16f: |
| 102 |
case ImageFormatRgba32f: |
| 103 |
case ImageFormatRgba8i: |
| 104 |
case ImageFormatRgba16i: |
| 105 |
case ImageFormatRgba32i: |
| 106 |
case ImageFormatRgba8ui: |
| 107 |
case ImageFormatRgba16ui: |
| 108 |
case ImageFormatRgba32ui: |
| 109 |
case ImageFormatRgb10a2ui: |
| 110 |
return 4; |
| 111 |
|
| 112 |
case ImageFormatUnknown: |
| 113 |
return 4; // Assume 4. |
| 114 |
|
| 115 |
default: |
| 116 |
SPIRV_CROSS_THROW("Unrecognized typed image format."); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
static string image_format_to_type(ImageFormat fmt, SPIRType::BaseType basetype) |
| 121 |
{ |
| 122 |
switch (fmt) |
| 123 |
{ |
| 124 |
case ImageFormatR8: |
| 125 |
case ImageFormatR16: |
| 126 |
if (basetype != SPIRType::Float) |
| 127 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 128 |
return "unorm float"; |
| 129 |
case ImageFormatRg8: |
| 130 |
case ImageFormatRg16: |
| 131 |
if (basetype != SPIRType::Float) |
| 132 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 133 |
return "unorm float2"; |
| 134 |
case ImageFormatRgba8: |
| 135 |
case ImageFormatRgba16: |
| 136 |
if (basetype != SPIRType::Float) |
| 137 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 138 |
return "unorm float4"; |
| 139 |
case ImageFormatRgb10A2: |
| 140 |
if (basetype != SPIRType::Float) |
| 141 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 142 |
return "unorm float4"; |
| 143 |
|
| 144 |
case ImageFormatR8Snorm: |
| 145 |
case ImageFormatR16Snorm: |
| 146 |
if (basetype != SPIRType::Float) |
| 147 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 148 |
return "snorm float"; |
| 149 |
case ImageFormatRg8Snorm: |
| 150 |
case ImageFormatRg16Snorm: |
| 151 |
if (basetype != SPIRType::Float) |
| 152 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 153 |
return "snorm float2"; |
| 154 |
case ImageFormatRgba8Snorm: |
| 155 |
case ImageFormatRgba16Snorm: |
| 156 |
if (basetype != SPIRType::Float) |
| 157 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 158 |
return "snorm float4"; |
| 159 |
|
| 160 |
case ImageFormatR16f: |
| 161 |
case ImageFormatR32f: |
| 162 |
if (basetype != SPIRType::Float) |
| 163 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 164 |
return "float"; |
| 165 |
case ImageFormatRg16f: |
| 166 |
case ImageFormatRg32f: |
| 167 |
if (basetype != SPIRType::Float) |
| 168 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 169 |
return "float2"; |
| 170 |
case ImageFormatRgba16f: |
| 171 |
case ImageFormatRgba32f: |
| 172 |
if (basetype != SPIRType::Float) |
| 173 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 174 |
return "float4"; |
| 175 |
|
| 176 |
case ImageFormatR11fG11fB10f: |
| 177 |
if (basetype != SPIRType::Float) |
| 178 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 179 |
return "float3"; |
| 180 |
|
| 181 |
case ImageFormatR8i: |
| 182 |
case ImageFormatR16i: |
| 183 |
case ImageFormatR32i: |
| 184 |
if (basetype != SPIRType::Int) |
| 185 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 186 |
return "int"; |
| 187 |
case ImageFormatRg8i: |
| 188 |
case ImageFormatRg16i: |
| 189 |
case ImageFormatRg32i: |
| 190 |
if (basetype != SPIRType::Int) |
| 191 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 192 |
return "int2"; |
| 193 |
case ImageFormatRgba8i: |
| 194 |
case ImageFormatRgba16i: |
| 195 |
case ImageFormatRgba32i: |
| 196 |
if (basetype != SPIRType::Int) |
| 197 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 198 |
return "int4"; |
| 199 |
|
| 200 |
case ImageFormatR8ui: |
| 201 |
case ImageFormatR16ui: |
| 202 |
case ImageFormatR32ui: |
| 203 |
if (basetype != SPIRType::UInt) |
| 204 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 205 |
return "uint"; |
| 206 |
case ImageFormatRg8ui: |
| 207 |
case ImageFormatRg16ui: |
| 208 |
case ImageFormatRg32ui: |
| 209 |
if (basetype != SPIRType::UInt) |
| 210 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 211 |
return "uint2"; |
| 212 |
case ImageFormatRgba8ui: |
| 213 |
case ImageFormatRgba16ui: |
| 214 |
case ImageFormatRgba32ui: |
| 215 |
if (basetype != SPIRType::UInt) |
| 216 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 217 |
return "uint4"; |
| 218 |
case ImageFormatRgb10a2ui: |
| 219 |
if (basetype != SPIRType::UInt) |
| 220 |
SPIRV_CROSS_THROW("Mismatch in image type and base type of image."); |
| 221 |
return "uint4"; |
| 222 |
|
| 223 |
case ImageFormatUnknown: |
| 224 |
switch (basetype) |
| 225 |
{ |
| 226 |
case SPIRType::Float: |
| 227 |
return "float4"; |
| 228 |
case SPIRType::Int: |
| 229 |
return "int4"; |
| 230 |
case SPIRType::UInt: |
| 231 |
return "uint4"; |
| 232 |
default: |
| 233 |
SPIRV_CROSS_THROW("Unsupported base type for image."); |
| 234 |
} |
| 235 |
|
| 236 |
default: |
| 237 |
SPIRV_CROSS_THROW("Unrecognized typed image format."); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
string CompilerHLSL::image_type_hlsl_modern(const SPIRType &type, uint32_t id) |
| 242 |
{ |
| 243 |
auto &imagetype = get<SPIRType>(type.image.type); |
| 244 |
const char *dim = nullptr; |
| 245 |
bool typed_load = false; |
| 246 |
uint32_t components = 4; |
| 247 |
|
| 248 |
bool force_image_srv = hlsl_options.nonwritable_uav_texture_as_srv && has_decoration(id, DecorationNonWritable); |
| 249 |
|
| 250 |
switch (type.image.dim) |
| 251 |
{ |
| 252 |
case Dim1D: |
| 253 |
typed_load = type.image.sampled == 2; |
| 254 |
dim = "1D"; |
| 255 |
break; |
| 256 |
case Dim2D: |
| 257 |
typed_load = type.image.sampled == 2; |
| 258 |
dim = "2D"; |
| 259 |
break; |
| 260 |
case Dim3D: |
| 261 |
typed_load = type.image.sampled == 2; |
| 262 |
dim = "3D"; |
| 263 |
break; |
| 264 |
case DimCube: |
| 265 |
if (type.image.sampled == 2) |
| 266 |
SPIRV_CROSS_THROW("RWTextureCube does not exist in HLSL."); |
| 267 |
dim = "Cube"; |
| 268 |
break; |
| 269 |
case DimRect: |
| 270 |
SPIRV_CROSS_THROW("Rectangle texture support is not yet implemented for HLSL."); // TODO |
| 271 |
case DimBuffer: |
| 272 |
if (type.image.sampled == 1) |
| 273 |
return join("Buffer<", type_to_glsl(imagetype), components, ">"); |
| 274 |
else if (type.image.sampled == 2) |
| 275 |
{ |
| 276 |
if (interlocked_resources.count(id)) |
| 277 |
return join("RasterizerOrderedBuffer<", image_format_to_type(type.image.format, imagetype.basetype), |
| 278 |
">"); |
| 279 |
|
| 280 |
typed_load = !force_image_srv && type.image.sampled == 2; |
| 281 |
|
| 282 |
const char *rw = force_image_srv ? "" : "RW"; |
| 283 |
return join(rw, "Buffer<", |
| 284 |
typed_load ? image_format_to_type(type.image.format, imagetype.basetype) : |
| 285 |
join(type_to_glsl(imagetype), components), |
| 286 |
">"); |
| 287 |
} |
| 288 |
else |
| 289 |
SPIRV_CROSS_THROW("Sampler buffers must be either sampled or unsampled. Cannot deduce in runtime."); |
| 290 |
case DimSubpassData: |
| 291 |
dim = "2D"; |
| 292 |
typed_load = false; |
| 293 |
break; |
| 294 |
default: |
| 295 |
SPIRV_CROSS_THROW("Invalid dimension."); |
| 296 |
} |
| 297 |
const char *arrayed = type.image.arrayed ? "Array" : ""; |
| 298 |
const char *ms = type.image.ms ? "MS" : ""; |
| 299 |
const char *rw = typed_load && !force_image_srv ? "RW" : ""; |
| 300 |
|
| 301 |
if (force_image_srv) |
| 302 |
typed_load = false; |
| 303 |
|
| 304 |
if (typed_load && interlocked_resources.count(id)) |
| 305 |
rw = "RasterizerOrdered"; |
| 306 |
|
| 307 |
return join(rw, "Texture", dim, ms, arrayed, "<", |
| 308 |
typed_load ? image_format_to_type(type.image.format, imagetype.basetype) : |
| 309 |
join(type_to_glsl(imagetype), components), |
| 310 |
">"); |
| 311 |
} |
| 312 |
|
| 313 |
string CompilerHLSL::image_type_hlsl_legacy(const SPIRType &type, uint32_t /*id*/) |
| 314 |
{ |
| 315 |
auto &imagetype = get<SPIRType>(type.image.type); |
| 316 |
string res; |
| 317 |
|
| 318 |
switch (imagetype.basetype) |
| 319 |
{ |
| 320 |
case SPIRType::Int: |
| 321 |
res = "i"; |
| 322 |
break; |
| 323 |
case SPIRType::UInt: |
| 324 |
res = "u"; |
| 325 |
break; |
| 326 |
default: |
| 327 |
break; |
| 328 |
} |
| 329 |
|
| 330 |
if (type.basetype == SPIRType::Image && type.image.dim == DimSubpassData) |
| 331 |
return res + "subpassInput" + (type.image.ms ? "MS" : ""); |
| 332 |
|
| 333 |
// If we're emulating subpassInput with samplers, force sampler2D |
| 334 |
// so we don't have to specify format. |
| 335 |
if (type.basetype == SPIRType::Image && type.image.dim != DimSubpassData) |
| 336 |
{ |
| 337 |
// Sampler buffers are always declared as samplerBuffer even though they might be separate images in the SPIR-V. |
| 338 |
if (type.image.dim == DimBuffer && type.image.sampled == 1) |
| 339 |
res += "sampler"; |
| 340 |
else |
| 341 |
res += type.image.sampled == 2 ? "image" : "texture"; |
| 342 |
} |
| 343 |
else |
| 344 |
res += "sampler"; |
| 345 |
|
| 346 |
switch (type.image.dim) |
| 347 |
{ |
| 348 |
case Dim1D: |
| 349 |
res += "1D"; |
| 350 |
break; |
| 351 |
case Dim2D: |
| 352 |
res += "2D"; |
| 353 |
break; |
| 354 |
case Dim3D: |
| 355 |
res += "3D"; |
| 356 |
break; |
| 357 |
case DimCube: |
| 358 |
res += "CUBE"; |
| 359 |
break; |
| 360 |
|
| 361 |
case DimBuffer: |
| 362 |
res += "Buffer"; |
| 363 |
break; |
| 364 |
|
| 365 |
case DimSubpassData: |
| 366 |
res += "2D"; |
| 367 |
break; |
| 368 |
default: |
| 369 |
SPIRV_CROSS_THROW("Only 1D, 2D, 3D, Buffer, InputTarget and Cube textures supported."); |
| 370 |
} |
| 371 |
|
| 372 |
if (type.image.ms) |
| 373 |
res += "MS"; |
| 374 |
if (type.image.arrayed) |
| 375 |
res += "Array"; |
| 376 |
|
| 377 |
return res; |
| 378 |
} |
| 379 |
|
| 380 |
string CompilerHLSL::image_type_hlsl(const SPIRType &type, uint32_t id) |
| 381 |
{ |
| 382 |
if (hlsl_options.shader_model <= 30) |
| 383 |
return image_type_hlsl_legacy(type, id); |
| 384 |
else |
| 385 |
return image_type_hlsl_modern(type, id); |
| 386 |
} |
| 387 |
|
| 388 |
// The optional id parameter indicates the object whose type we are trying |
| 389 |
// to find the description for. It is optional. Most type descriptions do not |
| 390 |
// depend on a specific object's use of that type. |
| 391 |
string CompilerHLSL::type_to_glsl(const SPIRType &type, uint32_t id) |
| 392 |
{ |
| 393 |
// Ignore the pointer type since GLSL doesn't have pointers. |
| 394 |
|
| 395 |
switch (type.basetype) |
| 396 |
{ |
| 397 |
case SPIRType::Struct: |
| 398 |
// Need OpName lookup here to get a "sensible" name for a struct. |
| 399 |
if (backend.explicit_struct_type) |
| 400 |
return join("struct ", to_name(type.self)); |
| 401 |
else |
| 402 |
return to_name(type.self); |
| 403 |
|
| 404 |
case SPIRType::Image: |
| 405 |
case SPIRType::SampledImage: |
| 406 |
return image_type_hlsl(type, id); |
| 407 |
|
| 408 |
case SPIRType::Sampler: |
| 409 |
return comparison_ids.count(id) ? "SamplerComparisonState" : "SamplerState"; |
| 410 |
|
| 411 |
case SPIRType::Void: |
| 412 |
return "void"; |
| 413 |
|
| 414 |
default: |
| 415 |
break; |
| 416 |
} |
| 417 |
|
| 418 |
if (type.vecsize == 1 && type.columns == 1) // Scalar builtin |
| 419 |
{ |
| 420 |
switch (type.basetype) |
| 421 |
{ |
| 422 |
case SPIRType::Boolean: |
| 423 |
return "bool"; |
| 424 |
case SPIRType::Int: |
| 425 |
return backend.basic_int_type; |
| 426 |
case SPIRType::UInt: |
| 427 |
return backend.basic_uint_type; |
| 428 |
case SPIRType::AtomicCounter: |
| 429 |
return "atomic_uint"; |
| 430 |
case SPIRType::Half: |
| 431 |
if (hlsl_options.enable_16bit_types) |
| 432 |
return "half"; |
| 433 |
else |
| 434 |
return "min16float"; |
| 435 |
case SPIRType::Short: |
| 436 |
if (hlsl_options.enable_16bit_types) |
| 437 |
return "int16_t"; |
| 438 |
else |
| 439 |
return "min16int"; |
| 440 |
case SPIRType::UShort: |
| 441 |
if (hlsl_options.enable_16bit_types) |
| 442 |
return "uint16_t"; |
| 443 |
else |
| 444 |
return "min16uint"; |
| 445 |
case SPIRType::Float: |
| 446 |
return "float"; |
| 447 |
case SPIRType::Double: |
| 448 |
return "double"; |
| 449 |
case SPIRType::Int64: |
| 450 |
if (hlsl_options.shader_model < 60) |
| 451 |
SPIRV_CROSS_THROW("64-bit integers only supported in SM 6.0."); |
| 452 |
return "int64_t"; |
| 453 |
case SPIRType::UInt64: |
| 454 |
if (hlsl_options.shader_model < 60) |
| 455 |
SPIRV_CROSS_THROW("64-bit integers only supported in SM 6.0."); |
| 456 |
return "uint64_t"; |
| 457 |
default: |
| 458 |
return "???"; |
| 459 |
} |
| 460 |
} |
| 461 |
else if (type.vecsize > 1 && type.columns == 1) // Vector builtin |
| 462 |
{ |
| 463 |
switch (type.basetype) |
| 464 |
{ |
| 465 |
case SPIRType::Boolean: |
| 466 |
return join("bool", type.vecsize); |
| 467 |
case SPIRType::Int: |
| 468 |
return join("int", type.vecsize); |
| 469 |
case SPIRType::UInt: |
| 470 |
return join("uint", type.vecsize); |
| 471 |
case SPIRType::Half: |
| 472 |
return join(hlsl_options.enable_16bit_types ? "half" : "min16float", type.vecsize); |
| 473 |
case SPIRType::Short: |
| 474 |
return join(hlsl_options.enable_16bit_types ? "int16_t" : "min16int", type.vecsize); |
| 475 |
case SPIRType::UShort: |
| 476 |
return join(hlsl_options.enable_16bit_types ? "uint16_t" : "min16uint", type.vecsize); |
| 477 |
case SPIRType::Float: |
| 478 |
return join("float", type.vecsize); |
| 479 |
case SPIRType::Double: |
| 480 |
return join("double", type.vecsize); |
| 481 |
case SPIRType::Int64: |
| 482 |
return join("i64vec", type.vecsize); |
| 483 |
case SPIRType::UInt64: |
| 484 |
return join("u64vec", type.vecsize); |
| 485 |
default: |
| 486 |
return "???"; |
| 487 |
} |
| 488 |
} |
| 489 |
else |
| 490 |
{ |
| 491 |
switch (type.basetype) |
| 492 |
{ |
| 493 |
case SPIRType::Boolean: |
| 494 |
return join("bool", type.columns, "x", type.vecsize); |
| 495 |
case SPIRType::Int: |
| 496 |
return join("int", type.columns, "x", type.vecsize); |
| 497 |
case SPIRType::UInt: |
| 498 |
return join("uint", type.columns, "x", type.vecsize); |
| 499 |
case SPIRType::Half: |
| 500 |
return join(hlsl_options.enable_16bit_types ? "half" : "min16float", type.columns, "x", type.vecsize); |
| 501 |
case SPIRType::Short: |
| 502 |
return join(hlsl_options.enable_16bit_types ? "int16_t" : "min16int", type.columns, "x", type.vecsize); |
| 503 |
case SPIRType::UShort: |
| 504 |
return join(hlsl_options.enable_16bit_types ? "uint16_t" : "min16uint", type.columns, "x", type.vecsize); |
| 505 |
case SPIRType::Float: |
| 506 |
return join("float", type.columns, "x", type.vecsize); |
| 507 |
case SPIRType::Double: |
| 508 |
return join("double", type.columns, "x", type.vecsize); |
| 509 |
// Matrix types not supported for int64/uint64. |
| 510 |
default: |
| 511 |
return "???"; |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
void CompilerHLSL::emit_header() |
| 517 |
{ |
| 518 |
for (auto &header : header_lines) |
| 519 |
statement(header); |
| 520 |
|
| 521 |
if (header_lines.size() > 0) |
| 522 |
{ |
| 523 |
statement(""); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
void CompilerHLSL::emit_interface_block_globally(const SPIRVariable &var) |
| 528 |
{ |
| 529 |
add_resource_name(var.self); |
| 530 |
|
| 531 |
// The global copies of I/O variables should not contain interpolation qualifiers. |
| 532 |
// These are emitted inside the interface structs. |
| 533 |
auto &flags = ir.meta[var.self].decoration.decoration_flags; |
| 534 |
auto old_flags = flags; |
| 535 |
flags.reset(); |
| 536 |
statement("static ", variable_decl(var), ";"); |
| 537 |
flags = old_flags; |
| 538 |
} |
| 539 |
|
| 540 |
const char *CompilerHLSL::to_storage_qualifiers_glsl(const SPIRVariable &var) |
| 541 |
{ |
| 542 |
// Input and output variables are handled specially in HLSL backend. |
| 543 |
// The variables are declared as global, private variables, and do not need any qualifiers. |
| 544 |
if (var.storage == StorageClassUniformConstant || var.storage == StorageClassUniform || |
| 545 |
var.storage == StorageClassPushConstant) |
| 546 |
{ |
| 547 |
return "uniform "; |
| 548 |
} |
| 549 |
|
| 550 |
return ""; |
| 551 |
} |
| 552 |
|
| 553 |
void CompilerHLSL::emit_builtin_outputs_in_struct() |
| 554 |
{ |
| 555 |
auto &execution = get_entry_point(); |
| 556 |
|
| 557 |
bool legacy = hlsl_options.shader_model <= 30; |
| 558 |
active_output_builtins.for_each_bit([&](uint32_t i) { |
| 559 |
const char *type = nullptr; |
| 560 |
const char *semantic = nullptr; |
| 561 |
auto builtin = static_cast<BuiltIn>(i); |
| 562 |
switch (builtin) |
| 563 |
{ |
| 564 |
case BuiltInPosition: |
| 565 |
type = "float4"; |
| 566 |
semantic = legacy ? "POSITION" : "SV_Position"; |
| 567 |
break; |
| 568 |
|
| 569 |
case BuiltInSampleMask: |
| 570 |
if (hlsl_options.shader_model < 41 || execution.model != ExecutionModelFragment) |
| 571 |
SPIRV_CROSS_THROW("Sample Mask output is only supported in PS 4.1 or higher."); |
| 572 |
type = "uint"; |
| 573 |
semantic = "SV_Coverage"; |
| 574 |
break; |
| 575 |
|
| 576 |
case BuiltInFragDepth: |
| 577 |
type = "float"; |
| 578 |
if (legacy) |
| 579 |
{ |
| 580 |
semantic = "DEPTH"; |
| 581 |
} |
| 582 |
else |
| 583 |
{ |
| 584 |
if (hlsl_options.shader_model >= 50 && execution.flags.get(ExecutionModeDepthGreater)) |
| 585 |
semantic = "SV_DepthGreaterEqual"; |
| 586 |
else if (hlsl_options.shader_model >= 50 && execution.flags.get(ExecutionModeDepthLess)) |
| 587 |
semantic = "SV_DepthLessEqual"; |
| 588 |
else |
| 589 |
semantic = "SV_Depth"; |
| 590 |
} |
| 591 |
break; |
| 592 |
|
| 593 |
case BuiltInClipDistance: |
| 594 |
// HLSL is a bit weird here, use SV_ClipDistance0, SV_ClipDistance1 and so on with vectors. |
| 595 |
for (uint32_t clip = 0; clip < clip_distance_count; clip += 4) |
| 596 |
{ |
| 597 |
uint32_t to_declare = clip_distance_count - clip; |
| 598 |
if (to_declare > 4) |
| 599 |
to_declare = 4; |
| 600 |
|
| 601 |
uint32_t semantic_index = clip / 4; |
| 602 |
|
| 603 |
static const char *types[] = { "float", "float2", "float3", "float4" }; |
| 604 |
statement(types[to_declare - 1], " ", builtin_to_glsl(builtin, StorageClassOutput), semantic_index, |
| 605 |
" : SV_ClipDistance", semantic_index, ";"); |
| 606 |
} |
| 607 |
break; |
| 608 |
|
| 609 |
case BuiltInCullDistance: |
| 610 |
// HLSL is a bit weird here, use SV_CullDistance0, SV_CullDistance1 and so on with vectors. |
| 611 |
for (uint32_t cull = 0; cull < cull_distance_count; cull += 4) |
| 612 |
{ |
| 613 |
uint32_t to_declare = cull_distance_count - cull; |
| 614 |
if (to_declare > 4) |
| 615 |
to_declare = 4; |
| 616 |
|
| 617 |
uint32_t semantic_index = cull / 4; |
| 618 |
|
| 619 |
static const char *types[] = { "float", "float2", "float3", "float4" }; |
| 620 |
statement(types[to_declare - 1], " ", builtin_to_glsl(builtin, StorageClassOutput), semantic_index, |
| 621 |
" : SV_CullDistance", semantic_index, ";"); |
| 622 |
} |
| 623 |
break; |
| 624 |
|
| 625 |
case BuiltInPointSize: |
| 626 |
// If point_size_compat is enabled, just ignore PointSize. |
| 627 |
// PointSize does not exist in HLSL, but some code bases might want to be able to use these shaders, |
| 628 |
// even if it means working around the missing feature. |
| 629 |
if (hlsl_options.point_size_compat) |
| 630 |
break; |
| 631 |
else |
| 632 |
SPIRV_CROSS_THROW("Unsupported builtin in HLSL."); |
| 633 |
|
| 634 |
default: |
| 635 |
SPIRV_CROSS_THROW("Unsupported builtin in HLSL."); |
| 636 |
break; |
| 637 |
} |
| 638 |
|
| 639 |
if (type && semantic) |
| 640 |
statement(type, " ", builtin_to_glsl(builtin, StorageClassOutput), " : ", semantic, ";"); |
| 641 |
}); |
| 642 |
} |
| 643 |
|
| 644 |
void CompilerHLSL::emit_builtin_inputs_in_struct() |
| 645 |
{ |
| 646 |
bool legacy = hlsl_options.shader_model <= 30; |
| 647 |
active_input_builtins.for_each_bit([&](uint32_t i) { |
| 648 |
const char *type = nullptr; |
| 649 |
const char *semantic = nullptr; |
| 650 |
auto builtin = static_cast<BuiltIn>(i); |
| 651 |
switch (builtin) |
| 652 |
{ |
| 653 |
case BuiltInFragCoord: |
| 654 |
type = "float4"; |
| 655 |
semantic = legacy ? "VPOS" : "SV_Position"; |
| 656 |
break; |
| 657 |
|
| 658 |
case BuiltInVertexId: |
| 659 |
case BuiltInVertexIndex: |
| 660 |
if (legacy) |
| 661 |
SPIRV_CROSS_THROW("Vertex index not supported in SM 3.0 or lower."); |
| 662 |
type = "uint"; |
| 663 |
semantic = "SV_VertexID"; |
| 664 |
break; |
| 665 |
|
| 666 |
case BuiltInInstanceId: |
| 667 |
case BuiltInInstanceIndex: |
| 668 |
if (legacy) |
| 669 |
SPIRV_CROSS_THROW("Instance index not supported in SM 3.0 or lower."); |
| 670 |
type = "uint"; |
| 671 |
semantic = "SV_InstanceID"; |
| 672 |
break; |
| 673 |
|
| 674 |
case BuiltInSampleId: |
| 675 |
if (legacy) |
| 676 |
SPIRV_CROSS_THROW("Sample ID not supported in SM 3.0 or lower."); |
| 677 |
type = "uint"; |
| 678 |
semantic = "SV_SampleIndex"; |
| 679 |
break; |
| 680 |
|
| 681 |
case BuiltInSampleMask: |
| 682 |
if (hlsl_options.shader_model < 50 || get_entry_point().model != ExecutionModelFragment) |
| 683 |
SPIRV_CROSS_THROW("Sample Mask input is only supported in PS 5.0 or higher."); |
| 684 |
type = "uint"; |
| 685 |
semantic = "SV_Coverage"; |
| 686 |
break; |
| 687 |
|
| 688 |
case BuiltInGlobalInvocationId: |
| 689 |
type = "uint3"; |
| 690 |
semantic = "SV_DispatchThreadID"; |
| 691 |
break; |
| 692 |
|
| 693 |
case BuiltInLocalInvocationId: |
| 694 |
type = "uint3"; |
| 695 |
semantic = "SV_GroupThreadID"; |
| 696 |
break; |
| 697 |
|
| 698 |
case BuiltInLocalInvocationIndex: |
| 699 |
type = "uint"; |
| 700 |
semantic = "SV_GroupIndex"; |
| 701 |
break; |
| 702 |
|
| 703 |
case BuiltInWorkgroupId: |
| 704 |
type = "uint3"; |
| 705 |
semantic = "SV_GroupID"; |
| 706 |
break; |
| 707 |
|
| 708 |
case BuiltInFrontFacing: |
| 709 |
type = "bool"; |
| 710 |
semantic = "SV_IsFrontFace"; |
| 711 |
break; |
| 712 |
|
| 713 |
case BuiltInNumWorkgroups: |
| 714 |
case BuiltInSubgroupSize: |
| 715 |
case BuiltInSubgroupLocalInvocationId: |
| 716 |
case BuiltInSubgroupEqMask: |
| 717 |
case BuiltInSubgroupLtMask: |
| 718 |
case BuiltInSubgroupLeMask: |
| 719 |
case BuiltInSubgroupGtMask: |
| 720 |
case BuiltInSubgroupGeMask: |
| 721 |
// Handled specially. |
| 722 |
break; |
| 723 |
|
| 724 |
case BuiltInClipDistance: |
| 725 |
// HLSL is a bit weird here, use SV_ClipDistance0, SV_ClipDistance1 and so on with vectors. |
| 726 |
for (uint32_t clip = 0; clip < clip_distance_count; clip += 4) |
| 727 |
{ |
| 728 |
uint32_t to_declare = clip_distance_count - clip; |
| 729 |
if (to_declare > 4) |
| 730 |
to_declare = 4; |
| 731 |
|
| 732 |
uint32_t semantic_index = clip / 4; |
| 733 |
|
| 734 |
static const char *types[] = { "float", "float2", "float3", "float4" }; |
| 735 |
statement(types[to_declare - 1], " ", builtin_to_glsl(builtin, StorageClassInput), semantic_index, |
| 736 |
" : SV_ClipDistance", semantic_index, ";"); |
| 737 |
} |
| 738 |
break; |
| 739 |
|
| 740 |
case BuiltInCullDistance: |
| 741 |
// HLSL is a bit weird here, use SV_CullDistance0, SV_CullDistance1 and so on with vectors. |
| 742 |
for (uint32_t cull = 0; cull < cull_distance_count; cull += 4) |
| 743 |
{ |
| 744 |
uint32_t to_declare = cull_distance_count - cull; |
| 745 |
if (to_declare > 4) |
| 746 |
to_declare = 4; |
| 747 |
|
| 748 |
uint32_t semantic_index = cull / 4; |
| 749 |
|
| 750 |
static const char *types[] = { "float", "float2", "float3", "float4" }; |
| 751 |
statement(types[to_declare - 1], " ", builtin_to_glsl(builtin, StorageClassInput), semantic_index, |
| 752 |
" : SV_CullDistance", semantic_index, ";"); |
| 753 |
} |
| 754 |
break; |
| 755 |
|
| 756 |
case BuiltInPointCoord: |
| 757 |
// PointCoord is not supported, but provide a way to just ignore that, similar to PointSize. |
| 758 |
if (hlsl_options.point_coord_compat) |
| 759 |
break; |
| 760 |
else |
| 761 |
SPIRV_CROSS_THROW("Unsupported builtin in HLSL."); |
| 762 |
|
| 763 |
default: |
| 764 |
SPIRV_CROSS_THROW("Unsupported builtin in HLSL."); |
| 765 |
break; |
| 766 |
} |
| 767 |
|
| 768 |
if (type && semantic) |
| 769 |
statement(type, " ", builtin_to_glsl(builtin, StorageClassInput), " : ", semantic, ";"); |
| 770 |
}); |
| 771 |
} |
| 772 |
|
| 773 |
uint32_t CompilerHLSL::type_to_consumed_locations(const SPIRType &type) const |
| 774 |
{ |
| 775 |
// TODO: Need to verify correctness. |
| 776 |
uint32_t elements = 0; |
| 777 |
|
| 778 |
if (type.basetype == SPIRType::Struct) |
| 779 |
{ |
| 780 |
for (uint32_t i = 0; i < uint32_t(type.member_types.size()); i++) |
| 781 |
elements += type_to_consumed_locations(get<SPIRType>(type.member_types[i])); |
| 782 |
} |
| 783 |
else |
| 784 |
{ |
| 785 |
uint32_t array_multiplier = 1; |
| 786 |
for (uint32_t i = 0; i < uint32_t(type.array.size()); i++) |
| 787 |
{ |
| 788 |
if (type.array_size_literal[i]) |
| 789 |
array_multiplier *= type.array[i]; |
| 790 |
else |
| 791 |
array_multiplier *= evaluate_constant_u32(type.array[i]); |
| 792 |
} |
| 793 |
elements += array_multiplier * type.columns; |
| 794 |
} |
| 795 |
return elements; |
| 796 |
} |
| 797 |
|
| 798 |
string CompilerHLSL::to_interpolation_qualifiers(const Bitset &flags) |
| 799 |
{ |
| 800 |
string res; |
| 801 |
//if (flags & (1ull << DecorationSmooth)) |
| 802 |
// res += "linear "; |
| 803 |
if (flags.get(DecorationFlat)) |
| 804 |
res += "nointerpolation "; |
| 805 |
if (flags.get(DecorationNoPerspective)) |
| 806 |
res += "noperspective "; |
| 807 |
if (flags.get(DecorationCentroid)) |
| 808 |
res += "centroid "; |
| 809 |
if (flags.get(DecorationPatch)) |
| 810 |
res += "patch "; // Seems to be different in actual HLSL. |
| 811 |
if (flags.get(DecorationSample)) |
| 812 |
res += "sample "; |
| 813 |
if (flags.get(DecorationInvariant)) |
| 814 |
res += "invariant "; // Not supported? |
| 815 |
|
| 816 |
return res; |
| 817 |
} |
| 818 |
|
| 819 |
std::string CompilerHLSL::to_semantic(uint32_t location, ExecutionModel em, StorageClass sc) |
| 820 |
{ |
| 821 |
if (em == ExecutionModelVertex && sc == StorageClassInput) |
| 822 |
{ |
| 823 |
// We have a vertex attribute - we should look at remapping it if the user provided |
| 824 |
// vertex attribute hints. |
| 825 |
for (auto &attribute : remap_vertex_attributes) |
| 826 |
if (attribute.location == location) |
| 827 |
return attribute.semantic; |
| 828 |
} |
| 829 |
|
| 830 |
// Not a vertex attribute, or no remap_vertex_attributes entry. |
| 831 |
return join("TEXCOORD", location); |
| 832 |
} |
| 833 |
|
| 834 |
void CompilerHLSL::emit_io_block(const SPIRVariable &var) |
| 835 |
{ |
| 836 |
auto &execution = get_entry_point(); |
| 837 |
|
| 838 |
auto &type = get<SPIRType>(var.basetype); |
| 839 |
add_resource_name(type.self); |
| 840 |
|
| 841 |
statement("struct ", to_name(type.self)); |
| 842 |
begin_scope(); |
| 843 |
type.member_name_cache.clear(); |
| 844 |
|
| 845 |
uint32_t base_location = get_decoration(var.self, DecorationLocation); |
| 846 |
|
| 847 |
for (uint32_t i = 0; i < uint32_t(type.member_types.size()); i++) |
| 848 |
{ |
| 849 |
string semantic; |
| 850 |
if (has_member_decoration(type.self, i, DecorationLocation)) |
| 851 |
{ |
| 852 |
uint32_t location = get_member_decoration(type.self, i, DecorationLocation); |
| 853 |
semantic = join(" : ", to_semantic(location, execution.model, var.storage)); |
| 854 |
} |
| 855 |
else |
| 856 |
{ |
| 857 |
// If the block itself has a location, but not its members, use the implicit location. |
| 858 |
// There could be a conflict if the block members partially specialize the locations. |
| 859 |
// It is unclear how SPIR-V deals with this. Assume this does not happen for now. |
| 860 |
uint32_t location = base_location + i; |
| 861 |
semantic = join(" : ", to_semantic(location, execution.model, var.storage)); |
| 862 |
} |
| 863 |
|
| 864 |
add_member_name(type, i); |
| 865 |
|
| 866 |
auto &membertype = get<SPIRType>(type.member_types[i]); |
| 867 |
statement(to_interpolation_qualifiers(get_member_decoration_bitset(type.self, i)), |
| 868 |
variable_decl(membertype, to_member_name(type, i)), semantic, ";"); |
| 869 |
} |
| 870 |
|
| 871 |
end_scope_decl(); |
| 872 |
statement(""); |
| 873 |
|
| 874 |
statement("static ", variable_decl(var), ";"); |
| 875 |
statement(""); |
| 876 |
} |
| 877 |
|
| 878 |
void CompilerHLSL::emit_interface_block_in_struct(const SPIRVariable &var, unordered_set<uint32_t> &active_locations) |
| 879 |
{ |
| 880 |
auto &execution = get_entry_point(); |
| 881 |
auto type = get<SPIRType>(var.basetype); |
| 882 |
|
| 883 |
string binding; |
| 884 |
bool use_location_number = true; |
| 885 |
bool legacy = hlsl_options.shader_model <= 30; |
| 886 |
if (execution.model == ExecutionModelFragment && var.storage == StorageClassOutput) |
| 887 |
{ |
| 888 |
// Dual-source blending is achieved in HLSL by emitting to SV_Target0 and 1. |
| 889 |
uint32_t index = get_decoration(var.self, DecorationIndex); |
| 890 |
uint32_t location = get_decoration(var.self, DecorationLocation); |
| 891 |
|
| 892 |
if (index != 0 && location != 0) |
| 893 |
SPIRV_CROSS_THROW("Dual-source blending is only supported on MRT #0 in HLSL."); |
| 894 |
|
| 895 |
binding = join(legacy ? "COLOR" : "SV_Target", location + index); |
| 896 |
use_location_number = false; |
| 897 |
if (legacy) // COLOR must be a four-component vector on legacy shader model targets (HLSL ERR_COLOR_4COMP) |
| 898 |
type.vecsize = 4; |
| 899 |
} |
| 900 |
|
| 901 |
const auto get_vacant_location = [&]() -> uint32_t { |
| 902 |
for (uint32_t i = 0; i < 64; i++) |
| 903 |
if (!active_locations.count(i)) |
| 904 |
return i; |
| 905 |
SPIRV_CROSS_THROW("All locations from 0 to 63 are exhausted."); |
| 906 |
}; |
| 907 |
|
| 908 |
bool need_matrix_unroll = var.storage == StorageClassInput && execution.model == ExecutionModelVertex; |
| 909 |
|
| 910 |
auto &m = ir.meta[var.self].decoration; |
| 911 |
auto name = to_name(var.self); |
| 912 |
if (use_location_number) |
| 913 |
{ |
| 914 |
uint32_t location_number; |
| 915 |
|
| 916 |
// If an explicit location exists, use it with TEXCOORD[N] semantic. |
| 917 |
// Otherwise, pick a vacant location. |
| 918 |
if (m.decoration_flags.get(DecorationLocation)) |
| 919 |
location_number = m.location; |
| 920 |
else |
| 921 |
location_number = get_vacant_location(); |
| 922 |
|
| 923 |
// Allow semantic remap if specified. |
| 924 |
auto semantic = to_semantic(location_number, execution.model, var.storage); |
| 925 |
|
| 926 |
if (need_matrix_unroll && type.columns > 1) |
| 927 |
{ |
| 928 |
if (!type.array.empty()) |
| 929 |
SPIRV_CROSS_THROW("Arrays of matrices used as input/output. This is not supported."); |
| 930 |
|
| 931 |
// Unroll matrices. |
| 932 |
for (uint32_t i = 0; i < type.columns; i++) |
| 933 |
{ |
| 934 |
SPIRType newtype = type; |
| 935 |
newtype.columns = 1; |
| 936 |
|
| 937 |
string effective_semantic; |
| 938 |
if (hlsl_options.flatten_matrix_vertex_input_semantics) |
| 939 |
effective_semantic = to_semantic(location_number, execution.model, var.storage); |
| 940 |
else |
| 941 |
effective_semantic = join(semantic, "_", i); |
| 942 |
|
| 943 |
statement(to_interpolation_qualifiers(get_decoration_bitset(var.self)), |
| 944 |
variable_decl(newtype, join(name, "_", i)), " : ", effective_semantic, ";"); |
| 945 |
active_locations.insert(location_number++); |
| 946 |
} |
| 947 |
} |
| 948 |
else |
| 949 |
{ |
| 950 |
statement(to_interpolation_qualifiers(get_decoration_bitset(var.self)), variable_decl(type, name), " : ", |
| 951 |
semantic, ";"); |
| 952 |
|
| 953 |
// Structs and arrays should consume more locations. |
| 954 |
uint32_t consumed_locations = type_to_consumed_locations(type); |
| 955 |
for (uint32_t i = 0; i < consumed_locations; i++) |
| 956 |
active_locations.insert(location_number + i); |
| 957 |
} |
| 958 |
} |
| 959 |
else |
| 960 |
statement(variable_decl(type, name), " : ", binding, ";"); |
| 961 |
} |
| 962 |
|
| 963 |
std::string CompilerHLSL::builtin_to_glsl(spv::BuiltIn builtin, spv::StorageClass storage) |
| 964 |
{ |
| 965 |
switch (builtin) |
| 966 |
{ |
| 967 |
case BuiltInVertexId: |
| 968 |
return "gl_VertexID"; |
| 969 |
case BuiltInInstanceId: |
| 970 |
return "gl_InstanceID"; |
| 971 |
case BuiltInNumWorkgroups: |
| 972 |
{ |
| 973 |
if (!num_workgroups_builtin) |
| 974 |
SPIRV_CROSS_THROW("NumWorkgroups builtin is used, but remap_num_workgroups_builtin() was not called. " |
| 975 |
"Cannot emit code for this builtin."); |
| 976 |
|
| 977 |
auto &var = get<SPIRVariable>(num_workgroups_builtin); |
| 978 |
auto &type = get<SPIRType>(var.basetype); |
| 979 |
auto ret = join(to_name(num_workgroups_builtin), "_", get_member_name(type.self, 0)); |
| 980 |
ParsedIR::sanitize_underscores(ret); |
| 981 |
return ret; |
| 982 |
} |
| 983 |
case BuiltInPointCoord: |
| 984 |
// Crude hack, but there is no real alternative. This path is only enabled if point_coord_compat is set. |
| 985 |
return "float2(0.5f, 0.5f)"; |
| 986 |
case BuiltInSubgroupLocalInvocationId: |
| 987 |
return "WaveGetLaneIndex()"; |
| 988 |
case BuiltInSubgroupSize: |
| 989 |
return "WaveGetLaneCount()"; |
| 990 |
|
| 991 |
default: |
| 992 |
return CompilerGLSL::builtin_to_glsl(builtin, storage); |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
void CompilerHLSL::emit_builtin_variables() |
| 997 |
{ |
| 998 |
Bitset builtins = active_input_builtins; |
| 999 |
builtins.merge_or(active_output_builtins); |
| 1000 |
|
| 1001 |
bool need_base_vertex_info = false; |
| 1002 |
|
| 1003 |
// Emit global variables for the interface variables which are statically used by the shader. |
| 1004 |
builtins.for_each_bit([&](uint32_t i) { |
| 1005 |
const char *type = nullptr; |
| 1006 |
auto builtin = static_cast<BuiltIn>(i); |
| 1007 |
uint32_t array_size = 0; |
| 1008 |
|
| 1009 |
switch (builtin) |
| 1010 |
{ |
| 1011 |
case BuiltInFragCoord: |
| 1012 |
case BuiltInPosition: |
| 1013 |
type = "float4"; |
| 1014 |
break; |
| 1015 |
|
| 1016 |
case BuiltInFragDepth: |
| 1017 |
type = "float"; |
| 1018 |
break; |
| 1019 |
|
| 1020 |
case BuiltInVertexId: |
| 1021 |
case BuiltInVertexIndex: |
| 1022 |
case BuiltInInstanceIndex: |
| 1023 |
type = "int"; |
| 1024 |
if (hlsl_options.support_nonzero_base_vertex_base_instance) |
| 1025 |
need_base_vertex_info = true; |
| 1026 |
break; |
| 1027 |
|
| 1028 |
case BuiltInInstanceId: |
| 1029 |
case BuiltInSampleId: |
| 1030 |
type = "int"; |
| 1031 |
break; |
| 1032 |
|
| 1033 |
case BuiltInPointSize: |
| 1034 |
if (hlsl_options.point_size_compat) |
| 1035 |
{ |
| 1036 |
// Just emit the global variable, it will be ignored. |
| 1037 |
type = "float"; |
| 1038 |
break; |
| 1039 |
} |
| 1040 |
else |
| 1041 |
SPIRV_CROSS_THROW(join("Unsupported builtin in HLSL: ", unsigned(builtin))); |
| 1042 |
|
| 1043 |
case BuiltInGlobalInvocationId: |
| 1044 |
case BuiltInLocalInvocationId: |
| 1045 |
case BuiltInWorkgroupId: |
| 1046 |
type = "uint3"; |
| 1047 |
break; |
| 1048 |
|
| 1049 |
case BuiltInLocalInvocationIndex: |
| 1050 |
type = "uint"; |
| 1051 |
break; |
| 1052 |
|
| 1053 |
case BuiltInFrontFacing: |
| 1054 |
type = "bool"; |
| 1055 |
break; |
| 1056 |
|
| 1057 |
case BuiltInNumWorkgroups: |
| 1058 |
case BuiltInPointCoord: |
| 1059 |
// Handled specially. |
| 1060 |
break; |
| 1061 |
|
| 1062 |
case BuiltInSubgroupLocalInvocationId: |
| 1063 |
case BuiltInSubgroupSize: |
| 1064 |
if (hlsl_options.shader_model < 60) |
| 1065 |
SPIRV_CROSS_THROW("Need SM 6.0 for Wave ops."); |
| 1066 |
break; |
| 1067 |
|
| 1068 |
case BuiltInSubgroupEqMask: |
| 1069 |
case BuiltInSubgroupLtMask: |
| 1070 |
case BuiltInSubgroupLeMask: |
| 1071 |
case BuiltInSubgroupGtMask: |
| 1072 |
case BuiltInSubgroupGeMask: |
| 1073 |
if (hlsl_options.shader_model < 60) |
| 1074 |
SPIRV_CROSS_THROW("Need SM 6.0 for Wave ops."); |
| 1075 |
type = "uint4"; |
| 1076 |
break; |
| 1077 |
|
| 1078 |
case BuiltInClipDistance: |
| 1079 |
array_size = clip_distance_count; |
| 1080 |
type = "float"; |
| 1081 |
break; |
| 1082 |
|
| 1083 |
case BuiltInCullDistance: |
| 1084 |
array_size = cull_distance_count; |
| 1085 |
type = "float"; |
| 1086 |
break; |
| 1087 |
|
| 1088 |
case BuiltInSampleMask: |
| 1089 |
type = "int"; |
| 1090 |
break; |
| 1091 |
|
| 1092 |
default: |
| 1093 |
SPIRV_CROSS_THROW(join("Unsupported builtin in HLSL: ", unsigned(builtin))); |
| 1094 |
} |
| 1095 |
|
| 1096 |
StorageClass storage = active_input_builtins.get(i) ? StorageClassInput : StorageClassOutput; |
| 1097 |
|
| 1098 |
if (type) |
| 1099 |
{ |
| 1100 |
if (array_size) |
| 1101 |
statement("static ", type, " ", builtin_to_glsl(builtin, storage), "[", array_size, "];"); |
| 1102 |
else |
| 1103 |
statement("static ", type, " ", builtin_to_glsl(builtin, storage), ";"); |
| 1104 |
} |
| 1105 |
|
| 1106 |
// SampleMask can be both in and out with sample builtin, in this case we have already |
| 1107 |
// declared the input variable and we need to add the output one now. |
| 1108 |
if (builtin == BuiltInSampleMask && storage == StorageClassInput && this->active_output_builtins.get(i)) |
| 1109 |
{ |
| 1110 |
statement("static ", type, " ", this->builtin_to_glsl(builtin, StorageClassOutput), ";"); |
| 1111 |
} |
| 1112 |
}); |
| 1113 |
|
| 1114 |
if (need_base_vertex_info) |
| 1115 |
{ |
| 1116 |
statement("cbuffer SPIRV_Cross_VertexInfo"); |
| 1117 |
begin_scope(); |
| 1118 |
statement("int SPIRV_Cross_BaseVertex;"); |
| 1119 |
statement("int SPIRV_Cross_BaseInstance;"); |
| 1120 |
end_scope_decl(); |
| 1121 |
statement(""); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
void CompilerHLSL::emit_composite_constants() |
| 1126 |
{ |
| 1127 |
// HLSL cannot declare structs or arrays inline, so we must move them out to |
| 1128 |
// global constants directly. |
| 1129 |
bool emitted = false; |
| 1130 |
|
| 1131 |
ir.for_each_typed_id<SPIRConstant>([&](uint32_t, SPIRConstant &c) { |
| 1132 |
if (c.specialization) |
| 1133 |
return; |
| 1134 |
|
| 1135 |
auto &type = this->get<SPIRType>(c.constant_type); |
| 1136 |
if (type.basetype == SPIRType::Struct || !type.array.empty()) |
| 1137 |
{ |
| 1138 |
auto name = to_name(c.self); |
| 1139 |
statement("static const ", variable_decl(type, name), " = ", constant_expression(c), ";"); |
| 1140 |
emitted = true; |
| 1141 |
} |
| 1142 |
}); |
| 1143 |
|
| 1144 |
if (emitted) |
| 1145 |
statement(""); |
| 1146 |
} |
| 1147 |
|
| 1148 |
void CompilerHLSL::emit_specialization_constants_and_structs() |
| 1149 |
{ |
| 1150 |
bool emitted = false; |
| 1151 |
SpecializationConstant wg_x, wg_y, wg_z; |
| 1152 |
ID workgroup_size_id = get_work_group_size_specialization_constants(wg_x, wg_y, wg_z); |
| 1153 |
|
| 1154 |
auto loop_lock = ir.create_loop_hard_lock(); |
| 1155 |
for (auto &id_ : ir.ids_for_constant_or_type) |
| 1156 |
{ |
| 1157 |
auto &id = ir.ids[id_]; |
| 1158 |
|
| 1159 |
if (id.get_type() == TypeConstant) |
| 1160 |
{ |
| 1161 |
auto &c = id.get<SPIRConstant>(); |
| 1162 |
|
| 1163 |
if (c.self == workgroup_size_id) |
| 1164 |
{ |
| 1165 |
statement("static const uint3 gl_WorkGroupSize = ", |
| 1166 |
constant_expression(get<SPIRConstant>(workgroup_size_id)), ";"); |
| 1167 |
emitted = true; |
| 1168 |
} |
| 1169 |
else if (c.specialization) |
| 1170 |
{ |
| 1171 |
auto &type = get<SPIRType>(c.constant_type); |
| 1172 |
auto name = to_name(c.self); |
| 1173 |
|
| 1174 |
// HLSL does not support specialization constants, so fallback to macros. |
| 1175 |
c.specialization_constant_macro_name = |
| 1176 |
constant_value_macro_name(get_decoration(c.self, DecorationSpecId)); |
| 1177 |
|
| 1178 |
statement("#ifndef ", c.specialization_constant_macro_name); |
| 1179 |
statement("#define ", c.specialization_constant_macro_name, " ", constant_expression(c)); |
| 1180 |
statement("#endif"); |
| 1181 |
statement("static const ", variable_decl(type, name), " = ", c.specialization_constant_macro_name, ";"); |
| 1182 |
emitted = true; |
| 1183 |
} |
| 1184 |
} |
| 1185 |
else if (id.get_type() == TypeConstantOp) |
| 1186 |
{ |
| 1187 |
auto &c = id.get<SPIRConstantOp>(); |
| 1188 |
auto &type = get<SPIRType>(c.basetype); |
| 1189 |
auto name = to_name(c.self); |
| 1190 |
statement("static const ", variable_decl(type, name), " = ", constant_op_expression(c), ";"); |
| 1191 |
emitted = true; |
| 1192 |
} |
| 1193 |
else if (id.get_type() == TypeType) |
| 1194 |
{ |
| 1195 |
auto &type = id.get<SPIRType>(); |
| 1196 |
if (type.basetype == SPIRType::Struct && type.array.empty() && !type.pointer && |
| 1197 |
(!ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock) && |
| 1198 |
!ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock))) |
| 1199 |
{ |
| 1200 |
if (emitted) |
| 1201 |
statement(""); |
| 1202 |
emitted = false; |
| 1203 |
|
| 1204 |
emit_struct(type); |
| 1205 |
} |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
if (emitted) |
| 1210 |
statement(""); |
| 1211 |
} |
| 1212 |
|
| 1213 |
void CompilerHLSL::replace_illegal_names() |
| 1214 |
{ |
| 1215 |
static const unordered_set<string> keywords = { |
| 1216 |
// Additional HLSL specific keywords. |
| 1217 |
"line", "linear", "matrix", "point", "row_major", "sampler", |
| 1218 |
}; |
| 1219 |
|
| 1220 |
CompilerGLSL::replace_illegal_names(keywords); |
| 1221 |
CompilerGLSL::replace_illegal_names(); |
| 1222 |
} |
| 1223 |
|
| 1224 |
void CompilerHLSL::declare_undefined_values() |
| 1225 |
{ |
| 1226 |
bool emitted = false; |
| 1227 |
ir.for_each_typed_id<SPIRUndef>([&](uint32_t, const SPIRUndef &undef) { |
| 1228 |
auto &type = this->get<SPIRType>(undef.basetype); |
| 1229 |
// OpUndef can be void for some reason ... |
| 1230 |
if (type.basetype == SPIRType::Void) |
| 1231 |
return; |
| 1232 |
|
| 1233 |
string initializer; |
| 1234 |
if (options.force_zero_initialized_variables && type_can_zero_initialize(type)) |
| 1235 |
initializer = join(" = ", to_zero_initialized_expression(undef.basetype)); |
| 1236 |
|
| 1237 |
statement("static ", variable_decl(type, to_name(undef.self), undef.self), initializer, ";"); |
| 1238 |
emitted = true; |
| 1239 |
}); |
| 1240 |
|
| 1241 |
if (emitted) |
| 1242 |
statement(""); |
| 1243 |
} |
| 1244 |
|
| 1245 |
void CompilerHLSL::emit_resources() |
| 1246 |
{ |
| 1247 |
auto &execution = get_entry_point(); |
| 1248 |
|
| 1249 |
replace_illegal_names(); |
| 1250 |
|
| 1251 |
emit_specialization_constants_and_structs(); |
| 1252 |
emit_composite_constants(); |
| 1253 |
|
| 1254 |
bool emitted = false; |
| 1255 |
|
| 1256 |
// Output UBOs and SSBOs |
| 1257 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 1258 |
auto &type = this->get<SPIRType>(var.basetype); |
| 1259 |
|
| 1260 |
bool is_block_storage = type.storage == StorageClassStorageBuffer || type.storage == StorageClassUniform; |
| 1261 |
bool has_block_flags = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock) || |
| 1262 |
ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock); |
| 1263 |
|
| 1264 |
if (var.storage != StorageClassFunction && type.pointer && is_block_storage && !is_hidden_variable(var) && |
| 1265 |
has_block_flags) |
| 1266 |
{ |
| 1267 |
emit_buffer_block(var); |
| 1268 |
emitted = true; |
| 1269 |
} |
| 1270 |
}); |
| 1271 |
|
| 1272 |
// Output push constant blocks |
| 1273 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 1274 |
auto &type = this->get<SPIRType>(var.basetype); |
| 1275 |
if (var.storage != StorageClassFunction && type.pointer && type.storage == StorageClassPushConstant && |
| 1276 |
!is_hidden_variable(var)) |
| 1277 |
{ |
| 1278 |
emit_push_constant_block(var); |
| 1279 |
emitted = true; |
| 1280 |
} |
| 1281 |
}); |
| 1282 |
|
| 1283 |
if (execution.model == ExecutionModelVertex && hlsl_options.shader_model <= 30) |
| 1284 |
{ |
| 1285 |
statement("uniform float4 gl_HalfPixel;"); |
| 1286 |
emitted = true; |
| 1287 |
} |
| 1288 |
|
| 1289 |
bool skip_separate_image_sampler = !combined_image_samplers.empty() || hlsl_options.shader_model <= 30; |
| 1290 |
|
| 1291 |
// Output Uniform Constants (values, samplers, images, etc). |
| 1292 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 1293 |
auto &type = this->get<SPIRType>(var.basetype); |
| 1294 |
|
| 1295 |
// If we're remapping separate samplers and images, only emit the combined samplers. |
| 1296 |
if (skip_separate_image_sampler) |
| 1297 |
{ |
| 1298 |
// Sampler buffers are always used without a sampler, and they will also work in regular D3D. |
| 1299 |
bool sampler_buffer = type.basetype == SPIRType::Image && type.image.dim == DimBuffer; |
| 1300 |
bool separate_image = type.basetype == SPIRType::Image && type.image.sampled == 1; |
| 1301 |
bool separate_sampler = type.basetype == SPIRType::Sampler; |
| 1302 |
if (!sampler_buffer && (separate_image || separate_sampler)) |
| 1303 |
return; |
| 1304 |
} |
| 1305 |
|
| 1306 |
if (var.storage != StorageClassFunction && !is_builtin_variable(var) && !var.remapped_variable && |
| 1307 |
type.pointer && (type.storage == StorageClassUniformConstant || type.storage == StorageClassAtomicCounter)) |
| 1308 |
{ |
| 1309 |
emit_uniform(var); |
| 1310 |
emitted = true; |
| 1311 |
} |
| 1312 |
}); |
| 1313 |
|
| 1314 |
if (emitted) |
| 1315 |
statement(""); |
| 1316 |
emitted = false; |
| 1317 |
|
| 1318 |
// Emit builtin input and output variables here. |
| 1319 |
emit_builtin_variables(); |
| 1320 |
|
| 1321 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 1322 |
auto &type = this->get<SPIRType>(var.basetype); |
| 1323 |
bool block = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock); |
| 1324 |
|
| 1325 |
// Do not emit I/O blocks here. |
| 1326 |
// I/O blocks can be arrayed, so we must deal with them separately to support geometry shaders |
| 1327 |
// and tessellation down the line. |
| 1328 |
if (!block && var.storage != StorageClassFunction && !var.remapped_variable && type.pointer && |
| 1329 |
(var.storage == StorageClassInput || var.storage == StorageClassOutput) && !is_builtin_variable(var) && |
| 1330 |
interface_variable_exists_in_entry_point(var.self)) |
| 1331 |
{ |
| 1332 |
// Only emit non-builtins which are not blocks here. Builtin variables are handled separately. |
| 1333 |
emit_interface_block_globally(var); |
| 1334 |
emitted = true; |
| 1335 |
} |
| 1336 |
}); |
| 1337 |
|
| 1338 |
if (emitted) |
| 1339 |
statement(""); |
| 1340 |
emitted = false; |
| 1341 |
|
| 1342 |
require_input = false; |
| 1343 |
require_output = false; |
| 1344 |
unordered_set<uint32_t> active_inputs; |
| 1345 |
unordered_set<uint32_t> active_outputs; |
| 1346 |
SmallVector<SPIRVariable *> input_variables; |
| 1347 |
SmallVector<SPIRVariable *> output_variables; |
| 1348 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 1349 |
auto &type = this->get<SPIRType>(var.basetype); |
| 1350 |
bool block = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock); |
| 1351 |
|
| 1352 |
if (var.storage != StorageClassInput && var.storage != StorageClassOutput) |
| 1353 |
return; |
| 1354 |
|
| 1355 |
// Do not emit I/O blocks here. |
| 1356 |
// I/O blocks can be arrayed, so we must deal with them separately to support geometry shaders |
| 1357 |
// and tessellation down the line. |
| 1358 |
if (!block && !var.remapped_variable && type.pointer && !is_builtin_variable(var) && |
| 1359 |
interface_variable_exists_in_entry_point(var.self)) |
| 1360 |
{ |
| 1361 |
if (var.storage == StorageClassInput) |
| 1362 |
input_variables.push_back(&var); |
| 1363 |
else |
| 1364 |
output_variables.push_back(&var); |
| 1365 |
} |
| 1366 |
|
| 1367 |
// Reserve input and output locations for block variables as necessary. |
| 1368 |
if (block && !is_builtin_variable(var) && interface_variable_exists_in_entry_point(var.self)) |
| 1369 |
{ |
| 1370 |
auto &active = var.storage == StorageClassInput ? active_inputs : active_outputs; |
| 1371 |
for (uint32_t i = 0; i < uint32_t(type.member_types.size()); i++) |
| 1372 |
{ |
| 1373 |
if (has_member_decoration(type.self, i, DecorationLocation)) |
| 1374 |
{ |
| 1375 |
uint32_t location = get_member_decoration(type.self, i, DecorationLocation); |
| 1376 |
active.insert(location); |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
// Emit the block struct and a global variable here. |
| 1381 |
emit_io_block(var); |
| 1382 |
} |
| 1383 |
}); |
| 1384 |
|
| 1385 |
const auto variable_compare = [&](const SPIRVariable *a, const SPIRVariable *b) -> bool { |
| 1386 |
// Sort input and output variables based on, from more robust to less robust: |
| 1387 |
// - Location |
| 1388 |
// - Variable has a location |
| 1389 |
// - Name comparison |
| 1390 |
// - Variable has a name |
| 1391 |
// - Fallback: ID |
| 1392 |
bool has_location_a = has_decoration(a->self, DecorationLocation); |
| 1393 |
bool has_location_b = has_decoration(b->self, DecorationLocation); |
| 1394 |
|
| 1395 |
if (has_location_a && has_location_b) |
| 1396 |
{ |
| 1397 |
return get_decoration(a->self, DecorationLocation) < get_decoration(b->self, DecorationLocation); |
| 1398 |
} |
| 1399 |
else if (has_location_a && !has_location_b) |
| 1400 |
return true; |
| 1401 |
else if (!has_location_a && has_location_b) |
| 1402 |
return false; |
| 1403 |
|
| 1404 |
const auto &name1 = to_name(a->self); |
| 1405 |
const auto &name2 = to_name(b->self); |
| 1406 |
|
| 1407 |
if (name1.empty() && name2.empty()) |
| 1408 |
return a->self < b->self; |
| 1409 |
else if (name1.empty()) |
| 1410 |
return true; |
| 1411 |
else if (name2.empty()) |
| 1412 |
return false; |
| 1413 |
|
| 1414 |
return name1.compare(name2) < 0; |
| 1415 |
}; |
| 1416 |
|
| 1417 |
auto input_builtins = active_input_builtins; |
| 1418 |
input_builtins.clear(BuiltInNumWorkgroups); |
| 1419 |
input_builtins.clear(BuiltInPointCoord); |
| 1420 |
input_builtins.clear(BuiltInSubgroupSize); |
| 1421 |
input_builtins.clear(BuiltInSubgroupLocalInvocationId); |
| 1422 |
input_builtins.clear(BuiltInSubgroupEqMask); |
| 1423 |
input_builtins.clear(BuiltInSubgroupLtMask); |
| 1424 |
input_builtins.clear(BuiltInSubgroupLeMask); |
| 1425 |
input_builtins.clear(BuiltInSubgroupGtMask); |
| 1426 |
input_builtins.clear(BuiltInSubgroupGeMask); |
| 1427 |
|
| 1428 |
if (!input_variables.empty() || !input_builtins.empty()) |
| 1429 |
{ |
| 1430 |
require_input = true; |
| 1431 |
statement("struct SPIRV_Cross_Input"); |
| 1432 |
|
| 1433 |
begin_scope(); |
| 1434 |
sort(input_variables.begin(), input_variables.end(), variable_compare); |
| 1435 |
for (auto var : input_variables) |
| 1436 |
emit_interface_block_in_struct(*var, active_inputs); |
| 1437 |
emit_builtin_inputs_in_struct(); |
| 1438 |
end_scope_decl(); |
| 1439 |
statement(""); |
| 1440 |
} |
| 1441 |
|
| 1442 |
if (!output_variables.empty() || !active_output_builtins.empty()) |
| 1443 |
{ |
| 1444 |
require_output = true; |
| 1445 |
statement("struct SPIRV_Cross_Output"); |
| 1446 |
|
| 1447 |
begin_scope(); |
| 1448 |
// FIXME: Use locations properly if they exist. |
| 1449 |
sort(output_variables.begin(), output_variables.end(), variable_compare); |
| 1450 |
for (auto var : output_variables) |
| 1451 |
emit_interface_block_in_struct(*var, active_outputs); |
| 1452 |
emit_builtin_outputs_in_struct(); |
| 1453 |
end_scope_decl(); |
| 1454 |
statement(""); |
| 1455 |
} |
| 1456 |
|
| 1457 |
// Global variables. |
| 1458 |
for (auto global : global_variables) |
| 1459 |
{ |
| 1460 |
auto &var = get<SPIRVariable>(global); |
| 1461 |
if (var.storage != StorageClassOutput) |
| 1462 |
{ |
| 1463 |
if (!variable_is_lut(var)) |
| 1464 |
{ |
| 1465 |
add_resource_name(var.self); |
| 1466 |
|
| 1467 |
const char *storage = nullptr; |
| 1468 |
switch (var.storage) |
| 1469 |
{ |
| 1470 |
case StorageClassWorkgroup: |
| 1471 |
storage = "groupshared"; |
| 1472 |
break; |
| 1473 |
|
| 1474 |
default: |
| 1475 |
storage = "static"; |
| 1476 |
break; |
| 1477 |
} |
| 1478 |
|
| 1479 |
string initializer; |
| 1480 |
if (options.force_zero_initialized_variables && var.storage == StorageClassPrivate && |
| 1481 |
!var.initializer && !var.static_expression && type_can_zero_initialize(get_variable_data_type(var))) |
| 1482 |
{ |
| 1483 |
initializer = join(" = ", to_zero_initialized_expression(get_variable_data_type_id(var))); |
| 1484 |
} |
| 1485 |
statement(storage, " ", variable_decl(var), initializer, ";"); |
| 1486 |
|
| 1487 |
emitted = true; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
} |
| 1491 |
|
| 1492 |
if (emitted) |
| 1493 |
statement(""); |
| 1494 |
|
| 1495 |
declare_undefined_values(); |
| 1496 |
|
| 1497 |
if (requires_op_fmod) |
| 1498 |
{ |
| 1499 |
static const char *types[] = { |
| 1500 |
"float", |
| 1501 |
"float2", |
| 1502 |
"float3", |
| 1503 |
"float4", |
| 1504 |
}; |
| 1505 |
|
| 1506 |
for (auto &type : types) |
| 1507 |
{ |
| 1508 |
statement(type, " mod(", type, " x, ", type, " y)"); |
| 1509 |
begin_scope(); |
| 1510 |
statement("return x - y * floor(x / y);"); |
| 1511 |
end_scope(); |
| 1512 |
statement(""); |
| 1513 |
} |
| 1514 |
} |
| 1515 |
|
| 1516 |
emit_texture_size_variants(required_texture_size_variants.srv, "4", false, ""); |
| 1517 |
for (uint32_t norm = 0; norm < 3; norm++) |
| 1518 |
{ |
| 1519 |
for (uint32_t comp = 0; comp < 4; comp++) |
| 1520 |
{ |
| 1521 |
static const char *qualifiers[] = { "", "unorm ", "snorm " }; |
| 1522 |
static const char *vecsizes[] = { "", "2", "3", "4" }; |
| 1523 |
emit_texture_size_variants(required_texture_size_variants.uav[norm][comp], vecsizes[comp], true, |
| 1524 |
qualifiers[norm]); |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
if (requires_fp16_packing) |
| 1529 |
{ |
| 1530 |
// HLSL does not pack into a single word sadly :( |
| 1531 |
statement("uint SPIRV_Cross_packHalf2x16(float2 value)"); |
| 1532 |
begin_scope(); |
| 1533 |
statement("uint2 Packed = f32tof16(value);"); |
| 1534 |
statement("return Packed.x | (Packed.y << 16);"); |
| 1535 |
end_scope(); |
| 1536 |
statement(""); |
| 1537 |
|
| 1538 |
statement("float2 SPIRV_Cross_unpackHalf2x16(uint value)"); |
| 1539 |
begin_scope(); |
| 1540 |
statement("return f16tof32(uint2(value & 0xffff, value >> 16));"); |
| 1541 |
end_scope(); |
| 1542 |
statement(""); |
| 1543 |
} |
| 1544 |
|
| 1545 |
if (requires_uint2_packing) |
| 1546 |
{ |
| 1547 |
statement("uint64_t SPIRV_Cross_packUint2x32(uint2 value)"); |
| 1548 |
begin_scope(); |
| 1549 |
statement("return (uint64_t(value.y) << 32) | uint64_t(value.x);"); |
| 1550 |
end_scope(); |
| 1551 |
statement(""); |
| 1552 |
|
| 1553 |
statement("uint2 SPIRV_Cross_unpackUint2x32(uint64_t value)"); |
| 1554 |
begin_scope(); |
| 1555 |
statement("uint2 Unpacked;"); |
| 1556 |
statement("Unpacked.x = uint(value & 0xffffffff);"); |
| 1557 |
statement("Unpacked.y = uint(value >> 32);"); |
| 1558 |
statement("return Unpacked;"); |
| 1559 |
end_scope(); |
| 1560 |
statement(""); |
| 1561 |
} |
| 1562 |
|
| 1563 |
if (requires_explicit_fp16_packing) |
| 1564 |
{ |
| 1565 |
// HLSL does not pack into a single word sadly :( |
| 1566 |
statement("uint SPIRV_Cross_packFloat2x16(min16float2 value)"); |
| 1567 |
begin_scope(); |
| 1568 |
statement("uint2 Packed = f32tof16(value);"); |
| 1569 |
statement("return Packed.x | (Packed.y << 16);"); |
| 1570 |
end_scope(); |
| 1571 |
statement(""); |
| 1572 |
|
| 1573 |
statement("min16float2 SPIRV_Cross_unpackFloat2x16(uint value)"); |
| 1574 |
begin_scope(); |
| 1575 |
statement("return min16float2(f16tof32(uint2(value & 0xffff, value >> 16)));"); |
| 1576 |
end_scope(); |
| 1577 |
statement(""); |
| 1578 |
} |
| 1579 |
|
| 1580 |
// HLSL does not seem to have builtins for these operation, so roll them by hand ... |
| 1581 |
if (requires_unorm8_packing) |
| 1582 |
{ |
| 1583 |
statement("uint SPIRV_Cross_packUnorm4x8(float4 value)"); |
| 1584 |
begin_scope(); |
| 1585 |
statement("uint4 Packed = uint4(round(saturate(value) * 255.0));"); |
| 1586 |
statement("return Packed.x | (Packed.y << 8) | (Packed.z << 16) | (Packed.w << 24);"); |
| 1587 |
end_scope(); |
| 1588 |
statement(""); |
| 1589 |
|
| 1590 |
statement("float4 SPIRV_Cross_unpackUnorm4x8(uint value)"); |
| 1591 |
begin_scope(); |
| 1592 |
statement("uint4 Packed = uint4(value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, value >> 24);"); |
| 1593 |
statement("return float4(Packed) / 255.0;"); |
| 1594 |
end_scope(); |
| 1595 |
statement(""); |
| 1596 |
} |
| 1597 |
|
| 1598 |
if (requires_snorm8_packing) |
| 1599 |
{ |
| 1600 |
statement("uint SPIRV_Cross_packSnorm4x8(float4 value)"); |
| 1601 |
begin_scope(); |
| 1602 |
statement("int4 Packed = int4(round(clamp(value, -1.0, 1.0) * 127.0)) & 0xff;"); |
| 1603 |
statement("return uint(Packed.x | (Packed.y << 8) | (Packed.z << 16) | (Packed.w << 24));"); |
| 1604 |
end_scope(); |
| 1605 |
statement(""); |
| 1606 |
|
| 1607 |
statement("float4 SPIRV_Cross_unpackSnorm4x8(uint value)"); |
| 1608 |
begin_scope(); |
| 1609 |
statement("int SignedValue = int(value);"); |
| 1610 |
statement("int4 Packed = int4(SignedValue << 24, SignedValue << 16, SignedValue << 8, SignedValue) >> 24;"); |
| 1611 |
statement("return clamp(float4(Packed) / 127.0, -1.0, 1.0);"); |
| 1612 |
end_scope(); |
| 1613 |
statement(""); |
| 1614 |
} |
| 1615 |
|
| 1616 |
if (requires_unorm16_packing) |
| 1617 |
{ |
| 1618 |
statement("uint SPIRV_Cross_packUnorm2x16(float2 value)"); |
| 1619 |
begin_scope(); |
| 1620 |
statement("uint2 Packed = uint2(round(saturate(value) * 65535.0));"); |
| 1621 |
statement("return Packed.x | (Packed.y << 16);"); |
| 1622 |
end_scope(); |
| 1623 |
statement(""); |
| 1624 |
|
| 1625 |
statement("float2 SPIRV_Cross_unpackUnorm2x16(uint value)"); |
| 1626 |
begin_scope(); |
| 1627 |
statement("uint2 Packed = uint2(value & 0xffff, value >> 16);"); |
| 1628 |
statement("return float2(Packed) / 65535.0;"); |
| 1629 |
end_scope(); |
| 1630 |
statement(""); |
| 1631 |
} |
| 1632 |
|
| 1633 |
if (requires_snorm16_packing) |
| 1634 |
{ |
| 1635 |
statement("uint SPIRV_Cross_packSnorm2x16(float2 value)"); |
| 1636 |
begin_scope(); |
| 1637 |
statement("int2 Packed = int2(round(clamp(value, -1.0, 1.0) * 32767.0)) & 0xffff;"); |
| 1638 |
statement("return uint(Packed.x | (Packed.y << 16));"); |
| 1639 |
end_scope(); |
| 1640 |
statement(""); |
| 1641 |
|
| 1642 |
statement("float2 SPIRV_Cross_unpackSnorm2x16(uint value)"); |
| 1643 |
begin_scope(); |
| 1644 |
statement("int SignedValue = int(value);"); |
| 1645 |
statement("int2 Packed = int2(SignedValue << 16, SignedValue) >> 16;"); |
| 1646 |
statement("return clamp(float2(Packed) / 32767.0, -1.0, 1.0);"); |
| 1647 |
end_scope(); |
| 1648 |
statement(""); |
| 1649 |
} |
| 1650 |
|
| 1651 |
if (requires_bitfield_insert) |
| 1652 |
{ |
| 1653 |
static const char *types[] = { "uint", "uint2", "uint3", "uint4" }; |
| 1654 |
for (auto &type : types) |
| 1655 |
{ |
| 1656 |
statement(type, " SPIRV_Cross_bitfieldInsert(", type, " Base, ", type, " Insert, uint Offset, uint Count)"); |
| 1657 |
begin_scope(); |
| 1658 |
statement("uint Mask = Count == 32 ? 0xffffffff : (((1u << Count) - 1) << (Offset & 31));"); |
| 1659 |
statement("return (Base & ~Mask) | ((Insert << Offset) & Mask);"); |
| 1660 |
end_scope(); |
| 1661 |
statement(""); |
| 1662 |
} |
| 1663 |
} |
| 1664 |
|
| 1665 |
if (requires_bitfield_extract) |
| 1666 |
{ |
| 1667 |
static const char *unsigned_types[] = { "uint", "uint2", "uint3", "uint4" }; |
| 1668 |
for (auto &type : unsigned_types) |
| 1669 |
{ |
| 1670 |
statement(type, " SPIRV_Cross_bitfieldUExtract(", type, " Base, uint Offset, uint Count)"); |
| 1671 |
begin_scope(); |
| 1672 |
statement("uint Mask = Count == 32 ? 0xffffffff : ((1 << Count) - 1);"); |
| 1673 |
statement("return (Base >> Offset) & Mask;"); |
| 1674 |
end_scope(); |
| 1675 |
statement(""); |
| 1676 |
} |
| 1677 |
|
| 1678 |
// In this overload, we will have to do sign-extension, which we will emulate by shifting up and down. |
| 1679 |
static const char *signed_types[] = { "int", "int2", "int3", "int4" }; |
| 1680 |
for (auto &type : signed_types) |
| 1681 |
{ |
| 1682 |
statement(type, " SPIRV_Cross_bitfieldSExtract(", type, " Base, int Offset, int Count)"); |
| 1683 |
begin_scope(); |
| 1684 |
statement("int Mask = Count == 32 ? -1 : ((1 << Count) - 1);"); |
| 1685 |
statement(type, " Masked = (Base >> Offset) & Mask;"); |
| 1686 |
statement("int ExtendShift = (32 - Count) & 31;"); |
| 1687 |
statement("return (Masked << ExtendShift) >> ExtendShift;"); |
| 1688 |
end_scope(); |
| 1689 |
statement(""); |
| 1690 |
} |
| 1691 |
} |
| 1692 |
|
| 1693 |
if (requires_inverse_2x2) |
| 1694 |
{ |
| 1695 |
statement("// Returns the inverse of a matrix, by using the algorithm of calculating the classical"); |
| 1696 |
statement("// adjoint and dividing by the determinant. The contents of the matrix are changed."); |
| 1697 |
statement("float2x2 SPIRV_Cross_Inverse(float2x2 m)"); |
| 1698 |
begin_scope(); |
| 1699 |
statement("float2x2 adj; // The adjoint matrix (inverse after dividing by determinant)"); |
| 1700 |
statement_no_indent(""); |
| 1701 |
statement("// Create the transpose of the cofactors, as the classical adjoint of the matrix."); |
| 1702 |
statement("adj[0][0] = m[1][1];"); |
| 1703 |
statement("adj[0][1] = -m[0][1];"); |
| 1704 |
statement_no_indent(""); |
| 1705 |
statement("adj[1][0] = -m[1][0];"); |
| 1706 |
statement("adj[1][1] = m[0][0];"); |
| 1707 |
statement_no_indent(""); |
| 1708 |
statement("// Calculate the determinant as a combination of the cofactors of the first row."); |
| 1709 |
statement("float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]);"); |
| 1710 |
statement_no_indent(""); |
| 1711 |
statement("// Divide the classical adjoint matrix by the determinant."); |
| 1712 |
statement("// If determinant is zero, matrix is not invertable, so leave it unchanged."); |
| 1713 |
statement("return (det != 0.0f) ? (adj * (1.0f / det)) : m;"); |
| 1714 |
end_scope(); |
| 1715 |
statement(""); |
| 1716 |
} |
| 1717 |
|
| 1718 |
if (requires_inverse_3x3) |
| 1719 |
{ |
| 1720 |
statement("// Returns the determinant of a 2x2 matrix."); |
| 1721 |
statement("float SPIRV_Cross_Det2x2(float a1, float a2, float b1, float b2)"); |
| 1722 |
begin_scope(); |
| 1723 |
statement("return a1 * b2 - b1 * a2;"); |
| 1724 |
end_scope(); |
| 1725 |
statement_no_indent(""); |
| 1726 |
statement("// Returns the inverse of a matrix, by using the algorithm of calculating the classical"); |
| 1727 |
statement("// adjoint and dividing by the determinant. The contents of the matrix are changed."); |
| 1728 |
statement("float3x3 SPIRV_Cross_Inverse(float3x3 m)"); |
| 1729 |
begin_scope(); |
| 1730 |
statement("float3x3 adj; // The adjoint matrix (inverse after dividing by determinant)"); |
| 1731 |
statement_no_indent(""); |
| 1732 |
statement("// Create the transpose of the cofactors, as the classical adjoint of the matrix."); |
| 1733 |
statement("adj[0][0] = SPIRV_Cross_Det2x2(m[1][1], m[1][2], m[2][1], m[2][2]);"); |
| 1734 |
statement("adj[0][1] = -SPIRV_Cross_Det2x2(m[0][1], m[0][2], m[2][1], m[2][2]);"); |
| 1735 |
statement("adj[0][2] = SPIRV_Cross_Det2x2(m[0][1], m[0][2], m[1][1], m[1][2]);"); |
| 1736 |
statement_no_indent(""); |
| 1737 |
statement("adj[1][0] = -SPIRV_Cross_Det2x2(m[1][0], m[1][2], m[2][0], m[2][2]);"); |
| 1738 |
statement("adj[1][1] = SPIRV_Cross_Det2x2(m[0][0], m[0][2], m[2][0], m[2][2]);"); |
| 1739 |
statement("adj[1][2] = -SPIRV_Cross_Det2x2(m[0][0], m[0][2], m[1][0], m[1][2]);"); |
| 1740 |
statement_no_indent(""); |
| 1741 |
statement("adj[2][0] = SPIRV_Cross_Det2x2(m[1][0], m[1][1], m[2][0], m[2][1]);"); |
| 1742 |
statement("adj[2][1] = -SPIRV_Cross_Det2x2(m[0][0], m[0][1], m[2][0], m[2][1]);"); |
| 1743 |
statement("adj[2][2] = SPIRV_Cross_Det2x2(m[0][0], m[0][1], m[1][0], m[1][1]);"); |
| 1744 |
statement_no_indent(""); |
| 1745 |
statement("// Calculate the determinant as a combination of the cofactors of the first row."); |
| 1746 |
statement("float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]);"); |
| 1747 |
statement_no_indent(""); |
| 1748 |
statement("// Divide the classical adjoint matrix by the determinant."); |
| 1749 |
statement("// If determinant is zero, matrix is not invertable, so leave it unchanged."); |
| 1750 |
statement("return (det != 0.0f) ? (adj * (1.0f / det)) : m;"); |
| 1751 |
end_scope(); |
| 1752 |
statement(""); |
| 1753 |
} |
| 1754 |
|
| 1755 |
if (requires_inverse_4x4) |
| 1756 |
{ |
| 1757 |
if (!requires_inverse_3x3) |
| 1758 |
{ |
| 1759 |
statement("// Returns the determinant of a 2x2 matrix."); |
| 1760 |
statement("float SPIRV_Cross_Det2x2(float a1, float a2, float b1, float b2)"); |
| 1761 |
begin_scope(); |
| 1762 |
statement("return a1 * b2 - b1 * a2;"); |
| 1763 |
end_scope(); |
| 1764 |
statement(""); |
| 1765 |
} |
| 1766 |
|
| 1767 |
statement("// Returns the determinant of a 3x3 matrix."); |
| 1768 |
statement("float SPIRV_Cross_Det3x3(float a1, float a2, float a3, float b1, float b2, float b3, float c1, " |
| 1769 |
"float c2, float c3)"); |
| 1770 |
begin_scope(); |
| 1771 |
statement("return a1 * SPIRV_Cross_Det2x2(b2, b3, c2, c3) - b1 * SPIRV_Cross_Det2x2(a2, a3, c2, c3) + c1 * " |
| 1772 |
"SPIRV_Cross_Det2x2(a2, a3, " |
| 1773 |
"b2, b3);"); |
| 1774 |
end_scope(); |
| 1775 |
statement_no_indent(""); |
| 1776 |
statement("// Returns the inverse of a matrix, by using the algorithm of calculating the classical"); |
| 1777 |
statement("// adjoint and dividing by the determinant. The contents of the matrix are changed."); |
| 1778 |
statement("float4x4 SPIRV_Cross_Inverse(float4x4 m)"); |
| 1779 |
begin_scope(); |
| 1780 |
statement("float4x4 adj; // The adjoint matrix (inverse after dividing by determinant)"); |
| 1781 |
statement_no_indent(""); |
| 1782 |
statement("// Create the transpose of the cofactors, as the classical adjoint of the matrix."); |
| 1783 |
statement( |
| 1784 |
"adj[0][0] = SPIRV_Cross_Det3x3(m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], " |
| 1785 |
"m[3][3]);"); |
| 1786 |
statement( |
| 1787 |
"adj[0][1] = -SPIRV_Cross_Det3x3(m[0][1], m[0][2], m[0][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], " |
| 1788 |
"m[3][3]);"); |
| 1789 |
statement( |
| 1790 |
"adj[0][2] = SPIRV_Cross_Det3x3(m[0][1], m[0][2], m[0][3], m[1][1], m[1][2], m[1][3], m[3][1], m[3][2], " |
| 1791 |
"m[3][3]);"); |
| 1792 |
statement( |
| 1793 |
"adj[0][3] = -SPIRV_Cross_Det3x3(m[0][1], m[0][2], m[0][3], m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], " |
| 1794 |
"m[2][3]);"); |
| 1795 |
statement_no_indent(""); |
| 1796 |
statement( |
| 1797 |
"adj[1][0] = -SPIRV_Cross_Det3x3(m[1][0], m[1][2], m[1][3], m[2][0], m[2][2], m[2][3], m[3][0], m[3][2], " |
| 1798 |
"m[3][3]);"); |
| 1799 |
statement( |
| 1800 |
"adj[1][1] = SPIRV_Cross_Det3x3(m[0][0], m[0][2], m[0][3], m[2][0], m[2][2], m[2][3], m[3][0], m[3][2], " |
| 1801 |
"m[3][3]);"); |
| 1802 |
statement( |
| 1803 |
"adj[1][2] = -SPIRV_Cross_Det3x3(m[0][0], m[0][2], m[0][3], m[1][0], m[1][2], m[1][3], m[3][0], m[3][2], " |
| 1804 |
"m[3][3]);"); |
| 1805 |
statement( |
| 1806 |
"adj[1][3] = SPIRV_Cross_Det3x3(m[0][0], m[0][2], m[0][3], m[1][0], m[1][2], m[1][3], m[2][0], m[2][2], " |
| 1807 |
"m[2][3]);"); |
| 1808 |
statement_no_indent(""); |
| 1809 |
statement( |
| 1810 |
"adj[2][0] = SPIRV_Cross_Det3x3(m[1][0], m[1][1], m[1][3], m[2][0], m[2][1], m[2][3], m[3][0], m[3][1], " |
| 1811 |
"m[3][3]);"); |
| 1812 |
statement( |
| 1813 |
"adj[2][1] = -SPIRV_Cross_Det3x3(m[0][0], m[0][1], m[0][3], m[2][0], m[2][1], m[2][3], m[3][0], m[3][1], " |
| 1814 |
"m[3][3]);"); |
| 1815 |
statement( |
| 1816 |
"adj[2][2] = SPIRV_Cross_Det3x3(m[0][0], m[0][1], m[0][3], m[1][0], m[1][1], m[1][3], m[3][0], m[3][1], " |
| 1817 |
"m[3][3]);"); |
| 1818 |
statement( |
| 1819 |
"adj[2][3] = -SPIRV_Cross_Det3x3(m[0][0], m[0][1], m[0][3], m[1][0], m[1][1], m[1][3], m[2][0], m[2][1], " |
| 1820 |
"m[2][3]);"); |
| 1821 |
statement_no_indent(""); |
| 1822 |
statement( |
| 1823 |
"adj[3][0] = -SPIRV_Cross_Det3x3(m[1][0], m[1][1], m[1][2], m[2][0], m[2][1], m[2][2], m[3][0], m[3][1], " |
| 1824 |
"m[3][2]);"); |
| 1825 |
statement( |
| 1826 |
"adj[3][1] = SPIRV_Cross_Det3x3(m[0][0], m[0][1], m[0][2], m[2][0], m[2][1], m[2][2], m[3][0], m[3][1], " |
| 1827 |
"m[3][2]);"); |
| 1828 |
statement( |
| 1829 |
"adj[3][2] = -SPIRV_Cross_Det3x3(m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], m[3][0], m[3][1], " |
| 1830 |
"m[3][2]);"); |
| 1831 |
statement( |
| 1832 |
"adj[3][3] = SPIRV_Cross_Det3x3(m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], m[2][0], m[2][1], " |
| 1833 |
"m[2][2]);"); |
| 1834 |
statement_no_indent(""); |
| 1835 |
statement("// Calculate the determinant as a combination of the cofactors of the first row."); |
| 1836 |
statement("float det = (adj[0][0] * m[0][0]) + (adj[0][1] * m[1][0]) + (adj[0][2] * m[2][0]) + (adj[0][3] " |
| 1837 |
"* m[3][0]);"); |
| 1838 |
statement_no_indent(""); |
| 1839 |
statement("// Divide the classical adjoint matrix by the determinant."); |
| 1840 |
statement("// If determinant is zero, matrix is not invertable, so leave it unchanged."); |
| 1841 |
statement("return (det != 0.0f) ? (adj * (1.0f / det)) : m;"); |
| 1842 |
end_scope(); |
| 1843 |
statement(""); |
| 1844 |
} |
| 1845 |
|
| 1846 |
if (requires_scalar_reflect) |
| 1847 |
{ |
| 1848 |
// FP16/FP64? No templates in HLSL. |
| 1849 |
statement("float SPIRV_Cross_Reflect(float i, float n)"); |
| 1850 |
begin_scope(); |
| 1851 |
statement("return i - 2.0 * dot(n, i) * n;"); |
| 1852 |
end_scope(); |
| 1853 |
statement(""); |
| 1854 |
} |
| 1855 |
|
| 1856 |
if (requires_scalar_refract) |
| 1857 |
{ |
| 1858 |
// FP16/FP64? No templates in HLSL. |
| 1859 |
statement("float SPIRV_Cross_Refract(float i, float n, float eta)"); |
| 1860 |
begin_scope(); |
| 1861 |
statement("float NoI = n * i;"); |
| 1862 |
statement("float NoI2 = NoI * NoI;"); |
| 1863 |
statement("float k = 1.0 - eta * eta * (1.0 - NoI2);"); |
| 1864 |
statement("if (k < 0.0)"); |
| 1865 |
begin_scope(); |
| 1866 |
statement("return 0.0;"); |
| 1867 |
end_scope(); |
| 1868 |
statement("else"); |
| 1869 |
begin_scope(); |
| 1870 |
statement("return eta * i - (eta * NoI + sqrt(k)) * n;"); |
| 1871 |
end_scope(); |
| 1872 |
end_scope(); |
| 1873 |
statement(""); |
| 1874 |
} |
| 1875 |
|
| 1876 |
if (requires_scalar_faceforward) |
| 1877 |
{ |
| 1878 |
// FP16/FP64? No templates in HLSL. |
| 1879 |
statement("float SPIRV_Cross_FaceForward(float n, float i, float nref)"); |
| 1880 |
begin_scope(); |
| 1881 |
statement("return i * nref < 0.0 ? n : -n;"); |
| 1882 |
end_scope(); |
| 1883 |
statement(""); |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
void CompilerHLSL::emit_texture_size_variants(uint64_t variant_mask, const char *vecsize_qualifier, bool uav, |
| 1888 |
const char *type_qualifier) |
| 1889 |
{ |
| 1890 |
if (variant_mask == 0) |
| 1891 |
return; |
| 1892 |
|
| 1893 |
static const char *types[QueryTypeCount] = { "float", "int", "uint" }; |
| 1894 |
static const char *dims[QueryDimCount] = { "Texture1D", "Texture1DArray", "Texture2D", "Texture2DArray", |
| 1895 |
"Texture3D", "Buffer", "TextureCube", "TextureCubeArray", |
| 1896 |
"Texture2DMS", "Texture2DMSArray" }; |
| 1897 |
|
| 1898 |
static const bool has_lod[QueryDimCount] = { true, true, true, true, true, false, true, true, false, false }; |
| 1899 |
|
| 1900 |
static const char *ret_types[QueryDimCount] = { |
| 1901 |
"uint", "uint2", "uint2", "uint3", "uint3", "uint", "uint2", "uint3", "uint2", "uint3", |
| 1902 |
}; |
| 1903 |
|
| 1904 |
static const uint32_t return_arguments[QueryDimCount] = { |
| 1905 |
1, 2, 2, 3, 3, 1, 2, 3, 2, 3, |
| 1906 |
}; |
| 1907 |
|
| 1908 |
for (uint32_t index = 0; index < QueryDimCount; index++) |
| 1909 |
{ |
| 1910 |
for (uint32_t type_index = 0; type_index < QueryTypeCount; type_index++) |
| 1911 |
{ |
| 1912 |
uint32_t bit = 16 * type_index + index; |
| 1913 |
uint64_t mask = 1ull << bit; |
| 1914 |
|
| 1915 |
if ((variant_mask & mask) == 0) |
| 1916 |
continue; |
| 1917 |
|
| 1918 |
statement(ret_types[index], " SPIRV_Cross_", (uav ? "image" : "texture"), "Size(", (uav ? "RW" : ""), |
| 1919 |
dims[index], "<", type_qualifier, types[type_index], vecsize_qualifier, "> Tex, ", |
| 1920 |
(uav ? "" : "uint Level, "), "out uint Param)"); |
| 1921 |
begin_scope(); |
| 1922 |
statement(ret_types[index], " ret;"); |
| 1923 |
switch (return_arguments[index]) |
| 1924 |
{ |
| 1925 |
case 1: |
| 1926 |
if (has_lod[index] && !uav) |
| 1927 |
statement("Tex.GetDimensions(Level, ret.x, Param);"); |
| 1928 |
else |
| 1929 |
{ |
| 1930 |
statement("Tex.GetDimensions(ret.x);"); |
| 1931 |
statement("Param = 0u;"); |
| 1932 |
} |
| 1933 |
break; |
| 1934 |
case 2: |
| 1935 |
if (has_lod[index] && !uav) |
| 1936 |
statement("Tex.GetDimensions(Level, ret.x, ret.y, Param);"); |
| 1937 |
else if (!uav) |
| 1938 |
statement("Tex.GetDimensions(ret.x, ret.y, Param);"); |
| 1939 |
else |
| 1940 |
{ |
| 1941 |
statement("Tex.GetDimensions(ret.x, ret.y);"); |
| 1942 |
statement("Param = 0u;"); |
| 1943 |
} |
| 1944 |
break; |
| 1945 |
case 3: |
| 1946 |
if (has_lod[index] && !uav) |
| 1947 |
statement("Tex.GetDimensions(Level, ret.x, ret.y, ret.z, Param);"); |
| 1948 |
else if (!uav) |
| 1949 |
statement("Tex.GetDimensions(ret.x, ret.y, ret.z, Param);"); |
| 1950 |
else |
| 1951 |
{ |
| 1952 |
statement("Tex.GetDimensions(ret.x, ret.y, ret.z);"); |
| 1953 |
statement("Param = 0u;"); |
| 1954 |
} |
| 1955 |
break; |
| 1956 |
} |
| 1957 |
|
| 1958 |
statement("return ret;"); |
| 1959 |
end_scope(); |
| 1960 |
statement(""); |
| 1961 |
} |
| 1962 |
} |
| 1963 |
} |
| 1964 |
|
| 1965 |
string CompilerHLSL::layout_for_member(const SPIRType &type, uint32_t index) |
| 1966 |
{ |
| 1967 |
auto &flags = get_member_decoration_bitset(type.self, index); |
| 1968 |
|
| 1969 |
// HLSL can emit row_major or column_major decoration in any struct. |
| 1970 |
// Do not try to merge combined decorations for children like in GLSL. |
| 1971 |
|
| 1972 |
// Flip the convention. HLSL is a bit odd in that the memory layout is column major ... but the language API is "row-major". |
| 1973 |
// The way to deal with this is to multiply everything in inverse order, and reverse the memory layout. |
| 1974 |
if (flags.get(DecorationColMajor)) |
| 1975 |
return "row_major "; |
| 1976 |
else if (flags.get(DecorationRowMajor)) |
| 1977 |
return "column_major "; |
| 1978 |
|
| 1979 |
return ""; |
| 1980 |
} |
| 1981 |
|
| 1982 |
void CompilerHLSL::emit_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index, |
| 1983 |
const string &qualifier, uint32_t base_offset) |
| 1984 |
{ |
| 1985 |
auto &membertype = get<SPIRType>(member_type_id); |
| 1986 |
|
| 1987 |
Bitset memberflags; |
| 1988 |
auto &memb = ir.meta[type.self].members; |
| 1989 |
if (index < memb.size()) |
| 1990 |
memberflags = memb[index].decoration_flags; |
| 1991 |
|
| 1992 |
string qualifiers; |
| 1993 |
bool is_block = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock) || |
| 1994 |
ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock); |
| 1995 |
|
| 1996 |
if (is_block) |
| 1997 |
qualifiers = to_interpolation_qualifiers(memberflags); |
| 1998 |
|
| 1999 |
string packing_offset; |
| 2000 |
bool is_push_constant = type.storage == StorageClassPushConstant; |
| 2001 |
|
| 2002 |
if ((has_extended_decoration(type.self, SPIRVCrossDecorationExplicitOffset) || is_push_constant) && |
| 2003 |
has_member_decoration(type.self, index, DecorationOffset)) |
| 2004 |
{ |
| 2005 |
uint32_t offset = memb[index].offset - base_offset; |
| 2006 |
if (offset & 3) |
| 2007 |
SPIRV_CROSS_THROW("Cannot pack on tighter bounds than 4 bytes in HLSL."); |
| 2008 |
|
| 2009 |
static const char *packing_swizzle[] = { "", ".y", ".z", ".w" }; |
| 2010 |
packing_offset = join(" : packoffset(c", offset / 16, packing_swizzle[(offset & 15) >> 2], ")"); |
| 2011 |
} |
| 2012 |
|
| 2013 |
statement(layout_for_member(type, index), qualifiers, qualifier, |
| 2014 |
variable_decl(membertype, to_member_name(type, index)), packing_offset, ";"); |
| 2015 |
} |
| 2016 |
|
| 2017 |
void CompilerHLSL::emit_buffer_block(const SPIRVariable &var) |
| 2018 |
{ |
| 2019 |
auto &type = get<SPIRType>(var.basetype); |
| 2020 |
|
| 2021 |
bool is_uav = var.storage == StorageClassStorageBuffer || has_decoration(type.self, DecorationBufferBlock); |
| 2022 |
|
| 2023 |
if (is_uav) |
| 2024 |
{ |
| 2025 |
Bitset flags = ir.get_buffer_block_flags(var); |
| 2026 |
bool is_readonly = flags.get(DecorationNonWritable) && !is_hlsl_force_storage_buffer_as_uav(var.self); |
| 2027 |
bool is_coherent = flags.get(DecorationCoherent) && !is_readonly; |
| 2028 |
bool is_interlocked = interlocked_resources.count(var.self) > 0; |
| 2029 |
const char *type_name = "ByteAddressBuffer "; |
| 2030 |
if (!is_readonly) |
| 2031 |
type_name = is_interlocked ? "RasterizerOrderedByteAddressBuffer " : "RWByteAddressBuffer "; |
| 2032 |
add_resource_name(var.self); |
| 2033 |
statement(is_coherent ? "globallycoherent " : "", type_name, to_name(var.self), type_to_array_glsl(type), |
| 2034 |
to_resource_binding(var), ";"); |
| 2035 |
} |
| 2036 |
else |
| 2037 |
{ |
| 2038 |
if (type.array.empty()) |
| 2039 |
{ |
| 2040 |
// Flatten the top-level struct so we can use packoffset, |
| 2041 |
// this restriction is similar to GLSL where layout(offset) is not possible on sub-structs. |
| 2042 |
flattened_structs[var.self] = false; |
| 2043 |
|
| 2044 |
// Prefer the block name if possible. |
| 2045 |
auto buffer_name = to_name(type.self, false); |
| 2046 |
if (ir.meta[type.self].decoration.alias.empty() || |
| 2047 |
resource_names.find(buffer_name) != end(resource_names) || |
| 2048 |
block_names.find(buffer_name) != end(block_names)) |
| 2049 |
{ |
| 2050 |
buffer_name = get_block_fallback_name(var.self); |
| 2051 |
} |
| 2052 |
|
| 2053 |
add_variable(block_names, resource_names, buffer_name); |
| 2054 |
|
| 2055 |
// If for some reason buffer_name is an illegal name, make a final fallback to a workaround name. |
| 2056 |
// This cannot conflict with anything else, so we're safe now. |
| 2057 |
if (buffer_name.empty()) |
| 2058 |
buffer_name = join("_", get<SPIRType>(var.basetype).self, "_", var.self); |
| 2059 |
|
| 2060 |
uint32_t failed_index = 0; |
| 2061 |
if (buffer_is_packing_standard(type, BufferPackingHLSLCbufferPackOffset, &failed_index)) |
| 2062 |
set_extended_decoration(type.self, SPIRVCrossDecorationExplicitOffset); |
| 2063 |
else |
| 2064 |
{ |
| 2065 |
SPIRV_CROSS_THROW(join("cbuffer ID ", var.self, " (name: ", buffer_name, "), member index ", |
| 2066 |
failed_index, " (name: ", to_member_name(type, failed_index), |
| 2067 |
") cannot be expressed with either HLSL packing layout or packoffset.")); |
| 2068 |
} |
| 2069 |
|
| 2070 |
block_names.insert(buffer_name); |
| 2071 |
|
| 2072 |
// Save for post-reflection later. |
| 2073 |
declared_block_names[var.self] = buffer_name; |
| 2074 |
|
| 2075 |
type.member_name_cache.clear(); |
| 2076 |
// var.self can be used as a backup name for the block name, |
| 2077 |
// so we need to make sure we don't disturb the name here on a recompile. |
| 2078 |
// It will need to be reset if we have to recompile. |
| 2079 |
preserve_alias_on_reset(var.self); |
| 2080 |
add_resource_name(var.self); |
| 2081 |
statement("cbuffer ", buffer_name, to_resource_binding(var)); |
| 2082 |
begin_scope(); |
| 2083 |
|
| 2084 |
uint32_t i = 0; |
| 2085 |
for (auto &member : type.member_types) |
| 2086 |
{ |
| 2087 |
add_member_name(type, i); |
| 2088 |
auto backup_name = get_member_name(type.self, i); |
| 2089 |
auto member_name = to_member_name(type, i); |
| 2090 |
member_name = join(to_name(var.self), "_", member_name); |
| 2091 |
ParsedIR::sanitize_underscores(member_name); |
| 2092 |
set_member_name(type.self, i, member_name); |
| 2093 |
emit_struct_member(type, member, i, ""); |
| 2094 |
set_member_name(type.self, i, backup_name); |
| 2095 |
i++; |
| 2096 |
} |
| 2097 |
|
| 2098 |
end_scope_decl(); |
| 2099 |
statement(""); |
| 2100 |
} |
| 2101 |
else |
| 2102 |
{ |
| 2103 |
if (hlsl_options.shader_model < 51) |
| 2104 |
SPIRV_CROSS_THROW( |
| 2105 |
"Need ConstantBuffer<T> to use arrays of UBOs, but this is only supported in SM 5.1."); |
| 2106 |
|
| 2107 |
add_resource_name(type.self); |
| 2108 |
add_resource_name(var.self); |
| 2109 |
|
| 2110 |
// ConstantBuffer<T> does not support packoffset, so it is unuseable unless everything aligns as we expect. |
| 2111 |
uint32_t failed_index = 0; |
| 2112 |
if (!buffer_is_packing_standard(type, BufferPackingHLSLCbuffer, &failed_index)) |
| 2113 |
{ |
| 2114 |
SPIRV_CROSS_THROW(join("HLSL ConstantBuffer<T> ID ", var.self, " (name: ", to_name(type.self), |
| 2115 |
"), member index ", failed_index, " (name: ", to_member_name(type, failed_index), |
| 2116 |
") cannot be expressed with normal HLSL packing rules.")); |
| 2117 |
} |
| 2118 |
|
| 2119 |
emit_struct(get<SPIRType>(type.self)); |
| 2120 |
statement("ConstantBuffer<", to_name(type.self), "> ", to_name(var.self), type_to_array_glsl(type), |
| 2121 |
to_resource_binding(var), ";"); |
| 2122 |
} |
| 2123 |
} |
| 2124 |
} |
| 2125 |
|
| 2126 |
void CompilerHLSL::emit_push_constant_block(const SPIRVariable &var) |
| 2127 |
{ |
| 2128 |
if (root_constants_layout.empty()) |
| 2129 |
{ |
| 2130 |
emit_buffer_block(var); |
| 2131 |
} |
| 2132 |
else |
| 2133 |
{ |
| 2134 |
for (const auto &layout : root_constants_layout) |
| 2135 |
{ |
| 2136 |
auto &type = get<SPIRType>(var.basetype); |
| 2137 |
|
| 2138 |
uint32_t failed_index = 0; |
| 2139 |
if (buffer_is_packing_standard(type, BufferPackingHLSLCbufferPackOffset, &failed_index, layout.start, |
| 2140 |
layout.end)) |
| 2141 |
set_extended_decoration(type.self, SPIRVCrossDecorationExplicitOffset); |
| 2142 |
else |
| 2143 |
{ |
| 2144 |
SPIRV_CROSS_THROW(join("Root constant cbuffer ID ", var.self, " (name: ", to_name(type.self), ")", |
| 2145 |
", member index ", failed_index, " (name: ", to_member_name(type, failed_index), |
| 2146 |
") cannot be expressed with either HLSL packing layout or packoffset.")); |
| 2147 |
} |
| 2148 |
|
| 2149 |
flattened_structs[var.self] = false; |
| 2150 |
type.member_name_cache.clear(); |
| 2151 |
add_resource_name(var.self); |
| 2152 |
auto &memb = ir.meta[type.self].members; |
| 2153 |
|
| 2154 |
statement("cbuffer SPIRV_CROSS_RootConstant_", to_name(var.self), |
| 2155 |
to_resource_register(HLSL_BINDING_AUTO_PUSH_CONSTANT_BIT, 'b', layout.binding, layout.space)); |
| 2156 |
begin_scope(); |
| 2157 |
|
| 2158 |
// Index of the next field in the generated root constant constant buffer |
| 2159 |
auto constant_index = 0u; |
| 2160 |
|
| 2161 |
// Iterate over all member of the push constant and check which of the fields |
| 2162 |
// fit into the given root constant layout. |
| 2163 |
for (auto i = 0u; i < memb.size(); i++) |
| 2164 |
{ |
| 2165 |
const auto offset = memb[i].offset; |
| 2166 |
if (layout.start <= offset && offset < layout.end) |
| 2167 |
{ |
| 2168 |
const auto &member = type.member_types[i]; |
| 2169 |
|
| 2170 |
add_member_name(type, constant_index); |
| 2171 |
auto backup_name = get_member_name(type.self, i); |
| 2172 |
auto member_name = to_member_name(type, i); |
| 2173 |
member_name = join(to_name(var.self), "_", member_name); |
| 2174 |
ParsedIR::sanitize_underscores(member_name); |
| 2175 |
set_member_name(type.self, constant_index, member_name); |
| 2176 |
emit_struct_member(type, member, i, "", layout.start); |
| 2177 |
set_member_name(type.self, constant_index, backup_name); |
| 2178 |
|
| 2179 |
constant_index++; |
| 2180 |
} |
| 2181 |
} |
| 2182 |
|
| 2183 |
end_scope_decl(); |
| 2184 |
} |
| 2185 |
} |
| 2186 |
} |
| 2187 |
|
| 2188 |
string CompilerHLSL::to_sampler_expression(uint32_t id) |
| 2189 |
{ |
| 2190 |
auto expr = join("_", to_expression(id)); |
| 2191 |
auto index = expr.find_first_of('['); |
| 2192 |
if (index == string::npos) |
| 2193 |
{ |
| 2194 |
return expr + "_sampler"; |
| 2195 |
} |
| 2196 |
else |
| 2197 |
{ |
| 2198 |
// We have an expression like _ident[array], so we cannot tack on _sampler, insert it inside the string instead. |
| 2199 |
return expr.insert(index, "_sampler"); |
| 2200 |
} |
| 2201 |
} |
| 2202 |
|
| 2203 |
void CompilerHLSL::emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id) |
| 2204 |
{ |
| 2205 |
if (hlsl_options.shader_model >= 40 && combined_image_samplers.empty()) |
| 2206 |
{ |
| 2207 |
set<SPIRCombinedImageSampler>(result_id, result_type, image_id, samp_id); |
| 2208 |
} |
| 2209 |
else |
| 2210 |
{ |
| 2211 |
// Make sure to suppress usage tracking. It is illegal to create temporaries of opaque types. |
| 2212 |
emit_op(result_type, result_id, to_combined_image_sampler(image_id, samp_id), true, true); |
| 2213 |
} |
| 2214 |
} |
| 2215 |
|
| 2216 |
string CompilerHLSL::to_func_call_arg(const SPIRFunction::Parameter &arg, uint32_t id) |
| 2217 |
{ |
| 2218 |
string arg_str = CompilerGLSL::to_func_call_arg(arg, id); |
| 2219 |
|
| 2220 |
if (hlsl_options.shader_model <= 30) |
| 2221 |
return arg_str; |
| 2222 |
|
| 2223 |
// Manufacture automatic sampler arg if the arg is a SampledImage texture and we're in modern HLSL. |
| 2224 |
auto &type = expression_type(id); |
| 2225 |
|
| 2226 |
// We don't have to consider combined image samplers here via OpSampledImage because |
| 2227 |
// those variables cannot be passed as arguments to functions. |
| 2228 |
// Only global SampledImage variables may be used as arguments. |
| 2229 |
if (type.basetype == SPIRType::SampledImage && type.image.dim != DimBuffer) |
| 2230 |
arg_str += ", " + to_sampler_expression(id); |
| 2231 |
|
| 2232 |
return arg_str; |
| 2233 |
} |
| 2234 |
|
| 2235 |
void CompilerHLSL::emit_function_prototype(SPIRFunction &func, const Bitset &return_flags) |
| 2236 |
{ |
| 2237 |
if (func.self != ir.default_entry_point) |
| 2238 |
add_function_overload(func); |
| 2239 |
|
| 2240 |
auto &execution = get_entry_point(); |
| 2241 |
// Avoid shadow declarations. |
| 2242 |
local_variable_names = resource_names; |
| 2243 |
|
| 2244 |
string decl; |
| 2245 |
|
| 2246 |
auto &type = get<SPIRType>(func.return_type); |
| 2247 |
if (type.array.empty()) |
| 2248 |
{ |
| 2249 |
decl += flags_to_qualifiers_glsl(type, return_flags); |
| 2250 |
decl += type_to_glsl(type); |
| 2251 |
decl += " "; |
| 2252 |
} |
| 2253 |
else |
| 2254 |
{ |
| 2255 |
// We cannot return arrays in HLSL, so "return" through an out variable. |
| 2256 |
decl = "void "; |
| 2257 |
} |
| 2258 |
|
| 2259 |
if (func.self == ir.default_entry_point) |
| 2260 |
{ |
| 2261 |
if (execution.model == ExecutionModelVertex) |
| 2262 |
decl += "vert_main"; |
| 2263 |
else if (execution.model == ExecutionModelFragment) |
| 2264 |
decl += "frag_main"; |
| 2265 |
else if (execution.model == ExecutionModelGLCompute) |
| 2266 |
decl += "comp_main"; |
| 2267 |
else |
| 2268 |
SPIRV_CROSS_THROW("Unsupported execution model."); |
| 2269 |
processing_entry_point = true; |
| 2270 |
} |
| 2271 |
else |
| 2272 |
decl += to_name(func.self); |
| 2273 |
|
| 2274 |
decl += "("; |
| 2275 |
SmallVector<string> arglist; |
| 2276 |
|
| 2277 |
if (!type.array.empty()) |
| 2278 |
{ |
| 2279 |
// Fake array returns by writing to an out array instead. |
| 2280 |
string out_argument; |
| 2281 |
out_argument += "out "; |
| 2282 |
out_argument += type_to_glsl(type); |
| 2283 |
out_argument += " "; |
| 2284 |
out_argument += "SPIRV_Cross_return_value"; |
| 2285 |
out_argument += type_to_array_glsl(type); |
| 2286 |
arglist.push_back(move(out_argument)); |
| 2287 |
} |
| 2288 |
|
| 2289 |
for (auto &arg : func.arguments) |
| 2290 |
{ |
| 2291 |
// Do not pass in separate images or samplers if we're remapping |
| 2292 |
// to combined image samplers. |
| 2293 |
if (skip_argument(arg.id)) |
| 2294 |
continue; |
| 2295 |
|
| 2296 |
// Might change the variable name if it already exists in this function. |
| 2297 |
// SPIRV OpName doesn't have any semantic effect, so it's valid for an implementation |
| 2298 |
// to use same name for variables. |
| 2299 |
// Since we want to make the GLSL debuggable and somewhat sane, use fallback names for variables which are duplicates. |
| 2300 |
add_local_variable_name(arg.id); |
| 2301 |
|
| 2302 |
arglist.push_back(argument_decl(arg)); |
| 2303 |
|
| 2304 |
// Flatten a combined sampler to two separate arguments in modern HLSL. |
| 2305 |
auto &arg_type = get<SPIRType>(arg.type); |
| 2306 |
if (hlsl_options.shader_model > 30 && arg_type.basetype == SPIRType::SampledImage && |
| 2307 |
arg_type.image.dim != DimBuffer) |
| 2308 |
{ |
| 2309 |
// Manufacture automatic sampler arg for SampledImage texture |
| 2310 |
arglist.push_back(join(image_is_comparison(arg_type, arg.id) ? "SamplerComparisonState " : "SamplerState ", |
| 2311 |
to_sampler_expression(arg.id), type_to_array_glsl(arg_type))); |
| 2312 |
} |
| 2313 |
|
| 2314 |
// Hold a pointer to the parameter so we can invalidate the readonly field if needed. |
| 2315 |
auto *var = maybe_get<SPIRVariable>(arg.id); |
| 2316 |
if (var) |
| 2317 |
var->parameter = &arg; |
| 2318 |
} |
| 2319 |
|
| 2320 |
for (auto &arg : func.shadow_arguments) |
| 2321 |
{ |
| 2322 |
// Might change the variable name if it already exists in this function. |
| 2323 |
// SPIRV OpName doesn't have any semantic effect, so it's valid for an implementation |
| 2324 |
// to use same name for variables. |
| 2325 |
// Since we want to make the GLSL debuggable and somewhat sane, use fallback names for variables which are duplicates. |
| 2326 |
add_local_variable_name(arg.id); |
| 2327 |
|
| 2328 |
arglist.push_back(argument_decl(arg)); |
| 2329 |
|
| 2330 |
// Hold a pointer to the parameter so we can invalidate the readonly field if needed. |
| 2331 |
auto *var = maybe_get<SPIRVariable>(arg.id); |
| 2332 |
if (var) |
| 2333 |
var->parameter = &arg; |
| 2334 |
} |
| 2335 |
|
| 2336 |
decl += merge(arglist); |
| 2337 |
decl += ")"; |
| 2338 |
statement(decl); |
| 2339 |
} |
| 2340 |
|
| 2341 |
void CompilerHLSL::emit_hlsl_entry_point() |
| 2342 |
{ |
| 2343 |
SmallVector<string> arguments; |
| 2344 |
|
| 2345 |
if (require_input) |
| 2346 |
arguments.push_back("SPIRV_Cross_Input stage_input"); |
| 2347 |
|
| 2348 |
// Add I/O blocks as separate arguments with appropriate storage qualifier. |
| 2349 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 2350 |
auto &type = this->get<SPIRType>(var.basetype); |
| 2351 |
bool block = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock); |
| 2352 |
|
| 2353 |
if (var.storage != StorageClassInput && var.storage != StorageClassOutput) |
| 2354 |
return; |
| 2355 |
|
| 2356 |
if (block && !is_builtin_variable(var) && interface_variable_exists_in_entry_point(var.self)) |
| 2357 |
{ |
| 2358 |
if (var.storage == StorageClassInput) |
| 2359 |
{ |
| 2360 |
arguments.push_back(join("in ", variable_decl(type, join("stage_input", to_name(var.self))))); |
| 2361 |
} |
| 2362 |
else if (var.storage == StorageClassOutput) |
| 2363 |
{ |
| 2364 |
arguments.push_back(join("out ", variable_decl(type, join("stage_output", to_name(var.self))))); |
| 2365 |
} |
| 2366 |
} |
| 2367 |
}); |
| 2368 |
|
| 2369 |
auto &execution = get_entry_point(); |
| 2370 |
|
| 2371 |
switch (execution.model) |
| 2372 |
{ |
| 2373 |
case ExecutionModelGLCompute: |
| 2374 |
{ |
| 2375 |
SpecializationConstant wg_x, wg_y, wg_z; |
| 2376 |
get_work_group_size_specialization_constants(wg_x, wg_y, wg_z); |
| 2377 |
|
| 2378 |
uint32_t x = execution.workgroup_size.x; |
| 2379 |
uint32_t y = execution.workgroup_size.y; |
| 2380 |
uint32_t z = execution.workgroup_size.z; |
| 2381 |
|
| 2382 |
auto x_expr = wg_x.id ? get<SPIRConstant>(wg_x.id).specialization_constant_macro_name : to_string(x); |
| 2383 |
auto y_expr = wg_y.id ? get<SPIRConstant>(wg_y.id).specialization_constant_macro_name : to_string(y); |
| 2384 |
auto z_expr = wg_z.id ? get<SPIRConstant>(wg_z.id).specialization_constant_macro_name : to_string(z); |
| 2385 |
|
| 2386 |
statement("[numthreads(", x_expr, ", ", y_expr, ", ", z_expr, ")]"); |
| 2387 |
break; |
| 2388 |
} |
| 2389 |
case ExecutionModelFragment: |
| 2390 |
if (execution.flags.get(ExecutionModeEarlyFragmentTests)) |
| 2391 |
statement("[earlydepthstencil]"); |
| 2392 |
break; |
| 2393 |
default: |
| 2394 |
break; |
| 2395 |
} |
| 2396 |
|
| 2397 |
statement(require_output ? "SPIRV_Cross_Output " : "void ", "main(", merge(arguments), ")"); |
| 2398 |
begin_scope(); |
| 2399 |
bool legacy = hlsl_options.shader_model <= 30; |
| 2400 |
|
| 2401 |
// Copy builtins from entry point arguments to globals. |
| 2402 |
active_input_builtins.for_each_bit([&](uint32_t i) { |
| 2403 |
auto builtin = builtin_to_glsl(static_cast<BuiltIn>(i), StorageClassInput); |
| 2404 |
switch (static_cast<BuiltIn>(i)) |
| 2405 |
{ |
| 2406 |
case BuiltInFragCoord: |
| 2407 |
// VPOS in D3D9 is sampled at integer locations, apply half-pixel offset to be consistent. |
| 2408 |
// TODO: Do we need an option here? Any reason why a D3D9 shader would be used |
| 2409 |
// on a D3D10+ system with a different rasterization config? |
| 2410 |
if (legacy) |
| 2411 |
statement(builtin, " = stage_input.", builtin, " + float4(0.5f, 0.5f, 0.0f, 0.0f);"); |
| 2412 |
else |
| 2413 |
{ |
| 2414 |
statement(builtin, " = stage_input.", builtin, ";"); |
| 2415 |
// ZW are undefined in D3D9, only do this fixup here. |
| 2416 |
statement(builtin, ".w = 1.0 / ", builtin, ".w;"); |
| 2417 |
} |
| 2418 |
break; |
| 2419 |
|
| 2420 |
case BuiltInVertexId: |
| 2421 |
case BuiltInVertexIndex: |
| 2422 |
case BuiltInInstanceIndex: |
| 2423 |
// D3D semantics are uint, but shader wants int. |
| 2424 |
if (hlsl_options.support_nonzero_base_vertex_base_instance) |
| 2425 |
{ |
| 2426 |
if (static_cast<BuiltIn>(i) == BuiltInInstanceIndex) |
| 2427 |
statement(builtin, " = int(stage_input.", builtin, ") + SPIRV_Cross_BaseInstance;"); |
| 2428 |
else |
| 2429 |
statement(builtin, " = int(stage_input.", builtin, ") + SPIRV_Cross_BaseVertex;"); |
| 2430 |
} |
| 2431 |
else |
| 2432 |
statement(builtin, " = int(stage_input.", builtin, ");"); |
| 2433 |
break; |
| 2434 |
|
| 2435 |
case BuiltInInstanceId: |
| 2436 |
// D3D semantics are uint, but shader wants int. |
| 2437 |
statement(builtin, " = int(stage_input.", builtin, ");"); |
| 2438 |
break; |
| 2439 |
|
| 2440 |
case BuiltInNumWorkgroups: |
| 2441 |
case BuiltInPointCoord: |
| 2442 |
case BuiltInSubgroupSize: |
| 2443 |
case BuiltInSubgroupLocalInvocationId: |
| 2444 |
break; |
| 2445 |
|
| 2446 |
case BuiltInSubgroupEqMask: |
| 2447 |
// Emulate these ... |
| 2448 |
// No 64-bit in HLSL, so have to do it in 32-bit and unroll. |
| 2449 |
statement("gl_SubgroupEqMask = 1u << (WaveGetLaneIndex() - uint4(0, 32, 64, 96));"); |
| 2450 |
statement("if (WaveGetLaneIndex() >= 32) gl_SubgroupEqMask.x = 0;"); |
| 2451 |
statement("if (WaveGetLaneIndex() >= 64 || WaveGetLaneIndex() < 32) gl_SubgroupEqMask.y = 0;"); |
| 2452 |
statement("if (WaveGetLaneIndex() >= 96 || WaveGetLaneIndex() < 64) gl_SubgroupEqMask.z = 0;"); |
| 2453 |
statement("if (WaveGetLaneIndex() < 96) gl_SubgroupEqMask.w = 0;"); |
| 2454 |
break; |
| 2455 |
|
| 2456 |
case BuiltInSubgroupGeMask: |
| 2457 |
// Emulate these ... |
| 2458 |
// No 64-bit in HLSL, so have to do it in 32-bit and unroll. |
| 2459 |
statement("gl_SubgroupGeMask = ~((1u << (WaveGetLaneIndex() - uint4(0, 32, 64, 96))) - 1u);"); |
| 2460 |
statement("if (WaveGetLaneIndex() >= 32) gl_SubgroupGeMask.x = 0u;"); |
| 2461 |
statement("if (WaveGetLaneIndex() >= 64) gl_SubgroupGeMask.y = 0u;"); |
| 2462 |
statement("if (WaveGetLaneIndex() >= 96) gl_SubgroupGeMask.z = 0u;"); |
| 2463 |
statement("if (WaveGetLaneIndex() < 32) gl_SubgroupGeMask.y = ~0u;"); |
| 2464 |
statement("if (WaveGetLaneIndex() < 64) gl_SubgroupGeMask.z = ~0u;"); |
| 2465 |
statement("if (WaveGetLaneIndex() < 96) gl_SubgroupGeMask.w = ~0u;"); |
| 2466 |
break; |
| 2467 |
|
| 2468 |
case BuiltInSubgroupGtMask: |
| 2469 |
// Emulate these ... |
| 2470 |
// No 64-bit in HLSL, so have to do it in 32-bit and unroll. |
| 2471 |
statement("uint gt_lane_index = WaveGetLaneIndex() + 1;"); |
| 2472 |
statement("gl_SubgroupGtMask = ~((1u << (gt_lane_index - uint4(0, 32, 64, 96))) - 1u);"); |
| 2473 |
statement("if (gt_lane_index >= 32) gl_SubgroupGtMask.x = 0u;"); |
| 2474 |
statement("if (gt_lane_index >= 64) gl_SubgroupGtMask.y = 0u;"); |
| 2475 |
statement("if (gt_lane_index >= 96) gl_SubgroupGtMask.z = 0u;"); |
| 2476 |
statement("if (gt_lane_index >= 128) gl_SubgroupGtMask.w = 0u;"); |
| 2477 |
statement("if (gt_lane_index < 32) gl_SubgroupGtMask.y = ~0u;"); |
| 2478 |
statement("if (gt_lane_index < 64) gl_SubgroupGtMask.z = ~0u;"); |
| 2479 |
statement("if (gt_lane_index < 96) gl_SubgroupGtMask.w = ~0u;"); |
| 2480 |
break; |
| 2481 |
|
| 2482 |
case BuiltInSubgroupLeMask: |
| 2483 |
// Emulate these ... |
| 2484 |
// No 64-bit in HLSL, so have to do it in 32-bit and unroll. |
| 2485 |
statement("uint le_lane_index = WaveGetLaneIndex() + 1;"); |
| 2486 |
statement("gl_SubgroupLeMask = (1u << (le_lane_index - uint4(0, 32, 64, 96))) - 1u;"); |
| 2487 |
statement("if (le_lane_index >= 32) gl_SubgroupLeMask.x = ~0u;"); |
| 2488 |
statement("if (le_lane_index >= 64) gl_SubgroupLeMask.y = ~0u;"); |
| 2489 |
statement("if (le_lane_index >= 96) gl_SubgroupLeMask.z = ~0u;"); |
| 2490 |
statement("if (le_lane_index >= 128) gl_SubgroupLeMask.w = ~0u;"); |
| 2491 |
statement("if (le_lane_index < 32) gl_SubgroupLeMask.y = 0u;"); |
| 2492 |
statement("if (le_lane_index < 64) gl_SubgroupLeMask.z = 0u;"); |
| 2493 |
statement("if (le_lane_index < 96) gl_SubgroupLeMask.w = 0u;"); |
| 2494 |
break; |
| 2495 |
|
| 2496 |
case BuiltInSubgroupLtMask: |
| 2497 |
// Emulate these ... |
| 2498 |
// No 64-bit in HLSL, so have to do it in 32-bit and unroll. |
| 2499 |
statement("gl_SubgroupLtMask = (1u << (WaveGetLaneIndex() - uint4(0, 32, 64, 96))) - 1u;"); |
| 2500 |
statement("if (WaveGetLaneIndex() >= 32) gl_SubgroupLtMask.x = ~0u;"); |
| 2501 |
statement("if (WaveGetLaneIndex() >= 64) gl_SubgroupLtMask.y = ~0u;"); |
| 2502 |
statement("if (WaveGetLaneIndex() >= 96) gl_SubgroupLtMask.z = ~0u;"); |
| 2503 |
statement("if (WaveGetLaneIndex() < 32) gl_SubgroupLtMask.y = 0u;"); |
| 2504 |
statement("if (WaveGetLaneIndex() < 64) gl_SubgroupLtMask.z = 0u;"); |
| 2505 |
statement("if (WaveGetLaneIndex() < 96) gl_SubgroupLtMask.w = 0u;"); |
| 2506 |
break; |
| 2507 |
|
| 2508 |
case BuiltInClipDistance: |
| 2509 |
for (uint32_t clip = 0; clip < clip_distance_count; clip++) |
| 2510 |
statement("gl_ClipDistance[", clip, "] = stage_input.gl_ClipDistance", clip / 4, ".", "xyzw"[clip & 3], |
| 2511 |
";"); |
| 2512 |
break; |
| 2513 |
|
| 2514 |
case BuiltInCullDistance: |
| 2515 |
for (uint32_t cull = 0; cull < cull_distance_count; cull++) |
| 2516 |
statement("gl_CullDistance[", cull, "] = stage_input.gl_CullDistance", cull / 4, ".", "xyzw"[cull & 3], |
| 2517 |
";"); |
| 2518 |
break; |
| 2519 |
|
| 2520 |
default: |
| 2521 |
statement(builtin, " = stage_input.", builtin, ";"); |
| 2522 |
break; |
| 2523 |
} |
| 2524 |
}); |
| 2525 |
|
| 2526 |
// Copy from stage input struct to globals. |
| 2527 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 2528 |
auto &type = this->get<SPIRType>(var.basetype); |
| 2529 |
bool block = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock); |
| 2530 |
|
| 2531 |
if (var.storage != StorageClassInput) |
| 2532 |
return; |
| 2533 |
|
| 2534 |
bool need_matrix_unroll = var.storage == StorageClassInput && execution.model == ExecutionModelVertex; |
| 2535 |
|
| 2536 |
if (!block && !var.remapped_variable && type.pointer && !is_builtin_variable(var) && |
| 2537 |
interface_variable_exists_in_entry_point(var.self)) |
| 2538 |
{ |
| 2539 |
auto name = to_name(var.self); |
| 2540 |
auto &mtype = this->get<SPIRType>(var.basetype); |
| 2541 |
if (need_matrix_unroll && mtype.columns > 1) |
| 2542 |
{ |
| 2543 |
// Unroll matrices. |
| 2544 |
for (uint32_t col = 0; col < mtype.columns; col++) |
| 2545 |
statement(name, "[", col, "] = stage_input.", name, "_", col, ";"); |
| 2546 |
} |
| 2547 |
else |
| 2548 |
{ |
| 2549 |
statement(name, " = stage_input.", name, ";"); |
| 2550 |
} |
| 2551 |
} |
| 2552 |
|
| 2553 |
// I/O blocks don't use the common stage input/output struct, but separate outputs. |
| 2554 |
if (block && !is_builtin_variable(var) && interface_variable_exists_in_entry_point(var.self)) |
| 2555 |
{ |
| 2556 |
auto name = to_name(var.self); |
| 2557 |
statement(name, " = stage_input", name, ";"); |
| 2558 |
} |
| 2559 |
}); |
| 2560 |
|
| 2561 |
// Run the shader. |
| 2562 |
if (execution.model == ExecutionModelVertex) |
| 2563 |
statement("vert_main();"); |
| 2564 |
else if (execution.model == ExecutionModelFragment) |
| 2565 |
statement("frag_main();"); |
| 2566 |
else if (execution.model == ExecutionModelGLCompute) |
| 2567 |
statement("comp_main();"); |
| 2568 |
else |
| 2569 |
SPIRV_CROSS_THROW("Unsupported shader stage."); |
| 2570 |
|
| 2571 |
// Copy block outputs. |
| 2572 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 2573 |
auto &type = this->get<SPIRType>(var.basetype); |
| 2574 |
bool block = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock); |
| 2575 |
|
| 2576 |
if (var.storage != StorageClassOutput) |
| 2577 |
return; |
| 2578 |
|
| 2579 |
// I/O blocks don't use the common stage input/output struct, but separate outputs. |
| 2580 |
if (block && !is_builtin_variable(var) && interface_variable_exists_in_entry_point(var.self)) |
| 2581 |
{ |
| 2582 |
auto name = to_name(var.self); |
| 2583 |
statement("stage_output", name, " = ", name, ";"); |
| 2584 |
} |
| 2585 |
}); |
| 2586 |
|
| 2587 |
// Copy stage outputs. |
| 2588 |
if (require_output) |
| 2589 |
{ |
| 2590 |
statement("SPIRV_Cross_Output stage_output;"); |
| 2591 |
|
| 2592 |
// Copy builtins from globals to return struct. |
| 2593 |
active_output_builtins.for_each_bit([&](uint32_t i) { |
| 2594 |
// PointSize doesn't exist in HLSL. |
| 2595 |
if (i == BuiltInPointSize) |
| 2596 |
return; |
| 2597 |
|
| 2598 |
switch (static_cast<BuiltIn>(i)) |
| 2599 |
{ |
| 2600 |
case BuiltInClipDistance: |
| 2601 |
for (uint32_t clip = 0; clip < clip_distance_count; clip++) |
| 2602 |
statement("stage_output.gl_ClipDistance", clip / 4, ".", "xyzw"[clip & 3], " = gl_ClipDistance[", |
| 2603 |
clip, "];"); |
| 2604 |
break; |
| 2605 |
|
| 2606 |
case BuiltInCullDistance: |
| 2607 |
for (uint32_t cull = 0; cull < cull_distance_count; cull++) |
| 2608 |
statement("stage_output.gl_CullDistance", cull / 4, ".", "xyzw"[cull & 3], " = gl_CullDistance[", |
| 2609 |
cull, "];"); |
| 2610 |
break; |
| 2611 |
|
| 2612 |
default: |
| 2613 |
{ |
| 2614 |
auto builtin_expr = builtin_to_glsl(static_cast<BuiltIn>(i), StorageClassOutput); |
| 2615 |
statement("stage_output.", builtin_expr, " = ", builtin_expr, ";"); |
| 2616 |
break; |
| 2617 |
} |
| 2618 |
} |
| 2619 |
}); |
| 2620 |
|
| 2621 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) { |
| 2622 |
auto &type = this->get<SPIRType>(var.basetype); |
| 2623 |
bool block = ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock); |
| 2624 |
|
| 2625 |
if (var.storage != StorageClassOutput) |
| 2626 |
return; |
| 2627 |
|
| 2628 |
if (!block && var.storage != StorageClassFunction && !var.remapped_variable && type.pointer && |
| 2629 |
!is_builtin_variable(var) && interface_variable_exists_in_entry_point(var.self)) |
| 2630 |
{ |
| 2631 |
auto name = to_name(var.self); |
| 2632 |
|
| 2633 |
if (legacy && execution.model == ExecutionModelFragment) |
| 2634 |
{ |
| 2635 |
string output_filler; |
| 2636 |
for (uint32_t size = type.vecsize; size < 4; ++size) |
| 2637 |
output_filler += ", 0.0"; |
| 2638 |
|
| 2639 |
statement("stage_output.", name, " = float4(", name, output_filler, ");"); |
| 2640 |
} |
| 2641 |
else |
| 2642 |
{ |
| 2643 |
statement("stage_output.", name, " = ", name, ";"); |
| 2644 |
} |
| 2645 |
} |
| 2646 |
}); |
| 2647 |
|
| 2648 |
statement("return stage_output;"); |
| 2649 |
} |
| 2650 |
|
| 2651 |
end_scope(); |
| 2652 |
} |
| 2653 |
|
| 2654 |
void CompilerHLSL::emit_fixup() |
| 2655 |
{ |
| 2656 |
if (is_vertex_like_shader()) |
| 2657 |
{ |
| 2658 |
// Do various mangling on the gl_Position. |
| 2659 |
if (hlsl_options.shader_model <= 30) |
| 2660 |
{ |
| 2661 |
statement("gl_Position.x = gl_Position.x - gl_HalfPixel.x * " |
| 2662 |
"gl_Position.w;"); |
| 2663 |
statement("gl_Position.y = gl_Position.y + gl_HalfPixel.y * " |
| 2664 |
"gl_Position.w;"); |
| 2665 |
} |
| 2666 |
|
| 2667 |
if (options.vertex.flip_vert_y) |
| 2668 |
statement("gl_Position.y = -gl_Position.y;"); |
| 2669 |
if (options.vertex.fixup_clipspace) |
| 2670 |
statement("gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;"); |
| 2671 |
} |
| 2672 |
} |
| 2673 |
|
| 2674 |
void CompilerHLSL::emit_texture_op(const Instruction &i, bool sparse) |
| 2675 |
{ |
| 2676 |
if (sparse) |
| 2677 |
SPIRV_CROSS_THROW("Sparse feedback not yet supported in HLSL."); |
| 2678 |
|
| 2679 |
auto *ops = stream(i); |
| 2680 |
auto op = static_cast<Op>(i.op); |
| 2681 |
uint32_t length = i.length; |
| 2682 |
|
| 2683 |
SmallVector<uint32_t> inherited_expressions; |
| 2684 |
|
| 2685 |
uint32_t result_type = ops[0]; |
| 2686 |
uint32_t id = ops[1]; |
| 2687 |
VariableID img = ops[2]; |
| 2688 |
uint32_t coord = ops[3]; |
| 2689 |
uint32_t dref = 0; |
| 2690 |
uint32_t comp = 0; |
| 2691 |
bool gather = false; |
| 2692 |
bool proj = false; |
| 2693 |
const uint32_t *opt = nullptr; |
| 2694 |
auto *combined_image = maybe_get<SPIRCombinedImageSampler>(img); |
| 2695 |
auto img_expr = to_expression(combined_image ? combined_image->image : img); |
| 2696 |
|
| 2697 |
inherited_expressions.push_back(coord); |
| 2698 |
|
| 2699 |
// Make sure non-uniform decoration is back-propagated to where it needs to be. |
| 2700 |
if (has_decoration(img, DecorationNonUniformEXT)) |
| 2701 |
propagate_nonuniform_qualifier(img); |
| 2702 |
|
| 2703 |
switch (op) |
| 2704 |
{ |
| 2705 |
case OpImageSampleDrefImplicitLod: |
| 2706 |
case OpImageSampleDrefExplicitLod: |
| 2707 |
dref = ops[4]; |
| 2708 |
opt = &ops[5]; |
| 2709 |
length -= 5; |
| 2710 |
break; |
| 2711 |
|
| 2712 |
case OpImageSampleProjDrefImplicitLod: |
| 2713 |
case OpImageSampleProjDrefExplicitLod: |
| 2714 |
dref = ops[4]; |
| 2715 |
proj = true; |
| 2716 |
opt = &ops[5]; |
| 2717 |
length -= 5; |
| 2718 |
break; |
| 2719 |
|
| 2720 |
case OpImageDrefGather: |
| 2721 |
dref = ops[4]; |
| 2722 |
opt = &ops[5]; |
| 2723 |
gather = true; |
| 2724 |
length -= 5; |
| 2725 |
break; |
| 2726 |
|
| 2727 |
case OpImageGather: |
| 2728 |
comp = ops[4]; |
| 2729 |
opt = &ops[5]; |
| 2730 |
gather = true; |
| 2731 |
length -= 5; |
| 2732 |
break; |
| 2733 |
|
| 2734 |
case OpImageSampleProjImplicitLod: |
| 2735 |
case OpImageSampleProjExplicitLod: |
| 2736 |
opt = &ops[4]; |
| 2737 |
length -= 4; |
| 2738 |
proj = true; |
| 2739 |
break; |
| 2740 |
|
| 2741 |
case OpImageQueryLod: |
| 2742 |
opt = &ops[4]; |
| 2743 |
length -= 4; |
| 2744 |
break; |
| 2745 |
|
| 2746 |
default: |
| 2747 |
opt = &ops[4]; |
| 2748 |
length -= 4; |
| 2749 |
break; |
| 2750 |
} |
| 2751 |
|
| 2752 |
auto &imgtype = expression_type(img); |
| 2753 |
uint32_t coord_components = 0; |
| 2754 |
switch (imgtype.image.dim) |
| 2755 |
{ |
| 2756 |
case spv::Dim1D: |
| 2757 |
coord_components = 1; |
| 2758 |
break; |
| 2759 |
case spv::Dim2D: |
| 2760 |
coord_components = 2; |
| 2761 |
break; |
| 2762 |
case spv::Dim3D: |
| 2763 |
coord_components = 3; |
| 2764 |
break; |
| 2765 |
case spv::DimCube: |
| 2766 |
coord_components = 3; |
| 2767 |
break; |
| 2768 |
case spv::DimBuffer: |
| 2769 |
coord_components = 1; |
| 2770 |
break; |
| 2771 |
default: |
| 2772 |
coord_components = 2; |
| 2773 |
break; |
| 2774 |
} |
| 2775 |
|
| 2776 |
if (dref) |
| 2777 |
inherited_expressions.push_back(dref); |
| 2778 |
|
| 2779 |
if (imgtype.image.arrayed) |
| 2780 |
coord_components++; |
| 2781 |
|
| 2782 |
uint32_t bias = 0; |
| 2783 |
uint32_t lod = 0; |
| 2784 |
uint32_t grad_x = 0; |
| 2785 |
uint32_t grad_y = 0; |
| 2786 |
uint32_t coffset = 0; |
| 2787 |
uint32_t offset = 0; |
| 2788 |
uint32_t coffsets = 0; |
| 2789 |
uint32_t sample = 0; |
| 2790 |
uint32_t minlod = 0; |
| 2791 |
uint32_t flags = 0; |
| 2792 |
|
| 2793 |
if (length) |
| 2794 |
{ |
| 2795 |
flags = opt[0]; |
| 2796 |
opt++; |
| 2797 |
length--; |
| 2798 |
} |
| 2799 |
|
| 2800 |
auto test = [&](uint32_t &v, uint32_t flag) { |
| 2801 |
if (length && (flags & flag)) |
| 2802 |
{ |
| 2803 |
v = *opt++; |
| 2804 |
inherited_expressions.push_back(v); |
| 2805 |
length--; |
| 2806 |
} |
| 2807 |
}; |
| 2808 |
|
| 2809 |
test(bias, ImageOperandsBiasMask); |
| 2810 |
test(lod, ImageOperandsLodMask); |
| 2811 |
test(grad_x, ImageOperandsGradMask); |
| 2812 |
test(grad_y, ImageOperandsGradMask); |
| 2813 |
test(coffset, ImageOperandsConstOffsetMask); |
| 2814 |
test(offset, ImageOperandsOffsetMask); |
| 2815 |
test(coffsets, ImageOperandsConstOffsetsMask); |
| 2816 |
test(sample, ImageOperandsSampleMask); |
| 2817 |
test(minlod, ImageOperandsMinLodMask); |
| 2818 |
|
| 2819 |
string expr; |
| 2820 |
string texop; |
| 2821 |
|
| 2822 |
if (minlod != 0) |
| 2823 |
SPIRV_CROSS_THROW("MinLod texture operand not supported in HLSL."); |
| 2824 |
|
| 2825 |
if (op == OpImageFetch) |
| 2826 |
{ |
| 2827 |
if (hlsl_options.shader_model < 40) |
| 2828 |
{ |
| 2829 |
SPIRV_CROSS_THROW("texelFetch is not supported in HLSL shader model 2/3."); |
| 2830 |
} |
| 2831 |
texop += img_expr; |
| 2832 |
texop += ".Load"; |
| 2833 |
} |
| 2834 |
else if (op == OpImageQueryLod) |
| 2835 |
{ |
| 2836 |
texop += img_expr; |
| 2837 |
texop += ".CalculateLevelOfDetail"; |
| 2838 |
} |
| 2839 |
else |
| 2840 |
{ |
| 2841 |
auto &imgformat = get<SPIRType>(imgtype.image.type); |
| 2842 |
if (imgformat.basetype != SPIRType::Float) |
| 2843 |
{ |
| 2844 |
SPIRV_CROSS_THROW("Sampling non-float textures is not supported in HLSL."); |
| 2845 |
} |
| 2846 |
|
| 2847 |
if (hlsl_options.shader_model >= 40) |
| 2848 |
{ |
| 2849 |
texop += img_expr; |
| 2850 |
|
| 2851 |
if (image_is_comparison(imgtype, img)) |
| 2852 |
{ |
| 2853 |
if (gather) |
| 2854 |
{ |
| 2855 |
SPIRV_CROSS_THROW("GatherCmp does not exist in HLSL."); |
| 2856 |
} |
| 2857 |
else if (lod || grad_x || grad_y) |
| 2858 |
{ |
| 2859 |
// Assume we want a fixed level, and the only thing we can get in HLSL is SampleCmpLevelZero. |
| 2860 |
texop += ".SampleCmpLevelZero"; |
| 2861 |
} |
| 2862 |
else |
| 2863 |
texop += ".SampleCmp"; |
| 2864 |
} |
| 2865 |
else if (gather) |
| 2866 |
{ |
| 2867 |
uint32_t comp_num = evaluate_constant_u32(comp); |
| 2868 |
if (hlsl_options.shader_model >= 50) |
| 2869 |
{ |
| 2870 |
switch (comp_num) |
| 2871 |
{ |
| 2872 |
case 0: |
| 2873 |
texop += ".GatherRed"; |
| 2874 |
break; |
| 2875 |
case 1: |
| 2876 |
texop += ".GatherGreen"; |
| 2877 |
break; |
| 2878 |
case 2: |
| 2879 |
texop += ".GatherBlue"; |
| 2880 |
break; |
| 2881 |
case 3: |
| 2882 |
texop += ".GatherAlpha"; |
| 2883 |
break; |
| 2884 |
default: |
| 2885 |
SPIRV_CROSS_THROW("Invalid component."); |
| 2886 |
} |
| 2887 |
} |
| 2888 |
else |
| 2889 |
{ |
| 2890 |
if (comp_num == 0) |
| 2891 |
texop += ".Gather"; |
| 2892 |
else |
| 2893 |
SPIRV_CROSS_THROW("HLSL shader model 4 can only gather from the red component."); |
| 2894 |
} |
| 2895 |
} |
| 2896 |
else if (bias) |
| 2897 |
texop += ".SampleBias"; |
| 2898 |
else if (grad_x || grad_y) |
| 2899 |
texop += ".SampleGrad"; |
| 2900 |
else if (lod) |
| 2901 |
texop += ".SampleLevel"; |
| 2902 |
else |
| 2903 |
texop += ".Sample"; |
| 2904 |
} |
| 2905 |
else |
| 2906 |
{ |
| 2907 |
switch (imgtype.image.dim) |
| 2908 |
{ |
| 2909 |
case Dim1D: |
| 2910 |
texop += "tex1D"; |
| 2911 |
break; |
| 2912 |
case Dim2D: |
| 2913 |
texop += "tex2D"; |
| 2914 |
break; |
| 2915 |
case Dim3D: |
| 2916 |
texop += "tex3D"; |
| 2917 |
break; |
| 2918 |
case DimCube: |
| 2919 |
texop += "texCUBE"; |
| 2920 |
break; |
| 2921 |
case DimRect: |
| 2922 |
case DimBuffer: |
| 2923 |
case DimSubpassData: |
| 2924 |
SPIRV_CROSS_THROW("Buffer texture support is not yet implemented for HLSL"); // TODO |
| 2925 |
default: |
| 2926 |
SPIRV_CROSS_THROW("Invalid dimension."); |
| 2927 |
} |
| 2928 |
|
| 2929 |
if (gather) |
| 2930 |
SPIRV_CROSS_THROW("textureGather is not supported in HLSL shader model 2/3."); |
| 2931 |
if (offset || coffset) |
| 2932 |
SPIRV_CROSS_THROW("textureOffset is not supported in HLSL shader model 2/3."); |
| 2933 |
|
| 2934 |
if (grad_x || grad_y) |
| 2935 |
texop += "grad"; |
| 2936 |
else if (lod) |
| 2937 |
texop += "lod"; |
| 2938 |
else if (bias) |
| 2939 |
texop += "bias"; |
| 2940 |
else if (proj || dref) |
| 2941 |
texop += "proj"; |
| 2942 |
} |
| 2943 |
} |
| 2944 |
|
| 2945 |
expr += texop; |
| 2946 |
expr += "("; |
| 2947 |
if (hlsl_options.shader_model < 40) |
| 2948 |
{ |
| 2949 |
if (combined_image) |
| 2950 |
SPIRV_CROSS_THROW("Separate images/samplers are not supported in HLSL shader model 2/3."); |
| 2951 |
expr += to_expression(img); |
| 2952 |
} |
| 2953 |
else if (op != OpImageFetch) |
| 2954 |
{ |
| 2955 |
string sampler_expr; |
| 2956 |
if (combined_image) |
| 2957 |
sampler_expr = to_expression(combined_image->sampler); |
| 2958 |
else |
| 2959 |
sampler_expr = to_sampler_expression(img); |
| 2960 |
expr += sampler_expr; |
| 2961 |
} |
| 2962 |
|
| 2963 |
auto swizzle = [](uint32_t comps, uint32_t in_comps) -> const char * { |
| 2964 |
if (comps == in_comps) |
| 2965 |
return ""; |
| 2966 |
|
| 2967 |
switch (comps) |
| 2968 |
{ |
| 2969 |
case 1: |
| 2970 |
return ".x"; |
| 2971 |
case 2: |
| 2972 |
return ".xy"; |
| 2973 |
case 3: |
| 2974 |
return ".xyz"; |
| 2975 |
default: |
| 2976 |
return ""; |
| 2977 |
} |
| 2978 |
}; |
| 2979 |
|
| 2980 |
bool forward = should_forward(coord); |
| 2981 |
|
| 2982 |
// The IR can give us more components than we need, so chop them off as needed. |
| 2983 |
string coord_expr; |
| 2984 |
auto &coord_type = expression_type(coord); |
| 2985 |
if (coord_components != coord_type.vecsize) |
| 2986 |
coord_expr = to_enclosed_expression(coord) + swizzle(coord_components, expression_type(coord).vecsize); |
| 2987 |
else |
| 2988 |
coord_expr = to_expression(coord); |
| 2989 |
|
| 2990 |
if (proj && hlsl_options.shader_model >= 40) // Legacy HLSL has "proj" operations which do this for us. |
| 2991 |
coord_expr = coord_expr + " / " + to_extract_component_expression(coord, coord_components); |
| 2992 |
|
| 2993 |
if (hlsl_options.shader_model < 40) |
| 2994 |
{ |
| 2995 |
if (dref) |
| 2996 |
{ |
| 2997 |
if (imgtype.image.dim != spv::Dim1D && imgtype.image.dim != spv::Dim2D) |
| 2998 |
{ |
| 2999 |
SPIRV_CROSS_THROW( |
| 3000 |
"Depth comparison is only supported for 1D and 2D textures in HLSL shader model 2/3."); |
| 3001 |
} |
| 3002 |
|
| 3003 |
if (grad_x || grad_y) |
| 3004 |
SPIRV_CROSS_THROW("Depth comparison is not supported for grad sampling in HLSL shader model 2/3."); |
| 3005 |
|
| 3006 |
for (uint32_t size = coord_components; size < 2; ++size) |
| 3007 |
coord_expr += ", 0.0"; |
| 3008 |
|
| 3009 |
forward = forward && should_forward(dref); |
| 3010 |
coord_expr += ", " + to_expression(dref); |
| 3011 |
} |
| 3012 |
else if (lod || bias || proj) |
| 3013 |
{ |
| 3014 |
for (uint32_t size = coord_components; size < 3; ++size) |
| 3015 |
coord_expr += ", 0.0"; |
| 3016 |
} |
| 3017 |
|
| 3018 |
if (lod) |
| 3019 |
{ |
| 3020 |
coord_expr = "float4(" + coord_expr + ", " + to_expression(lod) + ")"; |
| 3021 |
} |
| 3022 |
else if (bias) |
| 3023 |
{ |
| 3024 |
coord_expr = "float4(" + coord_expr + ", " + to_expression(bias) + ")"; |
| 3025 |
} |
| 3026 |
else if (proj) |
| 3027 |
{ |
| 3028 |
coord_expr = "float4(" + coord_expr + ", " + to_extract_component_expression(coord, coord_components) + ")"; |
| 3029 |
} |
| 3030 |
else if (dref) |
| 3031 |
{ |
| 3032 |
// A "normal" sample gets fed into tex2Dproj as well, because the |
| 3033 |
// regular tex2D accepts only two coordinates. |
| 3034 |
coord_expr = "float4(" + coord_expr + ", 1.0)"; |
| 3035 |
} |
| 3036 |
|
| 3037 |
if (!!lod + !!bias + !!proj > 1) |
| 3038 |
SPIRV_CROSS_THROW("Legacy HLSL can only use one of lod/bias/proj modifiers."); |
| 3039 |
} |
| 3040 |
|
| 3041 |
if (op == OpImageFetch) |
| 3042 |
{ |
| 3043 |
if (imgtype.image.dim != DimBuffer && !imgtype.image.ms) |
| 3044 |
coord_expr = |
| 3045 |
join("int", coord_components + 1, "(", coord_expr, ", ", lod ? to_expression(lod) : string("0"), ")"); |
| 3046 |
} |
| 3047 |
else |
| 3048 |
expr += ", "; |
| 3049 |
expr += coord_expr; |
| 3050 |
|
| 3051 |
if (dref && hlsl_options.shader_model >= 40) |
| 3052 |
{ |
| 3053 |
forward = forward && should_forward(dref); |
| 3054 |
expr += ", "; |
| 3055 |
|
| 3056 |
if (proj) |
| 3057 |
expr += to_enclosed_expression(dref) + " / " + to_extract_component_expression(coord, coord_components); |
| 3058 |
else |
| 3059 |
expr += to_expression(dref); |
| 3060 |
} |
| 3061 |
|
| 3062 |
if (!dref && (grad_x || grad_y)) |
| 3063 |
{ |
| 3064 |
forward = forward && should_forward(grad_x); |
| 3065 |
forward = forward && should_forward(grad_y); |
| 3066 |
expr += ", "; |
| 3067 |
expr += to_expression(grad_x); |
| 3068 |
expr += ", "; |
| 3069 |
expr += to_expression(grad_y); |
| 3070 |
} |
| 3071 |
|
| 3072 |
if (!dref && lod && hlsl_options.shader_model >= 40 && op != OpImageFetch) |
| 3073 |
{ |
| 3074 |
forward = forward && should_forward(lod); |
| 3075 |
expr += ", "; |
| 3076 |
expr += to_expression(lod); |
| 3077 |
} |
| 3078 |
|
| 3079 |
if (!dref && bias && hlsl_options.shader_model >= 40) |
| 3080 |
{ |
| 3081 |
forward = forward && should_forward(bias); |
| 3082 |
expr += ", "; |
| 3083 |
expr += to_expression(bias); |
| 3084 |
} |
| 3085 |
|
| 3086 |
if (coffset) |
| 3087 |
{ |
| 3088 |
forward = forward && should_forward(coffset); |
| 3089 |
expr += ", "; |
| 3090 |
expr += to_expression(coffset); |
| 3091 |
} |
| 3092 |
else if (offset) |
| 3093 |
{ |
| 3094 |
forward = forward && should_forward(offset); |
| 3095 |
expr += ", "; |
| 3096 |
expr += to_expression(offset); |
| 3097 |
} |
| 3098 |
|
| 3099 |
if (sample) |
| 3100 |
{ |
| 3101 |
expr += ", "; |
| 3102 |
expr += to_expression(sample); |
| 3103 |
} |
| 3104 |
|
| 3105 |
expr += ")"; |
| 3106 |
|
| 3107 |
if (dref && hlsl_options.shader_model < 40) |
| 3108 |
expr += ".x"; |
| 3109 |
|
| 3110 |
if (op == OpImageQueryLod) |
| 3111 |
{ |
| 3112 |
// This is rather awkward. |
| 3113 |
// textureQueryLod returns two values, the "accessed level", |
| 3114 |
// as well as the actual LOD lambda. |
| 3115 |
// As far as I can tell, there is no way to get the .x component |
| 3116 |
// according to GLSL spec, and it depends on the sampler itself. |
| 3117 |
// Just assume X == Y, so we will need to splat the result to a float2. |
| 3118 |
statement("float _", id, "_tmp = ", expr, ";"); |
| 3119 |
statement("float2 _", id, " = _", id, "_tmp.xx;"); |
| 3120 |
set<SPIRExpression>(id, join("_", id), result_type, true); |
| 3121 |
} |
| 3122 |
else |
| 3123 |
{ |
| 3124 |
emit_op(result_type, id, expr, forward, false); |
| 3125 |
} |
| 3126 |
|
| 3127 |
for (auto &inherit : inherited_expressions) |
| 3128 |
inherit_expression_dependencies(id, inherit); |
| 3129 |
|
| 3130 |
switch (op) |
| 3131 |
{ |
| 3132 |
case OpImageSampleDrefImplicitLod: |
| 3133 |
case OpImageSampleImplicitLod: |
| 3134 |
case OpImageSampleProjImplicitLod: |
| 3135 |
case OpImageSampleProjDrefImplicitLod: |
| 3136 |
register_control_dependent_expression(id); |
| 3137 |
break; |
| 3138 |
|
| 3139 |
default: |
| 3140 |
break; |
| 3141 |
} |
| 3142 |
} |
| 3143 |
|
| 3144 |
string CompilerHLSL::to_resource_binding(const SPIRVariable &var) |
| 3145 |
{ |
| 3146 |
const auto &type = get<SPIRType>(var.basetype); |
| 3147 |
|
| 3148 |
// We can remap push constant blocks, even if they don't have any binding decoration. |
| 3149 |
if (type.storage != StorageClassPushConstant && !has_decoration(var.self, DecorationBinding)) |
| 3150 |
return ""; |
| 3151 |
|
| 3152 |
char space = '\0'; |
| 3153 |
|
| 3154 |
HLSLBindingFlagBits resource_flags = HLSL_BINDING_AUTO_NONE_BIT; |
| 3155 |
|
| 3156 |
switch (type.basetype) |
| 3157 |
{ |
| 3158 |
case SPIRType::SampledImage: |
| 3159 |
space = 't'; // SRV |
| 3160 |
resource_flags = HLSL_BINDING_AUTO_SRV_BIT; |
| 3161 |
break; |
| 3162 |
|
| 3163 |
case SPIRType::Image: |
| 3164 |
if (type.image.sampled == 2 && type.image.dim != DimSubpassData) |
| 3165 |
{ |
| 3166 |
if (has_decoration(var.self, DecorationNonWritable) && hlsl_options.nonwritable_uav_texture_as_srv) |
| 3167 |
{ |
| 3168 |
space = 't'; // SRV |
| 3169 |
resource_flags = HLSL_BINDING_AUTO_SRV_BIT; |
| 3170 |
} |
| 3171 |
else |
| 3172 |
{ |
| 3173 |
space = 'u'; // UAV |
| 3174 |
resource_flags = HLSL_BINDING_AUTO_UAV_BIT; |
| 3175 |
} |
| 3176 |
} |
| 3177 |
else |
| 3178 |
{ |
| 3179 |
space = 't'; // SRV |
| 3180 |
resource_flags = HLSL_BINDING_AUTO_SRV_BIT; |
| 3181 |
} |
| 3182 |
break; |
| 3183 |
|
| 3184 |
case SPIRType::Sampler: |
| 3185 |
space = 's'; |
| 3186 |
resource_flags = HLSL_BINDING_AUTO_SAMPLER_BIT; |
| 3187 |
break; |
| 3188 |
|
| 3189 |
case SPIRType::Struct: |
| 3190 |
{ |
| 3191 |
auto storage = type.storage; |
| 3192 |
if (storage == StorageClassUniform) |
| 3193 |
{ |
| 3194 |
if (has_decoration(type.self, DecorationBufferBlock)) |
| 3195 |
{ |
| 3196 |
Bitset flags = ir.get_buffer_block_flags(var); |
| 3197 |
bool is_readonly = flags.get(DecorationNonWritable) && !is_hlsl_force_storage_buffer_as_uav(var.self); |
| 3198 |
space = is_readonly ? 't' : 'u'; // UAV |
| 3199 |
resource_flags = is_readonly ? HLSL_BINDING_AUTO_SRV_BIT : HLSL_BINDING_AUTO_UAV_BIT; |
| 3200 |
} |
| 3201 |
else if (has_decoration(type.self, DecorationBlock)) |
| 3202 |
{ |
| 3203 |
space = 'b'; // Constant buffers |
| 3204 |
resource_flags = HLSL_BINDING_AUTO_CBV_BIT; |
| 3205 |
} |
| 3206 |
} |
| 3207 |
else if (storage == StorageClassPushConstant) |
| 3208 |
{ |
| 3209 |
space = 'b'; // Constant buffers |
| 3210 |
resource_flags = HLSL_BINDING_AUTO_PUSH_CONSTANT_BIT; |
| 3211 |
} |
| 3212 |
else if (storage == StorageClassStorageBuffer) |
| 3213 |
{ |
| 3214 |
// UAV or SRV depending on readonly flag. |
| 3215 |
Bitset flags = ir.get_buffer_block_flags(var); |
| 3216 |
bool is_readonly = flags.get(DecorationNonWritable) && !is_hlsl_force_storage_buffer_as_uav(var.self); |
| 3217 |
space = is_readonly ? 't' : 'u'; |
| 3218 |
resource_flags = is_readonly ? HLSL_BINDING_AUTO_SRV_BIT : HLSL_BINDING_AUTO_UAV_BIT; |
| 3219 |
} |
| 3220 |
|
| 3221 |
break; |
| 3222 |
} |
| 3223 |
default: |
| 3224 |
break; |
| 3225 |
} |
| 3226 |
|
| 3227 |
if (!space) |
| 3228 |
return ""; |
| 3229 |
|
| 3230 |
uint32_t desc_set = |
| 3231 |
resource_flags == HLSL_BINDING_AUTO_PUSH_CONSTANT_BIT ? ResourceBindingPushConstantDescriptorSet : 0u; |
| 3232 |
uint32_t binding = resource_flags == HLSL_BINDING_AUTO_PUSH_CONSTANT_BIT ? ResourceBindingPushConstantBinding : 0u; |
| 3233 |
|
| 3234 |
if (has_decoration(var.self, DecorationBinding)) |
| 3235 |
binding = get_decoration(var.self, DecorationBinding); |
| 3236 |
if (has_decoration(var.self, DecorationDescriptorSet)) |
| 3237 |
desc_set = get_decoration(var.self, DecorationDescriptorSet); |
| 3238 |
|
| 3239 |
return to_resource_register(resource_flags, space, binding, desc_set); |
| 3240 |
} |
| 3241 |
|
| 3242 |
string CompilerHLSL::to_resource_binding_sampler(const SPIRVariable &var) |
| 3243 |
{ |
| 3244 |
// For combined image samplers. |
| 3245 |
if (!has_decoration(var.self, DecorationBinding)) |
| 3246 |
return ""; |
| 3247 |
|
| 3248 |
return to_resource_register(HLSL_BINDING_AUTO_SAMPLER_BIT, 's', get_decoration(var.self, DecorationBinding), |
| 3249 |
get_decoration(var.self, DecorationDescriptorSet)); |
| 3250 |
} |
| 3251 |
|
| 3252 |
void CompilerHLSL::remap_hlsl_resource_binding(HLSLBindingFlagBits type, uint32_t &desc_set, uint32_t &binding) |
| 3253 |
{ |
| 3254 |
auto itr = resource_bindings.find({ get_execution_model(), desc_set, binding }); |
| 3255 |
if (itr != end(resource_bindings)) |
| 3256 |
{ |
| 3257 |
auto &remap = itr->second; |
| 3258 |
remap.second = true; |
| 3259 |
|
| 3260 |
switch (type) |
| 3261 |
{ |
| 3262 |
case HLSL_BINDING_AUTO_PUSH_CONSTANT_BIT: |
| 3263 |
case HLSL_BINDING_AUTO_CBV_BIT: |
| 3264 |
desc_set = remap.first.cbv.register_space; |
| 3265 |
binding = remap.first.cbv.register_binding; |
| 3266 |
break; |
| 3267 |
|
| 3268 |
case HLSL_BINDING_AUTO_SRV_BIT: |
| 3269 |
desc_set = remap.first.srv.register_space; |
| 3270 |
binding = remap.first.srv.register_binding; |
| 3271 |
break; |
| 3272 |
|
| 3273 |
case HLSL_BINDING_AUTO_SAMPLER_BIT: |
| 3274 |
desc_set = remap.first.sampler.register_space; |
| 3275 |
binding = remap.first.sampler.register_binding; |
| 3276 |
break; |
| 3277 |
|
| 3278 |
case HLSL_BINDING_AUTO_UAV_BIT: |
| 3279 |
desc_set = remap.first.uav.register_space; |
| 3280 |
binding = remap.first.uav.register_binding; |
| 3281 |
break; |
| 3282 |
|
| 3283 |
default: |
| 3284 |
break; |
| 3285 |
} |
| 3286 |
} |
| 3287 |
} |
| 3288 |
|
| 3289 |
string CompilerHLSL::to_resource_register(HLSLBindingFlagBits flag, char space, uint32_t binding, uint32_t space_set) |
| 3290 |
{ |
| 3291 |
if ((flag & resource_binding_flags) == 0) |
| 3292 |
{ |
| 3293 |
remap_hlsl_resource_binding(flag, space_set, binding); |
| 3294 |
|
| 3295 |
// The push constant block did not have a binding, and there were no remap for it, |
| 3296 |
// so, declare without register binding. |
| 3297 |
if (flag == HLSL_BINDING_AUTO_PUSH_CONSTANT_BIT && space_set == ResourceBindingPushConstantDescriptorSet) |
| 3298 |
return ""; |
| 3299 |
|
| 3300 |
if (hlsl_options.shader_model >= 51) |
| 3301 |
return join(" : register(", space, binding, ", space", space_set, ")"); |
| 3302 |
else |
| 3303 |
return join(" : register(", space, binding, ")"); |
| 3304 |
} |
| 3305 |
else |
| 3306 |
return ""; |
| 3307 |
} |
| 3308 |
|
| 3309 |
void CompilerHLSL::emit_modern_uniform(const SPIRVariable &var) |
| 3310 |
{ |
| 3311 |
auto &type = get<SPIRType>(var.basetype); |
| 3312 |
switch (type.basetype) |
| 3313 |
{ |
| 3314 |
case SPIRType::SampledImage: |
| 3315 |
case SPIRType::Image: |
| 3316 |
{ |
| 3317 |
bool is_coherent = false; |
| 3318 |
if (type.basetype == SPIRType::Image && type.image.sampled == 2) |
| 3319 |
is_coherent = has_decoration(var.self, DecorationCoherent); |
| 3320 |
|
| 3321 |
statement(is_coherent ? "globallycoherent " : "", image_type_hlsl_modern(type, var.self), " ", |
| 3322 |
to_name(var.self), type_to_array_glsl(type), to_resource_binding(var), ";"); |
| 3323 |
|
| 3324 |
if (type.basetype == SPIRType::SampledImage && type.image.dim != DimBuffer) |
| 3325 |
{ |
| 3326 |
// For combined image samplers, also emit a combined image sampler. |
| 3327 |
if (image_is_comparison(type, var.self)) |
| 3328 |
statement("SamplerComparisonState ", to_sampler_expression(var.self), type_to_array_glsl(type), |
| 3329 |
to_resource_binding_sampler(var), ";"); |
| 3330 |
else |
| 3331 |
statement("SamplerState ", to_sampler_expression(var.self), type_to_array_glsl(type), |
| 3332 |
to_resource_binding_sampler(var), ";"); |
| 3333 |
} |
| 3334 |
break; |
| 3335 |
} |
| 3336 |
|
| 3337 |
case SPIRType::Sampler: |
| 3338 |
if (comparison_ids.count(var.self)) |
| 3339 |
statement("SamplerComparisonState ", to_name(var.self), type_to_array_glsl(type), to_resource_binding(var), |
| 3340 |
";"); |
| 3341 |
else |
| 3342 |
statement("SamplerState ", to_name(var.self), type_to_array_glsl(type), to_resource_binding(var), ";"); |
| 3343 |
break; |
| 3344 |
|
| 3345 |
default: |
| 3346 |
statement(variable_decl(var), to_resource_binding(var), ";"); |
| 3347 |
break; |
| 3348 |
} |
| 3349 |
} |
| 3350 |
|
| 3351 |
void CompilerHLSL::emit_legacy_uniform(const SPIRVariable &var) |
| 3352 |
{ |
| 3353 |
auto &type = get<SPIRType>(var.basetype); |
| 3354 |
switch (type.basetype) |
| 3355 |
{ |
| 3356 |
case SPIRType::Sampler: |
| 3357 |
case SPIRType::Image: |
| 3358 |
SPIRV_CROSS_THROW("Separate image and samplers not supported in legacy HLSL."); |
| 3359 |
|
| 3360 |
default: |
| 3361 |
statement(variable_decl(var), ";"); |
| 3362 |
break; |
| 3363 |
} |
| 3364 |
} |
| 3365 |
|
| 3366 |
void CompilerHLSL::emit_uniform(const SPIRVariable &var) |
| 3367 |
{ |
| 3368 |
add_resource_name(var.self); |
| 3369 |
if (hlsl_options.shader_model >= 40) |
| 3370 |
emit_modern_uniform(var); |
| 3371 |
else |
| 3372 |
emit_legacy_uniform(var); |
| 3373 |
} |
| 3374 |
|
| 3375 |
bool CompilerHLSL::emit_complex_bitcast(uint32_t, uint32_t, uint32_t) |
| 3376 |
{ |
| 3377 |
return false; |
| 3378 |
} |
| 3379 |
|
| 3380 |
string CompilerHLSL::bitcast_glsl_op(const SPIRType &out_type, const SPIRType &in_type) |
| 3381 |
{ |
| 3382 |
if (out_type.basetype == SPIRType::UInt && in_type.basetype == SPIRType::Int) |
| 3383 |
return type_to_glsl(out_type); |
| 3384 |
else if (out_type.basetype == SPIRType::UInt64 && in_type.basetype == SPIRType::Int64) |
| 3385 |
return type_to_glsl(out_type); |
| 3386 |
else if (out_type.basetype == SPIRType::UInt && in_type.basetype == SPIRType::Float) |
| 3387 |
return "asuint"; |
| 3388 |
else if (out_type.basetype == SPIRType::Int && in_type.basetype == SPIRType::UInt) |
| 3389 |
return type_to_glsl(out_type); |
| 3390 |
else if (out_type.basetype == SPIRType::Int64 && in_type.basetype == SPIRType::UInt64) |
| 3391 |
return type_to_glsl(out_type); |
| 3392 |
else if (out_type.basetype == SPIRType::Int && in_type.basetype == SPIRType::Float) |
| 3393 |
return "asint"; |
| 3394 |
else if (out_type.basetype == SPIRType::Float && in_type.basetype == SPIRType::UInt) |
| 3395 |
return "asfloat"; |
| 3396 |
else if (out_type.basetype == SPIRType::Float && in_type.basetype == SPIRType::Int) |
| 3397 |
return "asfloat"; |
| 3398 |
else if (out_type.basetype == SPIRType::Int64 && in_type.basetype == SPIRType::Double) |
| 3399 |
SPIRV_CROSS_THROW("Double to Int64 is not supported in HLSL."); |
| 3400 |
else if (out_type.basetype == SPIRType::UInt64 && in_type.basetype == SPIRType::Double) |
| 3401 |
SPIRV_CROSS_THROW("Double to UInt64 is not supported in HLSL."); |
| 3402 |
else if (out_type.basetype == SPIRType::Double && in_type.basetype == SPIRType::Int64) |
| 3403 |
return "asdouble"; |
| 3404 |
else if (out_type.basetype == SPIRType::Double && in_type.basetype == SPIRType::UInt64) |
| 3405 |
return "asdouble"; |
| 3406 |
else if (out_type.basetype == SPIRType::Half && in_type.basetype == SPIRType::UInt && in_type.vecsize == 1) |
| 3407 |
{ |
| 3408 |
if (!requires_explicit_fp16_packing) |
| 3409 |
{ |
| 3410 |
requires_explicit_fp16_packing = true; |
| 3411 |
force_recompile(); |
| 3412 |
} |
| 3413 |
return "SPIRV_Cross_unpackFloat2x16"; |
| 3414 |
} |
| 3415 |
else if (out_type.basetype == SPIRType::UInt && in_type.basetype == SPIRType::Half && in_type.vecsize == 2) |
| 3416 |
{ |
| 3417 |
if (!requires_explicit_fp16_packing) |
| 3418 |
{ |
| 3419 |
requires_explicit_fp16_packing = true; |
| 3420 |
force_recompile(); |
| 3421 |
} |
| 3422 |
return "SPIRV_Cross_packFloat2x16"; |
| 3423 |
} |
| 3424 |
else |
| 3425 |
return ""; |
| 3426 |
} |
| 3427 |
|
| 3428 |
void CompilerHLSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop, const uint32_t *args, uint32_t count) |
| 3429 |
{ |
| 3430 |
auto op = static_cast<GLSLstd450>(eop); |
| 3431 |
|
| 3432 |
// If we need to do implicit bitcasts, make sure we do it with the correct type. |
| 3433 |
uint32_t integer_width = get_integer_width_for_glsl_instruction(op, args, count); |
| 3434 |
auto int_type = to_signed_basetype(integer_width); |
| 3435 |
auto uint_type = to_unsigned_basetype(integer_width); |
| 3436 |
|
| 3437 |
switch (op) |
| 3438 |
{ |
| 3439 |
case GLSLstd450InverseSqrt: |
| 3440 |
emit_unary_func_op(result_type, id, args[0], "rsqrt"); |
| 3441 |
break; |
| 3442 |
|
| 3443 |
case GLSLstd450Fract: |
| 3444 |
emit_unary_func_op(result_type, id, args[0], "frac"); |
| 3445 |
break; |
| 3446 |
|
| 3447 |
case GLSLstd450RoundEven: |
| 3448 |
if (hlsl_options.shader_model < 40) |
| 3449 |
SPIRV_CROSS_THROW("roundEven is not supported in HLSL shader model 2/3."); |
| 3450 |
emit_unary_func_op(result_type, id, args[0], "round"); |
| 3451 |
break; |
| 3452 |
|
| 3453 |
case GLSLstd450Acosh: |
| 3454 |
case GLSLstd450Asinh: |
| 3455 |
case GLSLstd450Atanh: |
| 3456 |
SPIRV_CROSS_THROW("Inverse hyperbolics are not supported on HLSL."); |
| 3457 |
|
| 3458 |
case GLSLstd450FMix: |
| 3459 |
case GLSLstd450IMix: |
| 3460 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "lerp"); |
| 3461 |
break; |
| 3462 |
|
| 3463 |
case GLSLstd450Atan2: |
| 3464 |
emit_binary_func_op(result_type, id, args[0], args[1], "atan2"); |
| 3465 |
break; |
| 3466 |
|
| 3467 |
case GLSLstd450Fma: |
| 3468 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "mad"); |
| 3469 |
break; |
| 3470 |
|
| 3471 |
case GLSLstd450InterpolateAtCentroid: |
| 3472 |
emit_unary_func_op(result_type, id, args[0], "EvaluateAttributeAtCentroid"); |
| 3473 |
break; |
| 3474 |
case GLSLstd450InterpolateAtSample: |
| 3475 |
emit_binary_func_op(result_type, id, args[0], args[1], "EvaluateAttributeAtSample"); |
| 3476 |
break; |
| 3477 |
case GLSLstd450InterpolateAtOffset: |
| 3478 |
emit_binary_func_op(result_type, id, args[0], args[1], "EvaluateAttributeSnapped"); |
| 3479 |
break; |
| 3480 |
|
| 3481 |
case GLSLstd450PackHalf2x16: |
| 3482 |
if (!requires_fp16_packing) |
| 3483 |
{ |
| 3484 |
requires_fp16_packing = true; |
| 3485 |
force_recompile(); |
| 3486 |
} |
| 3487 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_packHalf2x16"); |
| 3488 |
break; |
| 3489 |
|
| 3490 |
case GLSLstd450UnpackHalf2x16: |
| 3491 |
if (!requires_fp16_packing) |
| 3492 |
{ |
| 3493 |
requires_fp16_packing = true; |
| 3494 |
force_recompile(); |
| 3495 |
} |
| 3496 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_unpackHalf2x16"); |
| 3497 |
break; |
| 3498 |
|
| 3499 |
case GLSLstd450PackSnorm4x8: |
| 3500 |
if (!requires_snorm8_packing) |
| 3501 |
{ |
| 3502 |
requires_snorm8_packing = true; |
| 3503 |
force_recompile(); |
| 3504 |
} |
| 3505 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_packSnorm4x8"); |
| 3506 |
break; |
| 3507 |
|
| 3508 |
case GLSLstd450UnpackSnorm4x8: |
| 3509 |
if (!requires_snorm8_packing) |
| 3510 |
{ |
| 3511 |
requires_snorm8_packing = true; |
| 3512 |
force_recompile(); |
| 3513 |
} |
| 3514 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_unpackSnorm4x8"); |
| 3515 |
break; |
| 3516 |
|
| 3517 |
case GLSLstd450PackUnorm4x8: |
| 3518 |
if (!requires_unorm8_packing) |
| 3519 |
{ |
| 3520 |
requires_unorm8_packing = true; |
| 3521 |
force_recompile(); |
| 3522 |
} |
| 3523 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_packUnorm4x8"); |
| 3524 |
break; |
| 3525 |
|
| 3526 |
case GLSLstd450UnpackUnorm4x8: |
| 3527 |
if (!requires_unorm8_packing) |
| 3528 |
{ |
| 3529 |
requires_unorm8_packing = true; |
| 3530 |
force_recompile(); |
| 3531 |
} |
| 3532 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_unpackUnorm4x8"); |
| 3533 |
break; |
| 3534 |
|
| 3535 |
case GLSLstd450PackSnorm2x16: |
| 3536 |
if (!requires_snorm16_packing) |
| 3537 |
{ |
| 3538 |
requires_snorm16_packing = true; |
| 3539 |
force_recompile(); |
| 3540 |
} |
| 3541 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_packSnorm2x16"); |
| 3542 |
break; |
| 3543 |
|
| 3544 |
case GLSLstd450UnpackSnorm2x16: |
| 3545 |
if (!requires_snorm16_packing) |
| 3546 |
{ |
| 3547 |
requires_snorm16_packing = true; |
| 3548 |
force_recompile(); |
| 3549 |
} |
| 3550 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_unpackSnorm2x16"); |
| 3551 |
break; |
| 3552 |
|
| 3553 |
case GLSLstd450PackUnorm2x16: |
| 3554 |
if (!requires_unorm16_packing) |
| 3555 |
{ |
| 3556 |
requires_unorm16_packing = true; |
| 3557 |
force_recompile(); |
| 3558 |
} |
| 3559 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_packUnorm2x16"); |
| 3560 |
break; |
| 3561 |
|
| 3562 |
case GLSLstd450UnpackUnorm2x16: |
| 3563 |
if (!requires_unorm16_packing) |
| 3564 |
{ |
| 3565 |
requires_unorm16_packing = true; |
| 3566 |
force_recompile(); |
| 3567 |
} |
| 3568 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_unpackUnorm2x16"); |
| 3569 |
break; |
| 3570 |
|
| 3571 |
case GLSLstd450PackDouble2x32: |
| 3572 |
case GLSLstd450UnpackDouble2x32: |
| 3573 |
SPIRV_CROSS_THROW("packDouble2x32/unpackDouble2x32 not supported in HLSL."); |
| 3574 |
|
| 3575 |
case GLSLstd450FindILsb: |
| 3576 |
{ |
| 3577 |
auto basetype = expression_type(args[0]).basetype; |
| 3578 |
emit_unary_func_op_cast(result_type, id, args[0], "firstbitlow", basetype, basetype); |
| 3579 |
break; |
| 3580 |
} |
| 3581 |
|
| 3582 |
case GLSLstd450FindSMsb: |
| 3583 |
emit_unary_func_op_cast(result_type, id, args[0], "firstbithigh", int_type, int_type); |
| 3584 |
break; |
| 3585 |
|
| 3586 |
case GLSLstd450FindUMsb: |
| 3587 |
emit_unary_func_op_cast(result_type, id, args[0], "firstbithigh", uint_type, uint_type); |
| 3588 |
break; |
| 3589 |
|
| 3590 |
case GLSLstd450MatrixInverse: |
| 3591 |
{ |
| 3592 |
auto &type = get<SPIRType>(result_type); |
| 3593 |
if (type.vecsize == 2 && type.columns == 2) |
| 3594 |
{ |
| 3595 |
if (!requires_inverse_2x2) |
| 3596 |
{ |
| 3597 |
requires_inverse_2x2 = true; |
| 3598 |
force_recompile(); |
| 3599 |
} |
| 3600 |
} |
| 3601 |
else if (type.vecsize == 3 && type.columns == 3) |
| 3602 |
{ |
| 3603 |
if (!requires_inverse_3x3) |
| 3604 |
{ |
| 3605 |
requires_inverse_3x3 = true; |
| 3606 |
force_recompile(); |
| 3607 |
} |
| 3608 |
} |
| 3609 |
else if (type.vecsize == 4 && type.columns == 4) |
| 3610 |
{ |
| 3611 |
if (!requires_inverse_4x4) |
| 3612 |
{ |
| 3613 |
requires_inverse_4x4 = true; |
| 3614 |
force_recompile(); |
| 3615 |
} |
| 3616 |
} |
| 3617 |
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_Inverse"); |
| 3618 |
break; |
| 3619 |
} |
| 3620 |
|
| 3621 |
case GLSLstd450Normalize: |
| 3622 |
// HLSL does not support scalar versions here. |
| 3623 |
if (expression_type(args[0]).vecsize == 1) |
| 3624 |
{ |
| 3625 |
// Returns -1 or 1 for valid input, sign() does the job. |
| 3626 |
emit_unary_func_op(result_type, id, args[0], "sign"); |
| 3627 |
} |
| 3628 |
else |
| 3629 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 3630 |
break; |
| 3631 |
|
| 3632 |
case GLSLstd450Reflect: |
| 3633 |
if (get<SPIRType>(result_type).vecsize == 1) |
| 3634 |
{ |
| 3635 |
if (!requires_scalar_reflect) |
| 3636 |
{ |
| 3637 |
requires_scalar_reflect = true; |
| 3638 |
force_recompile(); |
| 3639 |
} |
| 3640 |
emit_binary_func_op(result_type, id, args[0], args[1], "SPIRV_Cross_Reflect"); |
| 3641 |
} |
| 3642 |
else |
| 3643 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 3644 |
break; |
| 3645 |
|
| 3646 |
case GLSLstd450Refract: |
| 3647 |
if (get<SPIRType>(result_type).vecsize == 1) |
| 3648 |
{ |
| 3649 |
if (!requires_scalar_refract) |
| 3650 |
{ |
| 3651 |
requires_scalar_refract = true; |
| 3652 |
force_recompile(); |
| 3653 |
} |
| 3654 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "SPIRV_Cross_Refract"); |
| 3655 |
} |
| 3656 |
else |
| 3657 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 3658 |
break; |
| 3659 |
|
| 3660 |
case GLSLstd450FaceForward: |
| 3661 |
if (get<SPIRType>(result_type).vecsize == 1) |
| 3662 |
{ |
| 3663 |
if (!requires_scalar_faceforward) |
| 3664 |
{ |
| 3665 |
requires_scalar_faceforward = true; |
| 3666 |
force_recompile(); |
| 3667 |
} |
| 3668 |
emit_trinary_func_op(result_type, id, args[0], args[1], args[2], "SPIRV_Cross_FaceForward"); |
| 3669 |
} |
| 3670 |
else |
| 3671 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 3672 |
break; |
| 3673 |
|
| 3674 |
default: |
| 3675 |
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count); |
| 3676 |
break; |
| 3677 |
} |
| 3678 |
} |
| 3679 |
|
| 3680 |
void CompilerHLSL::read_access_chain_array(const string &lhs, const SPIRAccessChain &chain) |
| 3681 |
{ |
| 3682 |
auto &type = get<SPIRType>(chain.basetype); |
| 3683 |
|
| 3684 |
// Need to use a reserved identifier here since it might shadow an identifier in the access chain input or other loops. |
| 3685 |
auto ident = get_unique_identifier(); |
| 3686 |
|
| 3687 |
statement("[unroll]"); |
| 3688 |
statement("for (int ", ident, " = 0; ", ident, " < ", to_array_size(type, uint32_t(type.array.size() - 1)), "; ", |
| 3689 |
ident, "++)"); |
| 3690 |
begin_scope(); |
| 3691 |
auto subchain = chain; |
| 3692 |
subchain.dynamic_index = join(ident, " * ", chain.array_stride, " + ", chain.dynamic_index); |
| 3693 |
subchain.basetype = type.parent_type; |
| 3694 |
if (!get<SPIRType>(subchain.basetype).array.empty()) |
| 3695 |
subchain.array_stride = get_decoration(subchain.basetype, DecorationArrayStride); |
| 3696 |
read_access_chain(nullptr, join(lhs, "[", ident, "]"), subchain); |
| 3697 |
end_scope(); |
| 3698 |
} |
| 3699 |
|
| 3700 |
void CompilerHLSL::read_access_chain_struct(const string &lhs, const SPIRAccessChain &chain) |
| 3701 |
{ |
| 3702 |
auto &type = get<SPIRType>(chain.basetype); |
| 3703 |
auto subchain = chain; |
| 3704 |
uint32_t member_count = uint32_t(type.member_types.size()); |
| 3705 |
|
| 3706 |
for (uint32_t i = 0; i < member_count; i++) |
| 3707 |
{ |
| 3708 |
uint32_t offset = type_struct_member_offset(type, i); |
| 3709 |
subchain.static_index = chain.static_index + offset; |
| 3710 |
subchain.basetype = type.member_types[i]; |
| 3711 |
|
| 3712 |
subchain.matrix_stride = 0; |
| 3713 |
subchain.array_stride = 0; |
| 3714 |
subchain.row_major_matrix = false; |
| 3715 |
|
| 3716 |
auto &member_type = get<SPIRType>(subchain.basetype); |
| 3717 |
if (member_type.columns > 1) |
| 3718 |
{ |
| 3719 |
subchain.matrix_stride = type_struct_member_matrix_stride(type, i); |
| 3720 |
subchain.row_major_matrix = has_member_decoration(type.self, i, DecorationRowMajor); |
| 3721 |
} |
| 3722 |
|
| 3723 |
if (!member_type.array.empty()) |
| 3724 |
subchain.array_stride = type_struct_member_array_stride(type, i); |
| 3725 |
|
| 3726 |
read_access_chain(nullptr, join(lhs, ".", to_member_name(type, i)), subchain); |
| 3727 |
} |
| 3728 |
} |
| 3729 |
|
| 3730 |
void CompilerHLSL::read_access_chain(string *expr, const string &lhs, const SPIRAccessChain &chain) |
| 3731 |
{ |
| 3732 |
auto &type = get<SPIRType>(chain.basetype); |
| 3733 |
|
| 3734 |
SPIRType target_type; |
| 3735 |
target_type.basetype = SPIRType::UInt; |
| 3736 |
target_type.vecsize = type.vecsize; |
| 3737 |
target_type.columns = type.columns; |
| 3738 |
|
| 3739 |
if (!type.array.empty()) |
| 3740 |
{ |
| 3741 |
read_access_chain_array(lhs, chain); |
| 3742 |
return; |
| 3743 |
} |
| 3744 |
else if (type.basetype == SPIRType::Struct) |
| 3745 |
{ |
| 3746 |
read_access_chain_struct(lhs, chain); |
| 3747 |
return; |
| 3748 |
} |
| 3749 |
else if (type.width != 32 && !hlsl_options.enable_16bit_types) |
| 3750 |
SPIRV_CROSS_THROW("Reading types other than 32-bit from ByteAddressBuffer not yet supported, unless SM 6.2 and " |
| 3751 |
"native 16-bit types are enabled."); |
| 3752 |
|
| 3753 |
bool templated_load = hlsl_options.shader_model >= 62; |
| 3754 |
string load_expr; |
| 3755 |
|
| 3756 |
string template_expr; |
| 3757 |
if (templated_load) |
| 3758 |
template_expr = join("<", type_to_glsl(type), ">"); |
| 3759 |
|
| 3760 |
// Load a vector or scalar. |
| 3761 |
if (type.columns == 1 && !chain.row_major_matrix) |
| 3762 |
{ |
| 3763 |
const char *load_op = nullptr; |
| 3764 |
switch (type.vecsize) |
| 3765 |
{ |
| 3766 |
case 1: |
| 3767 |
load_op = "Load"; |
| 3768 |
break; |
| 3769 |
case 2: |
| 3770 |
load_op = "Load2"; |
| 3771 |
break; |
| 3772 |
case 3: |
| 3773 |
load_op = "Load3"; |
| 3774 |
break; |
| 3775 |
case 4: |
| 3776 |
load_op = "Load4"; |
| 3777 |
break; |
| 3778 |
default: |
| 3779 |
SPIRV_CROSS_THROW("Unknown vector size."); |
| 3780 |
} |
| 3781 |
|
| 3782 |
if (templated_load) |
| 3783 |
load_op = "Load"; |
| 3784 |
|
| 3785 |
load_expr = join(chain.base, ".", load_op, template_expr, "(", chain.dynamic_index, chain.static_index, ")"); |
| 3786 |
} |
| 3787 |
else if (type.columns == 1) |
| 3788 |
{ |
| 3789 |
// Strided load since we are loading a column from a row-major matrix. |
| 3790 |
if (templated_load) |
| 3791 |
{ |
| 3792 |
auto scalar_type = type; |
| 3793 |
scalar_type.vecsize = 1; |
| 3794 |
scalar_type.columns = 1; |
| 3795 |
template_expr = join("<", type_to_glsl(scalar_type), ">"); |
| 3796 |
if (type.vecsize > 1) |
| 3797 |
load_expr += type_to_glsl(type) + "("; |
| 3798 |
} |
| 3799 |
else if (type.vecsize > 1) |
| 3800 |
{ |
| 3801 |
load_expr = type_to_glsl(target_type); |
| 3802 |
load_expr += "("; |
| 3803 |
} |
| 3804 |
|
| 3805 |
for (uint32_t r = 0; r < type.vecsize; r++) |
| 3806 |
{ |
| 3807 |
load_expr += join(chain.base, ".Load", template_expr, "(", chain.dynamic_index, |
| 3808 |
chain.static_index + r * chain.matrix_stride, ")"); |
| 3809 |
if (r + 1 < type.vecsize) |
| 3810 |
load_expr += ", "; |
| 3811 |
} |
| 3812 |
|
| 3813 |
if (type.vecsize > 1) |
| 3814 |
load_expr += ")"; |
| 3815 |
} |
| 3816 |
else if (!chain.row_major_matrix) |
| 3817 |
{ |
| 3818 |
// Load a matrix, column-major, the easy case. |
| 3819 |
const char *load_op = nullptr; |
| 3820 |
switch (type.vecsize) |
| 3821 |
{ |
| 3822 |
case 1: |
| 3823 |
load_op = "Load"; |
| 3824 |
break; |
| 3825 |
case 2: |
| 3826 |
load_op = "Load2"; |
| 3827 |
break; |
| 3828 |
case 3: |
| 3829 |
load_op = "Load3"; |
| 3830 |
break; |
| 3831 |
case 4: |
| 3832 |
load_op = "Load4"; |
| 3833 |
break; |
| 3834 |
default: |
| 3835 |
SPIRV_CROSS_THROW("Unknown vector size."); |
| 3836 |
} |
| 3837 |
|
| 3838 |
if (templated_load) |
| 3839 |
{ |
| 3840 |
auto vector_type = type; |
| 3841 |
vector_type.columns = 1; |
| 3842 |
template_expr = join("<", type_to_glsl(vector_type), ">"); |
| 3843 |
load_expr = type_to_glsl(type); |
| 3844 |
load_op = "Load"; |
| 3845 |
} |
| 3846 |
else |
| 3847 |
{ |
| 3848 |
// Note, this loading style in HLSL is *actually* row-major, but we always treat matrices as transposed in this backend, |
| 3849 |
// so row-major is technically column-major ... |
| 3850 |
load_expr = type_to_glsl(target_type); |
| 3851 |
} |
| 3852 |
load_expr += "("; |
| 3853 |
|
| 3854 |
for (uint32_t c = 0; c < type.columns; c++) |
| 3855 |
{ |
| 3856 |
load_expr += join(chain.base, ".", load_op, template_expr, "(", chain.dynamic_index, |
| 3857 |
chain.static_index + c * chain.matrix_stride, ")"); |
| 3858 |
if (c + 1 < type.columns) |
| 3859 |
load_expr += ", "; |
| 3860 |
} |
| 3861 |
load_expr += ")"; |
| 3862 |
} |
| 3863 |
else |
| 3864 |
{ |
| 3865 |
// Pick out elements one by one ... Hopefully compilers are smart enough to recognize this pattern |
| 3866 |
// considering HLSL is "row-major decl", but "column-major" memory layout (basically implicit transpose model, ugh) ... |
| 3867 |
|
| 3868 |
if (templated_load) |
| 3869 |
{ |
| 3870 |
load_expr = type_to_glsl(type); |
| 3871 |
auto scalar_type = type; |
| 3872 |
scalar_type.vecsize = 1; |
| 3873 |
scalar_type.columns = 1; |
| 3874 |
template_expr = join("<", type_to_glsl(scalar_type), ">"); |
| 3875 |
} |
| 3876 |
else |
| 3877 |
load_expr = type_to_glsl(target_type); |
| 3878 |
|
| 3879 |
load_expr += "("; |
| 3880 |
|
| 3881 |
for (uint32_t c = 0; c < type.columns; c++) |
| 3882 |
{ |
| 3883 |
for (uint32_t r = 0; r < type.vecsize; r++) |
| 3884 |
{ |
| 3885 |
load_expr += join(chain.base, ".Load", template_expr, "(", chain.dynamic_index, |
| 3886 |
chain.static_index + c * (type.width / 8) + r * chain.matrix_stride, ")"); |
| 3887 |
|
| 3888 |
if ((r + 1 < type.vecsize) || (c + 1 < type.columns)) |
| 3889 |
load_expr += ", "; |
| 3890 |
} |
| 3891 |
} |
| 3892 |
load_expr += ")"; |
| 3893 |
} |
| 3894 |
|
| 3895 |
if (!templated_load) |
| 3896 |
{ |
| 3897 |
auto bitcast_op = bitcast_glsl_op(type, target_type); |
| 3898 |
if (!bitcast_op.empty()) |
| 3899 |
load_expr = join(bitcast_op, "(", load_expr, ")"); |
| 3900 |
} |
| 3901 |
|
| 3902 |
if (lhs.empty()) |
| 3903 |
{ |
| 3904 |
assert(expr); |
| 3905 |
*expr = move(load_expr); |
| 3906 |
} |
| 3907 |
else |
| 3908 |
statement(lhs, " = ", load_expr, ";"); |
| 3909 |
} |
| 3910 |
|
| 3911 |
void CompilerHLSL::emit_load(const Instruction &instruction) |
| 3912 |
{ |
| 3913 |
auto ops = stream(instruction); |
| 3914 |
|
| 3915 |
auto *chain = maybe_get<SPIRAccessChain>(ops[2]); |
| 3916 |
if (chain) |
| 3917 |
{ |
| 3918 |
uint32_t result_type = ops[0]; |
| 3919 |
uint32_t id = ops[1]; |
| 3920 |
uint32_t ptr = ops[2]; |
| 3921 |
|
| 3922 |
if (has_decoration(ptr, DecorationNonUniformEXT)) |
| 3923 |
propagate_nonuniform_qualifier(ptr); |
| 3924 |
|
| 3925 |
auto &type = get<SPIRType>(result_type); |
| 3926 |
bool composite_load = !type.array.empty() || type.basetype == SPIRType::Struct; |
| 3927 |
|
| 3928 |
if (composite_load) |
| 3929 |
{ |
| 3930 |
// We cannot make this work in one single expression as we might have nested structures and arrays, |
| 3931 |
// so unroll the load to an uninitialized temporary. |
| 3932 |
emit_uninitialized_temporary_expression(result_type, id); |
| 3933 |
read_access_chain(nullptr, to_expression(id), *chain); |
| 3934 |
track_expression_read(chain->self); |
| 3935 |
} |
| 3936 |
else |
| 3937 |
{ |
| 3938 |
string load_expr; |
| 3939 |
read_access_chain(&load_expr, "", *chain); |
| 3940 |
|
| 3941 |
bool forward = should_forward(ptr) && forced_temporaries.find(id) == end(forced_temporaries); |
| 3942 |
|
| 3943 |
// If we are forwarding this load, |
| 3944 |
// don't register the read to access chain here, defer that to when we actually use the expression, |
| 3945 |
// using the add_implied_read_expression mechanism. |
| 3946 |
if (!forward) |
| 3947 |
track_expression_read(chain->self); |
| 3948 |
|
| 3949 |
// Do not forward complex load sequences like matrices, structs and arrays. |
| 3950 |
if (type.columns > 1) |
| 3951 |
forward = false; |
| 3952 |
|
| 3953 |
auto &e = emit_op(result_type, id, load_expr, forward, true); |
| 3954 |
e.need_transpose = false; |
| 3955 |
register_read(id, ptr, forward); |
| 3956 |
inherit_expression_dependencies(id, ptr); |
| 3957 |
if (forward) |
| 3958 |
add_implied_read_expression(e, chain->self); |
| 3959 |
} |
| 3960 |
} |
| 3961 |
else |
| 3962 |
CompilerGLSL::emit_instruction(instruction); |
| 3963 |
} |
| 3964 |
|
| 3965 |
void CompilerHLSL::write_access_chain_array(const SPIRAccessChain &chain, uint32_t value, |
| 3966 |
const SmallVector<uint32_t> &composite_chain) |
| 3967 |
{ |
| 3968 |
auto &type = get<SPIRType>(chain.basetype); |
| 3969 |
|
| 3970 |
// Need to use a reserved identifier here since it might shadow an identifier in the access chain input or other loops. |
| 3971 |
auto ident = get_unique_identifier(); |
| 3972 |
|
| 3973 |
uint32_t id = ir.increase_bound_by(2); |
| 3974 |
uint32_t int_type_id = id + 1; |
| 3975 |
SPIRType int_type; |
| 3976 |
int_type.basetype = SPIRType::Int; |
| 3977 |
int_type.width = 32; |
| 3978 |
set<SPIRType>(int_type_id, int_type); |
| 3979 |
set<SPIRExpression>(id, ident, int_type_id, true); |
| 3980 |
set_name(id, ident); |
| 3981 |
suppressed_usage_tracking.insert(id); |
| 3982 |
|
| 3983 |
statement("[unroll]"); |
| 3984 |
statement("for (int ", ident, " = 0; ", ident, " < ", to_array_size(type, uint32_t(type.array.size() - 1)), "; ", |
| 3985 |
ident, "++)"); |
| 3986 |
begin_scope(); |
| 3987 |
auto subchain = chain; |
| 3988 |
subchain.dynamic_index = join(ident, " * ", chain.array_stride, " + ", chain.dynamic_index); |
| 3989 |
subchain.basetype = type.parent_type; |
| 3990 |
|
| 3991 |
// Forcefully allow us to use an ID here by setting MSB. |
| 3992 |
auto subcomposite_chain = composite_chain; |
| 3993 |
subcomposite_chain.push_back(0x80000000u | id); |
| 3994 |
|
| 3995 |
if (!get<SPIRType>(subchain.basetype).array.empty()) |
| 3996 |
subchain.array_stride = get_decoration(subchain.basetype, DecorationArrayStride); |
| 3997 |
|
| 3998 |
write_access_chain(subchain, value, subcomposite_chain); |
| 3999 |
end_scope(); |
| 4000 |
} |
| 4001 |
|
| 4002 |
void CompilerHLSL::write_access_chain_struct(const SPIRAccessChain &chain, uint32_t value, |
| 4003 |
const SmallVector<uint32_t> &composite_chain) |
| 4004 |
{ |
| 4005 |
auto &type = get<SPIRType>(chain.basetype); |
| 4006 |
uint32_t member_count = uint32_t(type.member_types.size()); |
| 4007 |
auto subchain = chain; |
| 4008 |
|
| 4009 |
auto subcomposite_chain = composite_chain; |
| 4010 |
subcomposite_chain.push_back(0); |
| 4011 |
|
| 4012 |
for (uint32_t i = 0; i < member_count; i++) |
| 4013 |
{ |
| 4014 |
uint32_t offset = type_struct_member_offset(type, i); |
| 4015 |
subchain.static_index = chain.static_index + offset; |
| 4016 |
subchain.basetype = type.member_types[i]; |
| 4017 |
|
| 4018 |
subchain.matrix_stride = 0; |
| 4019 |
subchain.array_stride = 0; |
| 4020 |
subchain.row_major_matrix = false; |
| 4021 |
|
| 4022 |
auto &member_type = get<SPIRType>(subchain.basetype); |
| 4023 |
if (member_type.columns > 1) |
| 4024 |
{ |
| 4025 |
subchain.matrix_stride = type_struct_member_matrix_stride(type, i); |
| 4026 |
subchain.row_major_matrix = has_member_decoration(type.self, i, DecorationRowMajor); |
| 4027 |
} |
| 4028 |
|
| 4029 |
if (!member_type.array.empty()) |
| 4030 |
subchain.array_stride = type_struct_member_array_stride(type, i); |
| 4031 |
|
| 4032 |
subcomposite_chain.back() = i; |
| 4033 |
write_access_chain(subchain, value, subcomposite_chain); |
| 4034 |
} |
| 4035 |
} |
| 4036 |
|
| 4037 |
string CompilerHLSL::write_access_chain_value(uint32_t value, const SmallVector<uint32_t> &composite_chain, |
| 4038 |
bool enclose) |
| 4039 |
{ |
| 4040 |
string ret; |
| 4041 |
if (composite_chain.empty()) |
| 4042 |
ret = to_expression(value); |
| 4043 |
else |
| 4044 |
{ |
| 4045 |
AccessChainMeta meta; |
| 4046 |
ret = access_chain_internal(value, composite_chain.data(), uint32_t(composite_chain.size()), |
| 4047 |
ACCESS_CHAIN_INDEX_IS_LITERAL_BIT | ACCESS_CHAIN_LITERAL_MSB_FORCE_ID, &meta); |
| 4048 |
} |
| 4049 |
|
| 4050 |
if (enclose) |
| 4051 |
ret = enclose_expression(ret); |
| 4052 |
return ret; |
| 4053 |
} |
| 4054 |
|
| 4055 |
void CompilerHLSL::write_access_chain(const SPIRAccessChain &chain, uint32_t value, |
| 4056 |
const SmallVector<uint32_t> &composite_chain) |
| 4057 |
{ |
| 4058 |
auto &type = get<SPIRType>(chain.basetype); |
| 4059 |
|
| 4060 |
// Make sure we trigger a read of the constituents in the access chain. |
| 4061 |
track_expression_read(chain.self); |
| 4062 |
|
| 4063 |
if (has_decoration(chain.self, DecorationNonUniformEXT)) |
| 4064 |
propagate_nonuniform_qualifier(chain.self); |
| 4065 |
|
| 4066 |
SPIRType target_type; |
| 4067 |
target_type.basetype = SPIRType::UInt; |
| 4068 |
target_type.vecsize = type.vecsize; |
| 4069 |
target_type.columns = type.columns; |
| 4070 |
|
| 4071 |
if (!type.array.empty()) |
| 4072 |
{ |
| 4073 |
write_access_chain_array(chain, value, composite_chain); |
| 4074 |
register_write(chain.self); |
| 4075 |
return; |
| 4076 |
} |
| 4077 |
else if (type.basetype == SPIRType::Struct) |
| 4078 |
{ |
| 4079 |
write_access_chain_struct(chain, value, composite_chain); |
| 4080 |
register_write(chain.self); |
| 4081 |
return; |
| 4082 |
} |
| 4083 |
else if (type.width != 32 && !hlsl_options.enable_16bit_types) |
| 4084 |
SPIRV_CROSS_THROW("Writing types other than 32-bit to RWByteAddressBuffer not yet supported, unless SM 6.2 and " |
| 4085 |
"native 16-bit types are enabled."); |
| 4086 |
|
| 4087 |
bool templated_store = hlsl_options.shader_model >= 62; |
| 4088 |
|
| 4089 |
string template_expr; |
| 4090 |
if (templated_store) |
| 4091 |
template_expr = join("<", type_to_glsl(type), ">"); |
| 4092 |
|
| 4093 |
if (type.columns == 1 && !chain.row_major_matrix) |
| 4094 |
{ |
| 4095 |
const char *store_op = nullptr; |
| 4096 |
switch (type.vecsize) |
| 4097 |
{ |
| 4098 |
case 1: |
| 4099 |
store_op = "Store"; |
| 4100 |
break; |
| 4101 |
case 2: |
| 4102 |
store_op = "Store2"; |
| 4103 |
break; |
| 4104 |
case 3: |
| 4105 |
store_op = "Store3"; |
| 4106 |
break; |
| 4107 |
case 4: |
| 4108 |
store_op = "Store4"; |
| 4109 |
break; |
| 4110 |
default: |
| 4111 |
SPIRV_CROSS_THROW("Unknown vector size."); |
| 4112 |
} |
| 4113 |
|
| 4114 |
auto store_expr = write_access_chain_value(value, composite_chain, false); |
| 4115 |
|
| 4116 |
if (!templated_store) |
| 4117 |
{ |
| 4118 |
auto bitcast_op = bitcast_glsl_op(target_type, type); |
| 4119 |
if (!bitcast_op.empty()) |
| 4120 |
store_expr = join(bitcast_op, "(", store_expr, ")"); |
| 4121 |
} |
| 4122 |
else |
| 4123 |
store_op = "Store"; |
| 4124 |
statement(chain.base, ".", store_op, template_expr, "(", chain.dynamic_index, chain.static_index, ", ", |
| 4125 |
store_expr, ");"); |
| 4126 |
} |
| 4127 |
else if (type.columns == 1) |
| 4128 |
{ |
| 4129 |
if (templated_store) |
| 4130 |
{ |
| 4131 |
auto scalar_type = type; |
| 4132 |
scalar_type.vecsize = 1; |
| 4133 |
scalar_type.columns = 1; |
| 4134 |
template_expr = join("<", type_to_glsl(scalar_type), ">"); |
| 4135 |
} |
| 4136 |
|
| 4137 |
// Strided store. |
| 4138 |
for (uint32_t r = 0; r < type.vecsize; r++) |
| 4139 |
{ |
| 4140 |
auto store_expr = write_access_chain_value(value, composite_chain, true); |
| 4141 |
if (type.vecsize > 1) |
| 4142 |
{ |
| 4143 |
store_expr += "."; |
| 4144 |
store_expr += index_to_swizzle(r); |
| 4145 |
} |
| 4146 |
remove_duplicate_swizzle(store_expr); |
| 4147 |
|
| 4148 |
if (!templated_store) |
| 4149 |
{ |
| 4150 |
auto bitcast_op = bitcast_glsl_op(target_type, type); |
| 4151 |
if (!bitcast_op.empty()) |
| 4152 |
store_expr = join(bitcast_op, "(", store_expr, ")"); |
| 4153 |
} |
| 4154 |
|
| 4155 |
statement(chain.base, ".Store", template_expr, "(", chain.dynamic_index, |
| 4156 |
chain.static_index + chain.matrix_stride * r, ", ", store_expr, ");"); |
| 4157 |
} |
| 4158 |
} |
| 4159 |
else if (!chain.row_major_matrix) |
| 4160 |
{ |
| 4161 |
const char *store_op = nullptr; |
| 4162 |
switch (type.vecsize) |
| 4163 |
{ |
| 4164 |
case 1: |
| 4165 |
store_op = "Store"; |
| 4166 |
break; |
| 4167 |
case 2: |
| 4168 |
store_op = "Store2"; |
| 4169 |
break; |
| 4170 |
case 3: |
| 4171 |
store_op = "Store3"; |
| 4172 |
break; |
| 4173 |
case 4: |
| 4174 |
store_op = "Store4"; |
| 4175 |
break; |
| 4176 |
default: |
| 4177 |
SPIRV_CROSS_THROW("Unknown vector size."); |
| 4178 |
} |
| 4179 |
|
| 4180 |
if (templated_store) |
| 4181 |
{ |
| 4182 |
store_op = "Store"; |
| 4183 |
auto vector_type = type; |
| 4184 |
vector_type.columns = 1; |
| 4185 |
template_expr = join("<", type_to_glsl(vector_type), ">"); |
| 4186 |
} |
| 4187 |
|
| 4188 |
for (uint32_t c = 0; c < type.columns; c++) |
| 4189 |
{ |
| 4190 |
auto store_expr = join(write_access_chain_value(value, composite_chain, true), "[", c, "]"); |
| 4191 |
|
| 4192 |
if (!templated_store) |
| 4193 |
{ |
| 4194 |
auto bitcast_op = bitcast_glsl_op(target_type, type); |
| 4195 |
if (!bitcast_op.empty()) |
| 4196 |
store_expr = join(bitcast_op, "(", store_expr, ")"); |
| 4197 |
} |
| 4198 |
|
| 4199 |
statement(chain.base, ".", store_op, template_expr, "(", chain.dynamic_index, |
| 4200 |
chain.static_index + c * chain.matrix_stride, ", ", store_expr, ");"); |
| 4201 |
} |
| 4202 |
} |
| 4203 |
else |
| 4204 |
{ |
| 4205 |
if (templated_store) |
| 4206 |
{ |
| 4207 |
auto scalar_type = type; |
| 4208 |
scalar_type.vecsize = 1; |
| 4209 |
scalar_type.columns = 1; |
| 4210 |
template_expr = join("<", type_to_glsl(scalar_type), ">"); |
| 4211 |
} |
| 4212 |
|
| 4213 |
for (uint32_t r = 0; r < type.vecsize; r++) |
| 4214 |
{ |
| 4215 |
for (uint32_t c = 0; c < type.columns; c++) |
| 4216 |
{ |
| 4217 |
auto store_expr = |
| 4218 |
join(write_access_chain_value(value, composite_chain, true), "[", c, "].", index_to_swizzle(r)); |
| 4219 |
remove_duplicate_swizzle(store_expr); |
| 4220 |
auto bitcast_op = bitcast_glsl_op(target_type, type); |
| 4221 |
if (!bitcast_op.empty()) |
| 4222 |
store_expr = join(bitcast_op, "(", store_expr, ")"); |
| 4223 |
statement(chain.base, ".Store", template_expr, "(", chain.dynamic_index, |
| 4224 |
chain.static_index + c * (type.width / 8) + r * chain.matrix_stride, ", ", store_expr, ");"); |
| 4225 |
} |
| 4226 |
} |
| 4227 |
} |
| 4228 |
|
| 4229 |
register_write(chain.self); |
| 4230 |
} |
| 4231 |
|
| 4232 |
void CompilerHLSL::emit_store(const Instruction &instruction) |
| 4233 |
{ |
| 4234 |
auto ops = stream(instruction); |
| 4235 |
auto *chain = maybe_get<SPIRAccessChain>(ops[0]); |
| 4236 |
if (chain) |
| 4237 |
write_access_chain(*chain, ops[1], {}); |
| 4238 |
else |
| 4239 |
CompilerGLSL::emit_instruction(instruction); |
| 4240 |
} |
| 4241 |
|
| 4242 |
void CompilerHLSL::emit_access_chain(const Instruction &instruction) |
| 4243 |
{ |
| 4244 |
auto ops = stream(instruction); |
| 4245 |
uint32_t length = instruction.length; |
| 4246 |
|
| 4247 |
bool need_byte_access_chain = false; |
| 4248 |
auto &type = expression_type(ops[2]); |
| 4249 |
const auto *chain = maybe_get<SPIRAccessChain>(ops[2]); |
| 4250 |
|
| 4251 |
if (chain) |
| 4252 |
{ |
| 4253 |
// Keep tacking on an existing access chain. |
| 4254 |
need_byte_access_chain = true; |
| 4255 |
} |
| 4256 |
else if (type.storage == StorageClassStorageBuffer || has_decoration(type.self, DecorationBufferBlock)) |
| 4257 |
{ |
| 4258 |
// If we are starting to poke into an SSBO, we are dealing with ByteAddressBuffers, and we need |
| 4259 |
// to emit SPIRAccessChain rather than a plain SPIRExpression. |
| 4260 |
uint32_t chain_arguments = length - 3; |
| 4261 |
if (chain_arguments > type.array.size()) |
| 4262 |
need_byte_access_chain = true; |
| 4263 |
} |
| 4264 |
|
| 4265 |
if (need_byte_access_chain) |
| 4266 |
{ |
| 4267 |
// If we have a chain variable, we are already inside the SSBO, and any array type will refer to arrays within a block, |
| 4268 |
// and not array of SSBO. |
| 4269 |
uint32_t to_plain_buffer_length = chain ? 0u : static_cast<uint32_t>(type.array.size()); |
| 4270 |
|
| 4271 |
auto *backing_variable = maybe_get_backing_variable(ops[2]); |
| 4272 |
|
| 4273 |
string base; |
| 4274 |
if (to_plain_buffer_length != 0) |
| 4275 |
base = access_chain(ops[2], &ops[3], to_plain_buffer_length, get<SPIRType>(ops[0])); |
| 4276 |
else if (chain) |
| 4277 |
base = chain->base; |
| 4278 |
else |
| 4279 |
base = to_expression(ops[2]); |
| 4280 |
|
| 4281 |
// Start traversing type hierarchy at the proper non-pointer types. |
| 4282 |
auto *basetype = &get_pointee_type(type); |
| 4283 |
|
| 4284 |
// Traverse the type hierarchy down to the actual buffer types. |
| 4285 |
for (uint32_t i = 0; i < to_plain_buffer_length; i++) |
| 4286 |
{ |
| 4287 |
assert(basetype->parent_type); |
| 4288 |
basetype = &get<SPIRType>(basetype->parent_type); |
| 4289 |
} |
| 4290 |
|
| 4291 |
uint32_t matrix_stride = 0; |
| 4292 |
uint32_t array_stride = 0; |
| 4293 |
bool row_major_matrix = false; |
| 4294 |
|
| 4295 |
// Inherit matrix information. |
| 4296 |
if (chain) |
| 4297 |
{ |
| 4298 |
matrix_stride = chain->matrix_stride; |
| 4299 |
row_major_matrix = chain->row_major_matrix; |
| 4300 |
array_stride = chain->array_stride; |
| 4301 |
} |
| 4302 |
|
| 4303 |
auto offsets = flattened_access_chain_offset(*basetype, &ops[3 + to_plain_buffer_length], |
| 4304 |
length - 3 - to_plain_buffer_length, 0, 1, &row_major_matrix, |
| 4305 |
&matrix_stride, &array_stride); |
| 4306 |
|
| 4307 |
auto &e = set<SPIRAccessChain>(ops[1], ops[0], type.storage, base, offsets.first, offsets.second); |
| 4308 |
e.row_major_matrix = row_major_matrix; |
| 4309 |
e.matrix_stride = matrix_stride; |
| 4310 |
e.array_stride = array_stride; |
| 4311 |
e.immutable = should_forward(ops[2]); |
| 4312 |
e.loaded_from = backing_variable ? backing_variable->self : ID(0); |
| 4313 |
|
| 4314 |
if (chain) |
| 4315 |
{ |
| 4316 |
e.dynamic_index += chain->dynamic_index; |
| 4317 |
e.static_index += chain->static_index; |
| 4318 |
} |
| 4319 |
|
| 4320 |
for (uint32_t i = 2; i < length; i++) |
| 4321 |
{ |
| 4322 |
inherit_expression_dependencies(ops[1], ops[i]); |
| 4323 |
add_implied_read_expression(e, ops[i]); |
| 4324 |
} |
| 4325 |
|
| 4326 |
if (has_decoration(ops[1], DecorationNonUniformEXT)) |
| 4327 |
propagate_nonuniform_qualifier(ops[1]); |
| 4328 |
} |
| 4329 |
else |
| 4330 |
{ |
| 4331 |
CompilerGLSL::emit_instruction(instruction); |
| 4332 |
} |
| 4333 |
} |
| 4334 |
|
| 4335 |
void CompilerHLSL::emit_atomic(const uint32_t *ops, uint32_t length, spv::Op op) |
| 4336 |
{ |
| 4337 |
const char *atomic_op = nullptr; |
| 4338 |
|
| 4339 |
string value_expr; |
| 4340 |
if (op != OpAtomicIDecrement && op != OpAtomicIIncrement && op != OpAtomicLoad && op != OpAtomicStore) |
| 4341 |
value_expr = to_expression(ops[op == OpAtomicCompareExchange ? 6 : 5]); |
| 4342 |
|
| 4343 |
bool is_atomic_store = false; |
| 4344 |
|
| 4345 |
switch (op) |
| 4346 |
{ |
| 4347 |
case OpAtomicIIncrement: |
| 4348 |
atomic_op = "InterlockedAdd"; |
| 4349 |
value_expr = "1"; |
| 4350 |
break; |
| 4351 |
|
| 4352 |
case OpAtomicIDecrement: |
| 4353 |
atomic_op = "InterlockedAdd"; |
| 4354 |
value_expr = "-1"; |
| 4355 |
break; |
| 4356 |
|
| 4357 |
case OpAtomicLoad: |
| 4358 |
atomic_op = "InterlockedAdd"; |
| 4359 |
value_expr = "0"; |
| 4360 |
break; |
| 4361 |
|
| 4362 |
case OpAtomicISub: |
| 4363 |
atomic_op = "InterlockedAdd"; |
| 4364 |
value_expr = join("-", enclose_expression(value_expr)); |
| 4365 |
break; |
| 4366 |
|
| 4367 |
case OpAtomicSMin: |
| 4368 |
case OpAtomicUMin: |
| 4369 |
atomic_op = "InterlockedMin"; |
| 4370 |
break; |
| 4371 |
|
| 4372 |
case OpAtomicSMax: |
| 4373 |
case OpAtomicUMax: |
| 4374 |
atomic_op = "InterlockedMax"; |
| 4375 |
break; |
| 4376 |
|
| 4377 |
case OpAtomicAnd: |
| 4378 |
atomic_op = "InterlockedAnd"; |
| 4379 |
break; |
| 4380 |
|
| 4381 |
case OpAtomicOr: |
| 4382 |
atomic_op = "InterlockedOr"; |
| 4383 |
break; |
| 4384 |
|
| 4385 |
case OpAtomicXor: |
| 4386 |
atomic_op = "InterlockedXor"; |
| 4387 |
break; |
| 4388 |
|
| 4389 |
case OpAtomicIAdd: |
| 4390 |
atomic_op = "InterlockedAdd"; |
| 4391 |
break; |
| 4392 |
|
| 4393 |
case OpAtomicExchange: |
| 4394 |
atomic_op = "InterlockedExchange"; |
| 4395 |
break; |
| 4396 |
|
| 4397 |
case OpAtomicStore: |
| 4398 |
atomic_op = "InterlockedExchange"; |
| 4399 |
is_atomic_store = true; |
| 4400 |
break; |
| 4401 |
|
| 4402 |
case OpAtomicCompareExchange: |
| 4403 |
if (length < 8) |
| 4404 |
SPIRV_CROSS_THROW("Not enough data for opcode."); |
| 4405 |
atomic_op = "InterlockedCompareExchange"; |
| 4406 |
value_expr = join(to_expression(ops[7]), ", ", value_expr); |
| 4407 |
break; |
| 4408 |
|
| 4409 |
default: |
| 4410 |
SPIRV_CROSS_THROW("Unknown atomic opcode."); |
| 4411 |
} |
| 4412 |
|
| 4413 |
if (is_atomic_store) |
| 4414 |
{ |
| 4415 |
auto &data_type = expression_type(ops[0]); |
| 4416 |
auto *chain = maybe_get<SPIRAccessChain>(ops[0]); |
| 4417 |
|
| 4418 |
auto &tmp_id = extra_sub_expressions[ops[0]]; |
| 4419 |
if (!tmp_id) |
| 4420 |
{ |
| 4421 |
tmp_id = ir.increase_bound_by(1); |
| 4422 |
emit_uninitialized_temporary_expression(get_pointee_type(data_type).self, tmp_id); |
| 4423 |
} |
| 4424 |
|
| 4425 |
if (data_type.storage == StorageClassImage || !chain) |
| 4426 |
{ |
| 4427 |
statement(atomic_op, "(", to_expression(ops[0]), ", ", to_expression(ops[3]), ", ", to_expression(tmp_id), |
| 4428 |
");"); |
| 4429 |
} |
| 4430 |
else |
| 4431 |
{ |
| 4432 |
// RWByteAddress buffer is always uint in its underlying type. |
| 4433 |
statement(chain->base, ".", atomic_op, "(", chain->dynamic_index, chain->static_index, ", ", |
| 4434 |
to_expression(ops[3]), ", ", to_expression(tmp_id), ");"); |
| 4435 |
} |
| 4436 |
} |
| 4437 |
else |
| 4438 |
{ |
| 4439 |
uint32_t result_type = ops[0]; |
| 4440 |
uint32_t id = ops[1]; |
| 4441 |
forced_temporaries.insert(ops[1]); |
| 4442 |
|
| 4443 |
auto &type = get<SPIRType>(result_type); |
| 4444 |
statement(variable_decl(type, to_name(id)), ";"); |
| 4445 |
|
| 4446 |
auto &data_type = expression_type(ops[2]); |
| 4447 |
auto *chain = maybe_get<SPIRAccessChain>(ops[2]); |
| 4448 |
SPIRType::BaseType expr_type; |
| 4449 |
if (data_type.storage == StorageClassImage || !chain) |
| 4450 |
{ |
| 4451 |
statement(atomic_op, "(", to_expression(ops[2]), ", ", value_expr, ", ", to_name(id), ");"); |
| 4452 |
expr_type = data_type.basetype; |
| 4453 |
} |
| 4454 |
else |
| 4455 |
{ |
| 4456 |
// RWByteAddress buffer is always uint in its underlying type. |
| 4457 |
expr_type = SPIRType::UInt; |
| 4458 |
statement(chain->base, ".", atomic_op, "(", chain->dynamic_index, chain->static_index, ", ", value_expr, |
| 4459 |
", ", to_name(id), ");"); |
| 4460 |
} |
| 4461 |
|
| 4462 |
auto expr = bitcast_expression(type, expr_type, to_name(id)); |
| 4463 |
set<SPIRExpression>(id, expr, result_type, true); |
| 4464 |
} |
| 4465 |
flush_all_atomic_capable_variables(); |
| 4466 |
} |
| 4467 |
|
| 4468 |
void CompilerHLSL::emit_subgroup_op(const Instruction &i) |
| 4469 |
{ |
| 4470 |
if (hlsl_options.shader_model < 60) |
| 4471 |
SPIRV_CROSS_THROW("Wave ops requires SM 6.0 or higher."); |
| 4472 |
|
| 4473 |
const uint32_t *ops = stream(i); |
| 4474 |
auto op = static_cast<Op>(i.op); |
| 4475 |
|
| 4476 |
uint32_t result_type = ops[0]; |
| 4477 |
uint32_t id = ops[1]; |
| 4478 |
|
| 4479 |
auto scope = static_cast<Scope>(evaluate_constant_u32(ops[2])); |
| 4480 |
if (scope != ScopeSubgroup) |
| 4481 |
SPIRV_CROSS_THROW("Only subgroup scope is supported."); |
| 4482 |
|
| 4483 |
const auto make_inclusive_Sum = [&](const string &expr) -> string { |
| 4484 |
return join(expr, " + ", to_expression(ops[4])); |
| 4485 |
}; |
| 4486 |
|
| 4487 |
const auto make_inclusive_Product = [&](const string &expr) -> string { |
| 4488 |
return join(expr, " * ", to_expression(ops[4])); |
| 4489 |
}; |
| 4490 |
|
| 4491 |
// If we need to do implicit bitcasts, make sure we do it with the correct type. |
| 4492 |
uint32_t integer_width = get_integer_width_for_instruction(i); |
| 4493 |
auto int_type = to_signed_basetype(integer_width); |
| 4494 |
auto uint_type = to_unsigned_basetype(integer_width); |
| 4495 |
|
| 4496 |
#define make_inclusive_BitAnd(expr) "" |
| 4497 |
#define make_inclusive_BitOr(expr) "" |
| 4498 |
#define make_inclusive_BitXor(expr) "" |
| 4499 |
#define make_inclusive_Min(expr) "" |
| 4500 |
#define make_inclusive_Max(expr) "" |
| 4501 |
|
| 4502 |
switch (op) |
| 4503 |
{ |
| 4504 |
case OpGroupNonUniformElect: |
| 4505 |
emit_op(result_type, id, "WaveIsFirstLane()", true); |
| 4506 |
break; |
| 4507 |
|
| 4508 |
case OpGroupNonUniformBroadcast: |
| 4509 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "WaveReadLaneAt"); |
| 4510 |
break; |
| 4511 |
|
| 4512 |
case OpGroupNonUniformBroadcastFirst: |
| 4513 |
emit_unary_func_op(result_type, id, ops[3], "WaveReadLaneFirst"); |
| 4514 |
break; |
| 4515 |
|
| 4516 |
case OpGroupNonUniformBallot: |
| 4517 |
emit_unary_func_op(result_type, id, ops[3], "WaveActiveBallot"); |
| 4518 |
break; |
| 4519 |
|
| 4520 |
case OpGroupNonUniformInverseBallot: |
| 4521 |
SPIRV_CROSS_THROW("Cannot trivially implement InverseBallot in HLSL."); |
| 4522 |
break; |
| 4523 |
|
| 4524 |
case OpGroupNonUniformBallotBitExtract: |
| 4525 |
SPIRV_CROSS_THROW("Cannot trivially implement BallotBitExtract in HLSL."); |
| 4526 |
break; |
| 4527 |
|
| 4528 |
case OpGroupNonUniformBallotFindLSB: |
| 4529 |
SPIRV_CROSS_THROW("Cannot trivially implement BallotFindLSB in HLSL."); |
| 4530 |
break; |
| 4531 |
|
| 4532 |
case OpGroupNonUniformBallotFindMSB: |
| 4533 |
SPIRV_CROSS_THROW("Cannot trivially implement BallotFindMSB in HLSL."); |
| 4534 |
break; |
| 4535 |
|
| 4536 |
case OpGroupNonUniformBallotBitCount: |
| 4537 |
{ |
| 4538 |
auto operation = static_cast<GroupOperation>(ops[3]); |
| 4539 |
if (operation == GroupOperationReduce) |
| 4540 |
{ |
| 4541 |
bool forward = should_forward(ops[4]); |
| 4542 |
auto left = join("countbits(", to_enclosed_expression(ops[4]), ".x) + countbits(", |
| 4543 |
to_enclosed_expression(ops[4]), ".y)"); |
| 4544 |
auto right = join("countbits(", to_enclosed_expression(ops[4]), ".z) + countbits(", |
| 4545 |
to_enclosed_expression(ops[4]), ".w)"); |
| 4546 |
emit_op(result_type, id, join(left, " + ", right), forward); |
| 4547 |
inherit_expression_dependencies(id, ops[4]); |
| 4548 |
} |
| 4549 |
else if (operation == GroupOperationInclusiveScan) |
| 4550 |
SPIRV_CROSS_THROW("Cannot trivially implement BallotBitCount Inclusive Scan in HLSL."); |
| 4551 |
else if (operation == GroupOperationExclusiveScan) |
| 4552 |
SPIRV_CROSS_THROW("Cannot trivially implement BallotBitCount Exclusive Scan in HLSL."); |
| 4553 |
else |
| 4554 |
SPIRV_CROSS_THROW("Invalid BitCount operation."); |
| 4555 |
break; |
| 4556 |
} |
| 4557 |
|
| 4558 |
case OpGroupNonUniformShuffle: |
| 4559 |
SPIRV_CROSS_THROW("Cannot trivially implement Shuffle in HLSL."); |
| 4560 |
case OpGroupNonUniformShuffleXor: |
| 4561 |
SPIRV_CROSS_THROW("Cannot trivially implement ShuffleXor in HLSL."); |
| 4562 |
case OpGroupNonUniformShuffleUp: |
| 4563 |
SPIRV_CROSS_THROW("Cannot trivially implement ShuffleUp in HLSL."); |
| 4564 |
case OpGroupNonUniformShuffleDown: |
| 4565 |
SPIRV_CROSS_THROW("Cannot trivially implement ShuffleDown in HLSL."); |
| 4566 |
|
| 4567 |
case OpGroupNonUniformAll: |
| 4568 |
emit_unary_func_op(result_type, id, ops[3], "WaveActiveAllTrue"); |
| 4569 |
break; |
| 4570 |
|
| 4571 |
case OpGroupNonUniformAny: |
| 4572 |
emit_unary_func_op(result_type, id, ops[3], "WaveActiveAnyTrue"); |
| 4573 |
break; |
| 4574 |
|
| 4575 |
case OpGroupNonUniformAllEqual: |
| 4576 |
{ |
| 4577 |
auto &type = get<SPIRType>(result_type); |
| 4578 |
emit_unary_func_op(result_type, id, ops[3], |
| 4579 |
type.basetype == SPIRType::Boolean ? "WaveActiveAllEqualBool" : "WaveActiveAllEqual"); |
| 4580 |
break; |
| 4581 |
} |
| 4582 |
|
| 4583 |
// clang-format off |
| 4584 |
#define HLSL_GROUP_OP(op, hlsl_op, supports_scan) \ |
| 4585 |
case OpGroupNonUniform##op: \ |
| 4586 |
{ \ |
| 4587 |
auto operation = static_cast<GroupOperation>(ops[3]); \ |
| 4588 |
if (operation == GroupOperationReduce) \ |
| 4589 |
emit_unary_func_op(result_type, id, ops[4], "WaveActive" #hlsl_op); \ |
| 4590 |
else if (operation == GroupOperationInclusiveScan && supports_scan) \ |
| 4591 |
{ \ |
| 4592 |
bool forward = should_forward(ops[4]); \ |
| 4593 |
emit_op(result_type, id, make_inclusive_##hlsl_op (join("WavePrefix" #hlsl_op, "(", to_expression(ops[4]), ")")), forward); \ |
| 4594 |
inherit_expression_dependencies(id, ops[4]); \ |
| 4595 |
} \ |
| 4596 |
else if (operation == GroupOperationExclusiveScan && supports_scan) \ |
| 4597 |
emit_unary_func_op(result_type, id, ops[4], "WavePrefix" #hlsl_op); \ |
| 4598 |
else if (operation == GroupOperationClusteredReduce) \ |
| 4599 |
SPIRV_CROSS_THROW("Cannot trivially implement ClusteredReduce in HLSL."); \ |
| 4600 |
else \ |
| 4601 |
SPIRV_CROSS_THROW("Invalid group operation."); \ |
| 4602 |
break; \ |
| 4603 |
} |
| 4604 |
|
| 4605 |
#define HLSL_GROUP_OP_CAST(op, hlsl_op, type) \ |
| 4606 |
case OpGroupNonUniform##op: \ |
| 4607 |
{ \ |
| 4608 |
auto operation = static_cast<GroupOperation>(ops[3]); \ |
| 4609 |
if (operation == GroupOperationReduce) \ |
| 4610 |
emit_unary_func_op_cast(result_type, id, ops[4], "WaveActive" #hlsl_op, type, type); \ |
| 4611 |
else \ |
| 4612 |
SPIRV_CROSS_THROW("Invalid group operation."); \ |
| 4613 |
break; \ |
| 4614 |
} |
| 4615 |
|
| 4616 |
HLSL_GROUP_OP(FAdd, Sum, true) |
| 4617 |
HLSL_GROUP_OP(FMul, Product, true) |
| 4618 |
HLSL_GROUP_OP(FMin, Min, false) |
| 4619 |
HLSL_GROUP_OP(FMax, Max, false) |
| 4620 |
HLSL_GROUP_OP(IAdd, Sum, true) |
| 4621 |
HLSL_GROUP_OP(IMul, Product, true) |
| 4622 |
HLSL_GROUP_OP_CAST(SMin, Min, int_type) |
| 4623 |
HLSL_GROUP_OP_CAST(SMax, Max, int_type) |
| 4624 |
HLSL_GROUP_OP_CAST(UMin, Min, uint_type) |
| 4625 |
HLSL_GROUP_OP_CAST(UMax, Max, uint_type) |
| 4626 |
HLSL_GROUP_OP(BitwiseAnd, BitAnd, false) |
| 4627 |
HLSL_GROUP_OP(BitwiseOr, BitOr, false) |
| 4628 |
HLSL_GROUP_OP(BitwiseXor, BitXor, false) |
| 4629 |
|
| 4630 |
#undef HLSL_GROUP_OP |
| 4631 |
#undef HLSL_GROUP_OP_CAST |
| 4632 |
// clang-format on |
| 4633 |
|
| 4634 |
case OpGroupNonUniformQuadSwap: |
| 4635 |
{ |
| 4636 |
uint32_t direction = evaluate_constant_u32(ops[4]); |
| 4637 |
if (direction == 0) |
| 4638 |
emit_unary_func_op(result_type, id, ops[3], "QuadReadAcrossX"); |
| 4639 |
else if (direction == 1) |
| 4640 |
emit_unary_func_op(result_type, id, ops[3], "QuadReadAcrossY"); |
| 4641 |
else if (direction == 2) |
| 4642 |
emit_unary_func_op(result_type, id, ops[3], "QuadReadAcrossDiagonal"); |
| 4643 |
else |
| 4644 |
SPIRV_CROSS_THROW("Invalid quad swap direction."); |
| 4645 |
break; |
| 4646 |
} |
| 4647 |
|
| 4648 |
case OpGroupNonUniformQuadBroadcast: |
| 4649 |
{ |
| 4650 |
emit_binary_func_op(result_type, id, ops[3], ops[4], "QuadReadLaneAt"); |
| 4651 |
break; |
| 4652 |
} |
| 4653 |
|
| 4654 |
default: |
| 4655 |
SPIRV_CROSS_THROW("Invalid opcode for subgroup."); |
| 4656 |
} |
| 4657 |
|
| 4658 |
register_control_dependent_expression(id); |
| 4659 |
} |
| 4660 |
|
| 4661 |
void CompilerHLSL::emit_instruction(const Instruction &instruction) |
| 4662 |
{ |
| 4663 |
auto ops = stream(instruction); |
| 4664 |
auto opcode = static_cast<Op>(instruction.op); |
| 4665 |
|
| 4666 |
#define HLSL_BOP(op) emit_binary_op(ops[0], ops[1], ops[2], ops[3], #op) |
| 4667 |
#define HLSL_BOP_CAST(op, type) \ |
| 4668 |
emit_binary_op_cast(ops[0], ops[1], ops[2], ops[3], #op, type, opcode_is_sign_invariant(opcode)) |
| 4669 |
#define HLSL_UOP(op) emit_unary_op(ops[0], ops[1], ops[2], #op) |
| 4670 |
#define HLSL_QFOP(op) emit_quaternary_func_op(ops[0], ops[1], ops[2], ops[3], ops[4], ops[5], #op) |
| 4671 |
#define HLSL_TFOP(op) emit_trinary_func_op(ops[0], ops[1], ops[2], ops[3], ops[4], #op) |
| 4672 |
#define HLSL_BFOP(op) emit_binary_func_op(ops[0], ops[1], ops[2], ops[3], #op) |
| 4673 |
#define HLSL_BFOP_CAST(op, type) \ |
| 4674 |
emit_binary_func_op_cast(ops[0], ops[1], ops[2], ops[3], #op, type, opcode_is_sign_invariant(opcode)) |
| 4675 |
#define HLSL_BFOP(op) emit_binary_func_op(ops[0], ops[1], ops[2], ops[3], #op) |
| 4676 |
#define HLSL_UFOP(op) emit_unary_func_op(ops[0], ops[1], ops[2], #op) |
| 4677 |
|
| 4678 |
// If we need to do implicit bitcasts, make sure we do it with the correct type. |
| 4679 |
uint32_t integer_width = get_integer_width_for_instruction(instruction); |
| 4680 |
auto int_type = to_signed_basetype(integer_width); |
| 4681 |
auto uint_type = to_unsigned_basetype(integer_width); |
| 4682 |
|
| 4683 |
switch (opcode) |
| 4684 |
{ |
| 4685 |
case OpAccessChain: |
| 4686 |
case OpInBoundsAccessChain: |
| 4687 |
{ |
| 4688 |
emit_access_chain(instruction); |
| 4689 |
break; |
| 4690 |
} |
| 4691 |
case OpBitcast: |
| 4692 |
{ |
| 4693 |
auto bitcast_type = get_bitcast_type(ops[0], ops[2]); |
| 4694 |
if (bitcast_type == CompilerHLSL::TypeNormal) |
| 4695 |
CompilerGLSL::emit_instruction(instruction); |
| 4696 |
else |
| 4697 |
{ |
| 4698 |
if (!requires_uint2_packing) |
| 4699 |
{ |
| 4700 |
requires_uint2_packing = true; |
| 4701 |
force_recompile(); |
| 4702 |
} |
| 4703 |
|
| 4704 |
if (bitcast_type == CompilerHLSL::TypePackUint2x32) |
| 4705 |
emit_unary_func_op(ops[0], ops[1], ops[2], "SPIRV_Cross_packUint2x32"); |
| 4706 |
else |
| 4707 |
emit_unary_func_op(ops[0], ops[1], ops[2], "SPIRV_Cross_unpackUint2x32"); |
| 4708 |
} |
| 4709 |
|
| 4710 |
break; |
| 4711 |
} |
| 4712 |
|
| 4713 |
case OpStore: |
| 4714 |
{ |
| 4715 |
emit_store(instruction); |
| 4716 |
break; |
| 4717 |
} |
| 4718 |
|
| 4719 |
case OpLoad: |
| 4720 |
{ |
| 4721 |
emit_load(instruction); |
| 4722 |
break; |
| 4723 |
} |
| 4724 |
|
| 4725 |
case OpMatrixTimesVector: |
| 4726 |
{ |
| 4727 |
// Matrices are kept in a transposed state all the time, flip multiplication order always. |
| 4728 |
emit_binary_func_op(ops[0], ops[1], ops[3], ops[2], "mul"); |
| 4729 |
break; |
| 4730 |
} |
| 4731 |
|
| 4732 |
case OpVectorTimesMatrix: |
| 4733 |
{ |
| 4734 |
// Matrices are kept in a transposed state all the time, flip multiplication order always. |
| 4735 |
emit_binary_func_op(ops[0], ops[1], ops[3], ops[2], "mul"); |
| 4736 |
break; |
| 4737 |
} |
| 4738 |
|
| 4739 |
case OpMatrixTimesMatrix: |
| 4740 |
{ |
| 4741 |
// Matrices are kept in a transposed state all the time, flip multiplication order always. |
| 4742 |
emit_binary_func_op(ops[0], ops[1], ops[3], ops[2], "mul"); |
| 4743 |
break; |
| 4744 |
} |
| 4745 |
|
| 4746 |
case OpOuterProduct: |
| 4747 |
{ |
| 4748 |
uint32_t result_type = ops[0]; |
| 4749 |
uint32_t id = ops[1]; |
| 4750 |
uint32_t a = ops[2]; |
| 4751 |
uint32_t b = ops[3]; |
| 4752 |
|
| 4753 |
auto &type = get<SPIRType>(result_type); |
| 4754 |
string expr = type_to_glsl_constructor(type); |
| 4755 |
expr += "("; |
| 4756 |
for (uint32_t col = 0; col < type.columns; col++) |
| 4757 |
{ |
| 4758 |
expr += to_enclosed_expression(a); |
| 4759 |
expr += " * "; |
| 4760 |
expr += to_extract_component_expression(b, col); |
| 4761 |
if (col + 1 < type.columns) |
| 4762 |
expr += ", "; |
| 4763 |
} |
| 4764 |
expr += ")"; |
| 4765 |
emit_op(result_type, id, expr, should_forward(a) && should_forward(b)); |
| 4766 |
inherit_expression_dependencies(id, a); |
| 4767 |
inherit_expression_dependencies(id, b); |
| 4768 |
break; |
| 4769 |
} |
| 4770 |
|
| 4771 |
case OpFMod: |
| 4772 |
{ |
| 4773 |
if (!requires_op_fmod) |
| 4774 |
{ |
| 4775 |
requires_op_fmod = true; |
| 4776 |
force_recompile(); |
| 4777 |
} |
| 4778 |
CompilerGLSL::emit_instruction(instruction); |
| 4779 |
break; |
| 4780 |
} |
| 4781 |
|
| 4782 |
case OpFRem: |
| 4783 |
emit_binary_func_op(ops[0], ops[1], ops[2], ops[3], "fmod"); |
| 4784 |
break; |
| 4785 |
|
| 4786 |
case OpImage: |
| 4787 |
{ |
| 4788 |
uint32_t result_type = ops[0]; |
| 4789 |
uint32_t id = ops[1]; |
| 4790 |
auto *combined = maybe_get<SPIRCombinedImageSampler>(ops[2]); |
| 4791 |
|
| 4792 |
if (combined) |
| 4793 |
{ |
| 4794 |
auto &e = emit_op(result_type, id, to_expression(combined->image), true, true); |
| 4795 |
auto *var = maybe_get_backing_variable(combined->image); |
| 4796 |
if (var) |
| 4797 |
e.loaded_from = var->self; |
| 4798 |
} |
| 4799 |
else |
| 4800 |
{ |
| 4801 |
auto &e = emit_op(result_type, id, to_expression(ops[2]), true, true); |
| 4802 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 4803 |
if (var) |
| 4804 |
e.loaded_from = var->self; |
| 4805 |
} |
| 4806 |
break; |
| 4807 |
} |
| 4808 |
|
| 4809 |
case OpDPdx: |
| 4810 |
HLSL_UFOP(ddx); |
| 4811 |
register_control_dependent_expression(ops[1]); |
| 4812 |
break; |
| 4813 |
|
| 4814 |
case OpDPdy: |
| 4815 |
HLSL_UFOP(ddy); |
| 4816 |
register_control_dependent_expression(ops[1]); |
| 4817 |
break; |
| 4818 |
|
| 4819 |
case OpDPdxFine: |
| 4820 |
HLSL_UFOP(ddx_fine); |
| 4821 |
register_control_dependent_expression(ops[1]); |
| 4822 |
break; |
| 4823 |
|
| 4824 |
case OpDPdyFine: |
| 4825 |
HLSL_UFOP(ddy_fine); |
| 4826 |
register_control_dependent_expression(ops[1]); |
| 4827 |
break; |
| 4828 |
|
| 4829 |
case OpDPdxCoarse: |
| 4830 |
HLSL_UFOP(ddx_coarse); |
| 4831 |
register_control_dependent_expression(ops[1]); |
| 4832 |
break; |
| 4833 |
|
| 4834 |
case OpDPdyCoarse: |
| 4835 |
HLSL_UFOP(ddy_coarse); |
| 4836 |
register_control_dependent_expression(ops[1]); |
| 4837 |
break; |
| 4838 |
|
| 4839 |
case OpFwidth: |
| 4840 |
case OpFwidthCoarse: |
| 4841 |
case OpFwidthFine: |
| 4842 |
HLSL_UFOP(fwidth); |
| 4843 |
register_control_dependent_expression(ops[1]); |
| 4844 |
break; |
| 4845 |
|
| 4846 |
case OpLogicalNot: |
| 4847 |
{ |
| 4848 |
auto result_type = ops[0]; |
| 4849 |
auto id = ops[1]; |
| 4850 |
auto &type = get<SPIRType>(result_type); |
| 4851 |
|
| 4852 |
if (type.vecsize > 1) |
| 4853 |
emit_unrolled_unary_op(result_type, id, ops[2], "!"); |
| 4854 |
else |
| 4855 |
HLSL_UOP(!); |
| 4856 |
break; |
| 4857 |
} |
| 4858 |
|
| 4859 |
case OpIEqual: |
| 4860 |
{ |
| 4861 |
auto result_type = ops[0]; |
| 4862 |
auto id = ops[1]; |
| 4863 |
|
| 4864 |
if (expression_type(ops[2]).vecsize > 1) |
| 4865 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "==", false, SPIRType::Unknown); |
| 4866 |
else |
| 4867 |
HLSL_BOP_CAST(==, int_type); |
| 4868 |
break; |
| 4869 |
} |
| 4870 |
|
| 4871 |
case OpLogicalEqual: |
| 4872 |
case OpFOrdEqual: |
| 4873 |
case OpFUnordEqual: |
| 4874 |
{ |
| 4875 |
// HLSL != operator is unordered. |
| 4876 |
// https://docs.microsoft.com/en-us/windows/win32/direct3d10/d3d10-graphics-programming-guide-resources-float-rules. |
| 4877 |
// isnan() is apparently implemented as x != x as well. |
| 4878 |
// We cannot implement UnordEqual as !(OrdNotEqual), as HLSL cannot express OrdNotEqual. |
| 4879 |
// HACK: FUnordEqual will be implemented as FOrdEqual. |
| 4880 |
|
| 4881 |
auto result_type = ops[0]; |
| 4882 |
auto id = ops[1]; |
| 4883 |
|
| 4884 |
if (expression_type(ops[2]).vecsize > 1) |
| 4885 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "==", false, SPIRType::Unknown); |
| 4886 |
else |
| 4887 |
HLSL_BOP(==); |
| 4888 |
break; |
| 4889 |
} |
| 4890 |
|
| 4891 |
case OpINotEqual: |
| 4892 |
{ |
| 4893 |
auto result_type = ops[0]; |
| 4894 |
auto id = ops[1]; |
| 4895 |
|
| 4896 |
if (expression_type(ops[2]).vecsize > 1) |
| 4897 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "!=", false, SPIRType::Unknown); |
| 4898 |
else |
| 4899 |
HLSL_BOP_CAST(!=, int_type); |
| 4900 |
break; |
| 4901 |
} |
| 4902 |
|
| 4903 |
case OpLogicalNotEqual: |
| 4904 |
case OpFOrdNotEqual: |
| 4905 |
case OpFUnordNotEqual: |
| 4906 |
{ |
| 4907 |
// HLSL != operator is unordered. |
| 4908 |
// https://docs.microsoft.com/en-us/windows/win32/direct3d10/d3d10-graphics-programming-guide-resources-float-rules. |
| 4909 |
// isnan() is apparently implemented as x != x as well. |
| 4910 |
|
| 4911 |
// FIXME: FOrdNotEqual cannot be implemented in a crisp and simple way here. |
| 4912 |
// We would need to do something like not(UnordEqual), but that cannot be expressed either. |
| 4913 |
// Adding a lot of NaN checks would be a breaking change from perspective of performance. |
| 4914 |
// SPIR-V will generally use isnan() checks when this even matters. |
| 4915 |
// HACK: FOrdNotEqual will be implemented as FUnordEqual. |
| 4916 |
|
| 4917 |
auto result_type = ops[0]; |
| 4918 |
auto id = ops[1]; |
| 4919 |
|
| 4920 |
if (expression_type(ops[2]).vecsize > 1) |
| 4921 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "!=", false, SPIRType::Unknown); |
| 4922 |
else |
| 4923 |
HLSL_BOP(!=); |
| 4924 |
break; |
| 4925 |
} |
| 4926 |
|
| 4927 |
case OpUGreaterThan: |
| 4928 |
case OpSGreaterThan: |
| 4929 |
{ |
| 4930 |
auto result_type = ops[0]; |
| 4931 |
auto id = ops[1]; |
| 4932 |
auto type = opcode == OpUGreaterThan ? uint_type : int_type; |
| 4933 |
|
| 4934 |
if (expression_type(ops[2]).vecsize > 1) |
| 4935 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], ">", false, type); |
| 4936 |
else |
| 4937 |
HLSL_BOP_CAST(>, type); |
| 4938 |
break; |
| 4939 |
} |
| 4940 |
|
| 4941 |
case OpFOrdGreaterThan: |
| 4942 |
{ |
| 4943 |
auto result_type = ops[0]; |
| 4944 |
auto id = ops[1]; |
| 4945 |
|
| 4946 |
if (expression_type(ops[2]).vecsize > 1) |
| 4947 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], ">", false, SPIRType::Unknown); |
| 4948 |
else |
| 4949 |
HLSL_BOP(>); |
| 4950 |
break; |
| 4951 |
} |
| 4952 |
|
| 4953 |
case OpFUnordGreaterThan: |
| 4954 |
{ |
| 4955 |
auto result_type = ops[0]; |
| 4956 |
auto id = ops[1]; |
| 4957 |
|
| 4958 |
if (expression_type(ops[2]).vecsize > 1) |
| 4959 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "<=", true, SPIRType::Unknown); |
| 4960 |
else |
| 4961 |
CompilerGLSL::emit_instruction(instruction); |
| 4962 |
break; |
| 4963 |
} |
| 4964 |
|
| 4965 |
case OpUGreaterThanEqual: |
| 4966 |
case OpSGreaterThanEqual: |
| 4967 |
{ |
| 4968 |
auto result_type = ops[0]; |
| 4969 |
auto id = ops[1]; |
| 4970 |
|
| 4971 |
auto type = opcode == OpUGreaterThanEqual ? uint_type : int_type; |
| 4972 |
if (expression_type(ops[2]).vecsize > 1) |
| 4973 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], ">=", false, type); |
| 4974 |
else |
| 4975 |
HLSL_BOP_CAST(>=, type); |
| 4976 |
break; |
| 4977 |
} |
| 4978 |
|
| 4979 |
case OpFOrdGreaterThanEqual: |
| 4980 |
{ |
| 4981 |
auto result_type = ops[0]; |
| 4982 |
auto id = ops[1]; |
| 4983 |
|
| 4984 |
if (expression_type(ops[2]).vecsize > 1) |
| 4985 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], ">=", false, SPIRType::Unknown); |
| 4986 |
else |
| 4987 |
HLSL_BOP(>=); |
| 4988 |
break; |
| 4989 |
} |
| 4990 |
|
| 4991 |
case OpFUnordGreaterThanEqual: |
| 4992 |
{ |
| 4993 |
auto result_type = ops[0]; |
| 4994 |
auto id = ops[1]; |
| 4995 |
|
| 4996 |
if (expression_type(ops[2]).vecsize > 1) |
| 4997 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "<", true, SPIRType::Unknown); |
| 4998 |
else |
| 4999 |
CompilerGLSL::emit_instruction(instruction); |
| 5000 |
break; |
| 5001 |
} |
| 5002 |
|
| 5003 |
case OpULessThan: |
| 5004 |
case OpSLessThan: |
| 5005 |
{ |
| 5006 |
auto result_type = ops[0]; |
| 5007 |
auto id = ops[1]; |
| 5008 |
|
| 5009 |
auto type = opcode == OpULessThan ? uint_type : int_type; |
| 5010 |
if (expression_type(ops[2]).vecsize > 1) |
| 5011 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "<", false, type); |
| 5012 |
else |
| 5013 |
HLSL_BOP_CAST(<, type); |
| 5014 |
break; |
| 5015 |
} |
| 5016 |
|
| 5017 |
case OpFOrdLessThan: |
| 5018 |
{ |
| 5019 |
auto result_type = ops[0]; |
| 5020 |
auto id = ops[1]; |
| 5021 |
|
| 5022 |
if (expression_type(ops[2]).vecsize > 1) |
| 5023 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "<", false, SPIRType::Unknown); |
| 5024 |
else |
| 5025 |
HLSL_BOP(<); |
| 5026 |
break; |
| 5027 |
} |
| 5028 |
|
| 5029 |
case OpFUnordLessThan: |
| 5030 |
{ |
| 5031 |
auto result_type = ops[0]; |
| 5032 |
auto id = ops[1]; |
| 5033 |
|
| 5034 |
if (expression_type(ops[2]).vecsize > 1) |
| 5035 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], ">=", true, SPIRType::Unknown); |
| 5036 |
else |
| 5037 |
CompilerGLSL::emit_instruction(instruction); |
| 5038 |
break; |
| 5039 |
} |
| 5040 |
|
| 5041 |
case OpULessThanEqual: |
| 5042 |
case OpSLessThanEqual: |
| 5043 |
{ |
| 5044 |
auto result_type = ops[0]; |
| 5045 |
auto id = ops[1]; |
| 5046 |
|
| 5047 |
auto type = opcode == OpULessThanEqual ? uint_type : int_type; |
| 5048 |
if (expression_type(ops[2]).vecsize > 1) |
| 5049 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "<=", false, type); |
| 5050 |
else |
| 5051 |
HLSL_BOP_CAST(<=, type); |
| 5052 |
break; |
| 5053 |
} |
| 5054 |
|
| 5055 |
case OpFOrdLessThanEqual: |
| 5056 |
{ |
| 5057 |
auto result_type = ops[0]; |
| 5058 |
auto id = ops[1]; |
| 5059 |
|
| 5060 |
if (expression_type(ops[2]).vecsize > 1) |
| 5061 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], "<=", false, SPIRType::Unknown); |
| 5062 |
else |
| 5063 |
HLSL_BOP(<=); |
| 5064 |
break; |
| 5065 |
} |
| 5066 |
|
| 5067 |
case OpFUnordLessThanEqual: |
| 5068 |
{ |
| 5069 |
auto result_type = ops[0]; |
| 5070 |
auto id = ops[1]; |
| 5071 |
|
| 5072 |
if (expression_type(ops[2]).vecsize > 1) |
| 5073 |
emit_unrolled_binary_op(result_type, id, ops[2], ops[3], ">", true, SPIRType::Unknown); |
| 5074 |
else |
| 5075 |
CompilerGLSL::emit_instruction(instruction); |
| 5076 |
break; |
| 5077 |
} |
| 5078 |
|
| 5079 |
case OpImageQueryLod: |
| 5080 |
emit_texture_op(instruction, false); |
| 5081 |
break; |
| 5082 |
|
| 5083 |
case OpImageQuerySizeLod: |
| 5084 |
{ |
| 5085 |
auto result_type = ops[0]; |
| 5086 |
auto id = ops[1]; |
| 5087 |
|
| 5088 |
require_texture_query_variant(ops[2]); |
| 5089 |
auto dummy_samples_levels = join(get_fallback_name(id), "_dummy_parameter"); |
| 5090 |
statement("uint ", dummy_samples_levels, ";"); |
| 5091 |
|
| 5092 |
auto expr = join("SPIRV_Cross_textureSize(", to_expression(ops[2]), ", ", |
| 5093 |
bitcast_expression(SPIRType::UInt, ops[3]), ", ", dummy_samples_levels, ")"); |
| 5094 |
|
| 5095 |
auto &restype = get<SPIRType>(ops[0]); |
| 5096 |
expr = bitcast_expression(restype, SPIRType::UInt, expr); |
| 5097 |
emit_op(result_type, id, expr, true); |
| 5098 |
break; |
| 5099 |
} |
| 5100 |
|
| 5101 |
case OpImageQuerySize: |
| 5102 |
{ |
| 5103 |
auto result_type = ops[0]; |
| 5104 |
auto id = ops[1]; |
| 5105 |
|
| 5106 |
require_texture_query_variant(ops[2]); |
| 5107 |
bool uav = expression_type(ops[2]).image.sampled == 2; |
| 5108 |
|
| 5109 |
if (const auto *var = maybe_get_backing_variable(ops[2])) |
| 5110 |
if (hlsl_options.nonwritable_uav_texture_as_srv && has_decoration(var->self, DecorationNonWritable)) |
| 5111 |
uav = false; |
| 5112 |
|
| 5113 |
auto dummy_samples_levels = join(get_fallback_name(id), "_dummy_parameter"); |
| 5114 |
statement("uint ", dummy_samples_levels, ";"); |
| 5115 |
|
| 5116 |
string expr; |
| 5117 |
if (uav) |
| 5118 |
expr = join("SPIRV_Cross_imageSize(", to_expression(ops[2]), ", ", dummy_samples_levels, ")"); |
| 5119 |
else |
| 5120 |
expr = join("SPIRV_Cross_textureSize(", to_expression(ops[2]), ", 0u, ", dummy_samples_levels, ")"); |
| 5121 |
|
| 5122 |
auto &restype = get<SPIRType>(ops[0]); |
| 5123 |
expr = bitcast_expression(restype, SPIRType::UInt, expr); |
| 5124 |
emit_op(result_type, id, expr, true); |
| 5125 |
break; |
| 5126 |
} |
| 5127 |
|
| 5128 |
case OpImageQuerySamples: |
| 5129 |
case OpImageQueryLevels: |
| 5130 |
{ |
| 5131 |
auto result_type = ops[0]; |
| 5132 |
auto id = ops[1]; |
| 5133 |
|
| 5134 |
require_texture_query_variant(ops[2]); |
| 5135 |
bool uav = expression_type(ops[2]).image.sampled == 2; |
| 5136 |
if (opcode == OpImageQueryLevels && uav) |
| 5137 |
SPIRV_CROSS_THROW("Cannot query levels for UAV images."); |
| 5138 |
|
| 5139 |
if (const auto *var = maybe_get_backing_variable(ops[2])) |
| 5140 |
if (hlsl_options.nonwritable_uav_texture_as_srv && has_decoration(var->self, DecorationNonWritable)) |
| 5141 |
uav = false; |
| 5142 |
|
| 5143 |
// Keep it simple and do not emit special variants to make this look nicer ... |
| 5144 |
// This stuff is barely, if ever, used. |
| 5145 |
forced_temporaries.insert(id); |
| 5146 |
auto &type = get<SPIRType>(result_type); |
| 5147 |
statement(variable_decl(type, to_name(id)), ";"); |
| 5148 |
|
| 5149 |
if (uav) |
| 5150 |
statement("SPIRV_Cross_imageSize(", to_expression(ops[2]), ", ", to_name(id), ");"); |
| 5151 |
else |
| 5152 |
statement("SPIRV_Cross_textureSize(", to_expression(ops[2]), ", 0u, ", to_name(id), ");"); |
| 5153 |
|
| 5154 |
auto &restype = get<SPIRType>(ops[0]); |
| 5155 |
auto expr = bitcast_expression(restype, SPIRType::UInt, to_name(id)); |
| 5156 |
set<SPIRExpression>(id, expr, result_type, true); |
| 5157 |
break; |
| 5158 |
} |
| 5159 |
|
| 5160 |
case OpImageRead: |
| 5161 |
{ |
| 5162 |
uint32_t result_type = ops[0]; |
| 5163 |
uint32_t id = ops[1]; |
| 5164 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 5165 |
auto &type = expression_type(ops[2]); |
| 5166 |
bool subpass_data = type.image.dim == DimSubpassData; |
| 5167 |
bool pure = false; |
| 5168 |
|
| 5169 |
string imgexpr; |
| 5170 |
|
| 5171 |
if (subpass_data) |
| 5172 |
{ |
| 5173 |
if (hlsl_options.shader_model < 40) |
| 5174 |
SPIRV_CROSS_THROW("Subpass loads are not supported in HLSL shader model 2/3."); |
| 5175 |
|
| 5176 |
// Similar to GLSL, implement subpass loads using texelFetch. |
| 5177 |
if (type.image.ms) |
| 5178 |
{ |
| 5179 |
uint32_t operands = ops[4]; |
| 5180 |
if (operands != ImageOperandsSampleMask || instruction.length != 6) |
| 5181 |
SPIRV_CROSS_THROW("Multisampled image used in OpImageRead, but unexpected operand mask was used."); |
| 5182 |
uint32_t sample = ops[5]; |
| 5183 |
imgexpr = join(to_expression(ops[2]), ".Load(int2(gl_FragCoord.xy), ", to_expression(sample), ")"); |
| 5184 |
} |
| 5185 |
else |
| 5186 |
imgexpr = join(to_expression(ops[2]), ".Load(int3(int2(gl_FragCoord.xy), 0))"); |
| 5187 |
|
| 5188 |
pure = true; |
| 5189 |
} |
| 5190 |
else |
| 5191 |
{ |
| 5192 |
imgexpr = join(to_expression(ops[2]), "[", to_expression(ops[3]), "]"); |
| 5193 |
// The underlying image type in HLSL depends on the image format, unlike GLSL, where all images are "vec4", |
| 5194 |
// except that the underlying type changes how the data is interpreted. |
| 5195 |
|
| 5196 |
bool force_srv = |
| 5197 |
hlsl_options.nonwritable_uav_texture_as_srv && var && has_decoration(var->self, DecorationNonWritable); |
| 5198 |
pure = force_srv; |
| 5199 |
|
| 5200 |
if (var && !subpass_data && !force_srv) |
| 5201 |
imgexpr = remap_swizzle(get<SPIRType>(result_type), |
| 5202 |
image_format_to_components(get<SPIRType>(var->basetype).image.format), imgexpr); |
| 5203 |
} |
| 5204 |
|
| 5205 |
if (var && var->forwardable) |
| 5206 |
{ |
| 5207 |
bool forward = forced_temporaries.find(id) == end(forced_temporaries); |
| 5208 |
auto &e = emit_op(result_type, id, imgexpr, forward); |
| 5209 |
|
| 5210 |
if (!pure) |
| 5211 |
{ |
| 5212 |
e.loaded_from = var->self; |
| 5213 |
if (forward) |
| 5214 |
var->dependees.push_back(id); |
| 5215 |
} |
| 5216 |
} |
| 5217 |
else |
| 5218 |
emit_op(result_type, id, imgexpr, false); |
| 5219 |
|
| 5220 |
inherit_expression_dependencies(id, ops[2]); |
| 5221 |
if (type.image.ms) |
| 5222 |
inherit_expression_dependencies(id, ops[5]); |
| 5223 |
break; |
| 5224 |
} |
| 5225 |
|
| 5226 |
case OpImageWrite: |
| 5227 |
{ |
| 5228 |
auto *var = maybe_get_backing_variable(ops[0]); |
| 5229 |
|
| 5230 |
// The underlying image type in HLSL depends on the image format, unlike GLSL, where all images are "vec4", |
| 5231 |
// except that the underlying type changes how the data is interpreted. |
| 5232 |
auto value_expr = to_expression(ops[2]); |
| 5233 |
if (var) |
| 5234 |
{ |
| 5235 |
auto &type = get<SPIRType>(var->basetype); |
| 5236 |
auto narrowed_type = get<SPIRType>(type.image.type); |
| 5237 |
narrowed_type.vecsize = image_format_to_components(type.image.format); |
| 5238 |
value_expr = remap_swizzle(narrowed_type, expression_type(ops[2]).vecsize, value_expr); |
| 5239 |
} |
| 5240 |
|
| 5241 |
statement(to_expression(ops[0]), "[", to_expression(ops[1]), "] = ", value_expr, ";"); |
| 5242 |
if (var && variable_storage_is_aliased(*var)) |
| 5243 |
flush_all_aliased_variables(); |
| 5244 |
break; |
| 5245 |
} |
| 5246 |
|
| 5247 |
case OpImageTexelPointer: |
| 5248 |
{ |
| 5249 |
uint32_t result_type = ops[0]; |
| 5250 |
uint32_t id = ops[1]; |
| 5251 |
|
| 5252 |
auto expr = to_expression(ops[2]); |
| 5253 |
if (has_decoration(id, DecorationNonUniformEXT) || has_decoration(ops[2], DecorationNonUniformEXT)) |
| 5254 |
convert_non_uniform_expression(expression_type(ops[2]), expr); |
| 5255 |
expr += join("[", to_expression(ops[3]), "]"); |
| 5256 |
|
| 5257 |
auto &e = set<SPIRExpression>(id, expr, result_type, true); |
| 5258 |
|
| 5259 |
// When using the pointer, we need to know which variable it is actually loaded from. |
| 5260 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 5261 |
e.loaded_from = var ? var->self : ID(0); |
| 5262 |
inherit_expression_dependencies(id, ops[3]); |
| 5263 |
break; |
| 5264 |
} |
| 5265 |
|
| 5266 |
case OpAtomicCompareExchange: |
| 5267 |
case OpAtomicExchange: |
| 5268 |
case OpAtomicISub: |
| 5269 |
case OpAtomicSMin: |
| 5270 |
case OpAtomicUMin: |
| 5271 |
case OpAtomicSMax: |
| 5272 |
case OpAtomicUMax: |
| 5273 |
case OpAtomicAnd: |
| 5274 |
case OpAtomicOr: |
| 5275 |
case OpAtomicXor: |
| 5276 |
case OpAtomicIAdd: |
| 5277 |
case OpAtomicIIncrement: |
| 5278 |
case OpAtomicIDecrement: |
| 5279 |
case OpAtomicLoad: |
| 5280 |
case OpAtomicStore: |
| 5281 |
{ |
| 5282 |
emit_atomic(ops, instruction.length, opcode); |
| 5283 |
break; |
| 5284 |
} |
| 5285 |
|
| 5286 |
case OpControlBarrier: |
| 5287 |
case OpMemoryBarrier: |
| 5288 |
{ |
| 5289 |
uint32_t memory; |
| 5290 |
uint32_t semantics; |
| 5291 |
|
| 5292 |
if (opcode == OpMemoryBarrier) |
| 5293 |
{ |
| 5294 |
memory = evaluate_constant_u32(ops[0]); |
| 5295 |
semantics = evaluate_constant_u32(ops[1]); |
| 5296 |
} |
| 5297 |
else |
| 5298 |
{ |
| 5299 |
memory = evaluate_constant_u32(ops[1]); |
| 5300 |
semantics = evaluate_constant_u32(ops[2]); |
| 5301 |
} |
| 5302 |
|
| 5303 |
if (memory == ScopeSubgroup) |
| 5304 |
{ |
| 5305 |
// No Wave-barriers in HLSL. |
| 5306 |
break; |
| 5307 |
} |
| 5308 |
|
| 5309 |
// We only care about these flags, acquire/release and friends are not relevant to GLSL. |
| 5310 |
semantics = mask_relevant_memory_semantics(semantics); |
| 5311 |
|
| 5312 |
if (opcode == OpMemoryBarrier) |
| 5313 |
{ |
| 5314 |
// If we are a memory barrier, and the next instruction is a control barrier, check if that memory barrier |
| 5315 |
// does what we need, so we avoid redundant barriers. |
| 5316 |
const Instruction *next = get_next_instruction_in_block(instruction); |
| 5317 |
if (next && next->op == OpControlBarrier) |
| 5318 |
{ |
| 5319 |
auto *next_ops = stream(*next); |
| 5320 |
uint32_t next_memory = evaluate_constant_u32(next_ops[1]); |
| 5321 |
uint32_t next_semantics = evaluate_constant_u32(next_ops[2]); |
| 5322 |
next_semantics = mask_relevant_memory_semantics(next_semantics); |
| 5323 |
|
| 5324 |
// There is no "just execution barrier" in HLSL. |
| 5325 |
// If there are no memory semantics for next instruction, we will imply group shared memory is synced. |
| 5326 |
if (next_semantics == 0) |
| 5327 |
next_semantics = MemorySemanticsWorkgroupMemoryMask; |
| 5328 |
|
| 5329 |
bool memory_scope_covered = false; |
| 5330 |
if (next_memory == memory) |
| 5331 |
memory_scope_covered = true; |
| 5332 |
else if (next_semantics == MemorySemanticsWorkgroupMemoryMask) |
| 5333 |
{ |
| 5334 |
// If we only care about workgroup memory, either Device or Workgroup scope is fine, |
| 5335 |
// scope does not have to match. |
| 5336 |
if ((next_memory == ScopeDevice || next_memory == ScopeWorkgroup) && |
| 5337 |
(memory == ScopeDevice || memory == ScopeWorkgroup)) |
| 5338 |
{ |
| 5339 |
memory_scope_covered = true; |
| 5340 |
} |
| 5341 |
} |
| 5342 |
else if (memory == ScopeWorkgroup && next_memory == ScopeDevice) |
| 5343 |
{ |
| 5344 |
// The control barrier has device scope, but the memory barrier just has workgroup scope. |
| 5345 |
memory_scope_covered = true; |
| 5346 |
} |
| 5347 |
|
| 5348 |
// If we have the same memory scope, and all memory types are covered, we're good. |
| 5349 |
if (memory_scope_covered && (semantics & next_semantics) == semantics) |
| 5350 |
break; |
| 5351 |
} |
| 5352 |
} |
| 5353 |
|
| 5354 |
// We are synchronizing some memory or syncing execution, |
| 5355 |
// so we cannot forward any loads beyond the memory barrier. |
| 5356 |
if (semantics || opcode == OpControlBarrier) |
| 5357 |
{ |
| 5358 |
assert(current_emitting_block); |
| 5359 |
flush_control_dependent_expressions(current_emitting_block->self); |
| 5360 |
flush_all_active_variables(); |
| 5361 |
} |
| 5362 |
|
| 5363 |
if (opcode == OpControlBarrier) |
| 5364 |
{ |
| 5365 |
// We cannot emit just execution barrier, for no memory semantics pick the cheapest option. |
| 5366 |
if (semantics == MemorySemanticsWorkgroupMemoryMask || semantics == 0) |
| 5367 |
statement("GroupMemoryBarrierWithGroupSync();"); |
| 5368 |
else if (semantics != 0 && (semantics & MemorySemanticsWorkgroupMemoryMask) == 0) |
| 5369 |
statement("DeviceMemoryBarrierWithGroupSync();"); |
| 5370 |
else |
| 5371 |
statement("AllMemoryBarrierWithGroupSync();"); |
| 5372 |
} |
| 5373 |
else |
| 5374 |
{ |
| 5375 |
if (semantics == MemorySemanticsWorkgroupMemoryMask) |
| 5376 |
statement("GroupMemoryBarrier();"); |
| 5377 |
else if (semantics != 0 && (semantics & MemorySemanticsWorkgroupMemoryMask) == 0) |
| 5378 |
statement("DeviceMemoryBarrier();"); |
| 5379 |
else |
| 5380 |
statement("AllMemoryBarrier();"); |
| 5381 |
} |
| 5382 |
break; |
| 5383 |
} |
| 5384 |
|
| 5385 |
case OpBitFieldInsert: |
| 5386 |
{ |
| 5387 |
if (!requires_bitfield_insert) |
| 5388 |
{ |
| 5389 |
requires_bitfield_insert = true; |
| 5390 |
force_recompile(); |
| 5391 |
} |
| 5392 |
|
| 5393 |
auto expr = join("SPIRV_Cross_bitfieldInsert(", to_expression(ops[2]), ", ", to_expression(ops[3]), ", ", |
| 5394 |
to_expression(ops[4]), ", ", to_expression(ops[5]), ")"); |
| 5395 |
|
| 5396 |
bool forward = |
| 5397 |
should_forward(ops[2]) && should_forward(ops[3]) && should_forward(ops[4]) && should_forward(ops[5]); |
| 5398 |
|
| 5399 |
auto &restype = get<SPIRType>(ops[0]); |
| 5400 |
expr = bitcast_expression(restype, SPIRType::UInt, expr); |
| 5401 |
emit_op(ops[0], ops[1], expr, forward); |
| 5402 |
break; |
| 5403 |
} |
| 5404 |
|
| 5405 |
case OpBitFieldSExtract: |
| 5406 |
case OpBitFieldUExtract: |
| 5407 |
{ |
| 5408 |
if (!requires_bitfield_extract) |
| 5409 |
{ |
| 5410 |
requires_bitfield_extract = true; |
| 5411 |
force_recompile(); |
| 5412 |
} |
| 5413 |
|
| 5414 |
if (opcode == OpBitFieldSExtract) |
| 5415 |
HLSL_TFOP(SPIRV_Cross_bitfieldSExtract); |
| 5416 |
else |
| 5417 |
HLSL_TFOP(SPIRV_Cross_bitfieldUExtract); |
| 5418 |
break; |
| 5419 |
} |
| 5420 |
|
| 5421 |
case OpBitCount: |
| 5422 |
{ |
| 5423 |
auto basetype = expression_type(ops[2]).basetype; |
| 5424 |
emit_unary_func_op_cast(ops[0], ops[1], ops[2], "countbits", basetype, basetype); |
| 5425 |
break; |
| 5426 |
} |
| 5427 |
|
| 5428 |
case OpBitReverse: |
| 5429 |
HLSL_UFOP(reversebits); |
| 5430 |
break; |
| 5431 |
|
| 5432 |
case OpArrayLength: |
| 5433 |
{ |
| 5434 |
auto *var = maybe_get<SPIRVariable>(ops[2]); |
| 5435 |
if (!var) |
| 5436 |
SPIRV_CROSS_THROW("Array length must point directly to an SSBO block."); |
| 5437 |
|
| 5438 |
auto &type = get<SPIRType>(var->basetype); |
| 5439 |
if (!has_decoration(type.self, DecorationBlock) && !has_decoration(type.self, DecorationBufferBlock)) |
| 5440 |
SPIRV_CROSS_THROW("Array length expression must point to a block type."); |
| 5441 |
|
| 5442 |
// This must be 32-bit uint, so we're good to go. |
| 5443 |
emit_uninitialized_temporary_expression(ops[0], ops[1]); |
| 5444 |
statement(to_expression(ops[2]), ".GetDimensions(", to_expression(ops[1]), ");"); |
| 5445 |
uint32_t offset = type_struct_member_offset(type, ops[3]); |
| 5446 |
uint32_t stride = type_struct_member_array_stride(type, ops[3]); |
| 5447 |
statement(to_expression(ops[1]), " = (", to_expression(ops[1]), " - ", offset, ") / ", stride, ";"); |
| 5448 |
break; |
| 5449 |
} |
| 5450 |
|
| 5451 |
case OpIsHelperInvocationEXT: |
| 5452 |
SPIRV_CROSS_THROW("helperInvocationEXT() is not supported in HLSL."); |
| 5453 |
|
| 5454 |
case OpBeginInvocationInterlockEXT: |
| 5455 |
case OpEndInvocationInterlockEXT: |
| 5456 |
if (hlsl_options.shader_model < 51) |
| 5457 |
SPIRV_CROSS_THROW("Rasterizer order views require Shader Model 5.1."); |
| 5458 |
break; // Nothing to do in the body |
| 5459 |
|
| 5460 |
default: |
| 5461 |
CompilerGLSL::emit_instruction(instruction); |
| 5462 |
break; |
| 5463 |
} |
| 5464 |
} |
| 5465 |
|
| 5466 |
void CompilerHLSL::require_texture_query_variant(uint32_t var_id) |
| 5467 |
{ |
| 5468 |
if (const auto *var = maybe_get_backing_variable(var_id)) |
| 5469 |
var_id = var->self; |
| 5470 |
|
| 5471 |
auto &type = expression_type(var_id); |
| 5472 |
bool uav = type.image.sampled == 2; |
| 5473 |
if (hlsl_options.nonwritable_uav_texture_as_srv && has_decoration(var_id, DecorationNonWritable)) |
| 5474 |
uav = false; |
| 5475 |
|
| 5476 |
uint32_t bit = 0; |
| 5477 |
switch (type.image.dim) |
| 5478 |
{ |
| 5479 |
case Dim1D: |
| 5480 |
bit = type.image.arrayed ? Query1DArray : Query1D; |
| 5481 |
break; |
| 5482 |
|
| 5483 |
case Dim2D: |
| 5484 |
if (type.image.ms) |
| 5485 |
bit = type.image.arrayed ? Query2DMSArray : Query2DMS; |
| 5486 |
else |
| 5487 |
bit = type.image.arrayed ? Query2DArray : Query2D; |
| 5488 |
break; |
| 5489 |
|
| 5490 |
case Dim3D: |
| 5491 |
bit = Query3D; |
| 5492 |
break; |
| 5493 |
|
| 5494 |
case DimCube: |
| 5495 |
bit = type.image.arrayed ? QueryCubeArray : QueryCube; |
| 5496 |
break; |
| 5497 |
|
| 5498 |
case DimBuffer: |
| 5499 |
bit = QueryBuffer; |
| 5500 |
break; |
| 5501 |
|
| 5502 |
default: |
| 5503 |
SPIRV_CROSS_THROW("Unsupported query type."); |
| 5504 |
} |
| 5505 |
|
| 5506 |
switch (get<SPIRType>(type.image.type).basetype) |
| 5507 |
{ |
| 5508 |
case SPIRType::Float: |
| 5509 |
bit += QueryTypeFloat; |
| 5510 |
break; |
| 5511 |
|
| 5512 |
case SPIRType::Int: |
| 5513 |
bit += QueryTypeInt; |
| 5514 |
break; |
| 5515 |
|
| 5516 |
case SPIRType::UInt: |
| 5517 |
bit += QueryTypeUInt; |
| 5518 |
break; |
| 5519 |
|
| 5520 |
default: |
| 5521 |
SPIRV_CROSS_THROW("Unsupported query type."); |
| 5522 |
} |
| 5523 |
|
| 5524 |
auto norm_state = image_format_to_normalized_state(type.image.format); |
| 5525 |
auto &variant = uav ? required_texture_size_variants |
| 5526 |
.uav[uint32_t(norm_state)][image_format_to_components(type.image.format) - 1] : |
| 5527 |
required_texture_size_variants.srv; |
| 5528 |
|
| 5529 |
uint64_t mask = 1ull << bit; |
| 5530 |
if ((variant & mask) == 0) |
| 5531 |
{ |
| 5532 |
force_recompile(); |
| 5533 |
variant |= mask; |
| 5534 |
} |
| 5535 |
} |
| 5536 |
|
| 5537 |
void CompilerHLSL::set_root_constant_layouts(std::vector<RootConstants> layout) |
| 5538 |
{ |
| 5539 |
root_constants_layout = move(layout); |
| 5540 |
} |
| 5541 |
|
| 5542 |
void CompilerHLSL::add_vertex_attribute_remap(const HLSLVertexAttributeRemap &vertex_attributes) |
| 5543 |
{ |
| 5544 |
remap_vertex_attributes.push_back(vertex_attributes); |
| 5545 |
} |
| 5546 |
|
| 5547 |
VariableID CompilerHLSL::remap_num_workgroups_builtin() |
| 5548 |
{ |
| 5549 |
update_active_builtins(); |
| 5550 |
|
| 5551 |
if (!active_input_builtins.get(BuiltInNumWorkgroups)) |
| 5552 |
return 0; |
| 5553 |
|
| 5554 |
// Create a new, fake UBO. |
| 5555 |
uint32_t offset = ir.increase_bound_by(4); |
| 5556 |
|
| 5557 |
uint32_t uint_type_id = offset; |
| 5558 |
uint32_t block_type_id = offset + 1; |
| 5559 |
uint32_t block_pointer_type_id = offset + 2; |
| 5560 |
uint32_t variable_id = offset + 3; |
| 5561 |
|
| 5562 |
SPIRType uint_type; |
| 5563 |
uint_type.basetype = SPIRType::UInt; |
| 5564 |
uint_type.width = 32; |
| 5565 |
uint_type.vecsize = 3; |
| 5566 |
uint_type.columns = 1; |
| 5567 |
set<SPIRType>(uint_type_id, uint_type); |
| 5568 |
|
| 5569 |
SPIRType block_type; |
| 5570 |
block_type.basetype = SPIRType::Struct; |
| 5571 |
block_type.member_types.push_back(uint_type_id); |
| 5572 |
set<SPIRType>(block_type_id, block_type); |
| 5573 |
set_decoration(block_type_id, DecorationBlock); |
| 5574 |
set_member_name(block_type_id, 0, "count"); |
| 5575 |
set_member_decoration(block_type_id, 0, DecorationOffset, 0); |
| 5576 |
|
| 5577 |
SPIRType block_pointer_type = block_type; |
| 5578 |
block_pointer_type.pointer = true; |
| 5579 |
block_pointer_type.storage = StorageClassUniform; |
| 5580 |
block_pointer_type.parent_type = block_type_id; |
| 5581 |
auto &ptr_type = set<SPIRType>(block_pointer_type_id, block_pointer_type); |
| 5582 |
|
| 5583 |
// Preserve self. |
| 5584 |
ptr_type.self = block_type_id; |
| 5585 |
|
| 5586 |
set<SPIRVariable>(variable_id, block_pointer_type_id, StorageClassUniform); |
| 5587 |
ir.meta[variable_id].decoration.alias = "SPIRV_Cross_NumWorkgroups"; |
| 5588 |
|
| 5589 |
num_workgroups_builtin = variable_id; |
| 5590 |
return variable_id; |
| 5591 |
} |
| 5592 |
|
| 5593 |
void CompilerHLSL::set_resource_binding_flags(HLSLBindingFlags flags) |
| 5594 |
{ |
| 5595 |
resource_binding_flags = flags; |
| 5596 |
} |
| 5597 |
|
| 5598 |
void CompilerHLSL::validate_shader_model() |
| 5599 |
{ |
| 5600 |
// Check for nonuniform qualifier. |
| 5601 |
// Instead of looping over all decorations to find this, just look at capabilities. |
| 5602 |
for (auto &cap : ir.declared_capabilities) |
| 5603 |
{ |
| 5604 |
switch (cap) |
| 5605 |
{ |
| 5606 |
case CapabilityShaderNonUniformEXT: |
| 5607 |
case CapabilityRuntimeDescriptorArrayEXT: |
| 5608 |
if (hlsl_options.shader_model < 51) |
| 5609 |
SPIRV_CROSS_THROW( |
| 5610 |
"Shader model 5.1 or higher is required to use bindless resources or NonUniformResourceIndex."); |
| 5611 |
default: |
| 5612 |
break; |
| 5613 |
} |
| 5614 |
} |
| 5615 |
|
| 5616 |
if (ir.addressing_model != AddressingModelLogical) |
| 5617 |
SPIRV_CROSS_THROW("Only Logical addressing model can be used with HLSL."); |
| 5618 |
|
| 5619 |
if (hlsl_options.enable_16bit_types && hlsl_options.shader_model < 62) |
| 5620 |
SPIRV_CROSS_THROW("Need at least shader model 6.2 when enabling native 16-bit type support."); |
| 5621 |
} |
| 5622 |
|
| 5623 |
string CompilerHLSL::compile() |
| 5624 |
{ |
| 5625 |
ir.fixup_reserved_names(); |
| 5626 |
|
| 5627 |
// Do not deal with ES-isms like precision, older extensions and such. |
| 5628 |
options.es = false; |
| 5629 |
options.version = 450; |
| 5630 |
options.vulkan_semantics = true; |
| 5631 |
backend.float_literal_suffix = true; |
| 5632 |
backend.double_literal_suffix = false; |
| 5633 |
backend.long_long_literal_suffix = true; |
| 5634 |
backend.uint32_t_literal_suffix = true; |
| 5635 |
backend.int16_t_literal_suffix = ""; |
| 5636 |
backend.uint16_t_literal_suffix = "u"; |
| 5637 |
backend.basic_int_type = "int"; |
| 5638 |
backend.basic_uint_type = "uint"; |
| 5639 |
backend.demote_literal = "discard"; |
| 5640 |
backend.boolean_mix_function = ""; |
| 5641 |
backend.swizzle_is_function = false; |
| 5642 |
backend.shared_is_implied = true; |
| 5643 |
backend.unsized_array_supported = true; |
| 5644 |
backend.explicit_struct_type = false; |
| 5645 |
backend.use_initializer_list = true; |
| 5646 |
backend.use_constructor_splatting = false; |
| 5647 |
backend.can_swizzle_scalar = true; |
| 5648 |
backend.can_declare_struct_inline = false; |
| 5649 |
backend.can_declare_arrays_inline = false; |
| 5650 |
backend.can_return_array = false; |
| 5651 |
backend.nonuniform_qualifier = "NonUniformResourceIndex"; |
| 5652 |
backend.support_case_fallthrough = false; |
| 5653 |
|
| 5654 |
fixup_type_alias(); |
| 5655 |
reorder_type_alias(); |
| 5656 |
build_function_control_flow_graphs_and_analyze(); |
| 5657 |
validate_shader_model(); |
| 5658 |
update_active_builtins(); |
| 5659 |
analyze_image_and_sampler_usage(); |
| 5660 |
analyze_interlocked_resource_usage(); |
| 5661 |
|
| 5662 |
// Subpass input needs SV_Position. |
| 5663 |
if (need_subpass_input) |
| 5664 |
active_input_builtins.set(BuiltInFragCoord); |
| 5665 |
|
| 5666 |
uint32_t pass_count = 0; |
| 5667 |
do |
| 5668 |
{ |
| 5669 |
if (pass_count >= 3) |
| 5670 |
SPIRV_CROSS_THROW("Over 3 compilation loops detected. Must be a bug!"); |
| 5671 |
|
| 5672 |
reset(); |
| 5673 |
|
| 5674 |
// Move constructor for this type is broken on GCC 4.9 ... |
| 5675 |
buffer.reset(); |
| 5676 |
|
| 5677 |
emit_header(); |
| 5678 |
emit_resources(); |
| 5679 |
|
| 5680 |
emit_function(get<SPIRFunction>(ir.default_entry_point), Bitset()); |
| 5681 |
emit_hlsl_entry_point(); |
| 5682 |
|
| 5683 |
pass_count++; |
| 5684 |
} while (is_forcing_recompilation()); |
| 5685 |
|
| 5686 |
// Entry point in HLSL is always main() for the time being. |
| 5687 |
get_entry_point().name = "main"; |
| 5688 |
|
| 5689 |
return buffer.str(); |
| 5690 |
} |
| 5691 |
|
| 5692 |
void CompilerHLSL::emit_block_hints(const SPIRBlock &block) |
| 5693 |
{ |
| 5694 |
switch (block.hint) |
| 5695 |
{ |
| 5696 |
case SPIRBlock::HintFlatten: |
| 5697 |
statement("[flatten]"); |
| 5698 |
break; |
| 5699 |
case SPIRBlock::HintDontFlatten: |
| 5700 |
statement("[branch]"); |
| 5701 |
break; |
| 5702 |
case SPIRBlock::HintUnroll: |
| 5703 |
statement("[unroll]"); |
| 5704 |
break; |
| 5705 |
case SPIRBlock::HintDontUnroll: |
| 5706 |
statement("[loop]"); |
| 5707 |
break; |
| 5708 |
default: |
| 5709 |
break; |
| 5710 |
} |
| 5711 |
} |
| 5712 |
|
| 5713 |
string CompilerHLSL::get_unique_identifier() |
| 5714 |
{ |
| 5715 |
return join("_", unique_identifier_count++, "ident"); |
| 5716 |
} |
| 5717 |
|
| 5718 |
void CompilerHLSL::add_hlsl_resource_binding(const HLSLResourceBinding &binding) |
| 5719 |
{ |
| 5720 |
StageSetBinding tuple = { binding.stage, binding.desc_set, binding.binding }; |
| 5721 |
resource_bindings[tuple] = { binding, false }; |
| 5722 |
} |
| 5723 |
|
| 5724 |
bool CompilerHLSL::is_hlsl_resource_binding_used(ExecutionModel model, uint32_t desc_set, uint32_t binding) const |
| 5725 |
{ |
| 5726 |
StageSetBinding tuple = { model, desc_set, binding }; |
| 5727 |
auto itr = resource_bindings.find(tuple); |
| 5728 |
return itr != end(resource_bindings) && itr->second.second; |
| 5729 |
} |
| 5730 |
|
| 5731 |
CompilerHLSL::BitcastType CompilerHLSL::get_bitcast_type(uint32_t result_type, uint32_t op0) |
| 5732 |
{ |
| 5733 |
auto &rslt_type = get<SPIRType>(result_type); |
| 5734 |
auto &expr_type = expression_type(op0); |
| 5735 |
|
| 5736 |
if (rslt_type.basetype == SPIRType::BaseType::UInt64 && expr_type.basetype == SPIRType::BaseType::UInt && |
| 5737 |
expr_type.vecsize == 2) |
| 5738 |
return BitcastType::TypePackUint2x32; |
| 5739 |
else if (rslt_type.basetype == SPIRType::BaseType::UInt && rslt_type.vecsize == 2 && |
| 5740 |
expr_type.basetype == SPIRType::BaseType::UInt64) |
| 5741 |
return BitcastType::TypeUnpackUint64; |
| 5742 |
|
| 5743 |
return BitcastType::TypeNormal; |
| 5744 |
} |
| 5745 |
|
| 5746 |
bool CompilerHLSL::is_hlsl_force_storage_buffer_as_uav(ID id) const |
| 5747 |
{ |
| 5748 |
if (hlsl_options.force_storage_buffer_as_uav) |
| 5749 |
{ |
| 5750 |
return true; |
| 5751 |
} |
| 5752 |
|
| 5753 |
const uint32_t desc_set = get_decoration(id, spv::DecorationDescriptorSet); |
| 5754 |
const uint32_t binding = get_decoration(id, spv::DecorationBinding); |
| 5755 |
|
| 5756 |
return (force_uav_buffer_bindings.find({ desc_set, binding }) != force_uav_buffer_bindings.end()); |
| 5757 |
} |
| 5758 |
|
| 5759 |
void CompilerHLSL::set_hlsl_force_storage_buffer_as_uav(uint32_t desc_set, uint32_t binding) |
| 5760 |
{ |
| 5761 |
SetBindingPair pair = { desc_set, binding }; |
| 5762 |
force_uav_buffer_bindings.insert(pair); |
| 5763 |
} |
| 5764 |
|
| 5765 |
bool CompilerHLSL::builtin_translates_to_nonarray(spv::BuiltIn builtin) const |
| 5766 |
{ |
| 5767 |
return (builtin == BuiltInSampleMask); |
| 5768 |
} |