51 #pragma warning(disable:4201) // Non-standard extension used: nameless struct/union.
55 #define DRMTL_MAGIC_NUMBER 0x81DF7405
56 #define DRMTL_CURRENT_VERSION 1
58 #define DRMTL_INPUT_DESC_CONSTI ((unsigned char)-2)
59 #define DRMTL_INPUT_DESC_CONSTF ((unsigned char)-1)
60 #define DRMTL_INPUT_DESC_VARX ((unsigned char)0)
61 #define DRMTL_INPUT_DESC_VARY ((unsigned char)1)
62 #define DRMTL_INPUT_DESC_VARZ ((unsigned char)2)
63 #define DRMTL_INPUT_DESC_VARW ((unsigned char)3)
65 #define DRMTL_MAX_IDENTIFIER_NAME 28
66 #define DRMTL_MAX_CHANNEL_NAME 28
67 #define DRMTL_MAX_PROPERTY_NAME 28
69 #define DRMTL_MAX_INPUT_PATH 252
70 #define DRMTL_MAX_PROPERTY_PATH 224
215 unsigned int identifierIndex;
406 unsigned int texture;
426 unsigned int identifierIndex;
523 unsigned int identifierSizeInBytes;
526 unsigned int inputSizeInBytes;
529 unsigned int channelHeaderSizeInBytes;
532 unsigned int instructionSizeInBytes;
535 unsigned int propertySizeInBytes;
539 unsigned int identifierCount;
542 unsigned int privateInputCount;
545 unsigned int publicInputCount;
548 unsigned int channelCount;
551 unsigned int propertyCount;
555 unsigned int identifiersOffset;
558 unsigned int inputsOffset;
561 unsigned int channelsOffset;
564 unsigned int propertiesOffset;
575 unsigned int instructionCount;
793 drmtl_instruction drmtl_mulf4_v1v1v1v1(
unsigned int outputIdentifierIndex,
unsigned int inputIdentifierIndexX,
unsigned int inputIdentifierIndexY,
unsigned int inputIdentifierIndexZ,
unsigned int inputIdentifierIndexW);
804 drmtl_instruction drmtl_tex2(
unsigned int outputIdentifierIndex,
unsigned int textureIdentifierIndex,
unsigned int texcoordIdentifierIndex);
845 #ifndef DRMTL_NO_MTL_COMPILER
873 #ifndef DRMTL_NO_GLSL_CODEGEN
885 #if defined(_MSC_VER)
901 #ifdef DR_MTL_IMPLEMENTATION
908 #if defined(__clang__)
909 #pragma GCC diagnostic push
910 #pragma GCC diagnostic ignored "-Wcast-align"
915 #define DRMTL_CHUNK_SIZE 4096
917 #define DRMTL_STAGE_IDS 0
918 #define DRMTL_STAGE_PRIVATE_INPUTS 1
919 #define DRMTL_STAGE_PUBLIC_INPUTS 2
920 #define DRMTL_STAGE_CHANNELS 3
921 #define DRMTL_STAGE_PROPERTIES 4
922 #define DRMTL_STAGE_COMPLETE UINT_MAX
929 static int drmtl_strcpy(
char* dst,
size_t dstSizeInBytes,
const char* src)
931 #if defined(_MSC_VER)
932 return strcpy_s(dst, dstSizeInBytes, src);
937 if (dstSizeInBytes == 0) {
946 const char* iSrc = src;
947 size_t remainingSizeInBytes = dstSizeInBytes;
948 while (remainingSizeInBytes > 0 && iSrc[0] !=
'\0')
954 remainingSizeInBytes -= 1;
957 if (remainingSizeInBytes > 0) {
976 if (pMaterial !=
NULL)
978 assert(DRMTL_CHUNK_SIZE >= 256);
990 assert(pHeader !=
NULL);
1018 if (pMaterial !=
NULL)
1027 memcpy(pMaterial->
pRawData, pRawData, dataSizeInBytes);
1045 if (pMaterial !=
NULL)
1068 if (pMaterial !=
NULL)
1082 if (pMaterial !=
NULL)
1093 if (pMaterial !=
NULL)
1098 if (pHeader !=
NULL)
1102 if (!_drmtl_inflate(pMaterial))
1122 if (indexOut !=
NULL)
1137 if (pMaterial !=
NULL)
1139 if (pMaterial->
currentStage <= DRMTL_STAGE_PRIVATE_INPUTS)
1142 if (pHeader !=
NULL)
1146 if (!_drmtl_inflate(pMaterial))
1176 if (pMaterial !=
NULL)
1178 if (pMaterial->
currentStage <= DRMTL_STAGE_PUBLIC_INPUTS)
1181 if (pHeader !=
NULL)
1185 if (!_drmtl_inflate(pMaterial))
1215 if (pMaterial !=
NULL)
1220 if (pHeader !=
NULL)
1223 channelHeader.
channel = channel;
1228 if (!_drmtl_inflate(pMaterial))
1258 if (pMaterial !=
NULL)
1263 if (pHeader !=
NULL)
1267 if (!_drmtl_inflate(pMaterial))
1282 if (pChannelHeader !=
NULL)
1300 if (pMaterial !=
NULL)
1305 if (pHeader !=
NULL)
1309 if (!_drmtl_inflate(pMaterial))
1338 if (pMaterial !=
NULL)
1341 assert(pHeader !=
NULL);
1343 if (channelIndex < pHeader->channelCount)
1346 for (
unsigned int iChannel = 0; iChannel < channelIndex; ++iChannel)
1360 if (pMaterial !=
NULL)
1363 assert(pHeader !=
NULL);
1366 for (
unsigned int iChannel = 0; iChannel < pHeader->
channelCount; ++iChannel)
1382 if (pMaterial !=
NULL)
1385 assert(pHeader !=
NULL);
1395 if (pMaterial !=
NULL)
1398 assert(pHeader !=
NULL);
1400 if (index < pHeader->identifierCount)
1403 return firstIdentifier + index;
1412 if (pMaterial !=
NULL)
1415 assert(pHeader !=
NULL);
1426 if (pMaterial !=
NULL)
1429 assert(pHeader !=
NULL);
1439 if (pMaterial !=
NULL)
1442 assert(pHeader !=
NULL);
1447 return firstInput + index;
1456 if (pMaterial !=
NULL)
1459 assert(pHeader !=
NULL);
1469 if (pMaterial !=
NULL)
1472 assert(pHeader !=
NULL);
1474 if (index < pHeader->privateInputCount)
1477 return firstInput + index;
1486 if (pMaterial !=
NULL)
1489 assert(pHeader !=
NULL);
1499 if (pMaterial !=
NULL)
1502 assert(pHeader !=
NULL);
1504 if (index < pHeader->publicInputCount)
1517 if (pMaterial !=
NULL)
1520 assert(pHeader !=
NULL);
1530 if (pMaterial !=
NULL)
1533 assert(pHeader !=
NULL);
1535 if (index < pHeader->propertyCount)
1538 return firstProperty + index;
1547 if (pMaterial !=
NULL)
1550 assert(pHeader !=
NULL);
1555 assert(pProperty !=
NULL);
1557 if (strcmp(pProperty->
name,
name) == 0)
1573 assert(pMaterial !=
NULL);
1577 if (pNewBuffer !=
NULL)
1579 memcpy(pNewBuffer, pOldBuffer, pMaterial->
sizeInBytes);
1849 inst.
mov.inputX.id = inputIdentifierIndex;
1850 inst.
mov.output = outputIdentifierIndex;
1860 inst.
mov.inputX.valuef = x;
1861 inst.
mov.output = outputIdentifierIndex;
1872 inst.
mov.inputX.id = inputIdentifierIndex;
1873 inst.
mov.inputY.id = inputIdentifierIndex;
1874 inst.
mov.output = outputIdentifierIndex;
1885 inst.
mov.inputX.valuef = x;
1886 inst.
mov.inputY.valuef = y;
1887 inst.
mov.output = outputIdentifierIndex;
1899 inst.
mov.inputX.id = inputIdentifierIndex;
1900 inst.
mov.inputY.id = inputIdentifierIndex;
1901 inst.
mov.inputZ.id = inputIdentifierIndex;
1902 inst.
mov.output = outputIdentifierIndex;
1914 inst.
mov.inputX.valuef = x;
1915 inst.
mov.inputY.valuef = y;
1916 inst.
mov.inputZ.valuef = z;
1917 inst.
mov.output = outputIdentifierIndex;
1930 inst.
mov.inputX.id = inputIdentifierIndex;
1931 inst.
mov.inputY.id = inputIdentifierIndex;
1932 inst.
mov.inputZ.id = inputIdentifierIndex;
1933 inst.
mov.inputW.id = inputIdentifierIndex;
1934 inst.
mov.output = outputIdentifierIndex;
1947 inst.
mov.inputX.valuef = x;
1948 inst.
mov.inputY.valuef = y;
1949 inst.
mov.inputZ.valuef = z;
1950 inst.
mov.inputW.valuef = w;
1951 inst.
mov.output = outputIdentifierIndex;
1962 inst.
add.inputX.id = inputIdentifierIndex;
1963 inst.
add.output = outputIdentifierIndex;
1973 inst.
add.inputX.valuef = x;
1974 inst.
add.output = outputIdentifierIndex;
1985 inst.
add.inputX.id = inputIdentifierIndex;
1986 inst.
add.inputY.id = inputIdentifierIndex;
1987 inst.
add.output = outputIdentifierIndex;
1998 inst.
add.inputX.valuef = x;
1999 inst.
add.inputY.valuef = y;
2000 inst.
add.output = outputIdentifierIndex;
2012 inst.
add.inputX.id = inputIdentifierIndex;
2013 inst.
add.inputY.id = inputIdentifierIndex;
2014 inst.
add.inputZ.id = inputIdentifierIndex;
2015 inst.
add.output = outputIdentifierIndex;
2027 inst.
add.inputX.valuef = x;
2028 inst.
add.inputY.valuef = y;
2029 inst.
add.inputZ.valuef = z;
2030 inst.
add.output = outputIdentifierIndex;
2043 inst.
add.inputX.id = inputIdentifierIndex;
2044 inst.
add.inputY.id = inputIdentifierIndex;
2045 inst.
add.inputZ.id = inputIdentifierIndex;
2046 inst.
add.inputW.id = inputIdentifierIndex;
2047 inst.
add.output = outputIdentifierIndex;
2060 inst.
add.inputX.valuef = x;
2061 inst.
add.inputY.valuef = y;
2062 inst.
add.inputZ.valuef = z;
2063 inst.
add.inputW.valuef = w;
2064 inst.
add.output = outputIdentifierIndex;
2075 inst.
sub.inputX.id = inputIdentifierIndex;
2076 inst.
sub.output = outputIdentifierIndex;
2086 inst.
sub.inputX.valuef = x;
2087 inst.
sub.output = outputIdentifierIndex;
2098 inst.
sub.inputX.id = inputIdentifierIndex;
2099 inst.
sub.inputY.id = inputIdentifierIndex;
2100 inst.
sub.output = outputIdentifierIndex;
2111 inst.
sub.inputX.valuef = x;
2112 inst.
sub.inputY.valuef = y;
2113 inst.
sub.output = outputIdentifierIndex;
2125 inst.
sub.inputX.id = inputIdentifierIndex;
2126 inst.
sub.inputY.id = inputIdentifierIndex;
2127 inst.
sub.inputZ.id = inputIdentifierIndex;
2128 inst.
sub.output = outputIdentifierIndex;
2140 inst.
sub.inputX.valuef = x;
2141 inst.
sub.inputY.valuef = y;
2142 inst.
sub.inputZ.valuef = z;
2143 inst.
sub.output = outputIdentifierIndex;
2156 inst.
sub.inputX.id = inputIdentifierIndex;
2157 inst.
sub.inputY.id = inputIdentifierIndex;
2158 inst.
sub.inputZ.id = inputIdentifierIndex;
2159 inst.
sub.inputW.id = inputIdentifierIndex;
2160 inst.
sub.output = outputIdentifierIndex;
2173 inst.
sub.inputX.valuef = x;
2174 inst.
sub.inputY.valuef = y;
2175 inst.
sub.inputZ.valuef = z;
2176 inst.
sub.inputW.valuef = w;
2177 inst.
sub.output = outputIdentifierIndex;
2188 inst.
mul.inputX.id = inputIdentifierIndex;
2189 inst.
mul.output = outputIdentifierIndex;
2199 inst.
mul.inputX.valuef = x;
2200 inst.
mul.output = outputIdentifierIndex;
2211 inst.
mul.inputX.id = inputIdentifierIndex;
2212 inst.
mul.inputY.id = inputIdentifierIndex;
2213 inst.
mul.output = outputIdentifierIndex;
2224 inst.
mul.inputX.valuef = x;
2225 inst.
mul.inputY.valuef = y;
2226 inst.
mul.output = outputIdentifierIndex;
2238 inst.
mul.inputX.id = inputIdentifierIndex;
2239 inst.
mul.inputY.id = inputIdentifierIndex;
2240 inst.
mul.inputZ.id = inputIdentifierIndex;
2241 inst.
mul.output = outputIdentifierIndex;
2253 inst.
mul.inputX.valuef = x;
2254 inst.
mul.inputY.valuef = y;
2255 inst.
mul.inputZ.valuef = z;
2256 inst.
mul.output = outputIdentifierIndex;
2269 inst.
mul.inputX.id = inputIdentifierIndex;
2270 inst.
mul.inputY.id = inputIdentifierIndex;
2271 inst.
mul.inputZ.id = inputIdentifierIndex;
2272 inst.
mul.inputW.id = inputIdentifierIndex;
2273 inst.
mul.output = outputIdentifierIndex;
2286 inst.
mul.inputX.valuef = x;
2287 inst.
mul.inputY.valuef = y;
2288 inst.
mul.inputZ.valuef = z;
2289 inst.
mul.inputW.valuef = w;
2290 inst.
mul.output = outputIdentifierIndex;
2303 inst.
mul.inputX.id = inputIdentifierIndexXYZ;
2304 inst.
mul.inputY.id = inputIdentifierIndexXYZ;
2305 inst.
mul.inputZ.id = inputIdentifierIndexXYZ;
2306 inst.
mul.inputW.id = inputIdentifierIndexW;
2307 inst.
mul.output = outputIdentifierIndex;
2320 inst.
mul.inputX.id = inputIdentifierIndex;
2321 inst.
mul.inputY.id = inputIdentifierIndex;
2322 inst.
mul.inputZ.id = inputIdentifierIndex;
2323 inst.
mul.inputW.valuef = w;
2324 inst.
mul.output = outputIdentifierIndex;
2337 inst.
mul.inputX.id = inputIdentifierIndex;
2338 inst.
mul.inputY.id = inputIdentifierIndex;
2339 inst.
mul.inputZ.valuef = z;
2340 inst.
mul.inputW.valuef = w;
2341 inst.
mul.output = outputIdentifierIndex;
2354 inst.
mul.inputX.id = inputIdentifierIndex;
2355 inst.
mul.inputY.valuef = y;
2356 inst.
mul.inputZ.valuef = z;
2357 inst.
mul.inputW.valuef = w;
2358 inst.
mul.output = outputIdentifierIndex;
2363 drmtl_instruction drmtl_mulf4_v1v1v1v1(
unsigned int outputIdentifierIndex,
unsigned int inputIdentifierIndexX,
unsigned int inputIdentifierIndexY,
unsigned int inputIdentifierIndexZ,
unsigned int inputIdentifierIndexW)
2371 inst.
mul.inputX.id = inputIdentifierIndexX;
2372 inst.
mul.inputY.id = inputIdentifierIndexY;
2373 inst.
mul.inputZ.id = inputIdentifierIndexZ;
2374 inst.
mul.inputW.id = inputIdentifierIndexW;
2375 inst.
mul.output = outputIdentifierIndex;
2386 inst.
div.inputX.id = inputIdentifierIndex;
2387 inst.
div.output = outputIdentifierIndex;
2397 inst.
div.inputX.valuef = x;
2398 inst.
div.output = outputIdentifierIndex;
2409 inst.
div.inputX.id = inputIdentifierIndex;
2410 inst.
div.inputY.id = inputIdentifierIndex;
2411 inst.
div.output = outputIdentifierIndex;
2422 inst.
div.inputX.valuef = x;
2423 inst.
div.inputY.valuef = y;
2424 inst.
div.output = outputIdentifierIndex;
2436 inst.
div.inputX.id = inputIdentifierIndex;
2437 inst.
div.inputY.id = inputIdentifierIndex;
2438 inst.
div.inputZ.id = inputIdentifierIndex;
2439 inst.
div.output = outputIdentifierIndex;
2451 inst.
div.inputX.valuef = x;
2452 inst.
div.inputY.valuef = y;
2453 inst.
div.inputZ.valuef = z;
2454 inst.
div.output = outputIdentifierIndex;
2467 inst.
div.inputX.id = inputIdentifierIndex;
2468 inst.
div.inputY.id = inputIdentifierIndex;
2469 inst.
div.inputZ.id = inputIdentifierIndex;
2470 inst.
div.inputW.id = inputIdentifierIndex;
2471 inst.
div.output = outputIdentifierIndex;
2484 inst.
div.inputX.valuef = x;
2485 inst.
div.inputY.valuef = y;
2486 inst.
div.inputZ.valuef = z;
2487 inst.
div.inputW.valuef = w;
2488 inst.
div.output = outputIdentifierIndex;
2494 drmtl_instruction drmtl_tex2(
unsigned int outputIdentifierIndex,
unsigned int textureIdentifierIndex,
unsigned int texcoordIdentifierIndex)
2500 inst.
tex.inputX.id = texcoordIdentifierIndex;
2501 inst.
tex.inputY.id = texcoordIdentifierIndex;
2502 inst.
tex.texture = textureIdentifierIndex;
2503 inst.
tex.output = outputIdentifierIndex;
2512 inst.
var.identifierIndex = identifierIndex;
2522 inst.
ret.inputX.id = identifierIndex;
2533 inst.
ret.inputX.id = identifierIndex;
2534 inst.
ret.inputY.id = identifierIndex;
2546 inst.
ret.inputX.id = identifierIndex;
2547 inst.
ret.inputY.id = identifierIndex;
2548 inst.
ret.inputZ.id = identifierIndex;
2561 inst.
ret.inputX.id = identifierIndex;
2562 inst.
ret.inputY.id = identifierIndex;
2563 inst.
ret.inputZ.id = identifierIndex;
2564 inst.
ret.inputW.id = identifierIndex;
2574 inst.
ret.inputX.valuef = x;
2585 inst.
ret.inputX.valuef = x;
2586 inst.
ret.inputY.valuef = y;
2598 inst.
ret.inputX.valuef = x;
2599 inst.
ret.inputY.valuef = y;
2600 inst.
ret.inputZ.valuef = z;
2613 inst.
ret.inputX.valuef = x;
2614 inst.
ret.inputY.valuef = y;
2615 inst.
ret.inputZ.valuef = z;
2616 inst.
ret.inputW.valuef = w;
2626 inst.
ret.inputX.id = identifierIndex;
2637 inst.
ret.inputX.id = identifierIndex;
2638 inst.
ret.inputY.id = identifierIndex;
2650 inst.
ret.inputX.id = identifierIndex;
2651 inst.
ret.inputY.id = identifierIndex;
2652 inst.
ret.inputZ.id = identifierIndex;
2665 inst.
ret.inputX.id = identifierIndex;
2666 inst.
ret.inputY.id = identifierIndex;
2667 inst.
ret.inputZ.id = identifierIndex;
2668 inst.
ret.inputW.id = identifierIndex;
2678 inst.
ret.inputX.valuei = x;
2689 inst.
ret.inputX.valuei = x;
2690 inst.
ret.inputY.valuei = y;
2702 inst.
ret.inputX.valuei = x;
2703 inst.
ret.inputY.valuei = y;
2704 inst.
ret.inputZ.valuei = z;
2717 inst.
ret.inputX.valuei = x;
2718 inst.
ret.inputY.valuei = y;
2719 inst.
ret.inputZ.valuei = z;
2720 inst.
ret.inputW.valuei = w;
2841 #ifndef DRMTL_NO_MTL_COMPILER
2848 unsigned int dataSizeInBytes;
2851 const char* pDataCur;
2854 const char* pDataEnd;
2872 float specularExponent;
2887 bool drmtl_wavefront_is_whitespace(
char c)
2889 return c ==
' ' || c ==
'\t';
2892 bool drmtl_wavefront_is_valid_digit(
char c)
2894 return c >=
'0' && c <=
'9';
2897 bool drmtl_wavefront_atof(
const char* str,
const char* strEnd,
const char** strEndOut,
float* valueOut)
2900 while (str < strEnd && drmtl_wavefront_is_whitespace(*str))
2918 else if (*str ==
'+')
2926 while (str < strEnd && drmtl_wavefront_is_valid_digit(*str))
2928 value = value * 10.0f + (*str -
'0');
2936 float pow10 = 10.0f;
2939 while (str < strEnd && drmtl_wavefront_is_valid_digit(*str))
2941 value += (*str -
'0') / pow10;
2949 if (strEndOut !=
NULL)
2954 if (valueOut !=
NULL)
2956 *valueOut = sign * value;
2968 bool drmtl_wavefront_atof_3(
const char* str,
const char* strEnd,
const char** strEndOut,
float valueOut[3])
2971 if (drmtl_wavefront_atof(str, strEnd, &str, &value[0]))
2973 value[1] = value[0];
2974 value[2] = value[0];
2976 if (drmtl_wavefront_atof(str, strEnd, &str, &value[1]))
2979 if (!drmtl_wavefront_atof(str, strEnd, strEndOut, &value[2]))
2987 valueOut[0] = value[0];
2988 valueOut[1] = value[1];
2989 valueOut[2] = value[2];
2997 const char* drmtl_wavefront_find_end_of_line(
const char* pDataCur,
const char* pDataEnd)
2999 assert(pDataCur !=
NULL);
3000 assert(pDataEnd !=
NULL);
3002 while (pDataCur < pDataEnd)
3004 if (pDataCur[0] ==
'\n')
3010 if (pDataCur + 1 < pDataEnd)
3012 if (pDataCur[0] ==
'\r' && pDataCur[1] ==
'\n')
3026 const char* drmtl_wavefront_find_next_line(
const char* pDataCur,
const char* pDataEnd)
3028 assert(pDataCur !=
NULL);
3029 assert(pDataEnd !=
NULL);
3031 pDataCur = drmtl_wavefront_find_end_of_line(pDataCur, pDataEnd);
3032 if (pDataCur !=
NULL)
3034 if (pDataCur < pDataEnd)
3036 if (pDataCur[0] ==
'\n')
3038 return pDataCur + 1;
3042 if (pDataCur + 1 < pDataEnd)
3044 if (pDataCur[0] ==
'\r' && pDataCur[1] ==
'\n')
3046 return pDataCur + 2;
3056 const char* drmtl_wavefront_find_next_newmtl(
const char* pDataCur,
const char* pDataEnd)
3058 assert(pDataCur !=
NULL);
3059 assert(pDataEnd !=
NULL);
3061 while (pDataCur + 7 < pDataEnd)
3063 if (pDataCur[0] ==
'n' && pDataCur[1] ==
'e' && pDataCur[2] ==
'w' && pDataCur[3] ==
'm' && pDataCur[4] ==
't' && pDataCur[5] ==
'l')
3066 if (drmtl_wavefront_is_whitespace(pDataCur[6]))
3074 const char* nextLineStart = drmtl_wavefront_find_next_line(pDataCur, pDataEnd);
3075 if (nextLineStart !=
NULL)
3077 pDataCur = nextLineStart;
3089 const char* drmtl_wavefront_find_next_nonwhitespace(
const char* pDataCur,
const char* pDataEnd)
3091 assert(pDataCur !=
NULL);
3092 assert(pDataEnd !=
NULL);
3094 while (pDataCur < pDataEnd)
3096 if (!drmtl_wavefront_is_whitespace(pDataCur[0]))
3108 bool drmtl_wavefront_parse_K(
const char* pDataCur,
const char* pDataEnd,
float valueOut[3])
3110 assert(pDataCur !=
NULL);
3111 assert(pDataEnd !=
NULL);
3113 return drmtl_wavefront_atof_3(pDataCur, pDataEnd, &pDataEnd, valueOut);
3116 bool drmtl_wavefront_parse_N(
const char* pDataCur,
const char* pDataEnd,
float* valueOut)
3118 assert(pDataCur !=
NULL);
3119 assert(pDataEnd !=
NULL);
3121 return drmtl_wavefront_atof(pDataCur, pDataEnd, &pDataEnd, valueOut);
3124 bool drmtl_wavefront_parse_map(
const char* pDataCur,
const char* pDataEnd,
char* pathOut,
unsigned int pathSizeInBytes)
3126 assert(pDataCur !=
NULL);
3127 assert(pDataEnd !=
NULL);
3131 const char* pPathStart = drmtl_wavefront_find_next_nonwhitespace(pDataCur, pDataEnd);
3132 if (pPathStart !=
NULL)
3134 if (pPathStart < pDataEnd)
3136 if (pPathStart[0] !=
'#')
3139 pDataCur = pPathStart;
3140 const char* pPathEnd = pDataCur;
3141 while (pDataCur < pDataEnd && pDataCur[0] !=
'#')
3143 if (!drmtl_wavefront_is_whitespace(pDataCur[0]))
3145 pPathEnd = pDataCur + 1;
3151 assert(pPathStart < pPathEnd);
3153 ptrdiff_t pathLength = pPathEnd - pPathStart;
3154 if ((
size_t)pathLength + 1 < pathSizeInBytes)
3156 memcpy(pathOut, pPathStart, (
size_t)pathLength);
3157 pathOut[pathLength] =
'\0';
3169 bool drmtl_wavefront_seek_to_next_line(drmtl_wavefront* pWavefront)
3171 assert(pWavefront !=
NULL);
3173 const char* lineStart = drmtl_wavefront_find_next_line(pWavefront->pDataCur, pWavefront->pDataEnd);
3174 if (lineStart !=
NULL)
3176 pWavefront->pDataCur = lineStart;
3183 bool drmtl_wavefront_seek_to_newmtl(drmtl_wavefront* pWavefront)
3185 assert(pWavefront !=
NULL);
3187 const char* usemtl = drmtl_wavefront_find_next_newmtl(pWavefront->pDataCur, pWavefront->pDataEnd);
3190 pWavefront->pDataCur = usemtl;
3197 bool drmtl_wavefront_parse(drmtl_wavefront* pWavefront)
3199 assert(pWavefront !=
NULL);
3201 if (drmtl_wavefront_seek_to_newmtl(pWavefront) && drmtl_wavefront_seek_to_next_line(pWavefront))
3204 const char* usemtl2 = drmtl_wavefront_find_next_newmtl(pWavefront->pDataCur, pWavefront->pDataEnd);
3205 if (usemtl2 !=
NULL)
3207 pWavefront->pDataEnd = usemtl2;
3211 while (pWavefront->pDataCur < pWavefront->pDataEnd)
3213 const char* lineCur = pWavefront->pDataCur;
3214 const char* lineEnd = drmtl_wavefront_find_end_of_line(lineCur, pWavefront->pDataEnd);
3216 lineCur = drmtl_wavefront_find_next_nonwhitespace(lineCur, lineEnd);
3217 if (lineCur !=
NULL && (lineCur + 2 < lineEnd))
3219 if (lineCur[0] ==
'K' && lineCur[1] ==
'd' && drmtl_wavefront_is_whitespace(lineCur[2]))
3222 drmtl_wavefront_parse_K(lineCur, lineEnd, pWavefront->diffuse);
3224 else if (lineCur[0] ==
'K' && lineCur[1] ==
's' && drmtl_wavefront_is_whitespace(lineCur[2]))
3227 drmtl_wavefront_parse_K(lineCur, lineEnd, pWavefront->specular);
3229 else if (lineCur[0] ==
'N' && lineCur[1] ==
's' && drmtl_wavefront_is_whitespace(lineCur[2]))
3232 drmtl_wavefront_parse_N(lineCur, lineEnd, &pWavefront->specularExponent);
3234 else if (lineCur[0] ==
'd' && drmtl_wavefront_is_whitespace(lineCur[1]))
3237 drmtl_wavefront_parse_N(lineCur, lineEnd, &pWavefront->alpha);
3242 if (lineCur + 6 < lineEnd)
3244 if (lineCur[0] ==
'm' && lineCur[1] ==
'a' && lineCur[2] ==
'p' && lineCur[3] ==
'_')
3246 if (lineCur[4] ==
'K' && lineCur[5] ==
'd' && drmtl_wavefront_is_whitespace(lineCur[6]))
3251 else if (lineCur[4] ==
'K' && lineCur[5] ==
's' && drmtl_wavefront_is_whitespace(lineCur[6]))
3256 else if (lineCur[4] ==
'N' && lineCur[5] ==
's' && drmtl_wavefront_is_whitespace(lineCur[6]))
3259 drmtl_wavefront_parse_map(lineCur, lineEnd, pWavefront->specularExponentMap,
DRMTL_MAX_INPUT_PATH);
3261 else if (lineCur[4] ==
'd' && drmtl_wavefront_is_whitespace(lineCur[5]))
3273 pWavefront->pDataCur = lineEnd;
3276 if (!drmtl_wavefront_seek_to_next_line(pWavefront))
3289 bool drmtl_wavefront_compile(
drmtl_material* pMaterial, drmtl_wavefront* pWavefront,
const char* texcoordInputName)
3291 assert(pMaterial !=
NULL);
3292 assert(pWavefront !=
NULL);
3294 unsigned int texCoordID;
3295 unsigned int diffuseID;
3296 unsigned int specularID;
3297 unsigned int specularExponentID;
3298 unsigned int alphaID;
3299 unsigned int diffuseMapID = (
unsigned int)-1;
3300 unsigned int specularMapID = (
unsigned int)-1;
3301 unsigned int specularExponentMapID = (
unsigned int)-1;
3302 unsigned int alphaMapID = (
unsigned int)-1;
3303 unsigned int diffuseResultID = (
unsigned int)-1;
3304 unsigned int specularResultID = (
unsigned int)-1;
3305 unsigned int specularExponentResultID = (
unsigned int)-1;
3306 unsigned int alphaResultID = (
unsigned int)-1;
3316 if (pWavefront->diffuseMap[0] !=
'\0') {
3320 if (pWavefront->specularMap[0] !=
'\0') {
3324 if (pWavefront->specularExponentMap[0] !=
'\0') {
3328 if (pWavefront->alphaMap[0] !=
'\0') {
3341 if (pWavefront->diffuseMap[0] !=
'\0') {
3344 if (pWavefront->specularMap[0] !=
'\0') {
3347 if (pWavefront->specularExponentMap[0] !=
'\0') {
3350 if (pWavefront->alphaMap[0] !=
'\0') {
3357 if (pWavefront->diffuseMap[0] !=
'\0') {
3367 if (pWavefront->specularMap[0] !=
'\0') {
3377 if (pWavefront->specularExponentMap[0] !=
'\0') {
3387 if (pWavefront->alphaMap[0] !=
'\0') {
3399 if (pWavefront->alphaMap[0] !=
'\0' || pWavefront->alpha < 1)
3410 if (pMaterial !=
NULL && mtlData !=
NULL && mtlDataSizeInBytes > 0)
3414 drmtl_wavefront wavefront;
3415 wavefront.pData = mtlData;
3416 wavefront.dataSizeInBytes = mtlDataSizeInBytes;
3417 wavefront.pDataCur = wavefront.pData;
3418 wavefront.pDataEnd = wavefront.pData + wavefront.dataSizeInBytes;
3419 wavefront.diffuse[0] = 1; wavefront.diffuse[1] = 1; wavefront.diffuse[2] = 1;
3420 wavefront.diffuseMap[0] =
'\0';
3421 wavefront.specular[0] = 1; wavefront.specular[1] = 1; wavefront.specular[2] = 1;
3422 wavefront.specularMap[0] =
'\0';
3423 wavefront.specularExponent = 10;
3424 wavefront.specularExponentMap[0] =
'\0';
3425 wavefront.alpha = 1;
3426 wavefront.alphaMap[0] =
'\0';
3428 if (drmtl_wavefront_parse(&wavefront))
3430 if (drmtl_wavefront_compile(pMaterial, &wavefront, texcoordInputName))
3463 #ifndef DRMTL_NO_GLSL_CODEGEN
3466 #if defined(__clang__)
3467 #pragma GCC diagnostic push
3468 #pragma GCC diagnostic ignored "-Wswitch-enum"
3469 #pragma GCC diagnostic ignored "-Wcovered-switch-default"
3470 #pragma GCC diagnostic ignored "-Wused-but-marked-unused" // This ie emitted for snprintf() for some reason. Need to investigate...
3479 unsigned int bufferOutSizeInBytes;
3482 unsigned int runningLength;
3492 unsigned int identifierCount;
3496 unsigned int indentationLevel;
3498 } drmtl_codegen_glsl;
3500 bool drmtl_codegen_glsl_write(drmtl_codegen_glsl* pCodegen,
const char* src)
3502 assert(pCodegen !=
NULL);
3503 assert(src !=
NULL);
3505 if (pCodegen->pBufferOut !=
NULL)
3507 unsigned int dstSizeInBytes = (pCodegen->bufferOutSizeInBytes - pCodegen->runningLength);
3508 while (dstSizeInBytes > 0 && src[0] !=
'\0')
3510 pCodegen->pBufferOut[pCodegen->runningLength + 0] = src[0];
3512 pCodegen->runningLength += 1;
3514 dstSizeInBytes -= 1;
3517 if (dstSizeInBytes > 0)
3520 pCodegen->pBufferOut[pCodegen->runningLength] =
'\0';
3532 pCodegen->runningLength += (
unsigned int)strlen(src);
3537 bool drmtl_codegen_glsl_write_float(drmtl_codegen_glsl* pCodegen,
float src)
3539 assert(pCodegen !=
NULL);
3542 if (snprintf(str, 32,
"%f", src) > 0)
3544 return drmtl_codegen_glsl_write(pCodegen, str);
3552 bool drmtl_codegen_glsl_write_int(drmtl_codegen_glsl* pCodegen,
int src)
3554 assert(pCodegen !=
NULL);
3557 if (snprintf(str, 32,
"%d", src) > 0)
3559 return drmtl_codegen_glsl_write(pCodegen, str);
3567 bool drmtl_codegen_glsl_write_indentation(drmtl_codegen_glsl* pCodegen)
3569 assert(pCodegen !=
NULL);
3571 for (
unsigned int i = 0; i < pCodegen->indentationLevel; ++i)
3573 drmtl_codegen_glsl_write(pCodegen,
" ");
3579 bool drmtl_codegen_glsl_write_type(drmtl_codegen_glsl* pCodegen,
drmtl_type type)
3581 assert(pCodegen !=
NULL);
3587 if (!drmtl_codegen_glsl_write(pCodegen,
"float"))
3596 if (!drmtl_codegen_glsl_write(pCodegen,
"vec2"))
3605 if (!drmtl_codegen_glsl_write(pCodegen,
"vec3"))
3614 if (!drmtl_codegen_glsl_write(pCodegen,
"vec4"))
3624 if (!drmtl_codegen_glsl_write(pCodegen,
"int"))
3633 if (!drmtl_codegen_glsl_write(pCodegen,
"ivec2"))
3642 if (!drmtl_codegen_glsl_write(pCodegen,
"ivec3"))
3651 if (!drmtl_codegen_glsl_write(pCodegen,
"ivec4"))
3661 if (!drmtl_codegen_glsl_write(pCodegen,
"sampler1D"))
3670 if (!drmtl_codegen_glsl_write(pCodegen,
"sampler2D"))
3679 if (!drmtl_codegen_glsl_write(pCodegen,
"sampler3D"))
3688 if (!drmtl_codegen_glsl_write(pCodegen,
"samplerCube"))
3706 bool drmtl_codegen_glsl_write_instruction_input_scalar(drmtl_codegen_glsl* pCodegen,
unsigned char descriptor,
drmtl_instruction_input* pInput)
3708 assert(pCodegen !=
NULL);
3709 assert(pInput !=
NULL);
3714 return drmtl_codegen_glsl_write_float(pCodegen, pInput->
valuef);
3719 return drmtl_codegen_glsl_write_int(pCodegen, pInput->
valuei);
3724 if (pInput->
id < pCodegen->identifierCount)
3727 assert(pIdentifier !=
NULL);
3732 return drmtl_codegen_glsl_write(pCodegen, pIdentifier->
name);
3736 if (drmtl_codegen_glsl_write(pCodegen, pIdentifier->
name) && drmtl_codegen_glsl_write(pCodegen,
"."))
3740 case 0:
return drmtl_codegen_glsl_write(pCodegen,
"x");
3741 case 1:
return drmtl_codegen_glsl_write(pCodegen,
"y");
3742 case 2:
return drmtl_codegen_glsl_write(pCodegen,
"z");
3743 case 3:
return drmtl_codegen_glsl_write(pCodegen,
"w");
3756 assert(pCodegen !=
NULL);
3757 assert(pInputs !=
NULL);
3763 return drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
x, pInputs + 0);
3768 if (drmtl_codegen_glsl_write(pCodegen,
"vec2("))
3770 if (drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
x, pInputs + 0) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3771 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
y, pInputs + 1))
3773 return drmtl_codegen_glsl_write(pCodegen,
")");
3782 if (drmtl_codegen_glsl_write(pCodegen,
"vec3("))
3784 if (drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
x, pInputs + 0) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3785 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
y, pInputs + 1) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3786 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
z, pInputs + 2))
3788 return drmtl_codegen_glsl_write(pCodegen,
")");
3797 if (drmtl_codegen_glsl_write(pCodegen,
"vec4("))
3799 if (drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
x, pInputs + 0) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3800 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
y, pInputs + 1) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3801 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
z, pInputs + 2) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3802 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
w, pInputs + 3))
3804 return drmtl_codegen_glsl_write(pCodegen,
")");
3814 return drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
x, pInputs + 0);
3819 if (drmtl_codegen_glsl_write(pCodegen,
"ivec2("))
3821 if (drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
x, pInputs + 0) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3822 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
y, pInputs + 1))
3824 return drmtl_codegen_glsl_write(pCodegen,
")");
3833 if (drmtl_codegen_glsl_write(pCodegen,
"ivec3("))
3835 if (drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
x, pInputs + 0) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3836 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
y, pInputs + 1) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3837 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
z, pInputs + 2))
3839 return drmtl_codegen_glsl_write(pCodegen,
")");
3848 if (drmtl_codegen_glsl_write(pCodegen,
"ivec4("))
3850 if (drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
x, pInputs + 0) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3851 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
y, pInputs + 1) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3852 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
z, pInputs + 2) && drmtl_codegen_glsl_write(pCodegen,
", ") &&
3853 drmtl_codegen_glsl_write_instruction_input_scalar(pCodegen, inputDesc.
w, pInputs + 3))
3855 return drmtl_codegen_glsl_write(pCodegen,
")");
3874 bool drmtl_codegen_glsl_write_instruction_mov(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstruction)
3876 assert(pCodegen !=
NULL);
3877 assert(pInstruction !=
NULL);
3879 if (pInstruction->
mov.output < pCodegen->identifierCount)
3882 assert(pOutputIdentifier !=
NULL);
3884 if (drmtl_codegen_glsl_write(pCodegen, pOutputIdentifier->
name) && drmtl_codegen_glsl_write(pCodegen,
" = "))
3887 switch (pInstruction->
opcode)
3900 return drmtl_codegen_glsl_write_instruction_input_initializer(pCodegen, type, pInstruction->
mov.inputDesc, &pInstruction->
mov.inputX) && drmtl_codegen_glsl_write(pCodegen,
";\n");
3907 bool drmtl_codegen_glsl_write_instruction_add(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstruction)
3909 assert(pCodegen !=
NULL);
3910 assert(pInstruction !=
NULL);
3912 if (pInstruction->
add.output < pCodegen->identifierCount)
3915 assert(pOutputIdentifier !=
NULL);
3917 if (drmtl_codegen_glsl_write(pCodegen, pOutputIdentifier->
name) && drmtl_codegen_glsl_write(pCodegen,
" += "))
3920 switch (pInstruction->
opcode)
3933 return drmtl_codegen_glsl_write_instruction_input_initializer(pCodegen, type, pInstruction->
add.inputDesc, &pInstruction->
add.inputX) && drmtl_codegen_glsl_write(pCodegen,
";\n");
3940 bool drmtl_codegen_glsl_write_instruction_sub(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstruction)
3942 assert(pCodegen !=
NULL);
3943 assert(pInstruction !=
NULL);
3945 if (pInstruction->
add.output < pCodegen->identifierCount)
3948 assert(pOutputIdentifier !=
NULL);
3950 if (drmtl_codegen_glsl_write(pCodegen, pOutputIdentifier->
name) && drmtl_codegen_glsl_write(pCodegen,
" -= "))
3953 switch (pInstruction->
opcode)
3966 return drmtl_codegen_glsl_write_instruction_input_initializer(pCodegen, type, pInstruction->
sub.inputDesc, &pInstruction->
sub.inputX) && drmtl_codegen_glsl_write(pCodegen,
";\n");
3973 bool drmtl_codegen_glsl_write_instruction_mul(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstruction)
3975 assert(pCodegen !=
NULL);
3976 assert(pInstruction !=
NULL);
3978 if (pInstruction->
mul.output < pCodegen->identifierCount)
3981 assert(pOutputIdentifier !=
NULL);
3983 if (drmtl_codegen_glsl_write(pCodegen, pOutputIdentifier->
name) && drmtl_codegen_glsl_write(pCodegen,
" *= "))
3986 switch (pInstruction->
opcode)
3999 return drmtl_codegen_glsl_write_instruction_input_initializer(pCodegen, type, pInstruction->
mul.inputDesc, &pInstruction->
mul.inputX) && drmtl_codegen_glsl_write(pCodegen,
";\n");
4006 bool drmtl_codegen_glsl_write_instruction_div(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstruction)
4008 assert(pCodegen !=
NULL);
4009 assert(pInstruction !=
NULL);
4011 if (pInstruction->
div.output < pCodegen->identifierCount)
4014 assert(pOutputIdentifier !=
NULL);
4016 if (drmtl_codegen_glsl_write(pCodegen, pOutputIdentifier->
name) && drmtl_codegen_glsl_write(pCodegen,
" = "))
4019 switch (pInstruction->
opcode)
4032 return drmtl_codegen_glsl_write_instruction_input_initializer(pCodegen, type, pInstruction->
div.inputDesc, &pInstruction->
div.inputX) && drmtl_codegen_glsl_write(pCodegen,
";\n");
4039 bool drmtl_codegen_glsl_write_instruction_pow(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstruction)
4041 assert(pCodegen !=
NULL);
4042 assert(pInstruction !=
NULL);
4044 if (pInstruction->
pow.output < pCodegen->identifierCount)
4047 assert(pOutputIdentifier !=
NULL);
4049 if (drmtl_codegen_glsl_write(pCodegen, pOutputIdentifier->
name) && drmtl_codegen_glsl_write(pCodegen,
" = pow(") && drmtl_codegen_glsl_write(pCodegen, pOutputIdentifier->
name) && drmtl_codegen_glsl_write(pCodegen,
", "))
4052 switch (pInstruction->
opcode)
4065 return drmtl_codegen_glsl_write_instruction_input_initializer(pCodegen, type, pInstruction->
pow.inputDesc, &pInstruction->
pow.inputX) && drmtl_codegen_glsl_write(pCodegen,
");\n");
4072 bool drmtl_codegen_glsl_write_instruction_tex(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstruction)
4074 assert(pCodegen !=
NULL);
4075 assert(pInstruction !=
NULL);
4077 if (pInstruction->
tex.output < pCodegen->identifierCount && pInstruction->
tex.texture < pCodegen->identifierCount)
4080 assert(pOutputIdentifier !=
NULL);
4082 drmtl_identifier* pTextureIdentifier = pCodegen->pIdentifiers + pInstruction->
tex.texture;
4083 assert(pTextureIdentifier !=
NULL);
4085 if (drmtl_codegen_glsl_write(pCodegen, pOutputIdentifier->
name) && drmtl_codegen_glsl_write(pCodegen,
" = "))
4088 switch (pInstruction->
opcode)
4093 if (!drmtl_codegen_glsl_write(pCodegen,
"texture1D("))
4104 if (!drmtl_codegen_glsl_write(pCodegen,
"texture2D("))
4115 if (!drmtl_codegen_glsl_write(pCodegen,
"texture3D("))
4126 if (!drmtl_codegen_glsl_write(pCodegen,
"textureCube("))
4138 drmtl_codegen_glsl_write(pCodegen, pTextureIdentifier->
name) &&
4139 drmtl_codegen_glsl_write(pCodegen,
", ") &&
4140 drmtl_codegen_glsl_write_instruction_input_initializer(pCodegen, type, pInstruction->
tex.inputDesc, &pInstruction->
tex.inputX) &&
4141 drmtl_codegen_glsl_write(pCodegen,
");\n");
4148 bool drmtl_codegen_glsl_write_instruction_var(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstruction)
4150 assert(pCodegen !=
NULL);
4151 assert(pInstruction !=
NULL);
4153 if (pInstruction->
var.identifierIndex < pCodegen->identifierCount)
4155 drmtl_identifier* pIdentifier = pCodegen->pIdentifiers + pInstruction->
var.identifierIndex;
4156 assert(pIdentifier !=
NULL);
4158 return drmtl_codegen_glsl_write_type(pCodegen, pIdentifier->
type) && drmtl_codegen_glsl_write(pCodegen,
" ") && drmtl_codegen_glsl_write(pCodegen, pIdentifier->
name) && drmtl_codegen_glsl_write(pCodegen,
";\n");
4164 bool drmtl_codegen_glsl_write_instruction_ret(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstruction)
4166 assert(pCodegen !=
NULL);
4167 assert(pInstruction !=
NULL);
4169 if (drmtl_codegen_glsl_write(pCodegen,
"return "))
4172 switch (pInstruction->
opcode)
4185 return drmtl_codegen_glsl_write_instruction_input_initializer(pCodegen, type, pInstruction->
ret.inputDesc, &pInstruction->
ret.inputX) && drmtl_codegen_glsl_write(pCodegen,
";\n");
4191 bool drmtl_codegen_glsl_write_instruction(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstruction)
4193 assert(pCodegen !=
NULL);
4194 assert(pInstruction !=
NULL);
4196 if (drmtl_codegen_glsl_write_indentation(pCodegen))
4198 switch (pInstruction->
opcode)
4209 return drmtl_codegen_glsl_write_instruction_mov(pCodegen, pInstruction);
4222 return drmtl_codegen_glsl_write_instruction_add(pCodegen, pInstruction);
4234 return drmtl_codegen_glsl_write_instruction_sub(pCodegen, pInstruction);
4246 return drmtl_codegen_glsl_write_instruction_mul(pCodegen, pInstruction);
4258 return drmtl_codegen_glsl_write_instruction_div(pCodegen, pInstruction);
4270 return drmtl_codegen_glsl_write_instruction_pow(pCodegen, pInstruction);
4278 return drmtl_codegen_glsl_write_instruction_tex(pCodegen, pInstruction);
4284 return drmtl_codegen_glsl_write_instruction_var(pCodegen, pInstruction);
4296 return drmtl_codegen_glsl_write_instruction_ret(pCodegen, pInstruction);
4311 bool drmtl_codegen_glsl_write_instructions(drmtl_codegen_glsl* pCodegen,
drmtl_instruction* pInstructions,
unsigned int instructionCount)
4313 assert(pCodegen !=
NULL);
4314 assert(pInstructions !=
NULL);
4316 for (
unsigned int iInstruction = 0; iInstruction < instructionCount; ++iInstruction)
4319 assert(pInstruction !=
NULL);
4321 if (!drmtl_codegen_glsl_write_instruction(pCodegen, pInstruction))
4330 bool drmtl_codegen_glsl_channel_function_begin(drmtl_codegen_glsl* pCodegen,
drmtl_channel_header* pChannelHeader)
4332 assert(pCodegen !=
NULL);
4333 assert(pChannelHeader !=
NULL);
4337 drmtl_codegen_glsl_write_type(pCodegen, pChannelHeader->
channel.
type) &&
4338 drmtl_codegen_glsl_write(pCodegen,
" ") &&
4339 drmtl_codegen_glsl_write(pCodegen, pChannelHeader->
channel.
name) &&
4340 drmtl_codegen_glsl_write(pCodegen,
"() {\n");
4343 pCodegen->indentationLevel += 4;
4349 bool drmtl_codegen_glsl_channel_function_close(drmtl_codegen_glsl* pCodegen)
4351 assert(pCodegen !=
NULL);
4353 if (pCodegen->indentationLevel > 4) {
4354 pCodegen->indentationLevel -= 4;
4356 pCodegen->indentationLevel = 0;
4359 return drmtl_codegen_glsl_write(pCodegen,
"}\n");
4364 if (pMaterial !=
NULL)
4367 if (pHeader !=
NULL)
4370 if (pChannelHeader !=
NULL)
4372 drmtl_codegen_glsl codegen;
4373 codegen.pBufferOut = codeOut;
4374 codegen.bufferOutSizeInBytes = codeOutSizeInBytes;
4375 codegen.runningLength = 0;
4376 codegen.pMaterial = pMaterial;
4379 codegen.indentationLevel = 0;
4381 if (drmtl_codegen_glsl_channel_function_begin(&codegen, pChannelHeader))
4384 assert(pInstructions !=
NULL);
4386 if (drmtl_codegen_glsl_write_instructions(&codegen, pInstructions, pChannelHeader->
instructionCount))
4388 bool result = drmtl_codegen_glsl_channel_function_close(&codegen);
4391 if (pBytesWrittenOut !=
NULL)
4393 *pBytesWrittenOut = codegen.runningLength + 1;
4409 bool drmtl_codegen_glsl_uniform(drmtl_codegen_glsl* pCodegen,
drmtl_input* pInput)
4411 assert(pCodegen !=
NULL);
4412 assert(pInput !=
NULL);
4417 assert(pIdentifier !=
NULL);
4421 drmtl_codegen_glsl_write(pCodegen,
"uniform ") &&
4422 drmtl_codegen_glsl_write_type(pCodegen, pIdentifier->
type) &&
4423 drmtl_codegen_glsl_write(pCodegen,
" ") &&
4424 drmtl_codegen_glsl_write(pCodegen, pIdentifier->
name) &&
4425 drmtl_codegen_glsl_write(pCodegen,
";\n");
4433 if (pMaterial !=
NULL)
4435 drmtl_codegen_glsl codegen;
4436 codegen.pBufferOut = codeOut;
4437 codegen.bufferOutSizeInBytes = codeOutSizeInBytes;
4438 codegen.runningLength = 0;
4439 codegen.pMaterial = pMaterial;
4442 codegen.indentationLevel = 0;
4448 for (
unsigned int iInput = 0; iInput < inputCount; ++iInput)
4451 assert(pInput !=
NULL);
4453 if (!drmtl_codegen_glsl_uniform(&codegen, pInput))
4463 drmtl_codegen_glsl_write(&codegen,
"");
4466 if (pBytesWritteOut !=
NULL)
4468 *pBytesWritteOut = codegen.runningLength + 1;
4477 #if defined(__clang__)
4478 #pragma GCC diagnostic pop
4482 #if defined(__clang__)
4483 #pragma GCC diagnostic pop