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