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
|
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2016-2018 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include <QApplication>
#include <QRegularExpression>
#include <QtMath>
#include "QRDUtils.h"
static QVariant interpret(const ResourceFormat &f, uint16_t comp)
{
if(f.compByteWidth != 2 || f.compType == CompType::Float)
return QVariant();
if(f.compType == CompType::SInt)
{
return (int16_t)comp;
}
else if(f.compType == CompType::UInt)
{
return comp;
}
else if(f.compType == CompType::SScaled)
{
return (float)((int16_t)comp);
}
else if(f.compType == CompType::UScaled)
{
return (float)comp;
}
else if(f.compType == CompType::UNorm)
{
return (float)comp / (float)0xffff;
}
else if(f.compType == CompType::SNorm)
{
int16_t cast = (int16_t)comp;
float ret = -1.0f;
if(cast == -32768)
ret = -1.0f;
else
ret = ((float)cast) / 32767.0f;
return ret;
}
return QVariant();
}
static QVariant interpret(const ResourceFormat &f, byte comp)
{
if(f.compByteWidth != 1 || f.compType == CompType::Float)
return QVariant();
if(f.compType == CompType::SInt)
{
return (int8_t)comp;
}
else if(f.compType == CompType::UInt)
{
return comp;
}
else if(f.compType == CompType::SScaled)
{
return (float)((int8_t)comp);
}
else if(f.compType == CompType::UScaled)
{
return (float)comp;
}
else if(f.compType == CompType::UNorm)
{
return ((float)comp) / 255.0f;
}
else if(f.compType == CompType::SNorm)
{
int8_t cast = (int8_t)comp;
float ret = -1.0f;
if(cast == -128)
ret = -1.0f;
else
ret = ((float)cast) / 127.0f;
return ret;
}
return QVariant();
}
FormatElement::FormatElement()
{
buffer = 0;
offset = 0;
perinstance = false;
instancerate = 1;
rowmajor = false;
matrixdim = 0;
hex = false;
rgb = false;
systemValue = ShaderBuiltin::Undefined;
}
FormatElement::FormatElement(const QString &Name, int buf, uint offs, bool perInst, int instRate,
bool rowMat, uint matDim, ResourceFormat f, bool hexDisplay,
bool rgbDisplay)
{
name = Name;
buffer = buf;
offset = offs;
format = f;
perinstance = perInst;
instancerate = instRate;
rowmajor = rowMat;
matrixdim = matDim;
hex = hexDisplay;
rgb = rgbDisplay;
systemValue = ShaderBuiltin::Undefined;
}
QList<FormatElement> FormatElement::ParseFormatString(const QString &formatString, uint64_t maxLen,
bool tightPacking, QString &errors)
{
QList<FormatElement> elems;
// regex doesn't account for trailing or preceeding whitespace, or comments
QRegularExpression regExpr(
lit("^" // start of the line
"(row_major\\s+)?" // row_major matrix
"(rgb\\s+)?" // rgb element colourising
"(" // group the options for the type
"uintten|unormten" // R10G10B10A2 types
"|floateleven" // R11G11B10 special type
"|unormh|unormb" // UNORM 16-bit and 8-bit types
"|snormh|snormb" // SNORM 16-bit and 8-bit types
"|bool" // bool is stored as 4-byte int
"|byte|short|int" // signed ints
"|ubyte|ushort|uint" // unsigned ints
"|xbyte|xshort|xint" // hex ints
"|half|float|double" // float types
"|vec|uvec|ivec" // OpenGL vector types
"|mat|umat|imat" // OpenGL matrix types
")" // end of the type group
"([1-9])?" // might be a vector
"(x[1-9])?" // or a matrix
"(\\s+[A-Za-z_][A-Za-z0-9_]*)?" // get identifier name
"(\\[[0-9]+\\])?" // optional array dimension
"(\\s*:\\s*[A-Za-z_][A-Za-z0-9_]*)?" // optional semantic
"$"));
bool success = true;
errors = QString();
QString text = formatString;
text = text.replace(lit("{"), QString()).replace(lit("}"), QString());
QRegularExpression c_comments(lit("/\\*[^*]*\\*+(?:[^*/][^*]*\\*+)*/"));
QRegularExpression cpp_comments(lit("//.*"));
text = text.replace(c_comments, QString()).replace(cpp_comments, QString());
uint32_t offset = 0;
// get each line and parse it to determine the format the user wanted
for(QString &l : text.split(QLatin1Char(';')))
{
QString line = l.trimmed();
if(line.isEmpty())
continue;
QRegularExpressionMatch match = regExpr.match(line);
if(!match.hasMatch())
{
errors = tr("Couldn't parse line: %1\n").arg(line);
success = false;
break;
}
QString basetype = match.captured(3);
bool row_major = !match.captured(1).isEmpty();
bool rgb = !match.captured(2).isEmpty();
QString vectorDim = !match.captured(4).isEmpty() ? match.captured(4) : lit("1");
QString matrixDim = !match.captured(5).isEmpty() ? match.captured(5).mid(1) : lit("1");
QString name = !match.captured(6).isEmpty() ? match.captured(6).trimmed() : lit("data");
QString arrayDim = !match.captured(7).isEmpty() ? match.captured(7).trimmed() : lit("[1]");
arrayDim = arrayDim.mid(1, arrayDim.count() - 2);
if(!match.captured(5).isEmpty())
{
vectorDim.swap(matrixDim);
}
ResourceFormat fmt;
fmt.compType = CompType::Typeless;
bool hex = false;
CompType type = CompType::Float;
uint32_t count = 0;
uint32_t arrayCount = 1;
uint32_t matrixCount = 0;
uint32_t width = 0;
// check for square matrix declarations like 'mat4' and 'mat3'
if(basetype == lit("mat") && match.captured(5).isEmpty())
matrixDim = vectorDim;
// calculate format
{
bool ok = false;
count = vectorDim.toUInt(&ok);
if(!ok)
{
errors = tr("Invalid vector dimension on line: %1\n").arg(line);
success = false;
break;
}
arrayCount = arrayDim.toUInt(&ok);
if(!ok)
{
arrayCount = 1;
}
arrayCount = qMax(0U, arrayCount);
matrixCount = matrixDim.toUInt(&ok);
if(!ok)
{
errors = tr("Invalid matrix second dimension on line: %1\n").arg(line);
success = false;
break;
}
if(basetype == lit("bool"))
{
type = CompType::UInt;
width = 4;
}
else if(basetype == lit("byte"))
{
type = CompType::SInt;
width = 1;
}
else if(basetype == lit("ubyte") || basetype == lit("xbyte"))
{
type = CompType::UInt;
width = 1;
}
else if(basetype == lit("short"))
{
type = CompType::SInt;
width = 2;
}
else if(basetype == lit("ushort") || basetype == lit("xshort"))
{
type = CompType::UInt;
width = 2;
}
else if(basetype == lit("int") || basetype == lit("ivec") || basetype == lit("imat"))
{
type = CompType::SInt;
width = 4;
}
else if(basetype == lit("uint") || basetype == lit("xint") || basetype == lit("uvec") ||
basetype == lit("umat"))
{
type = CompType::UInt;
width = 4;
}
else if(basetype == lit("half"))
{
type = CompType::Float;
width = 2;
}
else if(basetype == lit("float") || basetype == lit("vec") || basetype == lit("mat"))
{
type = CompType::Float;
width = 4;
}
else if(basetype == lit("double"))
{
type = CompType::Double;
width = 8;
}
else if(basetype == lit("unormh"))
{
type = CompType::UNorm;
width = 2;
}
else if(basetype == lit("unormb"))
{
type = CompType::UNorm;
width = 1;
}
else if(basetype == lit("snormh"))
{
type = CompType::SNorm;
width = 2;
}
else if(basetype == lit("snormb"))
{
type = CompType::SNorm;
width = 1;
}
else if(basetype == lit("uintten"))
{
fmt.compType = CompType::UInt;
fmt.compCount = 4 * count;
fmt.compByteWidth = 1;
fmt.type = ResourceFormatType::R10G10B10A2;
}
else if(basetype == lit("unormten"))
{
fmt.compType = CompType::UInt;
fmt.compCount = 4 * count;
fmt.compByteWidth = 1;
fmt.type = ResourceFormatType::R10G10B10A2;
}
else if(basetype == lit("floateleven"))
{
fmt.compType = CompType::Float;
fmt.compCount = 3 * count;
fmt.compByteWidth = 1;
fmt.type = ResourceFormatType::R11G11B10;
}
else
{
errors = tr("Unrecognised basic type on line: %1\n").arg(line);
success = false;
break;
}
}
if(basetype == lit("xint") || basetype == lit("xshort") || basetype == lit("xbyte"))
hex = true;
if(fmt.compType == CompType::Typeless)
{
fmt.compType = type;
fmt.compCount = count;
fmt.compByteWidth = width;
}
if(arrayCount == 1)
{
FormatElement elem(name, 0, offset, false, 1, row_major, matrixCount, fmt, hex, rgb);
uint32_t advance = elem.byteSize();
if(!tightPacking)
{
// cbuffer packing always works in floats
advance = (advance + 3U) & (~3U);
// cbuffer packing doesn't allow elements to cross float4 boundaries, nudge up if this was
// the case
if(offset / 16 != (offset + elem.byteSize() - 1) / 16)
{
elem.offset = offset = (offset + 0xFU) & (~0xFU);
}
}
elems.push_back(elem);
offset += advance;
}
else
{
// when cbuffer packing, arrays are always aligned at float4 boundary
if(!tightPacking)
{
if(offset % 16 != 0)
{
offset = (offset + 0xFU) & (~0xFU);
}
}
for(uint a = 0; a < arrayCount; a++)
{
FormatElement elem(QFormatStr("%1[%2]").arg(name).arg(a), 0, offset, false, 1, row_major,
matrixCount, fmt, hex, rgb);
elems.push_back(elem);
uint32_t advance = elem.byteSize();
// cbuffer packing each array element is always float4 aligned
if(!tightPacking)
{
advance = (advance + 0xFU) & (~0xFU);
}
offset += advance;
}
}
}
if(!success || elems.isEmpty())
{
elems.clear();
ResourceFormat fmt;
fmt.compType = CompType::UInt;
fmt.compByteWidth = 4;
fmt.compCount = 4;
if(maxLen > 0 && maxLen < 16)
fmt.compCount = 1;
if(maxLen > 0 && maxLen < 4)
fmt.compByteWidth = 1;
elems.push_back(FormatElement(lit("data"), 0, 0, false, 1, false, 1, fmt, true, false));
}
return elems;
}
QString FormatElement::GenerateTextureBufferFormat(const TextureDescription &tex)
{
QString baseType;
QString varName = lit("pixels");
uint32_t w = tex.width;
switch(tex.format.type)
{
case ResourceFormatType::BC1:
case ResourceFormatType::BC2:
case ResourceFormatType::BC3:
case ResourceFormatType::BC4:
case ResourceFormatType::BC5:
case ResourceFormatType::BC6:
case ResourceFormatType::BC7:
case ResourceFormatType::ETC2:
case ResourceFormatType::EAC:
case ResourceFormatType::ASTC:
case ResourceFormatType::PVRTC:
varName = lit("block");
// display a 4x4 block at a time
w /= 4;
default: break;
}
switch(tex.format.type)
{
case ResourceFormatType::Regular:
{
if(tex.format.compByteWidth == 1)
baseType = lit("byte");
else if(tex.format.compByteWidth == 2)
baseType = lit("short");
else
baseType = lit("int");
baseType = QFormatStr("rgb x%1%2").arg(baseType).arg(tex.format.compCount);
break;
}
// 2x4 byte block, for 64-bit block formats
case ResourceFormatType::BC1:
case ResourceFormatType::BC4:
case ResourceFormatType::ETC2:
case ResourceFormatType::EAC:
case ResourceFormatType::PVRTC:
baseType = lit("row_major xint2x1");
break;
// 4x4 byte block, for 128-bit block formats
case ResourceFormatType::BC2:
case ResourceFormatType::BC3:
case ResourceFormatType::BC5:
case ResourceFormatType::BC6:
case ResourceFormatType::BC7:
case ResourceFormatType::ASTC: baseType = lit("row_major xint4x1"); break;
case ResourceFormatType::R10G10B10A2: baseType = lit("uintten"); break;
case ResourceFormatType::R11G11B10: baseType = lit("rgb floateleven"); break;
case ResourceFormatType::R5G6B5:
case ResourceFormatType::R5G5B5A1: baseType = lit("xshort"); break;
case ResourceFormatType::R9G9B9E5: baseType = lit("xint"); break;
case ResourceFormatType::R4G4B4A4: baseType = lit("xbyte2"); break;
case ResourceFormatType::R4G4: baseType = lit("xbyte"); break;
case ResourceFormatType::D16S8:
case ResourceFormatType::D24S8:
case ResourceFormatType::D32S8:
case ResourceFormatType::YUV: baseType = lit("xint4"); break;
case ResourceFormatType::S8:
case ResourceFormatType::Undefined: baseType = lit("xbyte"); break;
}
if(tex.type == TextureType::Buffer)
return QFormatStr("%1 %2;").arg(baseType).arg(varName);
return QFormatStr("%1 %2[%3];").arg(baseType).arg(varName).arg(w);
}
template <typename T>
inline T readObj(const byte *&data, const byte *end, bool &ok)
{
if(data + sizeof(T) > end)
{
ok = false;
return T();
}
T ret = *(T *)data;
data += sizeof(T);
return ret;
}
QVariantList FormatElement::GetVariants(const byte *&data, const byte *end) const
{
QVariantList ret;
bool ok = true;
if(format.type == ResourceFormatType::R5G5B5A1)
{
uint16_t packed = readObj<uint16_t>(data, end, ok);
ret.push_back((float)((packed >> 0) & 0x1f) / 31.0f);
ret.push_back((float)((packed >> 5) & 0x1f) / 31.0f);
ret.push_back((float)((packed >> 10) & 0x1f) / 31.0f);
ret.push_back(((packed & 0x8000) > 0) ? 1.0f : 0.0f);
if(format.bgraOrder)
{
QVariant tmp = ret[2];
ret[2] = ret[0];
ret[0] = tmp;
}
}
else if(format.type == ResourceFormatType::R5G6B5)
{
uint16_t packed = readObj<uint16_t>(data, end, ok);
ret.push_back((float)((packed >> 0) & 0x1f) / 31.0f);
ret.push_back((float)((packed >> 5) & 0x3f) / 63.0f);
ret.push_back((float)((packed >> 11) & 0x1f) / 31.0f);
if(format.bgraOrder)
{
QVariant tmp = ret[2];
ret[2] = ret[0];
ret[0] = tmp;
}
}
else if(format.type == ResourceFormatType::R4G4B4A4)
{
uint16_t packed = readObj<uint16_t>(data, end, ok);
ret.push_back((float)((packed >> 0) & 0xf) / 15.0f);
ret.push_back((float)((packed >> 4) & 0xf) / 15.0f);
ret.push_back((float)((packed >> 8) & 0xf) / 15.0f);
ret.push_back((float)((packed >> 12) & 0xf) / 15.0f);
if(format.bgraOrder)
{
QVariant tmp = ret[2];
ret[2] = ret[0];
ret[0] = tmp;
}
}
else if(format.type == ResourceFormatType::R10G10B10A2)
{
// allow for vectors of this format - for raw buffer viewer
for(int i = 0; i < int(format.compCount / 4); i++)
{
uint32_t packed = readObj<uint32_t>(data, end, ok);
uint32_t r = (packed >> 0) & 0x3ff;
uint32_t g = (packed >> 10) & 0x3ff;
uint32_t b = (packed >> 20) & 0x3ff;
uint32_t a = (packed >> 30) & 0x003;
if(format.bgraOrder)
{
uint32_t tmp = b;
b = r;
r = tmp;
}
if(format.compType == CompType::UInt)
{
ret.push_back(r);
ret.push_back(g);
ret.push_back(b);
ret.push_back(a);
}
else if(format.compType == CompType::UScaled)
{
ret.push_back((float)r);
ret.push_back((float)g);
ret.push_back((float)b);
ret.push_back((float)a);
}
else if(format.compType == CompType::SInt || format.compType == CompType::SScaled ||
format.compType == CompType::SNorm)
{
int ir, ig, ib, ia;
// interpret RGB as 10-bit signed integers
if(r <= 511)
ir = (int)r;
else
ir = ((int)r) - 1024;
if(g <= 511)
ig = (int)g;
else
ig = ((int)g) - 1024;
if(b <= 511)
ib = (int)b;
else
ib = ((int)b) - 1024;
// 2-bit signed integer
if(a <= 1)
ia = (int)a;
else
ia = ((int)a) - 4;
if(format.compType == CompType::SInt)
{
ret.push_back(ir);
ret.push_back(ig);
ret.push_back(ib);
ret.push_back(ia);
}
else if(format.compType == CompType::SScaled)
{
ret.push_back((float)ir);
ret.push_back((float)ig);
ret.push_back((float)ib);
ret.push_back((float)ia);
}
else if(format.compType == CompType::SNorm)
{
if(ir == -512)
ir = -511;
if(ig == -512)
ig = -511;
if(ib == -512)
ib = -511;
if(ia == -2)
ia = -1;
ret.push_back((float)ir / 511.0f);
ret.push_back((float)ig / 511.0f);
ret.push_back((float)ib / 511.0f);
ret.push_back((float)ia / 1.0f);
}
}
else
{
ret.push_back((float)r / 1023.0f);
ret.push_back((float)g / 1023.0f);
ret.push_back((float)b / 1023.0f);
ret.push_back((float)a / 3.0f);
}
}
}
else if(format.type == ResourceFormatType::R11G11B10)
{
uint32_t packed = readObj<uint32_t>(data, end, ok);
uint32_t mantissas[] = {
(packed >> 0) & 0x3f, (packed >> 11) & 0x3f, (packed >> 22) & 0x1f,
};
int32_t exponents[] = {
int32_t(packed >> 6) & 0x1f, int32_t(packed >> 17) & 0x1f, int32_t(packed >> 27) & 0x1f,
};
static const uint32_t leadbit[] = {
0x40, 0x40, 0x20,
};
for(int i = 0; i < 3; i++)
{
if(mantissas[i] == 0 && exponents[i] == 0)
{
ret.push_back((float)0.0f);
}
else
{
if(exponents[i] == 0x1f)
{
// no sign bit, can't be negative infinity
if(mantissas[i] == 0)
ret.push_back((float)qInf());
else
ret.push_back((float)qQNaN());
}
else if(exponents[i] != 0)
{
// normal value, add leading bit
uint32_t combined = leadbit[i] | mantissas[i];
// calculate value
ret.push_back(((float)combined / (float)leadbit[i]) *
qPow(2.0f, (float)exponents[i] - 15.0f));
}
else if(exponents[i] == 0)
{
// we know xMantissa isn't 0 also, or it would have been caught above so
// this is a subnormal value, pretend exponent is 1 and don't add leading bit
ret.push_back(((float)mantissas[i] / (float)leadbit[i]) * qPow(2.0f, (float)1.0f - 15.0f));
}
}
}
}
else
{
int dim = (int)(qMax(matrixdim, 1U) * format.compCount);
for(int i = 0; i < dim; i++)
{
if(format.compType == CompType::Float)
{
if(format.compByteWidth == 8)
ret.push_back(readObj<double>(data, end, ok));
else if(format.compByteWidth == 4)
ret.push_back(readObj<float>(data, end, ok));
else if(format.compByteWidth == 2)
ret.push_back(RENDERDOC_HalfToFloat(readObj<uint16_t>(data, end, ok)));
}
else if(format.compType == CompType::SInt)
{
if(format.compByteWidth == 4)
ret.push_back((int)readObj<int32_t>(data, end, ok));
else if(format.compByteWidth == 2)
ret.push_back((int)readObj<int16_t>(data, end, ok));
else if(format.compByteWidth == 1)
ret.push_back((int)readObj<int8_t>(data, end, ok));
}
else if(format.compType == CompType::UInt)
{
if(format.compByteWidth == 4)
ret.push_back((uint32_t)readObj<uint32_t>(data, end, ok));
else if(format.compByteWidth == 2)
ret.push_back((uint32_t)readObj<uint16_t>(data, end, ok));
else if(format.compByteWidth == 1)
ret.push_back((uint32_t)readObj<uint8_t>(data, end, ok));
}
else if(format.compType == CompType::UScaled)
{
if(format.compByteWidth == 4)
ret.push_back((float)readObj<uint32_t>(data, end, ok));
else if(format.compByteWidth == 2)
ret.push_back((float)readObj<uint16_t>(data, end, ok));
else if(format.compByteWidth == 1)
ret.push_back((float)readObj<uint8_t>(data, end, ok));
}
else if(format.compType == CompType::SScaled)
{
if(format.compByteWidth == 4)
ret.push_back((float)readObj<int32_t>(data, end, ok));
else if(format.compByteWidth == 2)
ret.push_back((float)readObj<int16_t>(data, end, ok));
else if(format.compByteWidth == 1)
ret.push_back((float)readObj<int8_t>(data, end, ok));
}
else if(format.compType == CompType::Depth)
{
if(format.compByteWidth == 4)
{
// 32-bit depth is native floats
ret.push_back(readObj<float>(data, end, ok));
}
else if(format.compByteWidth == 3)
{
// 32-bit depth is normalised, masked against non-stencil bits
uint32_t f = readObj<uint32_t>(data, end, ok);
f &= 0x00ffffff;
ret.push_back((float)f / (float)0x00ffffff);
}
else if(format.compByteWidth == 2)
{
// 16-bit depth is normalised
float f = (float)readObj<uint16_t>(data, end, ok);
ret.push_back(f / (float)0x0000ffff);
}
}
else if(format.compType == CompType::Double)
{
ret.push_back(readObj<double>(data, end, ok));
}
else
{
// unorm/snorm
if(format.compByteWidth == 4)
{
// should never hit this - no 32bit unorm/snorm type
qCritical() << "Unexpected 4-byte unorm/snorm value";
ret.push_back((float)readObj<uint32_t>(data, end, ok) / (float)0xffffffff);
}
else if(format.compByteWidth == 2)
{
ret.push_back(interpret(format, readObj<uint16_t>(data, end, ok)));
}
else if(format.compByteWidth == 1)
{
ret.push_back(interpret(format, readObj<uint8_t>(data, end, ok)));
}
}
}
if(format.bgraOrder)
{
QVariant tmp = ret[2];
ret[2] = ret[0];
ret[0] = tmp;
}
}
// we read off the end, return empty set
if(!ok)
ret.clear();
return ret;
}
ShaderVariable FormatElement::GetShaderVar(const byte *&data, const byte *end) const
{
QVariantList objs = GetVariants(data, end);
ShaderVariable ret;
ret.name = name.toUtf8().data();
ret.type = VarType::Float;
if(format.compType == CompType::UInt)
ret.type = VarType::UInt;
if(format.compType == CompType::SInt)
ret.type = VarType::Int;
if(format.compType == CompType::Double)
ret.type = VarType::Double;
ret.columns = qMin(format.compCount, uint8_t(4));
ret.rows = qMin(matrixdim, 4U);
ret.displayAsHex = hex;
for(uint32_t row = 0; row < ret.rows; row++)
{
for(uint32_t col = 0; col < ret.columns; col++)
{
uint32_t dst = row * ret.columns + col;
uint32_t src = row * format.compCount + col;
// if we partially read a failure, reset the variable and return
if((int)src >= objs.size())
{
ret.name = "-";
memset(ret.value.dv, 0, sizeof(ret.value.dv));
return ret;
}
const QVariant &o = objs[src];
if(ret.type == VarType::Double)
ret.value.dv[dst] = o.toDouble();
else if(ret.type == VarType::UInt)
ret.value.uv[dst] = o.toUInt();
else if(ret.type == VarType::Int)
ret.value.iv[dst] = o.toInt();
else
ret.value.fv[dst] = o.toFloat();
}
}
return ret;
}
uint32_t FormatElement::byteSize() const
{
uint32_t vecSize = format.compByteWidth * format.compCount;
if(format.type == ResourceFormatType::R5G5B5A1 || format.type == ResourceFormatType::R5G6B5 ||
format.type == ResourceFormatType::R4G4B4A4)
vecSize = 2;
if(format.type == ResourceFormatType::R10G10B10A2 || format.type == ResourceFormatType::R11G11B10)
vecSize = 4;
return vecSize * matrixdim;
}
QString TypeString(const ShaderVariable &v)
{
if(!v.members.isEmpty() || v.isStruct)
{
if(v.isStruct)
return lit("struct");
else
return QFormatStr("%1[%2]").arg(TypeString(v.members[0])).arg(v.members.count());
}
QString typeStr = ToQStr(v.type);
if(v.displayAsHex && v.type == VarType::UInt)
typeStr = lit("xint");
if(v.rows == 1 && v.columns == 1)
return typeStr;
if(v.rows == 1)
return QFormatStr("%1%2").arg(typeStr).arg(v.columns);
else
return QFormatStr("%1%2x%3 (%4)")
.arg(typeStr)
.arg(v.rows)
.arg(v.columns)
.arg(v.rowMajor ? QApplication::tr("row major", "FormatElement")
: QApplication::tr("column major", "FormatElement"));
}
template <typename el>
static QString RowValuesToString(int cols, el x, el y, el z, el w)
{
if(cols == 1)
return Formatter::Format(x);
else if(cols == 2)
return QFormatStr("%1, %2").arg(Formatter::Format(x)).arg(Formatter::Format(y));
else if(cols == 3)
return QFormatStr("%1, %2, %3")
.arg(Formatter::Format(x))
.arg(Formatter::Format(y))
.arg(Formatter::Format(z));
else
return QFormatStr("%1, %2, %3, %4")
.arg(Formatter::Format(x))
.arg(Formatter::Format(y))
.arg(Formatter::Format(z))
.arg(Formatter::Format(w));
}
QString RowString(const ShaderVariable &v, uint32_t row, VarType type)
{
if(type == VarType::Unknown)
type = v.type;
if(type == VarType::Double)
return RowValuesToString((int)v.columns, v.value.dv[row * v.columns + 0],
v.value.dv[row * v.columns + 1], v.value.dv[row * v.columns + 2],
v.value.dv[row * v.columns + 3]);
else if(type == VarType::Int)
return RowValuesToString((int)v.columns, v.value.iv[row * v.columns + 0],
v.value.iv[row * v.columns + 1], v.value.iv[row * v.columns + 2],
v.value.iv[row * v.columns + 3]);
else if(type == VarType::UInt)
return RowValuesToString((int)v.columns, v.value.uv[row * v.columns + 0],
v.value.uv[row * v.columns + 1], v.value.uv[row * v.columns + 2],
v.value.uv[row * v.columns + 3]);
else
return RowValuesToString((int)v.columns, v.value.fv[row * v.columns + 0],
v.value.fv[row * v.columns + 1], v.value.fv[row * v.columns + 2],
v.value.fv[row * v.columns + 3]);
}
QString VarString(const ShaderVariable &v)
{
if(!v.members.isEmpty())
return QString();
if(v.rows == 1)
return RowString(v, 0);
QString ret;
for(int i = 0; i < (int)v.rows; i++)
{
if(i > 0)
ret += lit("\n");
ret += lit("{") + RowString(v, i) + lit("}");
}
return ret;
}
QString RowTypeString(const ShaderVariable &v)
{
if(!v.members.isEmpty() || v.isStruct)
{
if(v.isStruct)
return lit("struct");
else
return lit("flibbertygibbet");
}
if(v.rows == 0 && v.columns == 0)
return lit("-");
QString typeStr = ToQStr(v.type);
if(v.displayAsHex && v.type == VarType::UInt)
typeStr = lit("xint");
if(v.columns == 1)
return typeStr;
return QFormatStr("%1%2").arg(typeStr).arg(v.columns);
}
|