blob: 28208bcae829bedca3afaedbbe459b00c81f67de [file] [log] [blame]
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001//
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 Etuaho12c03762018-01-25 12:22:33 +020014#include "compiler/translator/ImmutableStringBuilder.h"
Olli Etuaho5858f7e2016-04-08 13:08:46 +030015#include "compiler/translator/UtilsHLSL.h"
16
17namespace sh
18{
19
20namespace
21{
22
23void OutputIntTexCoordWrap(TInfoSinkBase &out,
24 const char *wrapMode,
25 const char *size,
Olli Etuahob4cc49f2018-01-25 14:37:06 +020026 const ImmutableString &texCoord,
27 const char *texCoordOffset,
Olli Etuaho5858f7e2016-04-08 13:08:46 +030028 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 Rathmannb8543632018-10-02 19:46:14 +020035 out << "bool " << texCoordOutName << "UseBorderColor = false;\n";
Olli Etuaho5858f7e2016-04-08 13:08:46 +030036
37 // CLAMP_TO_EDGE
Till Rathmannb8543632018-10-02 19:46:14 +020038 out << "if (" << wrapMode << " == 0)\n";
Olli Etuaho5858f7e2016-04-08 13:08:46 +030039 out << "{\n";
40 out << " " << texCoordOutName << " = clamp(int(floor(" << size << " * " << texCoordOutName
41 << "Offset)), 0, int(" << size << ") - 1);\n";
42 out << "}\n";
43
Till Rathmannb8543632018-10-02 19:46:14 +020044 // CLAMP_TO_BORDER
Olli Etuaho5858f7e2016-04-08 13:08:46 +030045 out << "else if (" << wrapMode << " == 3)\n";
46 out << "{\n";
Till Rathmannb8543632018-10-02 19:46:14 +020047 out << " int texCoordInt = int(floor(" << size << " * " << texCoordOutName << "Offset));\n";
48 out << " " << texCoordOutName << " = clamp(texCoordInt, 0, int(" << size << ") - 1);\n";
Jamie Madillb980c562018-11-27 11:34:27 -050049 out << " " << texCoordOutName << "UseBorderColor = (texCoordInt != " << texCoordOutName
50 << ");\n";
Till Rathmannb8543632018-10-02 19:46:14 +020051 out << "}\n";
52
53 // MIRRORED_REPEAT
54 out << "else if (" << wrapMode << " == 2)\n";
55 out << "{\n";
Olli Etuaho5858f7e2016-04-08 13:08:46 +030056 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
69void OutputIntTexCoordWraps(TInfoSinkBase &out,
70 const TextureFunctionHLSL::TextureFunction &textureFunction,
Olli Etuahob4cc49f2018-01-25 14:37:06 +020071 ImmutableString *texCoordX,
72 ImmutableString *texCoordY,
73 ImmutableString *texCoordZ)
Olli Etuaho5858f7e2016-04-08 13:08:46 +030074{
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 Etuahob4cc49f2018-01-25 14:37:06 +020085 *texCoordX = ImmutableString("tix");
Olli Etuaho5858f7e2016-04-08 13:08:46 +030086 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 Etuahob4cc49f2018-01-25 14:37:06 +020095 *texCoordY = ImmutableString("tiy");
Olli Etuaho5858f7e2016-04-08 13:08:46 +030096
Till Rathmannb8543632018-10-02 19:46:14 +020097 bool tizAvailable = false;
98
Olli Etuaho5858f7e2016-04-08 13:08:46 +030099 if (IsSamplerArray(textureFunction.sampler))
100 {
Olli Etuahob4cc49f2018-01-25 14:37:06 +0200101 *texCoordZ = ImmutableString("int(max(0, min(layers - 1, floor(0.5 + t.z))))");
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300102 }
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 Rathmannb8543632018-10-02 19:46:14 +0200114 *texCoordZ = ImmutableString("tiz");
115 tizAvailable = true;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300116 }
Till Rathmannb8543632018-10-02 19:46:14 +0200117
118 out << "bool useBorderColor = tixUseBorderColor || tiyUseBorderColor"
119 << (tizAvailable ? " || tizUseBorderColor" : "") << ";\n";
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300120}
121
122void OutputHLSL4SampleFunctionPrefix(TInfoSinkBase &out,
123 const TextureFunctionHLSL::TextureFunction &textureFunction,
Olli Etuaho12c03762018-01-25 12:22:33 +0200124 const ImmutableString &textureReference,
125 const ImmutableString &samplerReference)
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300126{
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
178const char *GetSamplerCoordinateTypeString(
179 const TextureFunctionHLSL::TextureFunction &textureFunction,
180 int hlslCoords)
181{
Jiawei Shaoa1ac3fe2018-10-10 12:29:31 +0800182 // 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 Etuaho5858f7e2016-04-08 13:08:46 +0300187 textureFunction.method == TextureFunctionHLSL::TextureFunction::FETCH)
188 {
189 switch (hlslCoords)
190 {
191 case 2:
Olli Etuaho2da04532018-08-24 13:59:44 +0300192 if (IsSampler2DMS(textureFunction.sampler))
193 {
JiangYizhou5b03f472017-01-09 10:22:53 +0800194 return "int2";
Olli Etuaho2da04532018-08-24 13:59:44 +0300195 }
JiangYizhou5b03f472017-01-09 10:22:53 +0800196 else
Olli Etuaho2da04532018-08-24 13:59:44 +0300197 {
JiangYizhou5b03f472017-01-09 10:22:53 +0800198 return "int3";
Olli Etuaho2da04532018-08-24 13:59:44 +0300199 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300200 case 3:
Olli Etuaho2da04532018-08-24 13:59:44 +0300201 if (IsSampler2DMSArray(textureFunction.sampler))
202 {
203 return "int3";
204 }
205 else
206 {
207 return "int4";
208 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300209 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
230int 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 Langb66a9092016-05-16 15:59:14 -0400239 case EbtSamplerExternalOES:
JiangYizhou5b03f472017-01-09 10:22:53 +0800240 case EbtSampler2DMS:
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300241 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 Langba992ab2017-04-19 11:18:14 -0400253 case TextureFunctionHLSL::TextureFunction::GRAD:
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300254 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 Etuaho2da04532018-08-24 13:59:44 +0300266 if (IsSampler3D(textureFunction.sampler) || IsSamplerArray(textureFunction.sampler) ||
267 IsSamplerCube(textureFunction.sampler))
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300268 {
Olli Etuaho2da04532018-08-24 13:59:44 +0300269 return 3;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300270 }
Olli Etuaho2da04532018-08-24 13:59:44 +0300271 ASSERT(IsSampler2D(textureFunction.sampler));
272 return 2;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300273 }
274 return 0;
275}
276
277void 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 Langb66a9092016-05-16 15:59:14 -0400286 case EbtSamplerExternalOES:
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300287 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 Madill8aeeed62017-03-15 18:09:26 -0400306 // 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 Etuaho5858f7e2016-04-08 13:08:46 +0300310 }
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 Etuaho92db39e2017-02-15 12:11:04 +0000332 case 0:
333 break; // textureSize(gSampler2DMS sampler)
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300334 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 Ewellbda75592016-04-18 17:25:54 -0400363 case EbtSamplerExternalOES:
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300364 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 Etuaho2da04532018-08-24 13:59:44 +0300396 if (IsSampler2DMS(textureFunction.sampler) ||
397 IsSampler2DMSArray(textureFunction.sampler))
JiangYizhou5b03f472017-01-09 10:22:53 +0800398 out << ", int index";
399 else
400 out << ", int mip";
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300401 break;
402 case TextureFunctionHLSL::TextureFunction::GRAD:
403 break;
Jiawei Shaoa977acc2018-09-19 12:46:05 +0800404 case TextureFunctionHLSL::TextureFunction::GATHER:
405 break;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300406 default:
407 UNREACHABLE();
408 }
409
Jiawei Shao19b51d22018-09-19 15:14:45 +0800410 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GATHER &&
411 IsShadowSampler(textureFunction.sampler))
412 {
413 out << ", float refZ";
414 }
415
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300416 if (textureFunction.offset)
417 {
418 switch (textureFunction.sampler)
419 {
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300420 case EbtSampler3D:
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300421 case EbtISampler3D:
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300422 case EbtUSampler3D:
423 out << ", int3 offset";
424 break;
Andi-Bogdan Postelnicu9e77ce32016-09-27 17:05:44 +0300425 case EbtSampler2D:
426 case EbtSampler2DArray:
427 case EbtISampler2D:
428 case EbtISampler2DArray:
429 case EbtUSampler2D:
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300430 case EbtUSampler2DArray:
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300431 case EbtSampler2DShadow:
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300432 case EbtSampler2DArrayShadow:
Ian Ewellbda75592016-04-18 17:25:54 -0400433 case EbtSamplerExternalOES:
434 out << ", int2 offset";
Andi-Bogdan Postelnicu9e77ce32016-09-27 17:05:44 +0300435 break;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300436 default:
Olli Etuaho2da04532018-08-24 13:59:44 +0300437 // Offset is not supported for multisampled textures.
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300438 UNREACHABLE();
439 }
440 }
441
442 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS ||
443 textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0BIAS)
444 {
445 out << ", float bias";
446 }
Jiawei Shao19b51d22018-09-19 15:14:45 +0800447 else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GATHER &&
448 !IsShadowSampler(textureFunction.sampler))
Jiawei Shaoa977acc2018-09-19 12:46:05 +0800449 {
450 out << ", int comp = 0";
451 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300452}
453
454void GetTextureReference(TInfoSinkBase &out,
455 const TextureFunctionHLSL::TextureFunction &textureFunction,
456 const ShShaderOutput outputType,
Olli Etuaho12c03762018-01-25 12:22:33 +0200457 ImmutableString *textureReference,
458 ImmutableString *samplerReference)
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300459{
460 if (outputType == SH_HLSL_4_1_OUTPUT)
461 {
Olli Etuaho12c03762018-01-25 12:22:33 +0200462 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 Etuaho5858f7e2016-04-08 13:08:46 +0300469 if (TextureGroup(textureFunction.sampler) == HLSL_TEXTURE_2D)
470 {
Olli Etuaho12c03762018-01-25 12:22:33 +0200471 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 Etuaho5858f7e2016-04-08 13:08:46 +0300479 }
480 else
481 {
Olli Etuaho12c03762018-01-25 12:22:33 +0200482 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 Etuaho5858f7e2016-04-08 13:08:46 +0300495 }
496 }
497 else
498 {
Olli Etuaho12c03762018-01-25 12:22:33 +0200499 *textureReference = ImmutableString("x");
500 *samplerReference = ImmutableString("s");
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300501 }
502}
503
504void OutputTextureSizeFunctionBody(TInfoSinkBase &out,
505 const TextureFunctionHLSL::TextureFunction &textureFunction,
Olli Etuaho12c03762018-01-25 12:22:33 +0200506 const ImmutableString &textureReference,
Geoff Lang1fe74c72016-08-25 13:23:01 -0400507 bool getDimensionsIgnoresBaseLevel)
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300508{
Olli Etuaho92db39e2017-02-15 12:11:04 +0000509 if (IsSampler2DMS(textureFunction.sampler))
Geoff Lang1fe74c72016-08-25 13:23:01 -0400510 {
Olli Etuaho92db39e2017-02-15 12:11:04 +0000511 out << " uint width; uint height; uint samples;\n"
512 << " " << textureReference << ".GetDimensions(width, height, samples);\n";
Geoff Lang1fe74c72016-08-25 13:23:01 -0400513 }
Olli Etuaho2da04532018-08-24 13:59:44 +0300514 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 Lang1fe74c72016-08-25 13:23:01 -0400519 else
520 {
Olli Etuaho92db39e2017-02-15 12:11:04 +0000521 if (getDimensionsIgnoresBaseLevel)
Geoff Lang1fe74c72016-08-25 13:23:01 -0400522 {
Olli Etuaho92db39e2017-02-15 12:11:04 +0000523 out << " int baseLevel = samplerMetadata[samplerIndex].baseLevel;\n";
Geoff Lang1fe74c72016-08-25 13:23:01 -0400524 }
Olli Etuaho92db39e2017-02-15 12:11:04 +0000525 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 Etuaho5858f7e2016-04-08 13:08:46 +0300555 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300556
557 if (strcmp(textureFunction.getReturnType(), "int3") == 0)
558 {
Olli Etuaho92db39e2017-02-15 12:11:04 +0000559 out << " return int3(width, height, depth);\n";
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300560 }
561 else
562 {
Olli Etuaho92db39e2017-02-15 12:11:04 +0000563 out << " return int2(width, height);\n";
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300564 }
565}
566
567void ProjectTextureCoordinates(const TextureFunctionHLSL::TextureFunction &textureFunction,
Olli Etuahob4cc49f2018-01-25 14:37:06 +0200568 ImmutableString *texCoordX,
569 ImmutableString *texCoordY,
570 ImmutableString *texCoordZ)
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300571{
572 if (textureFunction.proj)
573 {
Olli Etuahob4cc49f2018-01-25 14:37:06 +0200574 ImmutableString proj("");
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300575 switch (textureFunction.coords)
576 {
577 case 3:
Olli Etuahob4cc49f2018-01-25 14:37:06 +0200578 proj = ImmutableString(" / t.z");
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300579 break;
580 case 4:
Olli Etuahob4cc49f2018-01-25 14:37:06 +0200581 proj = ImmutableString(" / t.w");
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300582 break;
583 default:
584 UNREACHABLE();
585 }
Olli Etuahob4cc49f2018-01-25 14:37:06 +0200586 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 Etuaho5858f7e2016-04-08 13:08:46 +0300595 }
596}
597
598void OutputIntegerTextureSampleFunctionComputations(
599 TInfoSinkBase &out,
600 const TextureFunctionHLSL::TextureFunction &textureFunction,
601 const ShShaderOutput outputType,
Olli Etuaho12c03762018-01-25 12:22:33 +0200602 const ImmutableString &textureReference,
Olli Etuahob4cc49f2018-01-25 14:37:06 +0200603 ImmutableString *texCoordX,
604 ImmutableString *texCoordY,
605 ImmutableString *texCoordZ)
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300606{
607 if (!IsIntegerSampler(textureFunction.sampler))
608 {
609 return;
610 }
611 if (IsSamplerCube(textureFunction.sampler))
612 {
613 out << " float width; float height; float layers; float levels;\n";
614
615 out << " uint mip = 0;\n";
616
617 out << " " << textureReference
618 << ".GetDimensions(mip, width, height, layers, levels);\n";
619
620 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
621 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
622 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
623 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || "
624 "(zMajor && t.z < 0.0f);\n";
625
626 // FACE_POSITIVE_X = 000b
627 // FACE_NEGATIVE_X = 001b
628 // FACE_POSITIVE_Y = 010b
629 // FACE_NEGATIVE_Y = 011b
630 // FACE_POSITIVE_Z = 100b
631 // FACE_NEGATIVE_Z = 101b
632 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
633
634 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
635 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
636 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
637
638 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
639 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
640
641 // Mip level computation.
642 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::IMPLICIT ||
643 textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD ||
644 textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD)
645 {
646 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::IMPLICIT)
647 {
648 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
649 " float2 dx = ddx(tSized);\n"
650 " float2 dy = ddy(tSized);\n"
651 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n";
652 }
653 else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD)
654 {
655 // ESSL 3.00.6 spec section 8.8: "For the cube version, the partial
656 // derivatives of P are assumed to be in the coordinate system used before
657 // texture coordinates are projected onto the appropriate cube face."
658 // ddx[0] and ddy[0] are the derivatives of t.x passed into the function
659 // ddx[1] and ddy[1] are the derivatives of t.y passed into the function
660 // ddx[2] and ddy[2] are the derivatives of t.z passed into the function
661 // Determine the derivatives of u, v and m
662 out << " float dudx = xMajor ? ddx[2] : (yMajor && t.y < 0.0f ? -ddx[0] "
663 ": ddx[0]);\n"
664 " float dudy = xMajor ? ddy[2] : (yMajor && t.y < 0.0f ? -ddy[0] "
665 ": ddy[0]);\n"
666 " float dvdx = yMajor ? ddx[2] : (negative ? ddx[1] : -ddx[1]);\n"
667 " float dvdy = yMajor ? ddy[2] : (negative ? ddy[1] : -ddy[1]);\n"
668 " float dmdx = xMajor ? ddx[0] : (yMajor ? ddx[1] : ddx[2]);\n"
669 " float dmdy = xMajor ? ddy[0] : (yMajor ? ddy[1] : ddy[2]);\n";
670 // Now determine the derivatives of the face coordinates, using the
671 // derivatives calculated above.
672 // d / dx (u(x) * 0.5 / m(x) + 0.5)
673 // = 0.5 * (m(x) * u'(x) - u(x) * m'(x)) / m(x)^2
674 out << " float dfacexdx = 0.5f * (m * dudx - u * dmdx) / (m * m);\n"
675 " float dfaceydx = 0.5f * (m * dvdx - v * dmdx) / (m * m);\n"
676 " float dfacexdy = 0.5f * (m * dudy - u * dmdy) / (m * m);\n"
677 " float dfaceydy = 0.5f * (m * dvdy - v * dmdy) / (m * m);\n"
678 " float2 sizeVec = float2(width, height);\n"
679 " float2 faceddx = float2(dfacexdx, dfaceydx) * sizeVec;\n"
680 " float2 faceddy = float2(dfacexdy, dfaceydy) * sizeVec;\n";
681 // Optimization: instead of: log2(max(length(faceddx), length(faceddy)))
682 // we compute: log2(max(length(faceddx)^2, length(faceddy)^2)) / 2
683 out << " float lengthfaceddx2 = dot(faceddx, faceddx);\n"
684 " float lengthfaceddy2 = dot(faceddy, faceddy);\n"
685 " float lod = log2(max(lengthfaceddx2, lengthfaceddy2)) * 0.5f;\n";
686 }
687 out << " mip = uint(min(max(round(lod), 0), levels - 1));\n"
688 << " " << textureReference
689 << ".GetDimensions(mip, width, height, layers, levels);\n";
690 }
691
692 // Convert from normalized floating-point to integer
Olli Etuahob4cc49f2018-01-25 14:37:06 +0200693 static const ImmutableString kXPrefix("int(floor(width * frac(");
694 static const ImmutableString kYPrefix("int(floor(height * frac(");
695 static const ImmutableString kSuffix(")))");
696 ImmutableStringBuilder texCoordXBuilder(kXPrefix.length() + texCoordX->length() +
697 kSuffix.length());
698 texCoordXBuilder << kXPrefix << *texCoordX << kSuffix;
699 *texCoordX = texCoordXBuilder;
700 ImmutableStringBuilder texCoordYBuilder(kYPrefix.length() + texCoordX->length() +
701 kSuffix.length());
702 texCoordYBuilder << kYPrefix << *texCoordY << kSuffix;
703 *texCoordY = texCoordYBuilder;
704 *texCoordZ = ImmutableString("face");
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300705 }
706 else if (textureFunction.method != TextureFunctionHLSL::TextureFunction::FETCH)
707 {
Olli Etuaho2da04532018-08-24 13:59:44 +0300708 if (IsSamplerArray(textureFunction.sampler))
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300709 {
Olli Etuaho2da04532018-08-24 13:59:44 +0300710 out << " float width; float height; float layers; float levels;\n";
711
712 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0)
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300713 {
Olli Etuaho2da04532018-08-24 13:59:44 +0300714 out << " uint mip = 0;\n";
715 }
716 else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0BIAS)
717 {
718 out << " uint mip = bias;\n";
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300719 }
720 else
721 {
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300722
723 out << " " << textureReference
Olli Etuaho2da04532018-08-24 13:59:44 +0300724 << ".GetDimensions(0, width, height, layers, levels);\n";
725 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::IMPLICIT ||
726 textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS)
727 {
728 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
729 " float dx = length(ddx(tSized));\n"
730 " float dy = length(ddy(tSized));\n"
731 " float lod = log2(max(dx, dy));\n";
732
733 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS)
734 {
735 out << " lod += bias;\n";
736 }
737 }
738 else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD)
739 {
740 out << " float2 sizeVec = float2(width, height);\n"
741 " float2 sizeDdx = ddx * sizeVec;\n"
742 " float2 sizeDdy = ddy * sizeVec;\n"
743 " float lod = log2(max(dot(sizeDdx, sizeDdx), "
744 "dot(sizeDdy, sizeDdy))) * 0.5f;\n";
745 }
746
747 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300748 }
Olli Etuaho2da04532018-08-24 13:59:44 +0300749
750 out << " " << textureReference
751 << ".GetDimensions(mip, width, height, layers, levels);\n";
752 }
753 else if (IsSampler2D(textureFunction.sampler))
754 {
755 out << " float width; float height; float levels;\n";
756
757 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0)
758 {
759 out << " uint mip = 0;\n";
760 }
761 else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0BIAS)
762 {
763 out << " uint mip = bias;\n";
764 }
765 else
766 {
767 out << " " << textureReference << ".GetDimensions(0, width, height, levels);\n";
768
769 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::IMPLICIT ||
770 textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS)
771 {
772 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
773 " float dx = length(ddx(tSized));\n"
774 " float dy = length(ddy(tSized));\n"
775 " float lod = log2(max(dx, dy));\n";
776
777 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS)
778 {
779 out << " lod += bias;\n";
780 }
781 }
782 else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD)
783 {
784 out << " float2 sizeVec = float2(width, height);\n"
785 " float2 sizeDdx = ddx * sizeVec;\n"
786 " float2 sizeDdy = ddy * sizeVec;\n"
787 " float lod = log2(max(dot(sizeDdx, sizeDdx), "
788 "dot(sizeDdy, sizeDdy))) * 0.5f;\n";
789 }
790
791 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
792 }
793
794 out << " " << textureReference << ".GetDimensions(mip, width, height, levels);\n";
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300795 }
796 else if (IsSampler3D(textureFunction.sampler))
797 {
798 out << " float width; float height; float depth; float levels;\n";
799
800 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0)
801 {
802 out << " uint mip = 0;\n";
803 }
804 else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::LOD0BIAS)
805 {
806 out << " uint mip = bias;\n";
807 }
808 else
809 {
810 out << " " << textureReference
811 << ".GetDimensions(0, width, height, depth, levels);\n";
812
813 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::IMPLICIT ||
814 textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS)
815 {
816 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
817 " float dx = length(ddx(tSized));\n"
818 " float dy = length(ddy(tSized));\n"
819 " float lod = log2(max(dx, dy));\n";
820
821 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::BIAS)
822 {
823 out << " lod += bias;\n";
824 }
825 }
826 else if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD)
827 {
828 out << " float3 sizeVec = float3(width, height, depth);\n"
829 " float3 sizeDdx = ddx * sizeVec;\n"
830 " float3 sizeDdy = ddy * sizeVec;\n"
831 " float lod = log2(max(dot(sizeDdx, sizeDdx), dot(sizeDdy, "
832 "sizeDdy))) * 0.5f;\n";
833 }
834
835 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
836 }
837
838 out << " " << textureReference
839 << ".GetDimensions(mip, width, height, depth, levels);\n";
840 }
841 else
842 UNREACHABLE();
843
844 OutputIntTexCoordWraps(out, textureFunction, texCoordX, texCoordY, texCoordZ);
845 }
846}
847
Jiawei Shaoa977acc2018-09-19 12:46:05 +0800848void OutputTextureGatherFunctionBody(TInfoSinkBase &out,
849 const TextureFunctionHLSL::TextureFunction &textureFunction,
850 ShShaderOutput outputType,
851 const ImmutableString &textureReference,
852 const ImmutableString &samplerReference,
853 const ImmutableString &texCoordX,
854 const ImmutableString &texCoordY,
855 const ImmutableString &texCoordZ)
856{
857 const int hlslCoords = GetHLSLCoordCount(textureFunction, outputType);
858 ImmutableString samplerCoordTypeString(
859 GetSamplerCoordinateTypeString(textureFunction, hlslCoords));
860 ImmutableStringBuilder samplerCoordBuilder(
861 samplerCoordTypeString.length() + strlen("(") + texCoordX.length() + strlen(", ") +
862 texCoordY.length() + strlen(", ") + texCoordZ.length() + strlen(")"));
863
864 samplerCoordBuilder << samplerCoordTypeString << "(" << texCoordX << ", " << texCoordY;
865 if (hlslCoords >= 3)
866 {
867 if (textureFunction.coords < 3)
868 {
869 samplerCoordBuilder << ", 0";
870 }
871 else
872 {
873 samplerCoordBuilder << ", " << texCoordZ;
874 }
875 }
876 samplerCoordBuilder << ")";
877
878 ImmutableString samplerCoordString(samplerCoordBuilder);
879
Jiawei Shao19b51d22018-09-19 15:14:45 +0800880 if (IsShadowSampler(textureFunction.sampler))
881 {
882 out << "return " << textureReference << ".GatherCmp(" << samplerReference << ", "
883 << samplerCoordString << ", refZ";
884 if (textureFunction.offset)
885 {
886 out << ", offset";
887 }
888 out << ");\n";
889 return;
890 }
891
Jiawei Shaocf8ad762018-09-21 09:11:35 +0800892 constexpr std::array<const char *, 4> kHLSLGatherFunctions = {
893 {"GatherRed", "GatherGreen", "GatherBlue", "GatherAlpha"}};
894
Jiawei Shaoa977acc2018-09-19 12:46:05 +0800895 out << " switch(comp)\n"
Jiawei Shaocf8ad762018-09-21 09:11:35 +0800896 " {\n";
897 for (size_t component = 0; component < kHLSLGatherFunctions.size(); ++component)
898 {
899 out << " case " << component << ":\n"
900 << " return " << textureReference << "." << kHLSLGatherFunctions[component]
901 << "(" << samplerReference << ", " << samplerCoordString;
902 if (textureFunction.offset)
903 {
904 out << ", offset";
905 }
906 out << ");\n";
907 }
908
909 out << " default:\n"
Jiawei Shaoa977acc2018-09-19 12:46:05 +0800910 " return float4(0.0, 0.0, 0.0, 1.0);\n"
911 " }\n";
912}
913
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300914void OutputTextureSampleFunctionReturnStatement(
915 TInfoSinkBase &out,
916 const TextureFunctionHLSL::TextureFunction &textureFunction,
917 const ShShaderOutput outputType,
Olli Etuaho12c03762018-01-25 12:22:33 +0200918 const ImmutableString &textureReference,
919 const ImmutableString &samplerReference,
Olli Etuahob4cc49f2018-01-25 14:37:06 +0200920 const ImmutableString &texCoordX,
921 const ImmutableString &texCoordY,
922 const ImmutableString &texCoordZ)
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300923{
924 out << " return ";
925
Till Rathmannb8543632018-10-02 19:46:14 +0200926 if (IsIntegerSampler(textureFunction.sampler) && !IsSamplerCube(textureFunction.sampler) &&
927 textureFunction.method != TextureFunctionHLSL::TextureFunction::FETCH)
928 {
929 out << " useBorderColor ? ";
930 if (IsIntegerSamplerUnsigned(textureFunction.sampler))
931 {
932 out << "asuint";
933 }
934 out << "(samplerMetadata[samplerIndex].intBorderColor) : ";
935 }
936
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300937 // HLSL intrinsic
938 if (outputType == SH_HLSL_3_0_OUTPUT)
939 {
940 switch (textureFunction.sampler)
941 {
942 case EbtSampler2D:
Geoff Langb66a9092016-05-16 15:59:14 -0400943 case EbtSamplerExternalOES:
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300944 out << "tex2D";
945 break;
946 case EbtSamplerCube:
947 out << "texCUBE";
948 break;
949 default:
950 UNREACHABLE();
951 }
952
953 switch (textureFunction.method)
954 {
955 case TextureFunctionHLSL::TextureFunction::IMPLICIT:
956 out << "(" << samplerReference << ", ";
957 break;
958 case TextureFunctionHLSL::TextureFunction::BIAS:
959 out << "bias(" << samplerReference << ", ";
960 break;
961 case TextureFunctionHLSL::TextureFunction::LOD:
962 out << "lod(" << samplerReference << ", ";
963 break;
964 case TextureFunctionHLSL::TextureFunction::LOD0:
965 out << "lod(" << samplerReference << ", ";
966 break;
967 case TextureFunctionHLSL::TextureFunction::LOD0BIAS:
968 out << "lod(" << samplerReference << ", ";
969 break;
Geoff Langba992ab2017-04-19 11:18:14 -0400970 case TextureFunctionHLSL::TextureFunction::GRAD:
971 out << "grad(" << samplerReference << ", ";
972 break;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300973 default:
974 UNREACHABLE();
975 }
976 }
977 else if (outputType == SH_HLSL_4_1_OUTPUT || outputType == SH_HLSL_4_0_FL9_3_OUTPUT)
978 {
979 OutputHLSL4SampleFunctionPrefix(out, textureFunction, textureReference, samplerReference);
980 }
981 else
982 UNREACHABLE();
983
984 const int hlslCoords = GetHLSLCoordCount(textureFunction, outputType);
985
986 out << GetSamplerCoordinateTypeString(textureFunction, hlslCoords) << "(" << texCoordX << ", "
987 << texCoordY;
988
989 if (outputType == SH_HLSL_3_0_OUTPUT)
990 {
991 if (hlslCoords >= 3)
992 {
993 if (textureFunction.coords < 3)
994 {
995 out << ", 0";
996 }
997 else
998 {
999 out << ", " << texCoordZ;
1000 }
1001 }
1002
1003 if (hlslCoords == 4)
1004 {
1005 switch (textureFunction.method)
1006 {
1007 case TextureFunctionHLSL::TextureFunction::BIAS:
1008 out << ", bias";
1009 break;
1010 case TextureFunctionHLSL::TextureFunction::LOD:
1011 out << ", lod";
1012 break;
1013 case TextureFunctionHLSL::TextureFunction::LOD0:
1014 out << ", 0";
1015 break;
1016 case TextureFunctionHLSL::TextureFunction::LOD0BIAS:
1017 out << ", bias";
1018 break;
1019 default:
1020 UNREACHABLE();
1021 }
1022 }
1023
1024 out << ")";
1025 }
1026 else if (outputType == SH_HLSL_4_1_OUTPUT || outputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1027 {
1028 if (hlslCoords >= 3)
1029 {
1030 ASSERT(!IsIntegerSampler(textureFunction.sampler) ||
1031 !IsSamplerCube(textureFunction.sampler) || texCoordZ == "face");
1032 out << ", " << texCoordZ;
1033 }
1034
1035 if (textureFunction.method == TextureFunctionHLSL::TextureFunction::GRAD)
1036 {
1037 if (IsIntegerSampler(textureFunction.sampler))
1038 {
1039 out << ", mip)";
1040 }
1041 else if (IsShadowSampler(textureFunction.sampler))
1042 {
1043 // Compare value
1044 if (textureFunction.proj)
1045 {
1046 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1047 // The resulting third component of P' in the shadow forms is used as
1048 // Dref
1049 out << "), " << texCoordZ;
1050 }
1051 else
1052 {
1053 switch (textureFunction.coords)
1054 {
1055 case 3:
1056 out << "), t.z";
1057 break;
1058 case 4:
1059 out << "), t.w";
1060 break;
1061 default:
1062 UNREACHABLE();
1063 }
1064 }
1065 }
1066 else
1067 {
1068 out << "), ddx, ddy";
1069 }
1070 }
1071 else if (IsIntegerSampler(textureFunction.sampler) ||
1072 textureFunction.method == TextureFunctionHLSL::TextureFunction::FETCH)
1073 {
Olli Etuaho2da04532018-08-24 13:59:44 +03001074 if (IsSampler2DMS(textureFunction.sampler) ||
1075 IsSampler2DMSArray(textureFunction.sampler))
JiangYizhou5b03f472017-01-09 10:22:53 +08001076 out << "), index";
1077 else
1078 out << ", mip)";
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001079 }
1080 else if (IsShadowSampler(textureFunction.sampler))
1081 {
1082 // Compare value
1083 if (textureFunction.proj)
1084 {
1085 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1086 // The resulting third component of P' in the shadow forms is used as Dref
1087 out << "), " << texCoordZ;
1088 }
1089 else
1090 {
1091 switch (textureFunction.coords)
1092 {
1093 case 3:
1094 out << "), t.z";
1095 break;
1096 case 4:
1097 out << "), t.w";
1098 break;
1099 default:
1100 UNREACHABLE();
1101 }
1102 }
1103 }
1104 else
1105 {
1106 switch (textureFunction.method)
1107 {
1108 case TextureFunctionHLSL::TextureFunction::IMPLICIT:
1109 out << ")";
1110 break;
1111 case TextureFunctionHLSL::TextureFunction::BIAS:
1112 out << "), bias";
1113 break;
1114 case TextureFunctionHLSL::TextureFunction::LOD:
1115 out << "), lod";
1116 break;
1117 case TextureFunctionHLSL::TextureFunction::LOD0:
1118 out << "), 0";
1119 break;
1120 case TextureFunctionHLSL::TextureFunction::LOD0BIAS:
1121 out << "), bias";
1122 break;
1123 default:
1124 UNREACHABLE();
1125 }
1126 }
1127
1128 if (textureFunction.offset &&
1129 (!IsIntegerSampler(textureFunction.sampler) ||
1130 textureFunction.method == TextureFunctionHLSL::TextureFunction::FETCH))
1131 {
1132 out << ", offset";
1133 }
1134 }
1135 else
1136 UNREACHABLE();
1137
1138 out << ");\n"; // Close the sample function call and return statement
1139}
1140
1141} // Anonymous namespace
1142
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001143ImmutableString TextureFunctionHLSL::TextureFunction::name() const
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001144{
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001145 static const ImmutableString kGlTextureName("gl_texture");
1146
1147 ImmutableString suffix(TextureTypeSuffix(this->sampler));
1148
1149 ImmutableStringBuilder name(kGlTextureName.length() + suffix.length() + 4u + 6u + 5u);
1150
1151 name << kGlTextureName;
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001152
1153 // We need to include full the sampler type in the function name to make the signature unique
1154 // on D3D11, where samplers are passed to texture functions as indices.
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001155 name << suffix;
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001156
1157 if (proj)
1158 {
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001159 name << "Proj";
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001160 }
1161
1162 if (offset)
1163 {
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001164 name << "Offset";
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001165 }
1166
1167 switch (method)
1168 {
1169 case IMPLICIT:
1170 break;
1171 case BIAS:
1172 break; // Extra parameter makes the signature unique
1173 case LOD:
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001174 name << "Lod";
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001175 break;
1176 case LOD0:
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001177 name << "Lod0";
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001178 break;
1179 case LOD0BIAS:
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001180 name << "Lod0";
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001181 break; // Extra parameter makes the signature unique
1182 case SIZE:
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001183 name << "Size";
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001184 break;
1185 case FETCH:
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001186 name << "Fetch";
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001187 break;
1188 case GRAD:
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001189 name << "Grad";
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001190 break;
Jiawei Shaoa977acc2018-09-19 12:46:05 +08001191 case GATHER:
1192 name << "Gather";
1193 break;
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001194 default:
1195 UNREACHABLE();
1196 }
1197
1198 return name;
1199}
1200
1201const char *TextureFunctionHLSL::TextureFunction::getReturnType() const
1202{
1203 if (method == TextureFunction::SIZE)
1204 {
1205 switch (sampler)
1206 {
1207 case EbtSampler2D:
1208 case EbtISampler2D:
1209 case EbtUSampler2D:
1210 case EbtSampler2DShadow:
1211 case EbtSamplerCube:
1212 case EbtISamplerCube:
1213 case EbtUSamplerCube:
1214 case EbtSamplerCubeShadow:
Ian Ewellbda75592016-04-18 17:25:54 -04001215 case EbtSamplerExternalOES:
Olli Etuaho92db39e2017-02-15 12:11:04 +00001216 case EbtSampler2DMS:
1217 case EbtISampler2DMS:
1218 case EbtUSampler2DMS:
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001219 return "int2";
1220 case EbtSampler3D:
1221 case EbtISampler3D:
1222 case EbtUSampler3D:
1223 case EbtSampler2DArray:
1224 case EbtISampler2DArray:
1225 case EbtUSampler2DArray:
Olli Etuaho2da04532018-08-24 13:59:44 +03001226 case EbtSampler2DMSArray:
1227 case EbtISampler2DMSArray:
1228 case EbtUSampler2DMSArray:
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001229 case EbtSampler2DArrayShadow:
1230 return "int3";
1231 default:
1232 UNREACHABLE();
1233 }
1234 }
1235 else // Sampling function
1236 {
1237 switch (sampler)
1238 {
1239 case EbtSampler2D:
JiangYizhou34bc3152017-03-29 14:56:01 +08001240 case EbtSampler2DMS:
Olli Etuaho2da04532018-08-24 13:59:44 +03001241 case EbtSampler2DMSArray:
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001242 case EbtSampler3D:
1243 case EbtSamplerCube:
1244 case EbtSampler2DArray:
Ian Ewellbda75592016-04-18 17:25:54 -04001245 case EbtSamplerExternalOES:
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001246 return "float4";
1247 case EbtISampler2D:
JiangYizhou34bc3152017-03-29 14:56:01 +08001248 case EbtISampler2DMS:
Olli Etuaho2da04532018-08-24 13:59:44 +03001249 case EbtISampler2DMSArray:
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001250 case EbtISampler3D:
1251 case EbtISamplerCube:
1252 case EbtISampler2DArray:
1253 return "int4";
1254 case EbtUSampler2D:
JiangYizhou34bc3152017-03-29 14:56:01 +08001255 case EbtUSampler2DMS:
Olli Etuaho2da04532018-08-24 13:59:44 +03001256 case EbtUSampler2DMSArray:
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001257 case EbtUSampler3D:
1258 case EbtUSamplerCube:
1259 case EbtUSampler2DArray:
1260 return "uint4";
1261 case EbtSampler2DShadow:
1262 case EbtSamplerCubeShadow:
1263 case EbtSampler2DArrayShadow:
Jiawei Shao19b51d22018-09-19 15:14:45 +08001264 if (method == TextureFunctionHLSL::TextureFunction::GATHER)
1265 {
1266 return "float4";
1267 }
1268 else
1269 {
1270 return "float";
1271 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001272 default:
1273 UNREACHABLE();
1274 }
1275 }
1276 return "";
1277}
1278
1279bool TextureFunctionHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
1280{
Geoff Lang28a97ee2016-09-22 13:01:26 -04001281 return std::tie(sampler, coords, proj, offset, method) <
1282 std::tie(rhs.sampler, rhs.coords, rhs.proj, rhs.offset, rhs.method);
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001283}
1284
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001285ImmutableString TextureFunctionHLSL::useTextureFunction(const ImmutableString &name,
1286 TBasicType samplerType,
1287 int coords,
1288 size_t argumentCount,
1289 bool lod0,
1290 sh::GLenum shaderType)
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001291{
1292 TextureFunction textureFunction;
1293 textureFunction.sampler = samplerType;
1294 textureFunction.coords = coords;
1295 textureFunction.method = TextureFunction::IMPLICIT;
1296 textureFunction.proj = false;
1297 textureFunction.offset = false;
1298
1299 if (name == "texture2D" || name == "textureCube" || name == "texture")
1300 {
1301 textureFunction.method = TextureFunction::IMPLICIT;
1302 }
1303 else if (name == "texture2DProj" || name == "textureProj")
1304 {
1305 textureFunction.method = TextureFunction::IMPLICIT;
1306 textureFunction.proj = true;
1307 }
1308 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
1309 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
1310 {
1311 textureFunction.method = TextureFunction::LOD;
1312 }
1313 else if (name == "texture2DProjLod" || name == "textureProjLod" ||
1314 name == "texture2DProjLodEXT")
1315 {
1316 textureFunction.method = TextureFunction::LOD;
1317 textureFunction.proj = true;
1318 }
1319 else if (name == "textureSize")
1320 {
1321 textureFunction.method = TextureFunction::SIZE;
1322 }
1323 else if (name == "textureOffset")
1324 {
1325 textureFunction.method = TextureFunction::IMPLICIT;
1326 textureFunction.offset = true;
1327 }
1328 else if (name == "textureProjOffset")
1329 {
1330 textureFunction.method = TextureFunction::IMPLICIT;
1331 textureFunction.offset = true;
1332 textureFunction.proj = true;
1333 }
1334 else if (name == "textureLodOffset")
1335 {
1336 textureFunction.method = TextureFunction::LOD;
1337 textureFunction.offset = true;
1338 }
1339 else if (name == "textureProjLodOffset")
1340 {
1341 textureFunction.method = TextureFunction::LOD;
1342 textureFunction.proj = true;
1343 textureFunction.offset = true;
1344 }
1345 else if (name == "texelFetch")
1346 {
1347 textureFunction.method = TextureFunction::FETCH;
1348 }
1349 else if (name == "texelFetchOffset")
1350 {
1351 textureFunction.method = TextureFunction::FETCH;
1352 textureFunction.offset = true;
1353 }
1354 else if (name == "textureGrad" || name == "texture2DGradEXT")
1355 {
1356 textureFunction.method = TextureFunction::GRAD;
1357 }
1358 else if (name == "textureGradOffset")
1359 {
1360 textureFunction.method = TextureFunction::GRAD;
1361 textureFunction.offset = true;
1362 }
1363 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" ||
1364 name == "textureCubeGradEXT")
1365 {
1366 textureFunction.method = TextureFunction::GRAD;
1367 textureFunction.proj = true;
1368 }
1369 else if (name == "textureProjGradOffset")
1370 {
1371 textureFunction.method = TextureFunction::GRAD;
1372 textureFunction.proj = true;
1373 textureFunction.offset = true;
1374 }
Jiawei Shaoa977acc2018-09-19 12:46:05 +08001375 else if (name == "textureGather")
1376 {
1377 textureFunction.method = TextureFunction::GATHER;
1378 }
Jiawei Shaocf8ad762018-09-21 09:11:35 +08001379 else if (name == "textureGatherOffset")
1380 {
1381 textureFunction.method = TextureFunction::GATHER;
1382 textureFunction.offset = true;
1383 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001384 else
1385 UNREACHABLE();
1386
1387 if (textureFunction.method ==
1388 TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
1389 {
1390 size_t mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
1391
1392 if (textureFunction.offset)
1393 {
1394 mandatoryArgumentCount++;
1395 }
1396
1397 bool bias = (argumentCount > mandatoryArgumentCount); // Bias argument is optional
1398
Xinghua Cao0d218da2018-12-17 11:53:52 +08001399 if (lod0 || shaderType == GL_VERTEX_SHADER || shaderType == GL_COMPUTE_SHADER)
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001400 {
1401 if (bias)
1402 {
1403 textureFunction.method = TextureFunction::LOD0BIAS;
1404 }
1405 else
1406 {
1407 textureFunction.method = TextureFunction::LOD0;
1408 }
1409 }
1410 else if (bias)
1411 {
1412 textureFunction.method = TextureFunction::BIAS;
1413 }
1414 }
1415
1416 mUsesTexture.insert(textureFunction);
1417 return textureFunction.name();
1418}
1419
Geoff Lang1fe74c72016-08-25 13:23:01 -04001420void TextureFunctionHLSL::textureFunctionHeader(TInfoSinkBase &out,
1421 const ShShaderOutput outputType,
1422 bool getDimensionsIgnoresBaseLevel)
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001423{
1424 for (const TextureFunction &textureFunction : mUsesTexture)
1425 {
1426 // Function header
1427 out << textureFunction.getReturnType() << " " << textureFunction.name() << "(";
1428
1429 OutputTextureFunctionArgumentList(out, textureFunction, outputType);
1430
1431 out << ")\n"
1432 "{\n";
1433
1434 // In some cases we use a variable to store the texture/sampler objects, but to work around
1435 // a D3D11 compiler bug related to discard inside a loop that is conditional on texture
1436 // sampling we need to call the function directly on references to the texture and sampler
1437 // arrays. The bug was found using dEQP-GLES3.functional.shaders.discard*loop_texture*
1438 // tests.
Olli Etuaho12c03762018-01-25 12:22:33 +02001439 ImmutableString textureReference("");
1440 ImmutableString samplerReference("");
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001441 GetTextureReference(out, textureFunction, outputType, &textureReference, &samplerReference);
1442
1443 if (textureFunction.method == TextureFunction::SIZE)
1444 {
Geoff Lang1fe74c72016-08-25 13:23:01 -04001445 OutputTextureSizeFunctionBody(out, textureFunction, textureReference,
1446 getDimensionsIgnoresBaseLevel);
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001447 }
1448 else
1449 {
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001450 ImmutableString texCoordX("t.x");
1451 ImmutableString texCoordY("t.y");
1452 ImmutableString texCoordZ("t.z");
Jiawei Shaoa977acc2018-09-19 12:46:05 +08001453 if (textureFunction.method == TextureFunction::GATHER)
1454 {
1455 OutputTextureGatherFunctionBody(out, textureFunction, outputType, textureReference,
1456 samplerReference, texCoordX, texCoordY, texCoordZ);
1457 }
1458 else
1459 {
1460 ProjectTextureCoordinates(textureFunction, &texCoordX, &texCoordY, &texCoordZ);
1461 OutputIntegerTextureSampleFunctionComputations(out, textureFunction, outputType,
1462 textureReference, &texCoordX,
1463 &texCoordY, &texCoordZ);
1464 OutputTextureSampleFunctionReturnStatement(out, textureFunction, outputType,
1465 textureReference, samplerReference,
1466 texCoordX, texCoordY, texCoordZ);
1467 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001468 }
1469
1470 out << "}\n"
1471 "\n";
1472 }
1473}
1474
1475} // namespace sh