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