| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935 |
- /*
- * Copyright 2011-2024 Branimir Karadzic. All rights reserved.
- * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
- */
- #include "shaderc.h"
- #include <bx/commandline.h>
- #include <bx/filepath.h>
- #define MAX_TAGS 256
- extern "C"
- {
- #include <fpp.h>
- } // extern "C"
- #define BGFX_SHADER_BIN_VERSION 11
- #define BGFX_CHUNK_MAGIC_CSH BX_MAKEFOURCC('C', 'S', 'H', BGFX_SHADER_BIN_VERSION)
- #define BGFX_CHUNK_MAGIC_FSH BX_MAKEFOURCC('F', 'S', 'H', BGFX_SHADER_BIN_VERSION)
- #define BGFX_CHUNK_MAGIC_VSH BX_MAKEFOURCC('V', 'S', 'H', BGFX_SHADER_BIN_VERSION)
- #define BGFX_SHADERC_VERSION_MAJOR 1
- #define BGFX_SHADERC_VERSION_MINOR 18
- namespace bgfx
- {
- bool g_verbose = false;
- struct ShadingLang
- {
- enum Enum
- {
- ESSL,
- GLSL,
- HLSL,
- Metal,
- PSSL,
- SpirV,
- Count
- };
- };
- static const char* s_shadingLangName[] =
- {
- "OpenGL ES Shading Language / WebGL (ESSL)",
- "OpenGL Shading Language (GLSL)",
- "High-Level Shading Language (HLSL)",
- "Metal Shading Language (MSL)",
- "PlayStation Shader Language (PSSL)",
- "Standard Portable Intermediate Representation - V (SPIR-V)",
- "Unknown?!"
- };
- static_assert(BX_COUNTOF(s_shadingLangName) == ShadingLang::Count+1, "ShadingLang::Enum and s_shadingLangName mismatch");
- const char* getName(ShadingLang::Enum _lang)
- {
- return s_shadingLangName[_lang];
- }
- // c - compute
- // d - domain
- // f - fragment
- // g - geometry
- // h - hull
- // v - vertex
- //
- // OpenGL #version Features Direct3D Features Shader Model
- // 2.1 120 vf 9.0 vf 2.0
- // 3.0 130
- // 3.1 140
- // 3.2 150 vgf
- // 3.3 330 10.0 vgf 4.0
- // 4.0 400 vhdgf
- // 4.1 410
- // 4.2 420 11.0 vhdgf+c 5.0
- // 4.3 430 vhdgf+c
- // 4.4 440
- //
- // Metal Shading Language (MSL) profile naming convention:
- // metal<MSL version>-<SPIR-V version>
- //
- // See section "Compiler Options Controlling the Language Version" from the
- // MSL spec for the correlation between MSL version and platform OS version:
- // https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
- //
- // MSL version | SPIR-V version | shaderc encoding
- // 1.0 | 1.0 | 1000 (deprecated)
- // 1.1 | 1.0 | 1110
- // 1.2 | 1.0 | 1210
- // 2.0 | 1.1 | 2011
- // 2.1 | 1.1 | 2111
- // 2.2 | 1.1 | 2211
- // 2.3 | 1.4 | 2314
- // 2.4 | 1.4 | 2414
- // 3.0 | 1.4 | 3014
- // 3.1 | 1.4 | 3114
- //
- // SPIR-V profile naming convention:
- // spirv<SPIR-V version>-<Vulkan version>
- //
- // SPIR-V version | Vulkan version | shaderc encoding
- // 1.0 | 1.0 | 1010
- // 1.3 | 1.1 | 1311
- // 1.4 | 1.1 | 1411
- // 1.5 | 1.2 | 1512
- // 1.6 | 1.3 | 1613
- struct Profile
- {
- ShadingLang::Enum lang;
- uint32_t id;
- const bx::StringLiteral name;
- };
- static const Profile s_profiles[] =
- {
- { ShadingLang::ESSL, 100, "100_es" },
- { ShadingLang::ESSL, 300, "300_es" },
- { ShadingLang::ESSL, 310, "310_es" },
- { ShadingLang::ESSL, 320, "320_es" },
- { ShadingLang::HLSL, 400, "s_4_0" },
- { ShadingLang::HLSL, 500, "s_5_0" },
- { ShadingLang::Metal, 1210, "metal" },
- { ShadingLang::Metal, 1000, "metal10-10" },
- { ShadingLang::Metal, 1110, "metal11-10" },
- { ShadingLang::Metal, 1210, "metal12-10" },
- { ShadingLang::Metal, 2011, "metal20-11" },
- { ShadingLang::Metal, 2111, "metal21-11" },
- { ShadingLang::Metal, 2211, "metal22-11" },
- { ShadingLang::Metal, 2314, "metal23-14" },
- { ShadingLang::Metal, 2414, "metal24-14" },
- { ShadingLang::Metal, 3014, "metal30-14" },
- { ShadingLang::Metal, 3114, "metal31-14" },
- { ShadingLang::PSSL, 1000, "pssl" },
- { ShadingLang::SpirV, 1010, "spirv" },
- { ShadingLang::SpirV, 1010, "spirv10-10" },
- { ShadingLang::SpirV, 1311, "spirv13-11" },
- { ShadingLang::SpirV, 1411, "spirv14-11" },
- { ShadingLang::SpirV, 1512, "spirv15-12" },
- { ShadingLang::SpirV, 1613, "spirv16-13" },
- { ShadingLang::GLSL, 120, "120" },
- { ShadingLang::GLSL, 130, "130" },
- { ShadingLang::GLSL, 140, "140" },
- { ShadingLang::GLSL, 150, "150" },
- { ShadingLang::GLSL, 330, "330" },
- { ShadingLang::GLSL, 400, "400" },
- { ShadingLang::GLSL, 410, "410" },
- { ShadingLang::GLSL, 420, "420" },
- { ShadingLang::GLSL, 430, "430" },
- { ShadingLang::GLSL, 440, "440" },
- };
- static const char* s_ARB_shader_texture_lod[] =
- {
- "texture2DLod",
- "texture2DArrayLod", // BK - interacts with ARB_texture_array.
- "texture2DProjLod",
- "texture2DGrad",
- "texture2DProjGrad",
- "texture3DLod",
- "texture3DProjLod",
- "texture3DGrad",
- "texture3DProjGrad",
- "textureCubeLod",
- "textureCubeGrad",
- "shadow2DLod",
- "shadow2DProjLod",
- NULL
- // "texture1DLod",
- // "texture1DProjLod",
- // "shadow1DLod",
- // "shadow1DProjLod",
- };
- static const char* s_EXT_shader_texture_lod[] =
- {
- "texture2DLod",
- "texture2DProjLod",
- "textureCubeLod",
- "texture2DGrad",
- "texture2DProjGrad",
- "textureCubeGrad",
- NULL
- };
- static const char* s_EXT_shadow_samplers[] =
- {
- "shadow2D",
- "shadow2DProj",
- "sampler2DShadow",
- NULL
- };
- static const char* s_OES_standard_derivatives[] =
- {
- "dFdx",
- "dFdy",
- "fwidth",
- NULL
- };
- static const char* s_OES_texture_3D[] =
- {
- "texture3D",
- "texture3DProj",
- "texture3DLod",
- "texture3DProjLod",
- NULL
- };
- static const char* s_EXT_gpu_shader4[] =
- {
- "gl_VertexID",
- "gl_InstanceID",
- "texture2DLodOffset",
- NULL
- };
- // To be use from vertex program require:
- // https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_shader_viewport_layer_array.txt
- // DX11 11_1 feature level
- static const char* s_ARB_shader_viewport_layer_array[] =
- {
- "gl_ViewportIndex",
- "gl_Layer",
- NULL
- };
- static const char* s_ARB_gpu_shader5[] =
- {
- "bitfieldReverse",
- "floatBitsToInt",
- "floatBitsToUint",
- "intBitsToFloat",
- "uintBitsToFloat",
- NULL
- };
- static const char* s_ARB_shading_language_packing[] =
- {
- "packHalf2x16",
- "unpackHalf2x16",
- NULL
- };
- static const char* s_130[] =
- {
- "uint",
- "uint2",
- "uint3",
- "uint4",
- "isampler2D",
- "usampler2D",
- "isampler3D",
- "usampler3D",
- "isamplerCube",
- "usamplerCube",
- "textureSize",
- NULL
- };
- static const char* s_textureArray[] =
- {
- "sampler2DArray",
- "texture2DArray",
- "texture2DArrayLod",
- "shadow2DArray",
- NULL
- };
- static const char* s_ARB_texture_multisample[] =
- {
- "sampler2DMS",
- "isampler2DMS",
- "usampler2DMS",
- NULL
- };
- static const char* s_texelFetch[] =
- {
- "texelFetch",
- "texelFetchOffset",
- NULL
- };
- static const char* s_bitsToEncoders[] =
- {
- "floatBitsToUint",
- "floatBitsToInt",
- "intBitsToFloat",
- "uintBitsToFloat",
- NULL
- };
- static const char* s_integerVecs[] =
- {
- "ivec2",
- "uvec2",
- "ivec3",
- "uvec3",
- "ivec4",
- "uvec4",
- NULL
- };
- const char* s_uniformTypeName[] =
- {
- "int", "int",
- NULL, NULL,
- "vec4", "float4",
- "mat3", "float3x3",
- "mat4", "float4x4",
- };
- static_assert(BX_COUNTOF(s_uniformTypeName) == UniformType::Count*2);
- static const char* s_allowedVertexShaderInputs[] =
- {
- "a_position",
- "a_normal",
- "a_tangent",
- "a_bitangent",
- "a_color0",
- "a_color1",
- "a_color2",
- "a_color3",
- "a_indices",
- "a_weight",
- "a_texcoord0",
- "a_texcoord1",
- "a_texcoord2",
- "a_texcoord3",
- "a_texcoord4",
- "a_texcoord5",
- "a_texcoord6",
- "a_texcoord7",
- "i_data0",
- "i_data1",
- "i_data2",
- "i_data3",
- "i_data4",
- NULL
- };
- void fatal(const char* _filePath, uint16_t _line, Fatal::Enum _code, const char* _format, ...)
- {
- BX_UNUSED(_filePath, _line, _code);
- va_list argList;
- va_start(argList, _format);
- bx::vprintf(_format, argList);
- va_end(argList);
- abort();
- }
- void trace(const char* _filePath, uint16_t _line, const char* _format, ...)
- {
- BX_UNUSED(_filePath, _line);
- va_list argList;
- va_start(argList, _format);
- bx::vprintf(_format, argList);
- va_end(argList);
- }
- Options::Options()
- : shaderType(' ')
- , disasm(false)
- , raw(false)
- , preprocessOnly(false)
- , depends(false)
- , debugInformation(false)
- , avoidFlowControl(false)
- , noPreshader(false)
- , partialPrecision(false)
- , preferFlowControl(false)
- , backwardsCompatibility(false)
- , warningsAreErrors(false)
- , keepIntermediate(false)
- , optimize(false)
- , optimizationLevel(3)
- {
- }
- void Options::dump()
- {
- BX_TRACE("Options:\n"
- "\t shaderType: %c\n"
- "\t platform: %s\n"
- "\t profile: %s\n"
- "\t inputFile: %s\n"
- "\t outputFile: %s\n"
- "\t disasm: %s\n"
- "\t raw: %s\n"
- "\t preprocessOnly: %s\n"
- "\t depends: %s\n"
- "\t debugInformation: %s\n"
- "\t avoidFlowControl: %s\n"
- "\t noPreshader: %s\n"
- "\t partialPrecision: %s\n"
- "\t preferFlowControl: %s\n"
- "\t backwardsCompatibility: %s\n"
- "\t warningsAreErrors: %s\n"
- "\t keepIntermediate: %s\n"
- "\t optimize: %s\n"
- "\t optimizationLevel: %d\n"
- , shaderType
- , platform.c_str()
- , profile.c_str()
- , inputFilePath.c_str()
- , outputFilePath.c_str()
- , disasm ? "true" : "false"
- , raw ? "true" : "false"
- , preprocessOnly ? "true" : "false"
- , depends ? "true" : "false"
- , debugInformation ? "true" : "false"
- , avoidFlowControl ? "true" : "false"
- , noPreshader ? "true" : "false"
- , partialPrecision ? "true" : "false"
- , preferFlowControl ? "true" : "false"
- , backwardsCompatibility ? "true" : "false"
- , warningsAreErrors ? "true" : "false"
- , keepIntermediate ? "true" : "false"
- , optimize ? "true" : "false"
- , optimizationLevel
- );
- for (size_t ii = 0; ii < includeDirs.size(); ++ii)
- {
- BX_TRACE("\t include :%s\n", includeDirs[ii].c_str() );
- }
- for (size_t ii = 0; ii < defines.size(); ++ii)
- {
- BX_TRACE("\t define :%s\n", defines[ii].c_str() );
- }
- for (size_t ii = 0; ii < dependencies.size(); ++ii)
- {
- BX_TRACE("\t dependency :%s\n", dependencies[ii].c_str() );
- }
- }
- const char* interpolationDx11(const char* _glsl)
- {
- if (0 == bx::strCmp(_glsl, "smooth") )
- {
- return "linear";
- }
- else if (0 == bx::strCmp(_glsl, "flat") )
- {
- return "nointerpolation";
- }
- return _glsl; // centroid, noperspective
- }
- const char* getUniformTypeName(UniformType::Enum _enum)
- {
- uint32_t idx = _enum & ~(kUniformFragmentBit|kUniformSamplerBit);
- if (idx < UniformType::Count)
- {
- return s_uniformTypeName[idx];
- }
- return "Unknown uniform type?!";
- }
- UniformType::Enum nameToUniformTypeEnum(const char* _name)
- {
- for (uint32_t ii = 0; ii < UniformType::Count*2; ++ii)
- {
- if (NULL != s_uniformTypeName[ii]
- && 0 == bx::strCmp(_name, s_uniformTypeName[ii]) )
- {
- return UniformType::Enum(ii/2);
- }
- }
- return UniformType::Count;
- }
- int32_t writef(bx::WriterI* _writer, const char* _format, ...)
- {
- va_list argList;
- va_start(argList, _format);
- char temp[2048];
- char* out = temp;
- int32_t max = sizeof(temp);
- int32_t len = bx::vsnprintf(out, max, _format, argList);
- if (len > max)
- {
- out = (char*)alloca(len);
- len = bx::vsnprintf(out, len, _format, argList);
- }
- len = bx::write(_writer, out, len, bx::ErrorAssert{});
- va_end(argList);
- return len;
- }
- class Bin2cWriter : public bx::FileWriter
- {
- public:
- Bin2cWriter(const bx::StringView& _name)
- : m_name(_name)
- {
- }
- virtual ~Bin2cWriter()
- {
- }
- virtual void close() override
- {
- generate();
- return bx::FileWriter::close();
- }
- virtual int32_t write(const void* _data, int32_t _size, bx::Error*) override
- {
- const char* data = (const char*)_data;
- m_buffer.insert(m_buffer.end(), data, data+_size);
- return _size;
- }
- private:
- void generate()
- {
- #define HEX_DUMP_WIDTH 16
- #define HEX_DUMP_SPACE_WIDTH 96
- #define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
- const uint8_t* data = &m_buffer[0];
- uint32_t size = (uint32_t)m_buffer.size();
- outf("static const uint8_t %.*s[%d] =\n{\n", m_name.getLength(), m_name.getPtr(), size);
- if (NULL != data)
- {
- char hex[HEX_DUMP_SPACE_WIDTH+1];
- char ascii[HEX_DUMP_WIDTH+1];
- uint32_t hexPos = 0;
- uint32_t asciiPos = 0;
- for (uint32_t ii = 0; ii < size; ++ii)
- {
- bx::snprintf(&hex[hexPos], sizeof(hex)-hexPos, "0x%02x, ", data[asciiPos]);
- hexPos += 6;
- ascii[asciiPos] = isprint(data[asciiPos]) && data[asciiPos] != '\\' && data[asciiPos] != '\t' ? data[asciiPos] : '.';
- asciiPos++;
- if (HEX_DUMP_WIDTH == asciiPos)
- {
- ascii[asciiPos] = '\0';
- outf("\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
- data += asciiPos;
- hexPos = 0;
- asciiPos = 0;
- }
- }
- if (0 != asciiPos)
- {
- ascii[asciiPos] = '\0';
- outf("\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
- }
- }
- outf("};\n");
- #undef HEX_DUMP_WIDTH
- #undef HEX_DUMP_SPACE_WIDTH
- #undef HEX_DUMP_FORMAT
- }
- int32_t outf(const char* _format, ...)
- {
- va_list argList;
- va_start(argList, _format);
- char temp[2048];
- char* out = temp;
- int32_t max = sizeof(temp);
- int32_t len = bx::vsnprintf(out, max, _format, argList);
- if (len > max)
- {
- out = (char*)alloca(len);
- len = bx::vsnprintf(out, len, _format, argList);
- }
- int32_t size = bx::FileWriter::write(out, len, bx::ErrorAssert{});
- va_end(argList);
- return size;
- }
- bx::StringView m_name;
- typedef std::vector<uint8_t> Buffer;
- Buffer m_buffer;
- };
- struct Varying
- {
- std::string m_precision;
- std::string m_interpolation;
- std::string m_name;
- std::string m_type;
- std::string m_init;
- std::string m_semantics;
- };
- typedef std::unordered_map<std::string, Varying> VaryingMap;
- class File
- {
- public:
- File()
- : m_data(NULL)
- , m_size(0)
- {
- }
- ~File()
- {
- delete [] m_data;
- }
- void load(const bx::FilePath& _filePath)
- {
- bx::FileReader reader;
- if (bx::open(&reader, _filePath) )
- {
- m_size = (uint32_t)bx::getSize(&reader);
- m_data = new char[m_size+1];
- m_size = (uint32_t)bx::read(&reader, m_data, m_size, bx::ErrorAssert{});
- bx::close(&reader);
- if (m_data[0] == '\xef'
- && m_data[1] == '\xbb'
- && m_data[2] == '\xbf')
- {
- bx::memMove(m_data, &m_data[3], m_size-3);
- m_size -= 3;
- }
- m_data[m_size] = '\0';
- }
- }
- const char* getData() const
- {
- return m_data;
- }
- uint32_t getSize() const
- {
- return m_size;
- }
- private:
- char* m_data;
- uint32_t m_size;
- };
- char* strInsert(char* _str, const char* _insert)
- {
- uint32_t len = bx::strLen(_insert);
- bx::memMove(&_str[len], _str, bx::strLen(_str) );
- bx::memCopy(_str, _insert, len);
- return _str + len;
- }
- void strReplace(char* _str, const char* _find, const char* _replace)
- {
- const int32_t len = bx::strLen(_find);
- char* replace = (char*)alloca(len+1);
- bx::strCopy(replace, len+1, _replace);
- for (int32_t ii = bx::strLen(replace); ii < len; ++ii)
- {
- replace[ii] = ' ';
- }
- replace[len] = '\0';
- BX_ASSERT(len >= bx::strLen(_replace), "");
- for (bx::StringView ptr = bx::strFind(_str, _find)
- ; !ptr.isEmpty()
- ; ptr = bx::strFind(ptr.getPtr() + len, _find)
- )
- {
- bx::memCopy(const_cast<char*>(ptr.getPtr() ), replace, len);
- }
- }
- void strNormalizeEol(char* _str)
- {
- strReplace(_str, "\r\n", "\n");
- strReplace(_str, "\r", "\n");
- }
- void printCode(const char* _code, int32_t _line, int32_t _start, int32_t _end, int32_t _column)
- {
- bx::printf("Code:\n---\n");
- bx::LineReader reader(_code);
- for (int32_t line = 1; !reader.isDone() && line < _end; ++line)
- {
- bx::StringView strLine = reader.next();
- if (line >= _start)
- {
- if (_line == line)
- {
- bx::printf("\n");
- bx::printf(">>> %3d: %.*s\n", line, strLine.getLength(), strLine.getPtr() );
- if (-1 != _column)
- {
- bx::printf(">>> %3d: %*s\n", _column, _column, "^");
- }
- bx::printf("\n");
- }
- else
- {
- bx::printf(" %3d: %.*s\n", line, strLine.getLength(), strLine.getPtr() );
- }
- }
- }
- bx::printf("---\n");
- }
- void writeFile(const char* _filePath, const void* _data, int32_t _size)
- {
- bx::FileWriter out;
- if (bx::open(&out, _filePath) )
- {
- bx::write(&out, _data, _size, bx::ErrorAssert{});
- bx::close(&out);
- }
- }
- struct Preprocessor
- {
- Preprocessor(const char* _filePath, bool _essl, bx::WriterI* _messageWriter)
- : m_tagptr(m_tags)
- , m_scratchPos(0)
- , m_fgetsPos(0)
- , m_messageWriter(_messageWriter)
- {
- m_tagptr->tag = FPPTAG_USERDATA;
- m_tagptr->data = this;
- m_tagptr++;
- m_tagptr->tag = FPPTAG_DEPENDS;
- m_tagptr->data = (void*)fppDepends;
- m_tagptr++;
- m_tagptr->tag = FPPTAG_INPUT;
- m_tagptr->data = (void*)fppInput;
- m_tagptr++;
- m_tagptr->tag = FPPTAG_OUTPUT;
- m_tagptr->data = (void*)fppOutput;
- m_tagptr++;
- m_tagptr->tag = FPPTAG_ERROR;
- m_tagptr->data = (void*)fppError;
- m_tagptr++;
- m_tagptr->tag = FPPTAG_SHOWVERSION;
- m_tagptr->data = (void*)0;
- m_tagptr++;
- m_tagptr->tag = FPPTAG_LINE;
- m_tagptr->data = (void*)0;
- m_tagptr++;
- m_tagptr->tag = FPPTAG_INPUT_NAME;
- m_tagptr->data = scratch(_filePath);
- m_tagptr++;
- if (!_essl)
- {
- m_default = "#define lowp\n#define mediump\n#define highp\n";
- }
- }
- void setDefine(const char* _define)
- {
- m_tagptr->tag = FPPTAG_DEFINE;
- m_tagptr->data = scratch(_define);
- m_tagptr++;
- }
- void setDefaultDefine(const char* _name)
- {
- char temp[1024];
- bx::snprintf(temp, BX_COUNTOF(temp)
- , "#ifndef %s\n"
- "# define %s 0\n"
- "#endif // %s\n"
- "\n"
- , _name
- , _name
- , _name
- );
- m_default += temp;
- }
- void writef(const char* _format, ...)
- {
- va_list argList;
- va_start(argList, _format);
- bx::stringPrintfVargs(m_default, _format, argList);
- va_end(argList);
- }
- void addInclude(const char* _includeDir)
- {
- char* start = scratch(_includeDir);
- for (bx::StringView split = bx::strFind(start, ';')
- ; !split.isEmpty()
- ; split = bx::strFind(start, ';')
- )
- {
- *const_cast<char*>(split.getPtr() ) = '\0';
- m_tagptr->tag = FPPTAG_INCLUDE_DIR;
- m_tagptr->data = start;
- m_tagptr++;
- start = const_cast<char*>(split.getPtr() ) + 1;
- }
- m_tagptr->tag = FPPTAG_INCLUDE_DIR;
- m_tagptr->data = start;
- m_tagptr++;
- }
- void addDependency(const char* _fileName)
- {
- m_depends += " \\\n ";
- m_depends += _fileName;
- }
- bool run(const char* _input)
- {
- m_fgetsPos = 0;
- m_preprocessed.clear();
- m_input = m_default;
- m_input += "\n\n";
- int32_t len = bx::strLen(_input)+1;
- char* temp = new char[len];
- bx::StringView normalized = bx::normalizeEolLf(temp, len, _input);
- std::string str;
- str.assign(normalized.getPtr(), normalized.getTerm() );
- m_input += str;
- delete [] temp;
- fppTag* tagptr = m_tagptr;
- tagptr->tag = FPPTAG_END;
- tagptr->data = 0;
- tagptr++;
- int result = fppPreProcess(m_tags);
- return 0 == result;
- }
- char* fgets(char* _buffer, int _size)
- {
- int ii = 0;
- for (char ch = m_input[m_fgetsPos]; m_fgetsPos < m_input.size() && ii < _size-1; ch = m_input[++m_fgetsPos])
- {
- _buffer[ii++] = ch;
- if (ch == '\n' || ii == _size)
- {
- _buffer[ii] = '\0';
- m_fgetsPos++;
- return _buffer;
- }
- }
- return NULL;
- }
- static void fppDepends(char* _fileName, void* _userData)
- {
- Preprocessor* thisClass = (Preprocessor*)_userData;
- thisClass->addDependency(_fileName);
- }
- static char* fppInput(char* _buffer, int _size, void* _userData)
- {
- Preprocessor* thisClass = (Preprocessor*)_userData;
- return thisClass->fgets(_buffer, _size);
- }
- static void fppOutput(int _ch, void* _userData)
- {
- Preprocessor* thisClass = (Preprocessor*)_userData;
- thisClass->m_preprocessed += char(_ch);
- }
- static void fppError(void* _userData, char* _format, va_list _vargs)
- {
- bx::ErrorAssert err;
- Preprocessor* thisClass = (Preprocessor*)_userData;
- bx::write(thisClass->m_messageWriter, _format, _vargs, &err);
- }
- char* scratch(const char* _str)
- {
- char* result = &m_scratch[m_scratchPos];
- bx::strCopy(result, uint32_t(sizeof(m_scratch)-m_scratchPos), _str);
- m_scratchPos += (uint32_t)bx::strLen(_str)+1;
- return result;
- }
- fppTag m_tags[MAX_TAGS];
- fppTag* m_tagptr;
- std::string m_depends;
- std::string m_default;
- std::string m_input;
- std::string m_preprocessed;
- char m_scratch[16<<10];
- uint32_t m_scratchPos;
- uint32_t m_fgetsPos;
- bx::WriterI* m_messageWriter;
- };
- typedef std::vector<std::string> InOut;
- uint32_t parseInOut(InOut& _inout, const bx::StringView& _str)
- {
- uint32_t hash = 0;
- bx::StringView str = bx::strLTrimSpace(_str);
- if (!str.isEmpty() )
- {
- bx::StringView delim;
- do
- {
- delim = bx::strFind(str, ',');
- if (delim.isEmpty() )
- {
- delim = bx::strFind(str, ' ');
- }
- const bx::StringView token(bx::strRTrim(bx::StringView(str.getPtr(), delim.getPtr() ), " ") );
- if (!token.isEmpty() )
- {
- _inout.push_back(std::string(token.getPtr(), token.getTerm() ) );
- str = bx::strLTrimSpace(bx::StringView(delim.getPtr() + 1, str.getTerm() ) );
- }
- }
- while (!delim.isEmpty() );
- std::sort(_inout.begin(), _inout.end() );
- bx::HashMurmur2A murmur;
- murmur.begin();
- for (InOut::const_iterator it = _inout.begin(), itEnd = _inout.end(); it != itEnd; ++it)
- {
- murmur.add(it->c_str(), (uint32_t)it->size() );
- }
- hash = murmur.end();
- }
- return hash;
- }
- void addFragData(Preprocessor& _preprocessor, char* _data, uint32_t _idx, bool _comma)
- {
- char find[32];
- bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
- char replace[32];
- bx::snprintf(replace, sizeof(replace), "bgfx_FragData%d", _idx);
- strReplace(_data, find, replace);
- _preprocessor.writef(
- " \\\n\t%sout vec4 bgfx_FragData%d : SV_TARGET%d"
- , _comma ? ", " : " "
- , _idx
- , _idx
- );
- }
- void voidFragData(char* _data, uint32_t _idx)
- {
- char find[32];
- bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
- strReplace(_data, find, "bgfx_VoidFrag");
- }
- bx::StringView baseName(const bx::StringView& _filePath)
- {
- bx::FilePath fp(_filePath);
- return bx::strFind(_filePath, fp.getBaseName() );
- }
- void help(const char* _error = NULL)
- {
- if (NULL != _error)
- {
- bx::printf("Error:\n%s\n\n", _error);
- }
- bx::printf(
- "shaderc, bgfx shader compiler tool, version %d.%d.%d.\n"
- "Copyright 2011-2024 Branimir Karadzic. All rights reserved.\n"
- "License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE\n\n"
- , BGFX_SHADERC_VERSION_MAJOR
- , BGFX_SHADERC_VERSION_MINOR
- , BGFX_API_VERSION
- );
- bx::printf(
- "Usage: shaderc -f <in> -o <out> --type <v/f/c> --platform <platform>\n"
- "\n"
- "Options:\n"
- " -h, --help Display this help and exit.\n"
- " -v, --version Output version information and exit.\n"
- " -f <file path> Input's file path.\n"
- " -i <include path> Include path. (for multiple paths use -i multiple times)\n"
- " -o <file path> Output's file path.\n"
- " --stdout Output to console.\n"
- " --bin2c [array name] Generate C header file. If array name is not specified base file name will be used as name.\n"
- " --depends Generate makefile style depends file.\n"
- " --platform <platform> Target platform.\n"
- " android\n"
- " asm.js\n"
- " ios\n"
- " linux\n"
- " orbis\n"
- " osx\n"
- " windows\n"
- " -p, --profile <profile> Shader model. Defaults to GLSL.\n"
- );
- {
- ShadingLang::Enum lang = ShadingLang::Count;
- for (uint32_t ii = 0; ii < BX_COUNTOF(s_profiles); ++ii)
- {
- const Profile& profile = s_profiles[ii];
- if (lang != profile.lang)
- {
- lang = profile.lang;
- bx::printf("\n");
- bx::printf(" %-20S %s\n", &profile.name, getName(profile.lang) );
- }
- else
- {
- bx::printf(" %S\n", &profile.name);
- }
- }
- }
- bx::printf(
- " --preprocess Only pre-process.\n"
- " --define <defines> Add defines to preprocessor. (Semicolon-separated)\n"
- " --raw Do not process shader. No preprocessor, and no glsl-optimizer. (GLSL only)\n"
- " --type <type> Shader type. Can be 'vertex', 'fragment, or 'compute'.\n"
- " --varyingdef <file path> varying.def.sc's file path.\n"
- " --verbose Be verbose.\n"
- "\n"
- "(Vulkan, DirectX and Metal):\n"
- "\n"
- " --debug Debug information.\n"
- "\n"
- "(DirectX only):\n"
- "\n"
- " --disasm Disassemble compiled shader.\n"
- " -O <level> Set optimization level. Can be 0 to 3.\n"
- " --Werror Treat warnings as errors.\n"
- "\n"
- "For additional information, see https://github.com/bkaradzic/bgfx\n"
- );
- }
- bx::StringView nextWord(bx::StringView& _parse)
- {
- bx::StringView word = bx::strWord(bx::strLTrimSpace(_parse) );
- _parse = bx::strLTrimSpace(bx::StringView(word.getTerm(), _parse.getTerm() ) );
- return word;
- }
- bool compileShader(const char* _varying, const char* _comment, char* _shader, uint32_t _shaderLen, const Options& _options, bx::WriterI* _shaderWriter, bx::WriterI* _messageWriter)
- {
- bx::ErrorAssert messageErr;
- uint32_t profileId = 0;
- const bx::StringView profileOpt(_options.profile.c_str() );
- if (!profileOpt.isEmpty() )
- {
- const uint32_t count = BX_COUNTOF(s_profiles);
- for (profileId = 0; profileId < count; ++profileId)
- {
- if (0 == bx::strCmp(profileOpt, s_profiles[profileId].name) )
- {
- break;
- }
- }
- if (profileId == count)
- {
- bx::write(_messageWriter, &messageErr, "Unknown profile: %S\n", &profileOpt);
- return false;
- }
- }
- else
- {
- bx::write(_messageWriter, &messageErr, "Shader profile must be specified.\n");
- return false;
- }
- const Profile* profile = &s_profiles[profileId];
- Preprocessor preprocessor(_options.inputFilePath.c_str(), profile->lang == ShadingLang::ESSL, _messageWriter);
- for (size_t ii = 0; ii < _options.includeDirs.size(); ++ii)
- {
- preprocessor.addInclude(_options.includeDirs[ii].c_str() );
- }
- for (size_t ii = 0; ii < _options.defines.size(); ++ii)
- {
- preprocessor.setDefine(_options.defines[ii].c_str() );
- }
- for (size_t ii = 0; ii < _options.dependencies.size(); ++ii)
- {
- preprocessor.addDependency(_options.dependencies[ii].c_str() );
- }
- preprocessor.setDefaultDefine("BX_PLATFORM_ANDROID");
- preprocessor.setDefaultDefine("BX_PLATFORM_EMSCRIPTEN");
- preprocessor.setDefaultDefine("BX_PLATFORM_IOS");
- preprocessor.setDefaultDefine("BX_PLATFORM_VISIONOS");
- preprocessor.setDefaultDefine("BX_PLATFORM_LINUX");
- preprocessor.setDefaultDefine("BX_PLATFORM_OSX");
- preprocessor.setDefaultDefine("BX_PLATFORM_PS4");
- preprocessor.setDefaultDefine("BX_PLATFORM_WINDOWS");
- preprocessor.setDefaultDefine("BX_PLATFORM_XBOXONE");
- preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL");
- preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL");
- preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_METAL");
- preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_PSSL");
- preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_SPIRV");
- preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_COMPUTE");
- preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_FRAGMENT");
- preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_VERTEX");
- char glslDefine[128];
- if (profile->lang == ShadingLang::GLSL
- || profile->lang == ShadingLang::ESSL)
- {
- bx::snprintf(glslDefine, BX_COUNTOF(glslDefine)
- , "BGFX_SHADER_LANGUAGE_GLSL=%d"
- , profile->id
- );
- }
- char hlslDefine[128];
- if (profile->lang == ShadingLang::HLSL)
- {
- bx::snprintf(hlslDefine, BX_COUNTOF(hlslDefine)
- , "BGFX_SHADER_LANGUAGE_HLSL=%d"
- , profile->id);
- }
- const char* platform = _options.platform.c_str();
- if (0 == bx::strCmpI(platform, "android") )
- {
- preprocessor.setDefine("BX_PLATFORM_ANDROID=1");
- if (profile->lang == ShadingLang::SpirV)
- {
- preprocessor.setDefine("BGFX_SHADER_LANGUAGE_SPIRV=1");
- }
- else
- {
- preprocessor.setDefine(glslDefine);
- }
- }
- else if (0 == bx::strCmpI(platform, "asm.js") )
- {
- preprocessor.setDefine("BX_PLATFORM_EMSCRIPTEN=1");
- preprocessor.setDefine(glslDefine);
- }
- else if (0 == bx::strCmpI(platform, "linux") )
- {
- preprocessor.setDefine("BX_PLATFORM_LINUX=1");
- if (profile->lang == ShadingLang::SpirV)
- {
- preprocessor.setDefine("BGFX_SHADER_LANGUAGE_SPIRV=1");
- }
- else
- {
- preprocessor.setDefine(glslDefine);
- }
- }
- else if (
- 0 == bx::strCmpI(platform, "ios") ||
- 0 == bx::strCmpI(platform, "osx") ||
- 0 == bx::strCmpI(platform, "visionos")
- )
- {
- if (0 == bx::strCmpI(platform, "osx"))
- {
- preprocessor.setDefine("BX_PLATFORM_OSX=1");
- }
- else if (0 == bx::strCmpI(platform, "visionos")) {
- preprocessor.setDefine("BX_PLATFORM_VISIONOS=1");
- }
- else
- {
- preprocessor.setDefine("BX_PLATFORM_IOS=1");
- }
- if (profile->lang != ShadingLang::Metal)
- {
- preprocessor.setDefine(glslDefine);
- }
- char temp[32];
- bx::snprintf(
- temp
- , sizeof(temp)
- , "BGFX_SHADER_LANGUAGE_METAL=%d"
- , (profile->lang == ShadingLang::Metal) ? profile->id : 0
- );
- preprocessor.setDefine(temp);
- }
- else if (0 == bx::strCmpI(platform, "windows") )
- {
- preprocessor.setDefine("BX_PLATFORM_WINDOWS=1");
- if (profile->lang == ShadingLang::HLSL)
- {
- preprocessor.setDefine(hlslDefine);
- }
- else if (profile->lang == ShadingLang::GLSL
- || profile->lang == ShadingLang::ESSL)
- {
- preprocessor.setDefine(glslDefine);
- }
- else if (profile->lang == ShadingLang::SpirV)
- {
- preprocessor.setDefine("BGFX_SHADER_LANGUAGE_SPIRV=1");
- }
- }
- else if (0 == bx::strCmpI(platform, "orbis") )
- {
- preprocessor.setDefine("BX_PLATFORM_PS4=1");
- preprocessor.setDefine("BGFX_SHADER_LANGUAGE_PSSL=1");
- preprocessor.setDefine("lit=lit_reserved");
- }
- else
- {
- if (profile->lang == ShadingLang::HLSL)
- {
- preprocessor.setDefine(hlslDefine);
- }
- else if (profile->lang == ShadingLang::GLSL
- || profile->lang == ShadingLang::ESSL)
- {
- preprocessor.setDefine(glslDefine);
- }
- else if (profile->lang == ShadingLang::SpirV)
- {
- preprocessor.setDefine("BGFX_SHADER_LANGUAGE_SPIRV=1");
- }
- }
- preprocessor.setDefine("M_PI=3.1415926535897932384626433832795");
- switch (_options.shaderType)
- {
- case 'c':
- preprocessor.setDefine("BGFX_SHADER_TYPE_COMPUTE=1");
- break;
- case 'f':
- preprocessor.setDefine("BGFX_SHADER_TYPE_FRAGMENT=1");
- break;
- case 'v':
- preprocessor.setDefine("BGFX_SHADER_TYPE_VERTEX=1");
- break;
- default:
- bx::write(_messageWriter, &messageErr, "Unknown type: %c?!", _options.shaderType);
- return false;
- }
- bool compiled = false;
- VaryingMap varyingMap;
- bx::StringView parse(_varying);
- bx::StringView term(parse);
- bool usesInterpolationQualifiers = false;
- while (!parse.isEmpty() )
- {
- parse = bx::strLTrimSpace(parse);
- bx::StringView eol = bx::strFind(parse, ';');
- if (eol.isEmpty() )
- {
- eol = bx::strFindEol(parse);
- }
- if (!eol.isEmpty() )
- {
- eol.set(eol.getPtr() + 1, parse.getTerm() );
- bx::StringView precision;
- bx::StringView interpolation;
- bx::StringView typen = nextWord(parse);
- if (0 == bx::strCmp(typen, "lowp", 4)
- || 0 == bx::strCmp(typen, "mediump", 7)
- || 0 == bx::strCmp(typen, "highp", 5) )
- {
- precision = typen;
- typen = nextWord(parse);
- }
- if (0 == bx::strCmp(typen, "flat", 4)
- || 0 == bx::strCmp(typen, "smooth", 6)
- || 0 == bx::strCmp(typen, "noperspective", 13)
- || 0 == bx::strCmp(typen, "centroid", 8) )
- {
- if ('f' == _options.shaderType
- || profile->lang == ShadingLang::GLSL
- || profile->lang == ShadingLang::ESSL)
- {
- interpolation = typen;
- usesInterpolationQualifiers = true;
- }
- typen = nextWord(parse);
- }
- bx::StringView name = nextWord(parse);
- bx::StringView column = bx::strSubstr(parse, 0, 1);
- bx::StringView semantics;
- if (0 == bx::strCmp(column, ":", 1) )
- {
- parse = bx::strLTrimSpace(bx::StringView(parse.getPtr() + 1, parse.getTerm() ) );
- semantics = nextWord(parse);
- }
- bx::StringView assign = bx::strSubstr(parse, 0, 1);
- bx::StringView init;
- if (0 == bx::strCmp(assign, "=", 1) )
- {
- parse = bx::strLTrimSpace(bx::StringView(parse.getPtr() + 1, parse.getTerm() ) );
- init.set(parse.getPtr(), eol.getPtr() );
- }
- if (!typen.isEmpty()
- && !name.isEmpty()
- && !semantics.isEmpty() )
- {
- Varying var;
- if (!precision.isEmpty() )
- {
- var.m_precision.assign(precision.getPtr(), precision.getTerm() );
- }
- if (!interpolation.isEmpty() )
- {
- var.m_interpolation.assign(interpolation.getPtr(), interpolation.getTerm() );
- }
- var.m_type.assign(typen.getPtr(), typen.getTerm() );
- var.m_name.assign(name.getPtr(), name.getTerm() );
- var.m_semantics.assign(semantics.getPtr(), semantics.getTerm() );
- if (profile->lang == ShadingLang::HLSL
- && profile->id < 400
- && var.m_semantics == "BITANGENT")
- {
- var.m_semantics = "BINORMAL";
- }
- if (!init.isEmpty() )
- {
- var.m_init.assign(init.getPtr(), init.getTerm() );
- }
- varyingMap.insert(std::make_pair(var.m_name, var) );
- }
- parse = bx::strLTrimSpace(bx::strFindNl(bx::StringView(eol.getPtr(), term.getTerm() ) ) );
- }
- }
- bool raw = _options.raw;
- InOut shaderInputs;
- InOut shaderOutputs;
- uint32_t inputHash = 0;
- uint32_t outputHash = 0;
- bx::ErrorAssert err;
- char* data;
- char* input;
- {
- data = _shader;
- uint32_t size = _shaderLen;
- const size_t padding = 16384;
- if (!raw)
- {
- // To avoid commented code being recognized as used feature,
- // first preprocess pass is used to strip all comments before
- // substituting code.
- bool ok = preprocessor.run(data);
- delete [] data;
- if (!ok)
- {
- return false;
- }
- size = (uint32_t)preprocessor.m_preprocessed.size();
- data = new char[size+padding+1];
- bx::memCopy(data, preprocessor.m_preprocessed.c_str(), size);
- bx::memSet(&data[size], 0, padding+1);
- }
- strNormalizeEol(data);
- input = const_cast<char*>(bx::strLTrimSpace(data).getPtr() );
- while (input[0] == '$')
- {
- bx::StringView str = bx::strLTrimSpace(input+1);
- bx::StringView eol = bx::strFindEol(str);
- bx::StringView nl = bx::strFindNl(eol);
- input = const_cast<char*>(nl.getPtr() );
- if (0 == bx::strCmp(str, "input", 5) )
- {
- str = bx::StringView(str.getPtr() + 5, str.getTerm() );
- bx::StringView comment = bx::strFind(str, "//");
- eol = !comment.isEmpty() && comment.getPtr() < eol.getPtr() ? comment.getPtr() : eol;
- inputHash = parseInOut(shaderInputs, bx::StringView(str.getPtr(), eol.getPtr() ) );
- }
- else if (0 == bx::strCmp(str, "output", 6) )
- {
- str = bx::StringView(str.getPtr() + 6, str.getTerm() );
- bx::StringView comment = bx::strFind(str, "//");
- eol = !comment.isEmpty() && comment.getPtr() < eol.getPtr() ? comment.getPtr() : eol;
- outputHash = parseInOut(shaderOutputs, bx::StringView(str.getPtr(), eol.getPtr() ) );
- }
- else if (0 == bx::strCmp(str, "raw", 3) )
- {
- raw = true;
- str = bx::StringView(str.getPtr() + 3, str.getTerm() );
- }
- input = const_cast<char*>(bx::strLTrimSpace(input).getPtr() );
- }
- }
- bool invalidShaderAttribute = false;
- if ('v' == _options.shaderType)
- {
- for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
- {
- if (bx::findIdentifierMatch(it->c_str(), s_allowedVertexShaderInputs).isEmpty() )
- {
- invalidShaderAttribute = true;
- bx::write(_messageWriter, &messageErr,
- "Invalid vertex shader input attribute '%s'.\n"
- "\n"
- "Valid input attributes:\n"
- " a_position, a_normal, a_tangent, a_bitangent, a_color0, a_color1, a_color2, a_color3, a_indices, a_weight,\n"
- " a_texcoord0, a_texcoord1, a_texcoord2, a_texcoord3, a_texcoord4, a_texcoord5, a_texcoord6, a_texcoord7,\n"
- " i_data0, i_data1, i_data2, i_data3, i_data4.\n"
- "\n"
- , it->c_str() );
- break;
- }
- }
- }
- if (invalidShaderAttribute)
- {
- }
- else if (raw)
- {
- if ('f' == _options.shaderType)
- {
- bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_FSH, &err);
- }
- else if ('v' == _options.shaderType)
- {
- bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_VSH, &err);
- }
- else
- {
- bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_CSH, &err);
- }
- bx::write(_shaderWriter, inputHash, &err);
- bx::write(_shaderWriter, outputHash, &err);
- }
- if (raw)
- {
- if (profile->lang == ShadingLang::GLSL)
- {
- bx::write(_shaderWriter, uint16_t(0), &err);
- const uint32_t shaderSize = (uint32_t)bx::strLen(input);
- bx::write(_shaderWriter, shaderSize, &err);
- bx::write(_shaderWriter, input, shaderSize, &err);
- bx::write(_shaderWriter, uint8_t(0), &err);
- compiled = true;
- }
- else if (profile->lang == ShadingLang::Metal)
- {
- compiled = compileMetalShader(_options, profile->id, input, _shaderWriter, _messageWriter);
- }
- else if (profile->lang == ShadingLang::SpirV)
- {
- compiled = compileSPIRVShader(_options, profile->id, input, _shaderWriter, _messageWriter);
- }
- else if (profile->lang == ShadingLang::PSSL)
- {
- compiled = compilePSSLShader(_options, 0, input, _shaderWriter, _messageWriter);
- }
- else
- {
- compiled = compileHLSLShader(_options, profile->id, input, _shaderWriter, _messageWriter);
- }
- }
- else if ('c' == _options.shaderType) // Compute
- {
- bx::StringView entry = bx::strFind(input, "void main()");
- if (entry.isEmpty() )
- {
- bx::write(_messageWriter, &messageErr, "Shader entry point 'void main()' is not found.\n");
- }
- else
- {
- if (profile->lang == ShadingLang::GLSL
- || profile->lang == ShadingLang::ESSL)
- {
- }
- else
- {
- if (profile->lang == ShadingLang::PSSL)
- {
- preprocessor.writef(getPsslPreamble() );
- }
- preprocessor.writef(
- "#define lowp\n"
- "#define mediump\n"
- "#define highp\n"
- "#define ivec2 int2\n"
- "#define ivec3 int3\n"
- "#define ivec4 int4\n"
- "#define uvec2 uint2\n"
- "#define uvec3 uint3\n"
- "#define uvec4 uint4\n"
- "#define vec2 float2\n"
- "#define vec3 float3\n"
- "#define vec4 float4\n"
- "#define mat2 float2x2\n"
- "#define mat3 float3x3\n"
- "#define mat4 float4x4\n"
- );
- *const_cast<char*>(entry.getPtr() + 4) = '_';
- preprocessor.writef("#define void_main()");
- preprocessor.writef(" \\\n\tvoid main(");
- uint32_t arg = 0;
- const bool hasLocalInvocationID = !bx::strFind(input, "gl_LocalInvocationID").isEmpty();
- const bool hasLocalInvocationIndex = !bx::strFind(input, "gl_LocalInvocationIndex").isEmpty();
- const bool hasGlobalInvocationID = !bx::strFind(input, "gl_GlobalInvocationID").isEmpty();
- const bool hasWorkGroupID = !bx::strFind(input, "gl_WorkGroupID").isEmpty();
- if (hasLocalInvocationID)
- {
- preprocessor.writef(
- " \\\n\t%sint3 gl_LocalInvocationID : SV_GroupThreadID"
- , arg++ > 0 ? ", " : " "
- );
- }
- if (hasLocalInvocationIndex)
- {
- preprocessor.writef(
- " \\\n\t%sint gl_LocalInvocationIndex : SV_GroupIndex"
- , arg++ > 0 ? ", " : " "
- );
- }
- if (hasGlobalInvocationID)
- {
- preprocessor.writef(
- " \\\n\t%sint3 gl_GlobalInvocationID : SV_DispatchThreadID"
- , arg++ > 0 ? ", " : " "
- );
- }
- if (hasWorkGroupID)
- {
- preprocessor.writef(
- " \\\n\t%sint3 gl_WorkGroupID : SV_GroupID"
- , arg++ > 0 ? ", " : " "
- );
- }
- preprocessor.writef(
- " \\\n\t)\n"
- );
- }
- if (preprocessor.run(input) )
- {
- if (_options.preprocessOnly)
- {
- bx::write(
- _shaderWriter
- , preprocessor.m_preprocessed.c_str()
- , (int32_t)preprocessor.m_preprocessed.size()
- , &err
- );
- return true;
- }
- {
- std::string code;
- bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_CSH, &err);
- bx::write(_shaderWriter, uint32_t(0), &err);
- bx::write(_shaderWriter, outputHash, &err);
- if (profile->lang == ShadingLang::GLSL
- || profile->lang == ShadingLang::ESSL)
- {
- if (profile->lang == ShadingLang::ESSL)
- {
- bx::stringPrintf(code, "#version 310 es\n");
- }
- else
- {
- bx::stringPrintf(
- code
- , "#version %d\n"
- , (profile->lang != ShadingLang::GLSL) ? 430 : profile->id
- );
- }
- code += preprocessor.m_preprocessed;
- bx::write(_shaderWriter, uint16_t(0), &err);
- uint32_t shaderSize = (uint32_t)code.size();
- bx::write(_shaderWriter, shaderSize, &err);
- bx::write(_shaderWriter, code.c_str(), shaderSize, &err);
- bx::write(_shaderWriter, uint8_t(0), &err);
- compiled = true;
- }
- else
- {
- code += _comment;
- code += preprocessor.m_preprocessed;
- if (profile->lang == ShadingLang::Metal)
- {
- compiled = compileMetalShader(_options, profile->id, code, _shaderWriter, _messageWriter);
- }
- else if (profile->lang == ShadingLang::SpirV)
- {
- compiled = compileSPIRVShader(_options, profile->id, code, _shaderWriter, _messageWriter);
- }
- else if (profile->lang == ShadingLang::PSSL)
- {
- compiled = compilePSSLShader(_options, 0, code, _shaderWriter, _messageWriter);
- }
- else
- {
- compiled = compileHLSLShader(_options, profile->id, code, _shaderWriter, _messageWriter);
- }
- }
- }
- if (compiled)
- {
- if (_options.depends)
- {
- std::string ofp = _options.outputFilePath;
- ofp += ".d";
- bx::FileWriter writer;
- if (bx::open(&writer, ofp.c_str() ) )
- {
- writef(&writer, "%s : %s\n", _options.outputFilePath.c_str(), preprocessor.m_depends.c_str() );
- bx::close(&writer);
- }
- }
- }
- }
- }
- }
- else // Vertex/Fragment
- {
- bx::StringView shader(input);
- bx::StringView entry = bx::strFind(shader, "void main()");
- if (entry.isEmpty() )
- {
- bx::write(_messageWriter, &messageErr, "Shader entry point 'void main()' is not found.\n");
- }
- else
- {
- if (profile->lang == ShadingLang::GLSL
- || profile->lang == ShadingLang::ESSL)
- {
- if (profile->lang != ShadingLang::ESSL)
- {
- // bgfx shadow2D/Proj behave like EXT_shadow_samplers
- // not as GLSL language 1.2 specs shadow2D/Proj.
- preprocessor.writef(
- "#define shadow2D(_sampler, _coord) bgfxShadow2D(_sampler, _coord).x\n"
- "#define shadow2DProj(_sampler, _coord) bgfxShadow2DProj(_sampler, _coord).x\n"
- );
- }
- // gl_FragColor and gl_FragData are deprecated for essl > 300
- if (profile->lang == ShadingLang::ESSL
- && profile->id >= 300)
- {
- const bool hasFragColor = !bx::strFind(input, "gl_FragColor").isEmpty();
- bool hasFragData[8] = {};
- uint32_t numFragData = 0;
- for (uint32_t ii = 0; ii < BX_COUNTOF(hasFragData); ++ii)
- {
- char temp[32];
- bx::snprintf(temp, BX_COUNTOF(temp), "gl_FragData[%d]", ii);
- hasFragData[ii] = !bx::strFind(input, temp).isEmpty();
- numFragData += hasFragData[ii];
- }
- if (hasFragColor)
- {
- preprocessor.writef("#define gl_FragColor bgfx_FragColor\n");
- preprocessor.writef("out mediump vec4 bgfx_FragColor;\n");
- }
- else if (numFragData)
- {
- preprocessor.writef("#define gl_FragData bgfx_FragData\n");
- preprocessor.writef("out mediump vec4 bgfx_FragData[gl_MaxDrawBuffers];\n");
- }
- }
- for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
- {
- VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
- if (varyingIt != varyingMap.end() )
- {
- const Varying& var = varyingIt->second;
- const char* name = var.m_name.c_str();
- if (0 == bx::strCmp(name, "a_", 2)
- || 0 == bx::strCmp(name, "i_", 2) )
- {
- preprocessor.writef(
- "attribute %s %s %s %s;\n"
- , var.m_precision.c_str()
- , var.m_interpolation.c_str()
- , var.m_type.c_str()
- , name
- );
- }
- else
- {
- preprocessor.writef(
- "%s varying %s %s %s;\n"
- , var.m_interpolation.c_str()
- , var.m_precision.c_str()
- , var.m_type.c_str()
- , name
- );
- }
- }
- }
- for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
- {
- VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
- if (varyingIt != varyingMap.end() )
- {
- const Varying& var = varyingIt->second;
- preprocessor.writef("%s varying %s %s;\n"
- , var.m_interpolation.c_str()
- , var.m_type.c_str()
- , var.m_name.c_str()
- );
- }
- }
- }
- else
- {
- if (profile->lang == ShadingLang::PSSL)
- {
- preprocessor.writef(getPsslPreamble() );
- }
- preprocessor.writef(
- "#define lowp\n"
- "#define mediump\n"
- "#define highp\n"
- "#define ivec2 int2\n"
- "#define ivec3 int3\n"
- "#define ivec4 int4\n"
- "#define uvec2 uint2\n"
- "#define uvec3 uint3\n"
- "#define uvec4 uint4\n"
- "#define vec2 float2\n"
- "#define vec3 float3\n"
- "#define vec4 float4\n"
- "#define mat2 float2x2\n"
- "#define mat3 float3x3\n"
- "#define mat4 float4x4\n"
- );
- if (profile->lang == ShadingLang::HLSL
- && profile->id < 400)
- {
- preprocessor.writef(
- "#define centroid\n"
- "#define flat\n"
- "#define noperspective\n"
- "#define smooth\n"
- );
- }
- *const_cast<char*>(entry.getPtr() + 4) = '_';
- if ('f' == _options.shaderType)
- {
- bx::StringView insert = bx::strFind(bx::StringView(entry.getPtr(), shader.getTerm() ), "{");
- if (!insert.isEmpty() )
- {
- insert = strInsert(const_cast<char*>(insert.getPtr()+1), "\nvec4 bgfx_VoidFrag = vec4_splat(0.0);\n");
- }
- const bool hasFragColor = !bx::strFind(input, "gl_FragColor").isEmpty();
- const bool hasFragCoord = !bx::strFind(input, "gl_FragCoord").isEmpty() || profile->id >= 400;
- const bool hasFragDepth = !bx::strFind(input, "gl_FragDepth").isEmpty();
- const bool hasFrontFacing = !bx::strFind(input, "gl_FrontFacing").isEmpty();
- const bool hasPrimitiveId = !bx::strFind(input, "gl_PrimitiveID").isEmpty() && BGFX_CAPS_PRIMITIVE_ID;
- if (!hasPrimitiveId)
- {
- preprocessor.writef("#define gl_PrimitiveID 0\n");
- }
- bool hasFragData[8] = {};
- uint32_t numFragData = 0;
- for (uint32_t ii = 0; ii < BX_COUNTOF(hasFragData); ++ii)
- {
- char temp[32];
- bx::snprintf(temp, BX_COUNTOF(temp), "gl_FragData[%d]", ii);
- hasFragData[ii] = !bx::strFind(input, temp).isEmpty();
- numFragData += hasFragData[ii];
- }
- if (0 == numFragData)
- {
- // GL errors when both gl_FragColor and gl_FragData is used.
- // This will trigger the same error with HLSL compiler too.
- preprocessor.writef("#define gl_FragColor bgfx_FragData0\n");
- // If it has gl_FragData or gl_FragColor, color target at
- // index 0 exists, otherwise shader is not modifying color
- // targets.
- hasFragData[0] |= hasFragColor || profile->id < 400;
- if (!insert.isEmpty()
- && profile->id < 400
- && !hasFragColor)
- {
- insert = strInsert(const_cast<char*>(insert.getPtr()+1), "\ngl_FragColor = bgfx_VoidFrag;\n");
- }
- }
- preprocessor.writef("#define void_main()");
- preprocessor.writef(" \\\n\tvoid main(");
- uint32_t arg = 0;
- if (hasFragCoord)
- {
- preprocessor.writef(" \\\n\tvec4 gl_FragCoord : SV_POSITION");
- ++arg;
- }
- for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
- {
- VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
- if (varyingIt != varyingMap.end() )
- {
- const Varying& var = varyingIt->second;
- preprocessor.writef(" \\\n\t%s%s %s %s : %s"
- , arg++ > 0 ? ", " : " "
- , interpolationDx11(var.m_interpolation.c_str() )
- , var.m_type.c_str()
- , var.m_name.c_str()
- , var.m_semantics.c_str()
- );
- }
- }
- const uint32_t maxRT = profile->id >= 400 ? BX_COUNTOF(hasFragData) : 4;
- for (uint32_t ii = 0; ii < BX_COUNTOF(hasFragData); ++ii)
- {
- if (ii < maxRT)
- {
- if (hasFragData[ii])
- {
- addFragData(preprocessor, input, ii, arg++ > 0);
- }
- }
- else
- {
- voidFragData(input, ii);
- }
- }
- if (hasFragDepth)
- {
- preprocessor.writef(
- " \\\n\t%sout float gl_FragDepth : SV_DEPTH"
- , arg++ > 0 ? ", " : " "
- );
- }
- if (hasFrontFacing)
- {
- if (profile->id < 400)
- {
- preprocessor.writef(
- " \\\n\t%sfloat __vface : VFACE"
- , arg++ > 0 ? ", " : " "
- );
- }
- else
- {
- preprocessor.writef(
- " \\\n\t%sbool gl_FrontFacing : SV_IsFrontFace"
- , arg++ > 0 ? ", " : " "
- );
- }
- }
- if (hasPrimitiveId)
- {
- if (profile->id >= 400)
- {
- preprocessor.writef(
- " \\\n\t%suint gl_PrimitiveID : SV_PrimitiveID"
- , arg++ > 0 ? ", " : " "
- );
- }
- else
- {
- bx::write(_messageWriter, &messageErr, "gl_PrimitiveID builtin is not supported by D3D9 HLSL.\n");
- return false;
- }
- }
- preprocessor.writef(
- " \\\n\t)\n"
- );
- if (hasFrontFacing)
- {
- if (profile->id < 400)
- {
- preprocessor.writef(
- "#define gl_FrontFacing (__vface >= 0.0)\n"
- );
- }
- }
- }
- else if ('v' == _options.shaderType)
- {
- const bool hasVertexId = !bx::strFind(input, "gl_VertexID").isEmpty();
- const bool hasInstanceId = !bx::strFind(input, "gl_InstanceID").isEmpty();
- const bool hasViewportId = !bx::strFind(input, "gl_ViewportIndex").isEmpty();
- const bool hasLayerId = !bx::strFind(input, "gl_Layer").isEmpty();
- bx::StringView brace = bx::strFind(bx::StringView(entry.getPtr(), shader.getTerm() ), "{");
- if (!brace.isEmpty() )
- {
- bx::StringView block = bx::strFindBlock(bx::StringView(brace.getPtr(), shader.getTerm() ), '{', '}');
- if (!block.isEmpty() )
- {
- strInsert(const_cast<char*>(block.getTerm()-1), "__RETURN__;\n");
- }
- }
- preprocessor.writef(
- "struct Output\n"
- "{\n"
- "\tvec4 gl_Position : SV_POSITION;\n"
- "#define gl_Position _varying_.gl_Position\n"
- );
- for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
- {
- VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
- if (varyingIt != varyingMap.end() )
- {
- const Varying& var = varyingIt->second;
- preprocessor.writef(
- "\t%s %s %s : %s;\n"
- , interpolationDx11(var.m_interpolation.c_str() )
- , var.m_type.c_str()
- , var.m_name.c_str()
- , var.m_semantics.c_str()
- );
- preprocessor.writef(
- "#define %s _varying_.%s\n"
- , var.m_name.c_str()
- , var.m_name.c_str()
- );
- }
- }
- if (hasViewportId)
- {
- if (profile->id >= 400)
- {
- preprocessor.writef(
- "\tuint gl_ViewportIndex : SV_ViewportArrayIndex;\n"
- "#define gl_ViewportIndex _varying_.gl_ViewportIndex\n"
- );
- }
- else
- {
- bx::write(_messageWriter, &messageErr, "gl_ViewportIndex builtin is not supported by D3D9 HLSL.\n");
- return false;
- }
- }
- if (hasLayerId)
- {
- if (profile->id >= 400)
- {
- preprocessor.writef(
- "\tuint gl_Layer : SV_RenderTargetArrayIndex;\n"
- "#define gl_Layer _varying_.gl_Layer\n"
- );
- }
- else
- {
- bx::write(_messageWriter, &messageErr, "gl_Layer builtin is not supported by D3D9 HLSL.\n");
- return false;
- }
- }
- preprocessor.writef(
- "};\n"
- );
- preprocessor.writef("#define void_main() \\\n");
- preprocessor.writef("Output main(");
- uint32_t arg = 0;
- for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
- {
- VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
- if (varyingIt != varyingMap.end() )
- {
- const Varying& var = varyingIt->second;
- preprocessor.writef(
- " \\\n\t%s%s %s : %s"
- , arg++ > 0 ? ", " : ""
- , var.m_type.c_str()
- , var.m_name.c_str()
- , var.m_semantics.c_str()
- );
- }
- }
- if (hasVertexId)
- {
- if (profile->id >= 400)
- {
- preprocessor.writef(
- " \\\n\t%suint gl_VertexID : SV_VertexID"
- , arg++ > 0 ? ", " : " "
- );
- }
- else
- {
- bx::write(_messageWriter, &messageErr, "gl_VertexID builtin is not supported by D3D9 HLSL.\n");
- return false;
- }
- }
- if (hasInstanceId)
- {
- if (profile->id >= 400)
- {
- preprocessor.writef(
- " \\\n\t%suint gl_InstanceID : SV_InstanceID"
- , arg++ > 0 ? ", " : " "
- );
- }
- else
- {
- bx::write(_messageWriter, &messageErr, "gl_InstanceID builtin is not supported by D3D9 HLSL.\n");
- return false;
- }
- }
- preprocessor.writef(
- ") \\\n"
- "{ \\\n"
- "\tOutput _varying_;"
- );
- for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
- {
- VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
- if (varyingIt != varyingMap.end() )
- {
- const Varying& var = varyingIt->second;
- preprocessor.writef(" \\\n\t%s", var.m_name.c_str() );
- if (!var.m_init.empty() )
- {
- preprocessor.writef(" = %s", var.m_init.c_str() );
- }
- preprocessor.writef(";");
- }
- }
- preprocessor.writef(
- "\n#define __RETURN__ \\\n"
- "\t} \\\n"
- );
- preprocessor.writef(
- "\treturn _varying_"
- );
- }
- }
- if (preprocessor.run(input) )
- {
- if (_options.preprocessOnly)
- {
- bx::write(
- _shaderWriter
- , preprocessor.m_preprocessed.c_str()
- , (int32_t)preprocessor.m_preprocessed.size()
- , &err
- );
- return true;
- }
- {
- std::string code;
- if ('f' == _options.shaderType)
- {
- bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_FSH, &err);
- bx::write(_shaderWriter, inputHash, &err);
- bx::write(_shaderWriter, uint32_t(0), &err);
- }
- else if ('v' == _options.shaderType)
- {
- bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_VSH, &err);
- bx::write(_shaderWriter, uint32_t(0), &err);
- bx::write(_shaderWriter, outputHash, &err);
- }
- else
- {
- bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_CSH, &err);
- bx::write(_shaderWriter, uint32_t(0), &err);
- bx::write(_shaderWriter, outputHash, &err);
- }
- if (profile->lang == ShadingLang::GLSL
- || profile->lang == ShadingLang::ESSL)
- {
- const bx::StringView preprocessedInput(preprocessor.m_preprocessed.c_str() );
- uint32_t glsl_profile = profile->id;
- const bool usesBitsToEncoders = true
- && _options.shaderType == 'f'
- && !bx::findIdentifierMatch(preprocessedInput, s_bitsToEncoders).isEmpty()
- ;
- if (!bx::strFind(preprocessedInput, "layout(std430").isEmpty()
- || !bx::strFind(preprocessedInput, "image2D").isEmpty()
- || usesBitsToEncoders)
- {
- if (profile->lang == ShadingLang::GLSL
- && glsl_profile < 430)
- {
- glsl_profile = 430;
- }
- else if (glsl_profile < 310)
- {
- glsl_profile = 310;
- }
- }
- if (glsl_profile < 400)
- {
- const bool usesTextureLod = false
- || !bx::findIdentifierMatch(input, s_ARB_shader_texture_lod).isEmpty()
- || !bx::findIdentifierMatch(input, s_EXT_shader_texture_lod).isEmpty()
- ;
- const bool usesGpuShader5 = true
- && _options.shaderType != 'f'
- && !bx::findIdentifierMatch(input, s_ARB_gpu_shader5).isEmpty()
- ;
- const bool usesInstanceID = !bx::findIdentifierMatch(input, "gl_InstanceID").isEmpty();
- const bool usesGpuShader4 = !bx::findIdentifierMatch(input, s_EXT_gpu_shader4).isEmpty();
- const bool usesTexelFetch = !bx::findIdentifierMatch(input, s_texelFetch).isEmpty();
- const bool usesTextureMS = !bx::findIdentifierMatch(input, s_ARB_texture_multisample).isEmpty();
- const bool usesTextureArray = !bx::findIdentifierMatch(input, s_textureArray).isEmpty();
- const bool usesPacking = !bx::findIdentifierMatch(input, s_ARB_shading_language_packing).isEmpty();
- const bool usesViewportLayerArray = !bx::findIdentifierMatch(input, s_ARB_shader_viewport_layer_array).isEmpty();
- const bool usesIntegerVecs = !bx::findIdentifierMatch(preprocessedInput, s_integerVecs).isEmpty();
- if (profile->lang != ShadingLang::ESSL)
- {
- const bool need130 = (120 == glsl_profile && (false
- || !bx::findIdentifierMatch(input, s_130).isEmpty()
- || usesInterpolationQualifiers
- || usesTexelFetch
- || usesIntegerVecs
- ) );
- bx::stringPrintf(code, "#version %d\n", need130 ? 130 : glsl_profile);
- if (need130)
- {
- bx::stringPrintf(code, "#define varying %s\n"
- , 'f' == _options.shaderType ? "in" : "out"
- );
- }
- if (usesInstanceID)
- {
- bx::stringPrintf(code
- , "#extension GL_ARB_draw_instanced : enable\n"
- );
- }
- if (usesViewportLayerArray)
- {
- bx::stringPrintf(code
- , "#extension GL_ARB_shader_viewport_layer_array : enable\n"
- );
- }
- if (usesGpuShader4)
- {
- bx::stringPrintf(code
- , "#extension GL_EXT_gpu_shader4 : enable\n"
- );
- }
- if (usesGpuShader5)
- {
- bx::stringPrintf(code
- , "#extension GL_ARB_gpu_shader5 : enable\n"
- );
- }
- if (usesPacking)
- {
- bx::stringPrintf(code
- , "#extension GL_ARB_shading_language_packing : enable\n"
- );
- }
- bool ARB_shader_texture_lod = false;
- bool EXT_shader_texture_lod = false;
- if (usesTextureLod)
- {
- if ('f' == _options.shaderType)
- {
- ARB_shader_texture_lod = true;
- bx::stringPrintf(code
- , "#extension GL_ARB_shader_texture_lod : enable\n"
- );
- }
- else
- {
- EXT_shader_texture_lod = true;
- bx::stringPrintf(code
- , "#extension GL_EXT_shader_texture_lod : enable\n"
- );
- }
- }
- if (usesTextureMS)
- {
- bx::stringPrintf(code
- , "#extension GL_ARB_texture_multisample : enable\n"
- );
- }
- if (usesTextureArray)
- {
- bx::stringPrintf(code
- , "#extension GL_EXT_texture_array : enable\n"
- );
- }
- if (ARB_shader_texture_lod)
- {
- bx::stringPrintf(code,
- "#define texture2DProjLod texture2DProjLodARB\n"
- "#define texture2DGrad texture2DGradARB\n"
- "#define texture2DProjGrad texture2DProjGradARB\n"
- "#define textureCubeGrad textureCubeGradARB\n"
- );
- }
- else if (EXT_shader_texture_lod)
- {
- bx::stringPrintf(code,
- "#define texture2DProjLod texture2DProjLodEXT\n"
- "#define texture2DGrad texture2DGradEXT\n"
- "#define texture2DProjGrad texture2DProjGradEXT\n"
- "#define textureCubeGrad textureCubeGradEXT\n"
- );
- }
- if (need130 || (glsl_profile >= 130) )
- {
- bx::stringPrintf(code
- , "#define bgfxShadow2D(_sampler, _coord) vec4_splat(texture(_sampler, _coord) )\n"
- "#define bgfxShadow2DProj(_sampler, _coord) vec4_splat(textureProj(_sampler, _coord) )\n"
- );
- }
- else
- {
- bx::stringPrintf(code
- , "#define bgfxShadow2D shadow2D\n"
- "#define bgfxShadow2DProj shader2DProj\n"
- );
- }
- }
- else
- {
- if (glsl_profile < 300
- && usesIntegerVecs)
- {
- glsl_profile = 300;
- }
- if (glsl_profile > 100)
- {
- bx::stringPrintf(code, "#version %d es\n", glsl_profile);
- bx::stringPrintf(code, "#define attribute in\n");
- bx::stringPrintf(code, "#define varying %s\n"
- , 'f' == _options.shaderType ? "in" : "out"
- );
- bx::stringPrintf(code, "precision highp float;\n");
- bx::stringPrintf(code, "precision highp int;\n");
- }
- if (glsl_profile >= 300 && usesTextureArray)
- {
- bx::stringPrintf(code, "precision highp sampler2DArray;\n");
- }
- // Pretend that all extensions are available.
- // This will be stripped later.
- if (usesTextureLod)
- {
- bx::stringPrintf(code
- , "#extension GL_EXT_shader_texture_lod : enable\n"
- "#define texture2DLod texture2DLodEXT\n"
- "#define texture2DGrad texture2DGradEXT\n"
- "#define texture2DProjLod texture2DProjLodEXT\n"
- "#define texture2DProjGrad texture2DProjGradEXT\n"
- "#define textureCubeLod textureCubeLodEXT\n"
- "#define textureCubeGrad textureCubeGradEXT\n"
- );
- }
- if (!bx::findIdentifierMatch(input, s_OES_standard_derivatives).isEmpty() )
- {
- bx::stringPrintf(code, "#extension GL_OES_standard_derivatives : enable\n");
- }
- if (!bx::findIdentifierMatch(input, s_OES_texture_3D).isEmpty() )
- {
- bx::stringPrintf(code, "#extension GL_OES_texture_3D : enable\n");
- }
- if (glsl_profile < 300
- && !bx::findIdentifierMatch(input, s_EXT_shadow_samplers).isEmpty() )
- {
- bx::stringPrintf(code
- , "#extension GL_EXT_shadow_samplers : enable\n"
- "#define shadow2D shadow2DEXT\n"
- "#define shadow2DProj shadow2DProjEXT\n"
- );
- }
- else
- {
- bx::stringPrintf(code
- , "#define shadow2D(_sampler, _coord) texture(_sampler, _coord)\n"
- "#define shadow2DProj(_sampler, _coord) textureProj(_sampler, _coord)\n"
- );
- }
- if (usesGpuShader5)
- {
- bx::stringPrintf(code
- , "#extension GL_ARB_gpu_shader5 : enable\n"
- );
- }
- if (usesPacking)
- {
- bx::stringPrintf(code
- , "#extension GL_ARB_shading_language_packing : enable\n"
- );
- }
- if (glsl_profile < 300
- && !bx::findIdentifierMatch(input, "gl_FragDepth").isEmpty() )
- {
- bx::stringPrintf(code
- , "#extension GL_EXT_frag_depth : enable\n"
- "#define gl_FragDepth gl_FragDepthEXT\n"
- );
- }
- if (usesTextureArray)
- {
- bx::stringPrintf(code
- , "#extension GL_EXT_texture_array : enable\n"
- );
- }
- if (glsl_profile == 100)
- {
- code +=
- "mat2 transpose(mat2 _mtx)\n"
- "{\n"
- " vec2 v0 = _mtx[0];\n"
- " vec2 v1 = _mtx[1];\n"
- "\n"
- " return mat2(\n"
- " vec2(v0.x, v1.x)\n"
- " , vec2(v0.y, v1.y)\n"
- " );\n"
- "}\n"
- "\n"
- "mat3 transpose(mat3 _mtx)\n"
- "{\n"
- " vec3 v0 = _mtx[0];\n"
- " vec3 v1 = _mtx[1];\n"
- " vec3 v2 = _mtx[2];\n"
- "\n"
- " return mat3(\n"
- " vec3(v0.x, v1.x, v2.x)\n"
- " , vec3(v0.y, v1.y, v2.y)\n"
- " , vec3(v0.z, v1.z, v2.z)\n"
- " );\n"
- "}\n"
- "\n"
- "mat4 transpose(mat4 _mtx)\n"
- "{\n"
- " vec4 v0 = _mtx[0];\n"
- " vec4 v1 = _mtx[1];\n"
- " vec4 v2 = _mtx[2];\n"
- " vec4 v3 = _mtx[3];\n"
- "\n"
- " return mat4(\n"
- " vec4(v0.x, v1.x, v2.x, v3.x)\n"
- " , vec4(v0.y, v1.y, v2.y, v3.y)\n"
- " , vec4(v0.z, v1.z, v2.z, v3.z)\n"
- " , vec4(v0.w, v1.w, v2.w, v3.w)\n"
- " );\n"
- "}\n"
- ;
- }
- }
- }
- else
- {
- bx::stringPrintf(code, "#version %d\n", glsl_profile);
- if (120 < glsl_profile)
- {
- if (!bx::findIdentifierMatch(input, "gl_FragColor").isEmpty() )
- {
- bx::stringPrintf(code
- , "out vec4 bgfx_FragColor;\n"
- "#define gl_FragColor bgfx_FragColor\n"
- );
- }
- }
- bx::stringPrintf(code
- , "#define texture2D texture\n"
- "#define texture2DLod textureLod\n"
- "#define texture2DGrad textureGrad\n"
- "#define texture2DProjLod textureProjLod\n"
- "#define texture2DProjGrad textureProjGrad\n"
- "#define textureCubeLod textureLod\n"
- "#define textureCubeGrad textureGrad\n"
- "#define texture3D texture\n"
- "#define texture2DLodOffset textureLodOffset\n"
- );
- bx::stringPrintf(code, "#define attribute in\n");
- bx::stringPrintf(code, "#define varying %s\n"
- , 'f' == _options.shaderType ? "in" : "out"
- );
- bx::stringPrintf(code
- , "#define bgfxShadow2D(_sampler, _coord) vec4_splat(texture(_sampler, _coord) )\n"
- "#define bgfxShadow2DProj(_sampler, _coord) vec4_splat(textureProj(_sampler, _coord) )\n"
- );
- }
- if ( (profile->lang == ShadingLang::GLSL && glsl_profile > 400)
- || (profile->lang == ShadingLang::ESSL && glsl_profile > 300) )
- {
- code += preprocessor.m_preprocessed;
- bx::write(_shaderWriter, uint16_t(0), &err);
- uint32_t shaderSize = (uint32_t)code.size();
- bx::write(_shaderWriter, shaderSize, &err);
- bx::write(_shaderWriter, code.c_str(), shaderSize, &err);
- bx::write(_shaderWriter, uint8_t(0), &err);
- compiled = true;
- }
- else
- {
- code += _comment;
- code += preprocessor.m_preprocessed;
- if (profile->lang == ShadingLang::ESSL)
- {
- glsl_profile |= 0x80000000;
- }
- compiled = compileGLSLShader(_options, glsl_profile, code, _shaderWriter, _messageWriter);
- }
- }
- else
- {
- code += _comment;
- code += preprocessor.m_preprocessed;
- if (profile->lang == ShadingLang::Metal)
- {
- compiled = compileMetalShader(_options, profile->id, code, _shaderWriter, _messageWriter);
- }
- else if (profile->lang == ShadingLang::SpirV)
- {
- compiled = compileSPIRVShader(_options, profile->id, code, _shaderWriter, _messageWriter);
- }
- else if (profile->lang == ShadingLang::PSSL)
- {
- compiled = compilePSSLShader(_options, 0, code, _shaderWriter, _messageWriter);
- }
- else
- {
- compiled = compileHLSLShader(_options, profile->id, code, _shaderWriter, _messageWriter);
- }
- }
- }
- if (compiled)
- {
- if (_options.depends)
- {
- std::string ofp = _options.outputFilePath + ".d";
- bx::FileWriter writer;
- if (bx::open(&writer, ofp.c_str() ) )
- {
- writef(&writer, "%s : %s\n", _options.outputFilePath.c_str(), preprocessor.m_depends.c_str() );
- bx::close(&writer);
- }
- }
- }
- }
- }
- }
- delete [] data;
- return compiled;
- }
- int compileShader(int _argc, const char* _argv[])
- {
- bx::CommandLine cmdLine(_argc, _argv);
- if (cmdLine.hasArg('v', "version") )
- {
- bx::printf(
- "shaderc, bgfx shader compiler tool, version %d.%d.%d.\n"
- , BGFX_SHADERC_VERSION_MAJOR
- , BGFX_SHADERC_VERSION_MINOR
- , BGFX_API_VERSION
- );
- return bx::kExitSuccess;
- }
- if (cmdLine.hasArg('h', "help") )
- {
- help();
- return bx::kExitFailure;
- }
- g_verbose = cmdLine.hasArg("verbose");
- const char* filePath = cmdLine.findOption('f');
- if (NULL == filePath)
- {
- help("Shader file name must be specified.");
- return bx::kExitFailure;
- }
- bool consoleOut = cmdLine.hasArg("stdout");
- const char* outFilePath = cmdLine.findOption('o');
- if (NULL == outFilePath
- && !consoleOut)
- {
- help("Output file name must be specified or use \"--stdout\" to output to stdout.");
- return bx::kExitFailure;
- }
- const char* type = cmdLine.findOption('\0', "type");
- if (NULL == type)
- {
- help("Must specify shader type.");
- return bx::kExitFailure;
- }
- Options options;
- options.inputFilePath = filePath;
- options.outputFilePath = consoleOut ? "" : outFilePath;
- options.shaderType = bx::toLower(type[0]);
- options.disasm = cmdLine.hasArg('\0', "disasm");
- const char* platform = cmdLine.findOption('\0', "platform");
- if (NULL == platform)
- {
- platform = "";
- }
- options.platform = platform;
- options.raw = cmdLine.hasArg('\0', "raw");
- const char* profile = cmdLine.findOption('p', "profile");
- if ( NULL != profile)
- {
- options.profile = profile;
- }
- {
- options.debugInformation = cmdLine.hasArg('\0', "debug");
- options.avoidFlowControl = cmdLine.hasArg('\0', "avoid-flow-control");
- options.noPreshader = cmdLine.hasArg('\0', "no-preshader");
- options.partialPrecision = cmdLine.hasArg('\0', "partial-precision");
- options.preferFlowControl = cmdLine.hasArg('\0', "prefer-flow-control");
- options.backwardsCompatibility = cmdLine.hasArg('\0', "backwards-compatibility");
- options.warningsAreErrors = cmdLine.hasArg('\0', "Werror");
- options.keepIntermediate = cmdLine.hasArg('\0', "keep-intermediate");
- uint32_t optimization = 3;
- if (cmdLine.hasArg(optimization, 'O') )
- {
- options.optimize = true;
- options.optimizationLevel = optimization;
- }
- }
- bx::StringView bin2c;
- if (cmdLine.hasArg("bin2c") )
- {
- const char* bin2cArg = cmdLine.findOption("bin2c");
- if (NULL != bin2cArg)
- {
- bin2c.set(bin2cArg);
- }
- else
- {
- bin2c = baseName(outFilePath);
- if (!bin2c.isEmpty() )
- {
- char* temp = (char*)alloca(bin2c.getLength()+1);
- for (uint32_t ii = 0, num = bin2c.getLength(); ii < num; ++ii)
- {
- char ch = bin2c.getPtr()[ii];
- if (bx::isAlphaNum(ch) )
- {
- temp[ii] = ch;
- }
- else
- {
- temp[ii] = '_';
- }
- }
- temp[bin2c.getLength()] = '\0';
- bin2c = temp;
- }
- }
- }
- options.depends = cmdLine.hasArg("depends");
- options.preprocessOnly = cmdLine.hasArg("preprocess");
- const char* includeDir = cmdLine.findOption('i');
- BX_TRACE("depends: %d", options.depends);
- BX_TRACE("preprocessOnly: %d", options.preprocessOnly);
- BX_TRACE("includeDir: %s", includeDir);
- for (int ii = 1; NULL != includeDir; ++ii)
- {
- options.includeDirs.push_back(includeDir);
- includeDir = cmdLine.findOption(ii, 'i');
- }
- std::string dir;
- {
- bx::FilePath fp(filePath);
- bx::StringView path(fp.getPath() );
- dir.assign(path.getPtr(), path.getTerm() );
- options.includeDirs.push_back(dir);
- }
- const char* defines = cmdLine.findOption("define");
- while (NULL != defines
- && '\0' != *defines)
- {
- defines = bx::strLTrimSpace(defines).getPtr();
- bx::StringView eol = bx::strFind(defines, ';');
- std::string define(defines, eol.getPtr() );
- options.defines.push_back(define.c_str() );
- defines = ';' == *eol.getPtr() ? eol.getPtr()+1 : eol.getPtr();
- }
- std::string commandLineComment = "// shaderc command line:\n//";
- for (int32_t ii = 0, num = cmdLine.getNum(); ii < num; ++ii)
- {
- commandLineComment += " ";
- commandLineComment += cmdLine.get(ii);
- }
- commandLineComment += "\n\n";
- bool compiled = false;
- bx::FileReader reader;
- if (!bx::open(&reader, filePath) )
- {
- bx::printf("Unable to open file '%s'.\n", filePath);
- }
- else
- {
- const char* varying = NULL;
- File attribdef;
- if ('c' != options.shaderType)
- {
- std::string defaultVarying = dir + "varying.def.sc";
- const char* varyingdef = cmdLine.findOption("varyingdef", defaultVarying.c_str() );
- attribdef.load(varyingdef);
- varying = attribdef.getData();
- if (NULL != varying
- && *varying != '\0')
- {
- options.dependencies.push_back(varyingdef);
- }
- else
- {
- bx::printf("ERROR: Failed to parse varying def file: \"%s\" No input/output semantics will be generated in the code!\n", varyingdef);
- }
- }
- int32_t size = (int32_t)bx::getSize(&reader);
- const int32_t total = size + 16384;
- char* data = new char[total];
- size = bx::read(&reader, data, size, bx::ErrorAssert{});
- // Trim UTF-8 BOM
- if (data[0] == '\xef'
- && data[1] == '\xbb'
- && data[2] == '\xbf')
- {
- bx::memMove(data, &data[3], size-3);
- size -= 3;
- }
- const char ch = data[0];
- if (false // https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding
- || '\x00' == ch
- || '\x0e' == ch
- || '\x2b' == ch
- || '\x84' == ch
- || '\xdd' == ch
- || '\xf7' == ch
- || '\xfb' == ch
- || '\xfe' == ch
- || '\xff' == ch
- )
- {
- bx::printf("Shader input file has unsupported BOM.\n");
- return bx::kExitFailure;
- }
- // Compiler generates "error X3000: syntax error: unexpected end of file"
- // if input doesn't have empty line at EOF.
- data[size] = '\n';
- bx::memSet(&data[size+1], 0, total-size-1);
- bx::close(&reader);
- {
- bx::FileWriter* writer = NULL;
- if (!consoleOut)
- {
- if (!bin2c.isEmpty() )
- {
- writer = new Bin2cWriter(bin2c);
- }
- else
- {
- writer = new bx::FileWriter;
- }
- if (!bx::open(writer, outFilePath) )
- {
- bx::printf("Unable to open output file '%s'.\n", outFilePath);
- return bx::kExitFailure;
- }
- }
- compiled = compileShader(
- varying
- , commandLineComment.c_str()
- , data
- , size
- , options
- , consoleOut ? bx::getStdOut() : writer
- , bx::getStdOut()
- );
- if (!consoleOut)
- {
- bx::close(writer);
- delete writer;
- }
- }
- }
- if (compiled)
- {
- return bx::kExitSuccess;
- }
- bx::remove(outFilePath);
- bx::printf("Failed to build shader.\n");
- return bx::kExitFailure;
- }
- } // namespace bgfx
- int main(int _argc, const char* _argv[])
- {
- return bgfx::compileShader(_argc, _argv);
- }
|