Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2016 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // TextureFunctionHLSL: Class for writing implementations of ESSL texture functions into HLSL |
| 7 | // output. Some of the implementations are straightforward and just call the HLSL equivalent of the |
| 8 | // ESSL texture function, others do more work to emulate ESSL texture sampling or size query |
| 9 | // behavior. |
| 10 | // |
| 11 | |
| 12 | #include "compiler/translator/TextureFunctionHLSL.h" |
| 13 | |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 14 | #include "compiler/translator/ImmutableStringBuilder.h" |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 15 | #include "compiler/translator/UtilsHLSL.h" |
| 16 | |
| 17 | namespace sh |
| 18 | { |
| 19 | |
| 20 | namespace |
| 21 | { |
| 22 | |
| 23 | void OutputIntTexCoordWrap(TInfoSinkBase &out, |
| 24 | const char *wrapMode, |
| 25 | const char *size, |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 26 | const ImmutableString &texCoord, |
| 27 | const char *texCoordOffset, |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 28 | const char *texCoordOutName) |
| 29 | { |
| 30 | // GLES 3.0.4 table 3.22 specifies how the wrap modes work. We don't use the formulas verbatim |
| 31 | // but rather use equivalent formulas that map better to HLSL. |
| 32 | out << "int " << texCoordOutName << ";\n"; |
| 33 | out << "float " << texCoordOutName << "Offset = " << texCoord << " + float(" << texCoordOffset |
| 34 | << ") / " << size << ";\n"; |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 35 | out << "bool " << texCoordOutName << "UseBorderColor = false;\n"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 36 | |
| 37 | // CLAMP_TO_EDGE |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 38 | out << "if (" << wrapMode << " == 0)\n"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 39 | out << "{\n"; |
| 40 | out << " " << texCoordOutName << " = clamp(int(floor(" << size << " * " << texCoordOutName |
| 41 | << "Offset)), 0, int(" << size << ") - 1);\n"; |
| 42 | out << "}\n"; |
| 43 | |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 44 | // CLAMP_TO_BORDER |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 45 | out << "else if (" << wrapMode << " == 3)\n"; |
| 46 | out << "{\n"; |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 47 | out << " int texCoordInt = int(floor(" << size << " * " << texCoordOutName << "Offset));\n"; |
| 48 | out << " " << texCoordOutName << " = clamp(texCoordInt, 0, int(" << size << ") - 1);\n"; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 49 | out << " " << texCoordOutName << "UseBorderColor = (texCoordInt != " << texCoordOutName |
| 50 | << ");\n"; |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 51 | out << "}\n"; |
| 52 | |
| 53 | // MIRRORED_REPEAT |
| 54 | out << "else if (" << wrapMode << " == 2)\n"; |
| 55 | out << "{\n"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 56 | out << " float coordWrapped = 1.0 - abs(frac(abs(" << texCoordOutName |
| 57 | << "Offset) * 0.5) * 2.0 - 1.0);\n"; |
| 58 | out << " " << texCoordOutName << " = int(floor(" << size << " * coordWrapped));\n"; |
| 59 | out << "}\n"; |
| 60 | |
| 61 | // REPEAT |
| 62 | out << "else\n"; |
| 63 | out << "{\n"; |
| 64 | out << " " << texCoordOutName << " = int(floor(" << size << " * frac(" << texCoordOutName |
| 65 | << "Offset)));\n"; |
| 66 | out << "}\n"; |
| 67 | } |
| 68 | |
| 69 | void OutputIntTexCoordWraps(TInfoSinkBase &out, |
| 70 | const TextureFunctionHLSL::TextureFunction &textureFunction, |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 71 | ImmutableString *texCoordX, |
| 72 | ImmutableString *texCoordY, |
| 73 | ImmutableString *texCoordZ) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 74 | { |
| 75 | // Convert from normalized floating-point to integer |
| 76 | out << "int wrapS = samplerMetadata[samplerIndex].wrapModes & 0x3;\n"; |
| 77 | if (textureFunction.offset) |
| 78 | { |
| 79 | OutputIntTexCoordWrap(out, "wrapS", "width", *texCoordX, "offset.x", "tix"); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | OutputIntTexCoordWrap(out, "wrapS", "width", *texCoordX, "0", "tix"); |
| 84 | } |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 85 | *texCoordX = ImmutableString("tix"); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 86 | out << "int wrapT = (samplerMetadata[samplerIndex].wrapModes >> 2) & 0x3;\n"; |
| 87 | if (textureFunction.offset) |
| 88 | { |
| 89 | OutputIntTexCoordWrap(out, "wrapT", "height", *texCoordY, "offset.y", "tiy"); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | OutputIntTexCoordWrap(out, "wrapT", "height", *texCoordY, "0", "tiy"); |
| 94 | } |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 95 | *texCoordY = ImmutableString("tiy"); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 96 | |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 97 | bool tizAvailable = false; |
| 98 | |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 99 | if (IsSamplerArray(textureFunction.sampler)) |
| 100 | { |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 101 | *texCoordZ = ImmutableString("int(max(0, min(layers - 1, floor(0.5 + t.z))))"); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 102 | } |
| 103 | else if (!IsSamplerCube(textureFunction.sampler) && !IsSampler2D(textureFunction.sampler)) |
| 104 | { |
| 105 | out << "int wrapR = (samplerMetadata[samplerIndex].wrapModes >> 4) & 0x3;\n"; |
| 106 | if (textureFunction.offset) |
| 107 | { |
| 108 | OutputIntTexCoordWrap(out, "wrapR", "depth", *texCoordZ, "offset.z", "tiz"); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | OutputIntTexCoordWrap(out, "wrapR", "depth", *texCoordZ, "0", "tiz"); |
| 113 | } |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 114 | *texCoordZ = ImmutableString("tiz"); |
| 115 | tizAvailable = true; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 116 | } |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 117 | |
| 118 | out << "bool useBorderColor = tixUseBorderColor || tiyUseBorderColor" |
| 119 | << (tizAvailable ? " || tizUseBorderColor" : "") << ";\n"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void OutputHLSL4SampleFunctionPrefix(TInfoSinkBase &out, |
| 123 | const TextureFunctionHLSL::TextureFunction &textureFunction, |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 124 | const ImmutableString &textureReference, |
| 125 | const ImmutableString &samplerReference) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 126 | { |
| 127 | out << textureReference; |
| 128 | if (IsIntegerSampler(textureFunction.sampler) || |
| 129 | textureFunction.method == TextureFunctionHLSL::TextureFunction::FETCH) |
| 130 | { |
| 131 | out << ".Load("; |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (IsShadowSampler(textureFunction.sampler)) |
| 136 | { |
| 137 | switch (textureFunction.method) |
| 138 | { |
| 139 | case TextureFunctionHLSL::TextureFunction::IMPLICIT: |
| 140 | case TextureFunctionHLSL::TextureFunction::BIAS: |
| 141 | case TextureFunctionHLSL::TextureFunction::LOD: |
| 142 | out << ".SampleCmp("; |
| 143 | break; |
| 144 | case TextureFunctionHLSL::TextureFunction::LOD0: |
| 145 | case TextureFunctionHLSL::TextureFunction::LOD0BIAS: |
| 146 | case TextureFunctionHLSL::TextureFunction::GRAD: |
| 147 | out << ".SampleCmpLevelZero("; |
| 148 | break; |
| 149 | default: |
| 150 | UNREACHABLE(); |
| 151 | } |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | switch (textureFunction.method) |
| 156 | { |
| 157 | case TextureFunctionHLSL::TextureFunction::IMPLICIT: |
| 158 | out << ".Sample("; |
| 159 | break; |
| 160 | case TextureFunctionHLSL::TextureFunction::BIAS: |
| 161 | out << ".SampleBias("; |
| 162 | break; |
| 163 | case TextureFunctionHLSL::TextureFunction::LOD: |
| 164 | case TextureFunctionHLSL::TextureFunction::LOD0: |
| 165 | case TextureFunctionHLSL::TextureFunction::LOD0BIAS: |
| 166 | out << ".SampleLevel("; |
| 167 | break; |
| 168 | case TextureFunctionHLSL::TextureFunction::GRAD: |
| 169 | out << ".SampleGrad("; |
| 170 | break; |
| 171 | default: |
| 172 | UNREACHABLE(); |
| 173 | } |
| 174 | } |
| 175 | out << samplerReference << ", "; |
| 176 | } |
| 177 | |
| 178 | const char *GetSamplerCoordinateTypeString( |
| 179 | const TextureFunctionHLSL::TextureFunction &textureFunction, |
| 180 | int hlslCoords) |
| 181 | { |
Jiawei Shao | a1ac3fe | 2018-10-10 12:29:31 +0800 | [diff] [blame] | 182 | // Gather[Red|Green|Blue|Alpha] accepts float texture coordinates on textures in integer or |
| 183 | // unsigned integer formats. |
| 184 | // https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl-to-gather |
| 185 | if ((IsIntegerSampler(textureFunction.sampler) && |
| 186 | textureFunction.method != TextureFunctionHLSL::TextureFunction::GATHER) || |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 187 | textureFunction.method == TextureFunctionHLSL::TextureFunction::FETCH) |
| 188 | { |
| 189 | switch (hlslCoords) |
| 190 | { |
| 191 | case 2: |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 192 | if (IsSampler2DMS(textureFunction.sampler)) |
| 193 | { |
JiangYizhou | 5b03f47 | 2017-01-09 10:22:53 +0800 | [diff] [blame] | 194 | return "int2"; |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 195 | } |
JiangYizhou | 5b03f47 | 2017-01-09 10:22:53 +0800 | [diff] [blame] | 196 | else |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 197 | { |
JiangYizhou | 5b03f47 | 2017-01-09 10:22:53 +0800 | [diff] [blame] | 198 | return "int3"; |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 199 | } |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 200 | case 3: |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 201 | if (IsSampler2DMSArray(textureFunction.sampler)) |
| 202 | { |
| 203 | return "int3"; |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | return "int4"; |
| 208 | } |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 209 | default: |
| 210 | UNREACHABLE(); |
| 211 | } |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | switch (hlslCoords) |
| 216 | { |
| 217 | case 2: |
| 218 | return "float2"; |
| 219 | case 3: |
| 220 | return "float3"; |
| 221 | case 4: |
| 222 | return "float4"; |
| 223 | default: |
| 224 | UNREACHABLE(); |
| 225 | } |
| 226 | } |
| 227 | return ""; |
| 228 | } |
| 229 | |
| 230 | int GetHLSLCoordCount(const TextureFunctionHLSL::TextureFunction &textureFunction, |
| 231 | ShShaderOutput outputType) |
| 232 | { |
| 233 | if (outputType == SH_HLSL_3_0_OUTPUT) |
| 234 | { |
| 235 | int hlslCoords = 2; |
| 236 | switch (textureFunction.sampler) |
| 237 | { |
| 238 | case EbtSampler2D: |
Geoff Lang | b66a909 | 2016-05-16 15:59:14 -0400 | [diff] [blame] | 239 | case EbtSamplerExternalOES: |
JiangYizhou | 5b03f47 | 2017-01-09 10:22:53 +0800 | [diff] [blame] | 240 | case EbtSampler2DMS: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 241 | hlslCoords = 2; |
| 242 | break; |
| 243 | case EbtSamplerCube: |
| 244 | hlslCoords = 3; |
| 245 | break; |
| 246 | default: |
| 247 | UNREACHABLE(); |
| 248 | } |
| 249 | |
| 250 | switch (textureFunction.method) |
| 251 | { |
| 252 | case TextureFunctionHLSL::TextureFunction::IMPLICIT: |
Geoff Lang | ba992ab | 2017-04-19 11:18:14 -0400 | [diff] [blame] | 253 | case TextureFunctionHLSL::TextureFunction::GRAD: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 254 | return hlslCoords; |
| 255 | case TextureFunctionHLSL::TextureFunction::BIAS: |
| 256 | case TextureFunctionHLSL::TextureFunction::LOD: |
| 257 | case TextureFunctionHLSL::TextureFunction::LOD0: |
| 258 | case TextureFunctionHLSL::TextureFunction::LOD0BIAS: |
| 259 | return 4; |
| 260 | default: |
| 261 | UNREACHABLE(); |
| 262 | } |
| 263 | } |
| 264 | else |
| 265 | { |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 266 | if (IsSampler3D(textureFunction.sampler) || IsSamplerArray(textureFunction.sampler) || |
| 267 | IsSamplerCube(textureFunction.sampler)) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 268 | { |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 269 | return 3; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 270 | } |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 271 | ASSERT(IsSampler2D(textureFunction.sampler)); |
| 272 | return 2; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 273 | } |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | void OutputTextureFunctionArgumentList(TInfoSinkBase &out, |
| 278 | const TextureFunctionHLSL::TextureFunction &textureFunction, |
| 279 | const ShShaderOutput outputType) |
| 280 | { |
| 281 | if (outputType == SH_HLSL_3_0_OUTPUT) |
| 282 | { |
| 283 | switch (textureFunction.sampler) |
| 284 | { |
| 285 | case EbtSampler2D: |
Geoff Lang | b66a909 | 2016-05-16 15:59:14 -0400 | [diff] [blame] | 286 | case EbtSamplerExternalOES: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 287 | out << "sampler2D s"; |
| 288 | break; |
| 289 | case EbtSamplerCube: |
| 290 | out << "samplerCUBE s"; |
| 291 | break; |
| 292 | default: |
| 293 | UNREACHABLE(); |
| 294 | } |
| 295 | } |
| 296 | else |
| 297 | { |
| 298 | if (outputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
| 299 | { |
| 300 | out << TextureString(textureFunction.sampler) << " x, " |
| 301 | << SamplerString(textureFunction.sampler) << " s"; |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | ASSERT(outputType == SH_HLSL_4_1_OUTPUT); |
Jamie Madill | 8aeeed6 | 2017-03-15 18:09:26 -0400 | [diff] [blame] | 306 | // A bug in the D3D compiler causes some nested sampling operations to fail. |
| 307 | // See http://anglebug.com/1923 |
| 308 | // TODO(jmadill): Reinstate the const keyword when possible. |
| 309 | out << /*"const"*/ "uint samplerIndex"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | if (textureFunction.method == |
| 314 | TextureFunctionHLSL::TextureFunction::FETCH) // Integer coordinates |
| 315 | { |
| 316 | switch (textureFunction.coords) |
| 317 | { |
| 318 | case 2: |
| 319 | out << ", int2 t"; |
| 320 | break; |
| 321 | case 3: |
| 322 | out << ", int3 t"; |
| 323 | break; |
| 324 | default: |
| 325 | UNREACHABLE(); |
| 326 | } |
| 327 | } |
| 328 | else // Floating-point coordinates (except textureSize) |
| 329 | { |
| 330 | switch (textureFunction.coords) |
| 331 | { |
Olli Etuaho | 92db39e | 2017-02-15 12:11:04 +0000 | [diff] [blame] | 332 | case 0: |
| 333 | break; // textureSize(gSampler2DMS sampler) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 334 | case 1: |
| 335 | out << ", int lod"; |
| 336 | break; // textureSize() |
| 337 | case 2: |
| 338 | out << ", float2 t"; |
| 339 | break; |
| 340 | case 3: |
| 341 | out << ", float3 t"; |
| 342 | break; |
| 343 | case 4: |
| 344 | out << ", float4 t"; |
| 345 | break; |
| 346 | default: |
| 347 | UNREACHABLE(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD) |
| 352 | { |
| 353 | switch (textureFunction.sampler) |
| 354 | { |
| 355 | case EbtSampler2D: |
| 356 | case EbtISampler2D: |
| 357 | case EbtUSampler2D: |
| 358 | case EbtSampler2DArray: |
| 359 | case EbtISampler2DArray: |
| 360 | case EbtUSampler2DArray: |
| 361 | case EbtSampler2DShadow: |
| 362 | case EbtSampler2DArrayShadow: |
Ian Ewell | bda7559 | 2016-04-18 17:25:54 -0400 | [diff] [blame] | 363 | case EbtSamplerExternalOES: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 364 | out << ", float2 ddx, float2 ddy"; |
| 365 | break; |
| 366 | case EbtSampler3D: |
| 367 | case EbtISampler3D: |
| 368 | case EbtUSampler3D: |
| 369 | case EbtSamplerCube: |
| 370 | case EbtISamplerCube: |
| 371 | case EbtUSamplerCube: |
| 372 | case EbtSamplerCubeShadow: |
| 373 | out << ", float3 ddx, float3 ddy"; |
| 374 | break; |
| 375 | default: |
| 376 | UNREACHABLE(); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | switch (textureFunction.method) |
| 381 | { |
| 382 | case TextureFunctionHLSL::TextureFunction::IMPLICIT: |
| 383 | break; |
| 384 | case TextureFunctionHLSL::TextureFunction::BIAS: |
| 385 | break; // Comes after the offset parameter |
| 386 | case TextureFunctionHLSL::TextureFunction::LOD: |
| 387 | out << ", float lod"; |
| 388 | break; |
| 389 | case TextureFunctionHLSL::TextureFunction::LOD0: |
| 390 | break; |
| 391 | case TextureFunctionHLSL::TextureFunction::LOD0BIAS: |
| 392 | break; // Comes after the offset parameter |
| 393 | case TextureFunctionHLSL::TextureFunction::SIZE: |
| 394 | break; |
| 395 | case TextureFunctionHLSL::TextureFunction::FETCH: |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 396 | if (IsSampler2DMS(textureFunction.sampler) || |
| 397 | IsSampler2DMSArray(textureFunction.sampler)) |
JiangYizhou | 5b03f47 | 2017-01-09 10:22:53 +0800 | [diff] [blame] | 398 | out << ", int index"; |
| 399 | else |
| 400 | out << ", int mip"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 401 | break; |
| 402 | case TextureFunctionHLSL::TextureFunction::GRAD: |
| 403 | break; |
Jiawei Shao | a977acc | 2018-09-19 12:46:05 +0800 | [diff] [blame] | 404 | case TextureFunctionHLSL::TextureFunction::GATHER: |
| 405 | break; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 406 | default: |
| 407 | UNREACHABLE(); |
| 408 | } |
| 409 | |
Jiawei Shao | 19b51d2 | 2018-09-19 15:14:45 +0800 | [diff] [blame] | 410 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GATHER && |
| 411 | IsShadowSampler(textureFunction.sampler)) |
| 412 | { |
| 413 | out << ", float refZ"; |
| 414 | } |
| 415 | |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 416 | if (textureFunction.offset) |
| 417 | { |
| 418 | switch (textureFunction.sampler) |
| 419 | { |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 420 | case EbtSampler3D: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 421 | case EbtISampler3D: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 422 | case EbtUSampler3D: |
| 423 | out << ", int3 offset"; |
| 424 | break; |
Andi-Bogdan Postelnicu | 9e77ce3 | 2016-09-27 17:05:44 +0300 | [diff] [blame] | 425 | case EbtSampler2D: |
| 426 | case EbtSampler2DArray: |
| 427 | case EbtISampler2D: |
| 428 | case EbtISampler2DArray: |
| 429 | case EbtUSampler2D: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 430 | case EbtUSampler2DArray: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 431 | case EbtSampler2DShadow: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 432 | case EbtSampler2DArrayShadow: |
Ian Ewell | bda7559 | 2016-04-18 17:25:54 -0400 | [diff] [blame] | 433 | case EbtSamplerExternalOES: |
| 434 | out << ", int2 offset"; |
Andi-Bogdan Postelnicu | 9e77ce3 | 2016-09-27 17:05:44 +0300 | [diff] [blame] | 435 | break; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 436 | default: |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 437 | // Offset is not supported for multisampled textures. |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 438 | UNREACHABLE(); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS || |
| 443 | textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0BIAS) |
| 444 | { |
| 445 | out << ", float bias"; |
| 446 | } |
Jiawei Shao | 19b51d2 | 2018-09-19 15:14:45 +0800 | [diff] [blame] | 447 | else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GATHER && |
| 448 | !IsShadowSampler(textureFunction.sampler)) |
Jiawei Shao | a977acc | 2018-09-19 12:46:05 +0800 | [diff] [blame] | 449 | { |
| 450 | out << ", int comp = 0"; |
| 451 | } |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void GetTextureReference(TInfoSinkBase &out, |
| 455 | const TextureFunctionHLSL::TextureFunction &textureFunction, |
| 456 | const ShShaderOutput outputType, |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 457 | ImmutableString *textureReference, |
| 458 | ImmutableString *samplerReference) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 459 | { |
| 460 | if (outputType == SH_HLSL_4_1_OUTPUT) |
| 461 | { |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 462 | static const ImmutableString kTexturesStr("textures"); |
| 463 | static const ImmutableString kSamplersStr("samplers"); |
| 464 | static const ImmutableString kSamplerIndexStr("[samplerIndex]"); |
| 465 | static const ImmutableString kTextureIndexStr("[textureIndex]"); |
| 466 | static const ImmutableString kSamplerArrayIndexStr("[samplerArrayIndex]"); |
| 467 | ImmutableString suffix(TextureGroupSuffix(textureFunction.sampler)); |
| 468 | |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 469 | if (TextureGroup(textureFunction.sampler) == HLSL_TEXTURE_2D) |
| 470 | { |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 471 | ImmutableStringBuilder textureRefBuilder(kTexturesStr.length() + suffix.length() + |
| 472 | kSamplerIndexStr.length()); |
| 473 | textureRefBuilder << kTexturesStr << suffix << kSamplerIndexStr; |
| 474 | *textureReference = textureRefBuilder; |
| 475 | ImmutableStringBuilder samplerRefBuilder(kSamplersStr.length() + suffix.length() + |
| 476 | kSamplerIndexStr.length()); |
| 477 | samplerRefBuilder << kSamplersStr << suffix << kSamplerIndexStr; |
| 478 | *samplerReference = samplerRefBuilder; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 479 | } |
| 480 | else |
| 481 | { |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 482 | out << " const uint textureIndex = samplerIndex - textureIndexOffset" |
| 483 | << suffix.data() << ";\n"; |
| 484 | ImmutableStringBuilder textureRefBuilder(kTexturesStr.length() + suffix.length() + |
| 485 | kTextureIndexStr.length()); |
| 486 | textureRefBuilder << kTexturesStr << suffix << kTextureIndexStr; |
| 487 | *textureReference = textureRefBuilder; |
| 488 | |
| 489 | out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset" |
| 490 | << suffix.data() << ";\n"; |
| 491 | ImmutableStringBuilder samplerRefBuilder(kSamplersStr.length() + suffix.length() + |
| 492 | kSamplerArrayIndexStr.length()); |
| 493 | samplerRefBuilder << kSamplersStr << suffix << kSamplerArrayIndexStr; |
| 494 | *samplerReference = samplerRefBuilder; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | else |
| 498 | { |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 499 | *textureReference = ImmutableString("x"); |
| 500 | *samplerReference = ImmutableString("s"); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
| 504 | void OutputTextureSizeFunctionBody(TInfoSinkBase &out, |
| 505 | const TextureFunctionHLSL::TextureFunction &textureFunction, |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 506 | const ImmutableString &textureReference, |
Geoff Lang | 1fe74c7 | 2016-08-25 13:23:01 -0400 | [diff] [blame] | 507 | bool getDimensionsIgnoresBaseLevel) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 508 | { |
Olli Etuaho | 92db39e | 2017-02-15 12:11:04 +0000 | [diff] [blame] | 509 | if (IsSampler2DMS(textureFunction.sampler)) |
Geoff Lang | 1fe74c7 | 2016-08-25 13:23:01 -0400 | [diff] [blame] | 510 | { |
Olli Etuaho | 92db39e | 2017-02-15 12:11:04 +0000 | [diff] [blame] | 511 | out << " uint width; uint height; uint samples;\n" |
| 512 | << " " << textureReference << ".GetDimensions(width, height, samples);\n"; |
Geoff Lang | 1fe74c7 | 2016-08-25 13:23:01 -0400 | [diff] [blame] | 513 | } |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 514 | else if (IsSampler2DMSArray(textureFunction.sampler)) |
| 515 | { |
| 516 | out << " uint width; uint height; uint depth; uint samples;\n" |
| 517 | << " " << textureReference << ".GetDimensions(width, height, depth, samples);\n"; |
| 518 | } |
Geoff Lang | 1fe74c7 | 2016-08-25 13:23:01 -0400 | [diff] [blame] | 519 | else |
| 520 | { |
Olli Etuaho | 92db39e | 2017-02-15 12:11:04 +0000 | [diff] [blame] | 521 | if (getDimensionsIgnoresBaseLevel) |
Geoff Lang | 1fe74c7 | 2016-08-25 13:23:01 -0400 | [diff] [blame] | 522 | { |
Olli Etuaho | 92db39e | 2017-02-15 12:11:04 +0000 | [diff] [blame] | 523 | out << " int baseLevel = samplerMetadata[samplerIndex].baseLevel;\n"; |
Geoff Lang | 1fe74c7 | 2016-08-25 13:23:01 -0400 | [diff] [blame] | 524 | } |
Olli Etuaho | 92db39e | 2017-02-15 12:11:04 +0000 | [diff] [blame] | 525 | else |
| 526 | { |
| 527 | out << " int baseLevel = 0;\n"; |
| 528 | } |
| 529 | |
| 530 | if (IsSampler3D(textureFunction.sampler) || IsSamplerArray(textureFunction.sampler) || |
| 531 | (IsIntegerSampler(textureFunction.sampler) && IsSamplerCube(textureFunction.sampler))) |
| 532 | { |
| 533 | // "depth" stores either the number of layers in an array texture or 3D depth |
| 534 | out << " uint width; uint height; uint depth; uint numberOfLevels;\n" |
| 535 | << " " << textureReference |
| 536 | << ".GetDimensions(baseLevel, width, height, depth, numberOfLevels);\n" |
| 537 | << " width = max(width >> lod, 1);\n" |
| 538 | << " height = max(height >> lod, 1);\n"; |
| 539 | |
| 540 | if (!IsSamplerArray(textureFunction.sampler)) |
| 541 | { |
| 542 | out << " depth = max(depth >> lod, 1);\n"; |
| 543 | } |
| 544 | } |
| 545 | else if (IsSampler2D(textureFunction.sampler) || IsSamplerCube(textureFunction.sampler)) |
| 546 | { |
| 547 | out << " uint width; uint height; uint numberOfLevels;\n" |
| 548 | << " " << textureReference |
| 549 | << ".GetDimensions(baseLevel, width, height, numberOfLevels);\n" |
| 550 | << " width = max(width >> lod, 1);\n" |
| 551 | << " height = max(height >> lod, 1);\n"; |
| 552 | } |
| 553 | else |
| 554 | UNREACHABLE(); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 555 | } |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 556 | |
| 557 | if (strcmp(textureFunction.getReturnType(), "int3") == 0) |
| 558 | { |
Olli Etuaho | 92db39e | 2017-02-15 12:11:04 +0000 | [diff] [blame] | 559 | out << " return int3(width, height, depth);\n"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 560 | } |
| 561 | else |
| 562 | { |
Olli Etuaho | 92db39e | 2017-02-15 12:11:04 +0000 | [diff] [blame] | 563 | out << " return int2(width, height);\n"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | void ProjectTextureCoordinates(const TextureFunctionHLSL::TextureFunction &textureFunction, |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 568 | ImmutableString *texCoordX, |
| 569 | ImmutableString *texCoordY, |
| 570 | ImmutableString *texCoordZ) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 571 | { |
| 572 | if (textureFunction.proj) |
| 573 | { |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 574 | ImmutableString proj(""); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 575 | switch (textureFunction.coords) |
| 576 | { |
| 577 | case 3: |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 578 | proj = ImmutableString(" / t.z"); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 579 | break; |
| 580 | case 4: |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 581 | proj = ImmutableString(" / t.w"); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 582 | break; |
| 583 | default: |
| 584 | UNREACHABLE(); |
| 585 | } |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 586 | ImmutableStringBuilder texCoordXBuilder(texCoordX->length() + proj.length() + 2u); |
| 587 | texCoordXBuilder << '(' << *texCoordX << proj << ')'; |
| 588 | *texCoordX = texCoordXBuilder; |
| 589 | ImmutableStringBuilder texCoordYBuilder(texCoordY->length() + proj.length() + 2u); |
| 590 | texCoordYBuilder << '(' << *texCoordY << proj << ')'; |
| 591 | *texCoordY = texCoordYBuilder; |
| 592 | ImmutableStringBuilder texCoordZBuilder(texCoordZ->length() + proj.length() + 2u); |
| 593 | texCoordZBuilder << '(' << *texCoordZ << proj << ')'; |
| 594 | *texCoordZ = texCoordZBuilder; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | void OutputIntegerTextureSampleFunctionComputations( |
| 599 | TInfoSinkBase &out, |
| 600 | const TextureFunctionHLSL::TextureFunction &textureFunction, |
| 601 | const ShShaderOutput outputType, |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 602 | const ImmutableString &textureReference, |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 603 | ImmutableString *texCoordX, |
| 604 | ImmutableString *texCoordY, |
Anders Leino | f6cbe44 | 2019-04-18 15:32:07 +0300 | [diff] [blame] | 605 | ImmutableString *texCoordZ, |
| 606 | bool getDimensionsIgnoresBaseLevel) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 607 | { |
| 608 | if (!IsIntegerSampler(textureFunction.sampler)) |
| 609 | { |
| 610 | return; |
| 611 | } |
| 612 | if (IsSamplerCube(textureFunction.sampler)) |
| 613 | { |
| 614 | out << " float width; float height; float layers; float levels;\n"; |
| 615 | |
| 616 | out << " uint mip = 0;\n"; |
| 617 | |
| 618 | out << " " << textureReference |
| 619 | << ".GetDimensions(mip, width, height, layers, levels);\n"; |
| 620 | |
| 621 | out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n"; |
| 622 | out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n"; |
| 623 | out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n"; |
| 624 | out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || " |
| 625 | "(zMajor && t.z < 0.0f);\n"; |
| 626 | |
| 627 | // FACE_POSITIVE_X = 000b |
| 628 | // FACE_NEGATIVE_X = 001b |
| 629 | // FACE_POSITIVE_Y = 010b |
| 630 | // FACE_NEGATIVE_Y = 011b |
| 631 | // FACE_POSITIVE_Z = 100b |
| 632 | // FACE_NEGATIVE_Z = 101b |
| 633 | out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n"; |
| 634 | |
| 635 | out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n"; |
| 636 | out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n"; |
| 637 | out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n"; |
| 638 | |
| 639 | out << " t.x = (u * 0.5f / m) + 0.5f;\n"; |
| 640 | out << " t.y = (v * 0.5f / m) + 0.5f;\n"; |
| 641 | |
| 642 | // Mip level computation. |
| 643 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::IMPLICIT || |
| 644 | textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD || |
| 645 | textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD) |
| 646 | { |
| 647 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::IMPLICIT) |
| 648 | { |
| 649 | out << " float2 tSized = float2(t.x * width, t.y * height);\n" |
| 650 | " float2 dx = ddx(tSized);\n" |
| 651 | " float2 dy = ddy(tSized);\n" |
| 652 | " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"; |
| 653 | } |
| 654 | else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD) |
| 655 | { |
| 656 | // ESSL 3.00.6 spec section 8.8: "For the cube version, the partial |
| 657 | // derivatives of P are assumed to be in the coordinate system used before |
| 658 | // texture coordinates are projected onto the appropriate cube face." |
| 659 | // ddx[0] and ddy[0] are the derivatives of t.x passed into the function |
| 660 | // ddx[1] and ddy[1] are the derivatives of t.y passed into the function |
| 661 | // ddx[2] and ddy[2] are the derivatives of t.z passed into the function |
| 662 | // Determine the derivatives of u, v and m |
| 663 | out << " float dudx = xMajor ? ddx[2] : (yMajor && t.y < 0.0f ? -ddx[0] " |
| 664 | ": ddx[0]);\n" |
| 665 | " float dudy = xMajor ? ddy[2] : (yMajor && t.y < 0.0f ? -ddy[0] " |
| 666 | ": ddy[0]);\n" |
| 667 | " float dvdx = yMajor ? ddx[2] : (negative ? ddx[1] : -ddx[1]);\n" |
| 668 | " float dvdy = yMajor ? ddy[2] : (negative ? ddy[1] : -ddy[1]);\n" |
| 669 | " float dmdx = xMajor ? ddx[0] : (yMajor ? ddx[1] : ddx[2]);\n" |
| 670 | " float dmdy = xMajor ? ddy[0] : (yMajor ? ddy[1] : ddy[2]);\n"; |
| 671 | // Now determine the derivatives of the face coordinates, using the |
| 672 | // derivatives calculated above. |
| 673 | // d / dx (u(x) * 0.5 / m(x) + 0.5) |
| 674 | // = 0.5 * (m(x) * u'(x) - u(x) * m'(x)) / m(x)^2 |
| 675 | out << " float dfacexdx = 0.5f * (m * dudx - u * dmdx) / (m * m);\n" |
| 676 | " float dfaceydx = 0.5f * (m * dvdx - v * dmdx) / (m * m);\n" |
| 677 | " float dfacexdy = 0.5f * (m * dudy - u * dmdy) / (m * m);\n" |
| 678 | " float dfaceydy = 0.5f * (m * dvdy - v * dmdy) / (m * m);\n" |
| 679 | " float2 sizeVec = float2(width, height);\n" |
| 680 | " float2 faceddx = float2(dfacexdx, dfaceydx) * sizeVec;\n" |
| 681 | " float2 faceddy = float2(dfacexdy, dfaceydy) * sizeVec;\n"; |
| 682 | // Optimization: instead of: log2(max(length(faceddx), length(faceddy))) |
| 683 | // we compute: log2(max(length(faceddx)^2, length(faceddy)^2)) / 2 |
| 684 | out << " float lengthfaceddx2 = dot(faceddx, faceddx);\n" |
| 685 | " float lengthfaceddy2 = dot(faceddy, faceddy);\n" |
| 686 | " float lod = log2(max(lengthfaceddx2, lengthfaceddy2)) * 0.5f;\n"; |
| 687 | } |
| 688 | out << " mip = uint(min(max(round(lod), 0), levels - 1));\n" |
| 689 | << " " << textureReference |
| 690 | << ".GetDimensions(mip, width, height, layers, levels);\n"; |
| 691 | } |
| 692 | |
| 693 | // Convert from normalized floating-point to integer |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 694 | static const ImmutableString kXPrefix("int(floor(width * frac("); |
| 695 | static const ImmutableString kYPrefix("int(floor(height * frac("); |
| 696 | static const ImmutableString kSuffix(")))"); |
| 697 | ImmutableStringBuilder texCoordXBuilder(kXPrefix.length() + texCoordX->length() + |
| 698 | kSuffix.length()); |
| 699 | texCoordXBuilder << kXPrefix << *texCoordX << kSuffix; |
| 700 | *texCoordX = texCoordXBuilder; |
| 701 | ImmutableStringBuilder texCoordYBuilder(kYPrefix.length() + texCoordX->length() + |
| 702 | kSuffix.length()); |
| 703 | texCoordYBuilder << kYPrefix << *texCoordY << kSuffix; |
| 704 | *texCoordY = texCoordYBuilder; |
| 705 | *texCoordZ = ImmutableString("face"); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 706 | } |
| 707 | else if (textureFunction.method != TextureFunctionHLSL::TextureFunction::FETCH) |
| 708 | { |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 709 | if (IsSamplerArray(textureFunction.sampler)) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 710 | { |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 711 | out << " float width; float height; float layers; float levels;\n"; |
| 712 | |
| 713 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 714 | { |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 715 | out << " uint mip = 0;\n"; |
| 716 | } |
| 717 | else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0BIAS) |
| 718 | { |
| 719 | out << " uint mip = bias;\n"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 720 | } |
| 721 | else |
| 722 | { |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 723 | |
| 724 | out << " " << textureReference |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 725 | << ".GetDimensions(0, width, height, layers, levels);\n"; |
| 726 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::IMPLICIT || |
| 727 | textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS) |
| 728 | { |
| 729 | out << " float2 tSized = float2(t.x * width, t.y * height);\n" |
| 730 | " float dx = length(ddx(tSized));\n" |
| 731 | " float dy = length(ddy(tSized));\n" |
| 732 | " float lod = log2(max(dx, dy));\n"; |
| 733 | |
| 734 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS) |
| 735 | { |
| 736 | out << " lod += bias;\n"; |
| 737 | } |
| 738 | } |
| 739 | else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD) |
| 740 | { |
| 741 | out << " float2 sizeVec = float2(width, height);\n" |
| 742 | " float2 sizeDdx = ddx * sizeVec;\n" |
| 743 | " float2 sizeDdy = ddy * sizeVec;\n" |
| 744 | " float lod = log2(max(dot(sizeDdx, sizeDdx), " |
| 745 | "dot(sizeDdy, sizeDdy))) * 0.5f;\n"; |
| 746 | } |
| 747 | |
| 748 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 749 | } |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 750 | |
| 751 | out << " " << textureReference |
| 752 | << ".GetDimensions(mip, width, height, layers, levels);\n"; |
| 753 | } |
| 754 | else if (IsSampler2D(textureFunction.sampler)) |
| 755 | { |
Anders Leino | f6cbe44 | 2019-04-18 15:32:07 +0300 | [diff] [blame] | 756 | if (getDimensionsIgnoresBaseLevel) |
| 757 | { |
| 758 | out << " int baseLevel = samplerMetadata[samplerIndex].baseLevel;\n"; |
| 759 | } |
| 760 | else |
| 761 | { |
| 762 | out << " int baseLevel = 0;\n"; |
| 763 | } |
| 764 | |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 765 | out << " float width; float height; float levels;\n"; |
| 766 | |
| 767 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0) |
| 768 | { |
| 769 | out << " uint mip = 0;\n"; |
| 770 | } |
| 771 | else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0BIAS) |
| 772 | { |
| 773 | out << " uint mip = bias;\n"; |
| 774 | } |
| 775 | else |
| 776 | { |
Anders Leino | f6cbe44 | 2019-04-18 15:32:07 +0300 | [diff] [blame] | 777 | out << " " << textureReference |
| 778 | << ".GetDimensions(baseLevel, width, height, levels);\n"; |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 779 | |
| 780 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::IMPLICIT || |
| 781 | textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS) |
| 782 | { |
| 783 | out << " float2 tSized = float2(t.x * width, t.y * height);\n" |
| 784 | " float dx = length(ddx(tSized));\n" |
| 785 | " float dy = length(ddy(tSized));\n" |
| 786 | " float lod = log2(max(dx, dy));\n"; |
| 787 | |
| 788 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS) |
| 789 | { |
| 790 | out << " lod += bias;\n"; |
| 791 | } |
| 792 | } |
| 793 | else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD) |
| 794 | { |
| 795 | out << " float2 sizeVec = float2(width, height);\n" |
| 796 | " float2 sizeDdx = ddx * sizeVec;\n" |
| 797 | " float2 sizeDdy = ddy * sizeVec;\n" |
| 798 | " float lod = log2(max(dot(sizeDdx, sizeDdx), " |
| 799 | "dot(sizeDdy, sizeDdy))) * 0.5f;\n"; |
| 800 | } |
| 801 | |
| 802 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 803 | } |
| 804 | |
Anders Leino | f6cbe44 | 2019-04-18 15:32:07 +0300 | [diff] [blame] | 805 | out << " " << textureReference |
| 806 | << ".GetDimensions(baseLevel + mip, width, height, levels);\n"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 807 | } |
| 808 | else if (IsSampler3D(textureFunction.sampler)) |
| 809 | { |
| 810 | out << " float width; float height; float depth; float levels;\n"; |
| 811 | |
| 812 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0) |
| 813 | { |
| 814 | out << " uint mip = 0;\n"; |
| 815 | } |
| 816 | else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0BIAS) |
| 817 | { |
| 818 | out << " uint mip = bias;\n"; |
| 819 | } |
| 820 | else |
| 821 | { |
| 822 | out << " " << textureReference |
| 823 | << ".GetDimensions(0, width, height, depth, levels);\n"; |
| 824 | |
| 825 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::IMPLICIT || |
| 826 | textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS) |
| 827 | { |
| 828 | out << " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n" |
| 829 | " float dx = length(ddx(tSized));\n" |
| 830 | " float dy = length(ddy(tSized));\n" |
| 831 | " float lod = log2(max(dx, dy));\n"; |
| 832 | |
| 833 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS) |
| 834 | { |
| 835 | out << " lod += bias;\n"; |
| 836 | } |
| 837 | } |
| 838 | else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD) |
| 839 | { |
| 840 | out << " float3 sizeVec = float3(width, height, depth);\n" |
| 841 | " float3 sizeDdx = ddx * sizeVec;\n" |
| 842 | " float3 sizeDdy = ddy * sizeVec;\n" |
| 843 | " float lod = log2(max(dot(sizeDdx, sizeDdx), dot(sizeDdy, " |
| 844 | "sizeDdy))) * 0.5f;\n"; |
| 845 | } |
| 846 | |
| 847 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 848 | } |
| 849 | |
| 850 | out << " " << textureReference |
| 851 | << ".GetDimensions(mip, width, height, depth, levels);\n"; |
| 852 | } |
| 853 | else |
| 854 | UNREACHABLE(); |
| 855 | |
| 856 | OutputIntTexCoordWraps(out, textureFunction, texCoordX, texCoordY, texCoordZ); |
| 857 | } |
| 858 | } |
| 859 | |
Jiawei Shao | a977acc | 2018-09-19 12:46:05 +0800 | [diff] [blame] | 860 | void OutputTextureGatherFunctionBody(TInfoSinkBase &out, |
| 861 | const TextureFunctionHLSL::TextureFunction &textureFunction, |
| 862 | ShShaderOutput outputType, |
| 863 | const ImmutableString &textureReference, |
| 864 | const ImmutableString &samplerReference, |
| 865 | const ImmutableString &texCoordX, |
| 866 | const ImmutableString &texCoordY, |
| 867 | const ImmutableString &texCoordZ) |
| 868 | { |
| 869 | const int hlslCoords = GetHLSLCoordCount(textureFunction, outputType); |
| 870 | ImmutableString samplerCoordTypeString( |
| 871 | GetSamplerCoordinateTypeString(textureFunction, hlslCoords)); |
| 872 | ImmutableStringBuilder samplerCoordBuilder( |
| 873 | samplerCoordTypeString.length() + strlen("(") + texCoordX.length() + strlen(", ") + |
| 874 | texCoordY.length() + strlen(", ") + texCoordZ.length() + strlen(")")); |
| 875 | |
| 876 | samplerCoordBuilder << samplerCoordTypeString << "(" << texCoordX << ", " << texCoordY; |
| 877 | if (hlslCoords >= 3) |
| 878 | { |
| 879 | if (textureFunction.coords < 3) |
| 880 | { |
| 881 | samplerCoordBuilder << ", 0"; |
| 882 | } |
| 883 | else |
| 884 | { |
| 885 | samplerCoordBuilder << ", " << texCoordZ; |
| 886 | } |
| 887 | } |
| 888 | samplerCoordBuilder << ")"; |
| 889 | |
| 890 | ImmutableString samplerCoordString(samplerCoordBuilder); |
| 891 | |
Jiawei Shao | 19b51d2 | 2018-09-19 15:14:45 +0800 | [diff] [blame] | 892 | if (IsShadowSampler(textureFunction.sampler)) |
| 893 | { |
| 894 | out << "return " << textureReference << ".GatherCmp(" << samplerReference << ", " |
| 895 | << samplerCoordString << ", refZ"; |
| 896 | if (textureFunction.offset) |
| 897 | { |
| 898 | out << ", offset"; |
| 899 | } |
| 900 | out << ");\n"; |
| 901 | return; |
| 902 | } |
| 903 | |
Jiawei Shao | cf8ad76 | 2018-09-21 09:11:35 +0800 | [diff] [blame] | 904 | constexpr std::array<const char *, 4> kHLSLGatherFunctions = { |
| 905 | {"GatherRed", "GatherGreen", "GatherBlue", "GatherAlpha"}}; |
| 906 | |
Jiawei Shao | a977acc | 2018-09-19 12:46:05 +0800 | [diff] [blame] | 907 | out << " switch(comp)\n" |
Jiawei Shao | cf8ad76 | 2018-09-21 09:11:35 +0800 | [diff] [blame] | 908 | " {\n"; |
| 909 | for (size_t component = 0; component < kHLSLGatherFunctions.size(); ++component) |
| 910 | { |
| 911 | out << " case " << component << ":\n" |
| 912 | << " return " << textureReference << "." << kHLSLGatherFunctions[component] |
| 913 | << "(" << samplerReference << ", " << samplerCoordString; |
| 914 | if (textureFunction.offset) |
| 915 | { |
| 916 | out << ", offset"; |
| 917 | } |
| 918 | out << ");\n"; |
| 919 | } |
| 920 | |
| 921 | out << " default:\n" |
Jiawei Shao | a977acc | 2018-09-19 12:46:05 +0800 | [diff] [blame] | 922 | " return float4(0.0, 0.0, 0.0, 1.0);\n" |
| 923 | " }\n"; |
| 924 | } |
| 925 | |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 926 | void OutputTextureSampleFunctionReturnStatement( |
| 927 | TInfoSinkBase &out, |
| 928 | const TextureFunctionHLSL::TextureFunction &textureFunction, |
| 929 | const ShShaderOutput outputType, |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 930 | const ImmutableString &textureReference, |
| 931 | const ImmutableString &samplerReference, |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 932 | const ImmutableString &texCoordX, |
| 933 | const ImmutableString &texCoordY, |
| 934 | const ImmutableString &texCoordZ) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 935 | { |
| 936 | out << " return "; |
| 937 | |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 938 | if (IsIntegerSampler(textureFunction.sampler) && !IsSamplerCube(textureFunction.sampler) && |
| 939 | textureFunction.method != TextureFunctionHLSL::TextureFunction::FETCH) |
| 940 | { |
| 941 | out << " useBorderColor ? "; |
| 942 | if (IsIntegerSamplerUnsigned(textureFunction.sampler)) |
| 943 | { |
| 944 | out << "asuint"; |
| 945 | } |
| 946 | out << "(samplerMetadata[samplerIndex].intBorderColor) : "; |
| 947 | } |
| 948 | |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 949 | // HLSL intrinsic |
| 950 | if (outputType == SH_HLSL_3_0_OUTPUT) |
| 951 | { |
| 952 | switch (textureFunction.sampler) |
| 953 | { |
| 954 | case EbtSampler2D: |
Geoff Lang | b66a909 | 2016-05-16 15:59:14 -0400 | [diff] [blame] | 955 | case EbtSamplerExternalOES: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 956 | out << "tex2D"; |
| 957 | break; |
| 958 | case EbtSamplerCube: |
| 959 | out << "texCUBE"; |
| 960 | break; |
| 961 | default: |
| 962 | UNREACHABLE(); |
| 963 | } |
| 964 | |
| 965 | switch (textureFunction.method) |
| 966 | { |
| 967 | case TextureFunctionHLSL::TextureFunction::IMPLICIT: |
| 968 | out << "(" << samplerReference << ", "; |
| 969 | break; |
| 970 | case TextureFunctionHLSL::TextureFunction::BIAS: |
| 971 | out << "bias(" << samplerReference << ", "; |
| 972 | break; |
| 973 | case TextureFunctionHLSL::TextureFunction::LOD: |
| 974 | out << "lod(" << samplerReference << ", "; |
| 975 | break; |
| 976 | case TextureFunctionHLSL::TextureFunction::LOD0: |
| 977 | out << "lod(" << samplerReference << ", "; |
| 978 | break; |
| 979 | case TextureFunctionHLSL::TextureFunction::LOD0BIAS: |
| 980 | out << "lod(" << samplerReference << ", "; |
| 981 | break; |
Geoff Lang | ba992ab | 2017-04-19 11:18:14 -0400 | [diff] [blame] | 982 | case TextureFunctionHLSL::TextureFunction::GRAD: |
| 983 | out << "grad(" << samplerReference << ", "; |
| 984 | break; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 985 | default: |
| 986 | UNREACHABLE(); |
| 987 | } |
| 988 | } |
| 989 | else if (outputType == SH_HLSL_4_1_OUTPUT || outputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
| 990 | { |
| 991 | OutputHLSL4SampleFunctionPrefix(out, textureFunction, textureReference, samplerReference); |
| 992 | } |
| 993 | else |
| 994 | UNREACHABLE(); |
| 995 | |
| 996 | const int hlslCoords = GetHLSLCoordCount(textureFunction, outputType); |
| 997 | |
| 998 | out << GetSamplerCoordinateTypeString(textureFunction, hlslCoords) << "(" << texCoordX << ", " |
| 999 | << texCoordY; |
| 1000 | |
| 1001 | if (outputType == SH_HLSL_3_0_OUTPUT) |
| 1002 | { |
| 1003 | if (hlslCoords >= 3) |
| 1004 | { |
| 1005 | if (textureFunction.coords < 3) |
| 1006 | { |
| 1007 | out << ", 0"; |
| 1008 | } |
| 1009 | else |
| 1010 | { |
| 1011 | out << ", " << texCoordZ; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | if (hlslCoords == 4) |
| 1016 | { |
| 1017 | switch (textureFunction.method) |
| 1018 | { |
| 1019 | case TextureFunctionHLSL::TextureFunction::BIAS: |
| 1020 | out << ", bias"; |
| 1021 | break; |
| 1022 | case TextureFunctionHLSL::TextureFunction::LOD: |
| 1023 | out << ", lod"; |
| 1024 | break; |
| 1025 | case TextureFunctionHLSL::TextureFunction::LOD0: |
| 1026 | out << ", 0"; |
| 1027 | break; |
| 1028 | case TextureFunctionHLSL::TextureFunction::LOD0BIAS: |
| 1029 | out << ", bias"; |
| 1030 | break; |
| 1031 | default: |
| 1032 | UNREACHABLE(); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | out << ")"; |
| 1037 | } |
| 1038 | else if (outputType == SH_HLSL_4_1_OUTPUT || outputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
| 1039 | { |
| 1040 | if (hlslCoords >= 3) |
| 1041 | { |
| 1042 | ASSERT(!IsIntegerSampler(textureFunction.sampler) || |
| 1043 | !IsSamplerCube(textureFunction.sampler) || texCoordZ == "face"); |
| 1044 | out << ", " << texCoordZ; |
| 1045 | } |
| 1046 | |
| 1047 | if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD) |
| 1048 | { |
| 1049 | if (IsIntegerSampler(textureFunction.sampler)) |
| 1050 | { |
| 1051 | out << ", mip)"; |
| 1052 | } |
| 1053 | else if (IsShadowSampler(textureFunction.sampler)) |
| 1054 | { |
| 1055 | // Compare value |
| 1056 | if (textureFunction.proj) |
| 1057 | { |
| 1058 | // According to ESSL 3.00.4 sec 8.8 p95 on textureProj: |
| 1059 | // The resulting third component of P' in the shadow forms is used as |
| 1060 | // Dref |
| 1061 | out << "), " << texCoordZ; |
| 1062 | } |
| 1063 | else |
| 1064 | { |
| 1065 | switch (textureFunction.coords) |
| 1066 | { |
| 1067 | case 3: |
| 1068 | out << "), t.z"; |
| 1069 | break; |
| 1070 | case 4: |
| 1071 | out << "), t.w"; |
| 1072 | break; |
| 1073 | default: |
| 1074 | UNREACHABLE(); |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | else |
| 1079 | { |
| 1080 | out << "), ddx, ddy"; |
| 1081 | } |
| 1082 | } |
| 1083 | else if (IsIntegerSampler(textureFunction.sampler) || |
| 1084 | textureFunction.method == TextureFunctionHLSL::TextureFunction::FETCH) |
| 1085 | { |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 1086 | if (IsSampler2DMS(textureFunction.sampler) || |
| 1087 | IsSampler2DMSArray(textureFunction.sampler)) |
JiangYizhou | 5b03f47 | 2017-01-09 10:22:53 +0800 | [diff] [blame] | 1088 | out << "), index"; |
| 1089 | else |
| 1090 | out << ", mip)"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1091 | } |
| 1092 | else if (IsShadowSampler(textureFunction.sampler)) |
| 1093 | { |
| 1094 | // Compare value |
| 1095 | if (textureFunction.proj) |
| 1096 | { |
| 1097 | // According to ESSL 3.00.4 sec 8.8 p95 on textureProj: |
| 1098 | // The resulting third component of P' in the shadow forms is used as Dref |
| 1099 | out << "), " << texCoordZ; |
| 1100 | } |
| 1101 | else |
| 1102 | { |
| 1103 | switch (textureFunction.coords) |
| 1104 | { |
| 1105 | case 3: |
| 1106 | out << "), t.z"; |
| 1107 | break; |
| 1108 | case 4: |
| 1109 | out << "), t.w"; |
| 1110 | break; |
| 1111 | default: |
| 1112 | UNREACHABLE(); |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | else |
| 1117 | { |
| 1118 | switch (textureFunction.method) |
| 1119 | { |
| 1120 | case TextureFunctionHLSL::TextureFunction::IMPLICIT: |
| 1121 | out << ")"; |
| 1122 | break; |
| 1123 | case TextureFunctionHLSL::TextureFunction::BIAS: |
| 1124 | out << "), bias"; |
| 1125 | break; |
| 1126 | case TextureFunctionHLSL::TextureFunction::LOD: |
| 1127 | out << "), lod"; |
| 1128 | break; |
| 1129 | case TextureFunctionHLSL::TextureFunction::LOD0: |
| 1130 | out << "), 0"; |
| 1131 | break; |
| 1132 | case TextureFunctionHLSL::TextureFunction::LOD0BIAS: |
| 1133 | out << "), bias"; |
| 1134 | break; |
| 1135 | default: |
| 1136 | UNREACHABLE(); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | if (textureFunction.offset && |
| 1141 | (!IsIntegerSampler(textureFunction.sampler) || |
| 1142 | textureFunction.method == TextureFunctionHLSL::TextureFunction::FETCH)) |
| 1143 | { |
| 1144 | out << ", offset"; |
| 1145 | } |
| 1146 | } |
| 1147 | else |
| 1148 | UNREACHABLE(); |
| 1149 | |
| 1150 | out << ");\n"; // Close the sample function call and return statement |
| 1151 | } |
| 1152 | |
| 1153 | } // Anonymous namespace |
| 1154 | |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1155 | ImmutableString TextureFunctionHLSL::TextureFunction::name() const |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1156 | { |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1157 | static const ImmutableString kGlTextureName("gl_texture"); |
| 1158 | |
| 1159 | ImmutableString suffix(TextureTypeSuffix(this->sampler)); |
| 1160 | |
| 1161 | ImmutableStringBuilder name(kGlTextureName.length() + suffix.length() + 4u + 6u + 5u); |
| 1162 | |
| 1163 | name << kGlTextureName; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1164 | |
| 1165 | // We need to include full the sampler type in the function name to make the signature unique |
| 1166 | // on D3D11, where samplers are passed to texture functions as indices. |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1167 | name << suffix; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1168 | |
| 1169 | if (proj) |
| 1170 | { |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1171 | name << "Proj"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | if (offset) |
| 1175 | { |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1176 | name << "Offset"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | switch (method) |
| 1180 | { |
| 1181 | case IMPLICIT: |
| 1182 | break; |
| 1183 | case BIAS: |
| 1184 | break; // Extra parameter makes the signature unique |
| 1185 | case LOD: |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1186 | name << "Lod"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1187 | break; |
| 1188 | case LOD0: |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1189 | name << "Lod0"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1190 | break; |
| 1191 | case LOD0BIAS: |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1192 | name << "Lod0"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1193 | break; // Extra parameter makes the signature unique |
| 1194 | case SIZE: |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1195 | name << "Size"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1196 | break; |
| 1197 | case FETCH: |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1198 | name << "Fetch"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1199 | break; |
| 1200 | case GRAD: |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1201 | name << "Grad"; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1202 | break; |
Jiawei Shao | a977acc | 2018-09-19 12:46:05 +0800 | [diff] [blame] | 1203 | case GATHER: |
| 1204 | name << "Gather"; |
| 1205 | break; |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1206 | default: |
| 1207 | UNREACHABLE(); |
| 1208 | } |
| 1209 | |
| 1210 | return name; |
| 1211 | } |
| 1212 | |
| 1213 | const char *TextureFunctionHLSL::TextureFunction::getReturnType() const |
| 1214 | { |
| 1215 | if (method == TextureFunction::SIZE) |
| 1216 | { |
| 1217 | switch (sampler) |
| 1218 | { |
| 1219 | case EbtSampler2D: |
| 1220 | case EbtISampler2D: |
| 1221 | case EbtUSampler2D: |
| 1222 | case EbtSampler2DShadow: |
| 1223 | case EbtSamplerCube: |
| 1224 | case EbtISamplerCube: |
| 1225 | case EbtUSamplerCube: |
| 1226 | case EbtSamplerCubeShadow: |
Ian Ewell | bda7559 | 2016-04-18 17:25:54 -0400 | [diff] [blame] | 1227 | case EbtSamplerExternalOES: |
Olli Etuaho | 92db39e | 2017-02-15 12:11:04 +0000 | [diff] [blame] | 1228 | case EbtSampler2DMS: |
| 1229 | case EbtISampler2DMS: |
| 1230 | case EbtUSampler2DMS: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1231 | return "int2"; |
| 1232 | case EbtSampler3D: |
| 1233 | case EbtISampler3D: |
| 1234 | case EbtUSampler3D: |
| 1235 | case EbtSampler2DArray: |
| 1236 | case EbtISampler2DArray: |
| 1237 | case EbtUSampler2DArray: |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 1238 | case EbtSampler2DMSArray: |
| 1239 | case EbtISampler2DMSArray: |
| 1240 | case EbtUSampler2DMSArray: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1241 | case EbtSampler2DArrayShadow: |
| 1242 | return "int3"; |
| 1243 | default: |
| 1244 | UNREACHABLE(); |
| 1245 | } |
| 1246 | } |
| 1247 | else // Sampling function |
| 1248 | { |
| 1249 | switch (sampler) |
| 1250 | { |
| 1251 | case EbtSampler2D: |
JiangYizhou | 34bc315 | 2017-03-29 14:56:01 +0800 | [diff] [blame] | 1252 | case EbtSampler2DMS: |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 1253 | case EbtSampler2DMSArray: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1254 | case EbtSampler3D: |
| 1255 | case EbtSamplerCube: |
| 1256 | case EbtSampler2DArray: |
Ian Ewell | bda7559 | 2016-04-18 17:25:54 -0400 | [diff] [blame] | 1257 | case EbtSamplerExternalOES: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1258 | return "float4"; |
| 1259 | case EbtISampler2D: |
JiangYizhou | 34bc315 | 2017-03-29 14:56:01 +0800 | [diff] [blame] | 1260 | case EbtISampler2DMS: |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 1261 | case EbtISampler2DMSArray: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1262 | case EbtISampler3D: |
| 1263 | case EbtISamplerCube: |
| 1264 | case EbtISampler2DArray: |
| 1265 | return "int4"; |
| 1266 | case EbtUSampler2D: |
JiangYizhou | 34bc315 | 2017-03-29 14:56:01 +0800 | [diff] [blame] | 1267 | case EbtUSampler2DMS: |
Olli Etuaho | 2da0453 | 2018-08-24 13:59:44 +0300 | [diff] [blame] | 1268 | case EbtUSampler2DMSArray: |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1269 | case EbtUSampler3D: |
| 1270 | case EbtUSamplerCube: |
| 1271 | case EbtUSampler2DArray: |
| 1272 | return "uint4"; |
| 1273 | case EbtSampler2DShadow: |
| 1274 | case EbtSamplerCubeShadow: |
| 1275 | case EbtSampler2DArrayShadow: |
Jiawei Shao | 19b51d2 | 2018-09-19 15:14:45 +0800 | [diff] [blame] | 1276 | if (method == TextureFunctionHLSL::TextureFunction::GATHER) |
| 1277 | { |
| 1278 | return "float4"; |
| 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | return "float"; |
| 1283 | } |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1284 | default: |
| 1285 | UNREACHABLE(); |
| 1286 | } |
| 1287 | } |
| 1288 | return ""; |
| 1289 | } |
| 1290 | |
| 1291 | bool TextureFunctionHLSL::TextureFunction::operator<(const TextureFunction &rhs) const |
| 1292 | { |
Geoff Lang | 28a97ee | 2016-09-22 13:01:26 -0400 | [diff] [blame] | 1293 | return std::tie(sampler, coords, proj, offset, method) < |
| 1294 | std::tie(rhs.sampler, rhs.coords, rhs.proj, rhs.offset, rhs.method); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1295 | } |
| 1296 | |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1297 | ImmutableString TextureFunctionHLSL::useTextureFunction(const ImmutableString &name, |
| 1298 | TBasicType samplerType, |
| 1299 | int coords, |
| 1300 | size_t argumentCount, |
| 1301 | bool lod0, |
| 1302 | sh::GLenum shaderType) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1303 | { |
| 1304 | TextureFunction textureFunction; |
| 1305 | textureFunction.sampler = samplerType; |
| 1306 | textureFunction.coords = coords; |
| 1307 | textureFunction.method = TextureFunction::IMPLICIT; |
| 1308 | textureFunction.proj = false; |
| 1309 | textureFunction.offset = false; |
| 1310 | |
| 1311 | if (name == "texture2D" || name == "textureCube" || name == "texture") |
| 1312 | { |
| 1313 | textureFunction.method = TextureFunction::IMPLICIT; |
| 1314 | } |
| 1315 | else if (name == "texture2DProj" || name == "textureProj") |
| 1316 | { |
| 1317 | textureFunction.method = TextureFunction::IMPLICIT; |
| 1318 | textureFunction.proj = true; |
| 1319 | } |
| 1320 | else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" || |
| 1321 | name == "texture2DLodEXT" || name == "textureCubeLodEXT") |
| 1322 | { |
| 1323 | textureFunction.method = TextureFunction::LOD; |
| 1324 | } |
| 1325 | else if (name == "texture2DProjLod" || name == "textureProjLod" || |
| 1326 | name == "texture2DProjLodEXT") |
| 1327 | { |
| 1328 | textureFunction.method = TextureFunction::LOD; |
| 1329 | textureFunction.proj = true; |
| 1330 | } |
| 1331 | else if (name == "textureSize") |
| 1332 | { |
| 1333 | textureFunction.method = TextureFunction::SIZE; |
| 1334 | } |
| 1335 | else if (name == "textureOffset") |
| 1336 | { |
| 1337 | textureFunction.method = TextureFunction::IMPLICIT; |
| 1338 | textureFunction.offset = true; |
| 1339 | } |
| 1340 | else if (name == "textureProjOffset") |
| 1341 | { |
| 1342 | textureFunction.method = TextureFunction::IMPLICIT; |
| 1343 | textureFunction.offset = true; |
| 1344 | textureFunction.proj = true; |
| 1345 | } |
| 1346 | else if (name == "textureLodOffset") |
| 1347 | { |
| 1348 | textureFunction.method = TextureFunction::LOD; |
| 1349 | textureFunction.offset = true; |
| 1350 | } |
| 1351 | else if (name == "textureProjLodOffset") |
| 1352 | { |
| 1353 | textureFunction.method = TextureFunction::LOD; |
| 1354 | textureFunction.proj = true; |
| 1355 | textureFunction.offset = true; |
| 1356 | } |
| 1357 | else if (name == "texelFetch") |
| 1358 | { |
| 1359 | textureFunction.method = TextureFunction::FETCH; |
| 1360 | } |
| 1361 | else if (name == "texelFetchOffset") |
| 1362 | { |
| 1363 | textureFunction.method = TextureFunction::FETCH; |
| 1364 | textureFunction.offset = true; |
| 1365 | } |
| 1366 | else if (name == "textureGrad" || name == "texture2DGradEXT") |
| 1367 | { |
| 1368 | textureFunction.method = TextureFunction::GRAD; |
| 1369 | } |
| 1370 | else if (name == "textureGradOffset") |
| 1371 | { |
| 1372 | textureFunction.method = TextureFunction::GRAD; |
| 1373 | textureFunction.offset = true; |
| 1374 | } |
| 1375 | else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || |
| 1376 | name == "textureCubeGradEXT") |
| 1377 | { |
| 1378 | textureFunction.method = TextureFunction::GRAD; |
| 1379 | textureFunction.proj = true; |
| 1380 | } |
| 1381 | else if (name == "textureProjGradOffset") |
| 1382 | { |
| 1383 | textureFunction.method = TextureFunction::GRAD; |
| 1384 | textureFunction.proj = true; |
| 1385 | textureFunction.offset = true; |
| 1386 | } |
Jiawei Shao | a977acc | 2018-09-19 12:46:05 +0800 | [diff] [blame] | 1387 | else if (name == "textureGather") |
| 1388 | { |
| 1389 | textureFunction.method = TextureFunction::GATHER; |
| 1390 | } |
Jiawei Shao | cf8ad76 | 2018-09-21 09:11:35 +0800 | [diff] [blame] | 1391 | else if (name == "textureGatherOffset") |
| 1392 | { |
| 1393 | textureFunction.method = TextureFunction::GATHER; |
| 1394 | textureFunction.offset = true; |
| 1395 | } |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1396 | else |
| 1397 | UNREACHABLE(); |
| 1398 | |
| 1399 | if (textureFunction.method == |
| 1400 | TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument |
| 1401 | { |
| 1402 | size_t mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments |
| 1403 | |
| 1404 | if (textureFunction.offset) |
| 1405 | { |
| 1406 | mandatoryArgumentCount++; |
| 1407 | } |
| 1408 | |
| 1409 | bool bias = (argumentCount > mandatoryArgumentCount); // Bias argument is optional |
| 1410 | |
Xinghua Cao | 0d218da | 2018-12-17 11:53:52 +0800 | [diff] [blame] | 1411 | if (lod0 || shaderType == GL_VERTEX_SHADER || shaderType == GL_COMPUTE_SHADER) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1412 | { |
| 1413 | if (bias) |
| 1414 | { |
| 1415 | textureFunction.method = TextureFunction::LOD0BIAS; |
| 1416 | } |
| 1417 | else |
| 1418 | { |
| 1419 | textureFunction.method = TextureFunction::LOD0; |
| 1420 | } |
| 1421 | } |
| 1422 | else if (bias) |
| 1423 | { |
| 1424 | textureFunction.method = TextureFunction::BIAS; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | mUsesTexture.insert(textureFunction); |
| 1429 | return textureFunction.name(); |
| 1430 | } |
| 1431 | |
Geoff Lang | 1fe74c7 | 2016-08-25 13:23:01 -0400 | [diff] [blame] | 1432 | void TextureFunctionHLSL::textureFunctionHeader(TInfoSinkBase &out, |
| 1433 | const ShShaderOutput outputType, |
| 1434 | bool getDimensionsIgnoresBaseLevel) |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1435 | { |
| 1436 | for (const TextureFunction &textureFunction : mUsesTexture) |
| 1437 | { |
| 1438 | // Function header |
| 1439 | out << textureFunction.getReturnType() << " " << textureFunction.name() << "("; |
| 1440 | |
| 1441 | OutputTextureFunctionArgumentList(out, textureFunction, outputType); |
| 1442 | |
| 1443 | out << ")\n" |
| 1444 | "{\n"; |
| 1445 | |
| 1446 | // In some cases we use a variable to store the texture/sampler objects, but to work around |
| 1447 | // a D3D11 compiler bug related to discard inside a loop that is conditional on texture |
| 1448 | // sampling we need to call the function directly on references to the texture and sampler |
| 1449 | // arrays. The bug was found using dEQP-GLES3.functional.shaders.discard*loop_texture* |
| 1450 | // tests. |
Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame] | 1451 | ImmutableString textureReference(""); |
| 1452 | ImmutableString samplerReference(""); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1453 | GetTextureReference(out, textureFunction, outputType, &textureReference, &samplerReference); |
| 1454 | |
| 1455 | if (textureFunction.method == TextureFunction::SIZE) |
| 1456 | { |
Geoff Lang | 1fe74c7 | 2016-08-25 13:23:01 -0400 | [diff] [blame] | 1457 | OutputTextureSizeFunctionBody(out, textureFunction, textureReference, |
| 1458 | getDimensionsIgnoresBaseLevel); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1459 | } |
| 1460 | else |
| 1461 | { |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 1462 | ImmutableString texCoordX("t.x"); |
| 1463 | ImmutableString texCoordY("t.y"); |
| 1464 | ImmutableString texCoordZ("t.z"); |
Jiawei Shao | a977acc | 2018-09-19 12:46:05 +0800 | [diff] [blame] | 1465 | if (textureFunction.method == TextureFunction::GATHER) |
| 1466 | { |
| 1467 | OutputTextureGatherFunctionBody(out, textureFunction, outputType, textureReference, |
| 1468 | samplerReference, texCoordX, texCoordY, texCoordZ); |
| 1469 | } |
| 1470 | else |
| 1471 | { |
| 1472 | ProjectTextureCoordinates(textureFunction, &texCoordX, &texCoordY, &texCoordZ); |
Anders Leino | f6cbe44 | 2019-04-18 15:32:07 +0300 | [diff] [blame] | 1473 | OutputIntegerTextureSampleFunctionComputations( |
| 1474 | out, textureFunction, outputType, textureReference, &texCoordX, &texCoordY, |
| 1475 | &texCoordZ, getDimensionsIgnoresBaseLevel); |
Jiawei Shao | a977acc | 2018-09-19 12:46:05 +0800 | [diff] [blame] | 1476 | OutputTextureSampleFunctionReturnStatement(out, textureFunction, outputType, |
| 1477 | textureReference, samplerReference, |
| 1478 | texCoordX, texCoordY, texCoordZ); |
| 1479 | } |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | out << "}\n" |
| 1483 | "\n"; |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | } // namespace sh |