SPIR-V.rst 188 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703
  1. =====================================
  2. HLSL to SPIR-V Feature Mapping Manual
  3. =====================================
  4. .. contents::
  5. :local:
  6. :depth: 3
  7. Introduction
  8. ============
  9. This document describes the mappings from HLSL features to SPIR-V for Vulkan
  10. adopted by the SPIR-V codegen. For how to build, use, or contribute to the
  11. SPIR-V codegen and its internals, please see the
  12. `wiki <https://github.com/Microsoft/DirectXShaderCompiler/wiki/SPIR%E2%80%90V-CodeGen>`_
  13. page.
  14. `SPIR-V <https://www.khronos.org/registry/spir-v/>`_ is a binary intermediate
  15. language for representing graphical-shader stages and compute kernels for
  16. multiple Khronos APIs, such as Vulkan, OpenGL, and OpenCL. At the moment we
  17. only intend to support the Vulkan flavor of SPIR-V.
  18. DirectXShaderCompiler is the reference compiler for HLSL. Adding SPIR-V codegen
  19. in DirectXShaderCompiler will enable the usage of HLSL as a frontend language
  20. for Vulkan shader programming. Sharing the same code base also means we can
  21. track the evolution of HLSL more closely and always deliver the best of HLSL to
  22. developers. Moreover, developers will also have a unified compiler toolchain for
  23. targeting both DirectX and Vulkan. We believe this effort will benefit the
  24. general graphics ecosystem.
  25. Note that this document is expected to be an ongoing effort and grow as we
  26. implement more and more HLSL features.
  27. Overview
  28. ========
  29. Although they share the same basic concepts, DirectX and Vulkan are still
  30. different graphics APIs with semantic gaps. HLSL is the native shading language
  31. for DirectX, so certain HLSL features do not have corresponding mappings in
  32. Vulkan, and certain Vulkan specific information does not have native ways to
  33. express in HLSL source code. This section describes the general translation
  34. paradigms and how we close some of the major semantic gaps.
  35. Note that the term "semantic" is overloaded. In HLSL, it can mean the string
  36. attached to shader input or output. For such cases, we refer it as "HLSL
  37. semantic" or "semantic string". For other cases, we just use the normal
  38. "semantic" term.
  39. Shader entry function
  40. ---------------------
  41. HLSL entry functions can read data from the previous shader stage and write
  42. data to the next shader stage via function parameters and return value. On the
  43. contrary, Vulkan requires all SPIR-V entry functions taking no parameters and
  44. returning void. All data passing between stages should use global variables
  45. in the ``Input`` and ``Output`` storage class.
  46. To handle this difference, we emit a wrapper function as the SPIR-V entry
  47. function around the HLSL source code entry function. The wrapper function is
  48. responsible to read data from SPIR-V ``Input`` global variables and prepare
  49. them to the types required in the source code entry function signature, call
  50. the source code entry function, and then decompose the contents in return value
  51. (and ``out``/``inout`` parameters) to the types required by the SPIR-V
  52. ``Output`` global variables, and then write out. For details about the wrapper
  53. function, please refer to the `entry function wrapper`_ section.
  54. Shader stage IO interface matching
  55. ----------------------------------
  56. HLSL leverages semantic strings to link variables and pass data between shader
  57. stages. Great flexibility is allowed as for how to use the semantic strings.
  58. They can appear on function parameters, function returns, and struct members.
  59. In Vulkan, linking variables and passing data between shader stages is done via
  60. numeric ``Location`` decorations on SPIR-V global variables in the ``Input`` and
  61. ``Output`` storage class.
  62. To help handling such differences, we provide `Vulkan specific attributes`_ to
  63. let the developer to express precisely their intents. The compiler will also try
  64. its best to deduce the mapping from semantic strings to SPIR-V ``Location``
  65. numbers when such explicit Vulkan specific attributes are absent. Please see the
  66. `HLSL semantic and Vulkan Location`_ section for more details about the mapping
  67. and ``Location`` assignment.
  68. What makes the story complicated is Vulkan's strict requirements on interface
  69. matching. Basically, a variable in the previous stage is considered a match to
  70. a variable in the next stage if and only if they are decorated with the same
  71. ``Location`` number and with the exact same type, except for the outermost
  72. arrayness in hull/domain/geometry shader, which can be ignored regarding
  73. interface matching. This is causing problems together with the flexibility of
  74. HLSL semantic strings.
  75. Some HLSL system-value (SV) semantic strings will be mapped into SPIR-V
  76. variables with builtin decorations, some are not. HLSL non-SV semantic strings
  77. should all be mapped to SPIR-V variables without builtin decorations (but with
  78. ``Location`` decorations).
  79. With these complications, if we are grouping multiple semantic strings in a
  80. struct in the HLSL source code, that struct should be flattened and each of
  81. its members should be mapped separately. For example, for the following:
  82. .. code:: hlsl
  83. struct T {
  84. float2 clip0 : SV_ClipDistance0;
  85. float3 cull0 : SV_CullDistance0;
  86. float4 foo : FOO;
  87. };
  88. struct S {
  89. float4 pos : SV_Position;
  90. float2 clip1 : SV_ClipDistance1;
  91. float3 cull1 : SV_CullDistance1;
  92. float4 bar : BAR;
  93. T t;
  94. };
  95. If we have an ``S`` input parameter in pixel shader, we should flatten it
  96. recursively to generate five SPIR-V ``Input`` variables. Three of them are
  97. decorated by the ``Position``, ``ClipDistance``, ``CullDistance`` builtin,
  98. and two of them are decorated by the ``Location`` decoration. (Note that
  99. ``clip0`` and ``clip1`` are concatenated, also ``cull0`` and ``cull1``.
  100. The ``ClipDistance`` and ``CullDistance`` builtins are special and explained
  101. in the `ClipDistance & CullDistance`_ section.)
  102. Flattening is infective because of Vulkan interface matching rules. If we
  103. flatten a struct in the output of a previous stage, which may create multiple
  104. variables decorated with different ``Location`` numbers, we also need to
  105. flatten it in the input of the next stage. otherwise we may have ``Location``
  106. mismatch even if we share the same definition of the struct. Because
  107. hull/domain/geometry shader is optional, we can have different chains of shader
  108. stages, which means we need to flatten all shader stage interfaces. For
  109. hull/domain/geometry shader, their inputs/outputs have an additional arrayness.
  110. So if we are seeing an array of structs in these shaders, we need to flatten
  111. them into arrays of its fields.
  112. Vulkan specific features
  113. ------------------------
  114. We try to implement Vulkan specific features using the most intuitive and
  115. non-intrusive ways in HLSL, which means we will prefer native language
  116. constructs when possible. If that is inadequate, we then consider attaching
  117. `Vulkan specific attributes`_ to them, or introducing new syntax.
  118. Descriptors
  119. ~~~~~~~~~~~
  120. The compiler provides multiple mechanisms to specify which Vulkan descriptor
  121. a particular resource binds to.
  122. In the source code, you can use the ``[[vk::binding(X[, Y])]]`` and
  123. ``[[vk::counter_binding(X)]]`` attribute. The native ``:register()`` attribute
  124. is also respected.
  125. On the command-line, you can use the ``-fvk-{b|s|t|u}-shift`` or
  126. ``-fvk-bind-register`` option.
  127. If you can modify the source code, the ``[[vk::binding(X[, Y])]]`` and
  128. ``[[vk::counter_binding(X)]]`` attribute gives you find-grained control over
  129. descriptor assignment.
  130. If you cannot modify the source code, you can use command-line options to change
  131. how ``:register()`` attribute is handled by the compiler. ``-fvk-bind-register``
  132. lets you to specify the descriptor for the source at a certain register.
  133. ``-fvk-{b|s|t|u}-shift`` lets you to apply shifts to all register numbers
  134. of a certain register type. They cannot be used together, though.
  135. Without attribute and command-line option, ``:register(xX, spaceY)`` will be
  136. mapped to binding ``X`` in descriptor set ``Y``. Note that register type ``x``
  137. is ignored, so this may cause overlap.
  138. The more specific a mechanism is, the higher precedence it has, and command-line
  139. option has higher precedence over source code attribute.
  140. For more details, see `HLSL register and Vulkan binding`_, `Vulkan specific
  141. attributes`_, and `Vulkan-specific options`_.
  142. Subpass inputs
  143. ~~~~~~~~~~~~~~
  144. Within a Vulkan `rendering pass <https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#renderpass>`_,
  145. a subpass can write results to an output target that can then be read by the
  146. next subpass as an input subpass. The "Subpass Input" feature regards the
  147. ability to read an output target.
  148. Subpasses are read through two new builtin resource types, available only in
  149. pixel shader:
  150. .. code:: hlsl
  151. class SubpassInput<T> {
  152. T SubpassLoad();
  153. };
  154. class SubpassInputMS<T> {
  155. T SubpassLoad(int sampleIndex);
  156. };
  157. In the above, ``T`` is a scalar or vector type. If omitted, it will defaults to
  158. ``float4``.
  159. Subpass inputs are implicitly addressed by the pixel's (x, y, layer) coordinate.
  160. These objects support reading the subpass input through the methods as shown
  161. in the above.
  162. A subpass input is selected by using a new attribute ``vk::input_attachment_index``.
  163. For example:
  164. .. code:: hlsl
  165. [[vk::input_attachment_index(i)]] SubpassInput input;
  166. An ``vk::input_attachment_index`` of ``i`` selects the ith entry in the input
  167. pass list. (See Vulkan API spec for more information.)
  168. Push constants
  169. ~~~~~~~~~~~~~~
  170. Vulkan push constant blocks are represented using normal global variables of
  171. struct types in HLSL. The variables (not the underlying struct types) should be
  172. annotated with the ``[[vk::push_constant]]`` attribute.
  173. Please note as per the requirements of Vulkan, "there must be no more than one
  174. push constant block statically used per shader entry point."
  175. Specialization constants
  176. ~~~~~~~~~~~~~~~~~~~~~~~~
  177. To use Vulkan specialization constants, annotate global constants with the
  178. ``[[vk::constant_id(X)]]`` attribute. For example,
  179. .. code:: hlsl
  180. [[vk::constant_id(1)]] const bool specConstBool = true;
  181. [[vk::constant_id(2)]] const int specConstInt = 42;
  182. [[vk::constant_id(3)]] const float specConstFloat = 1.5;
  183. Shader Record Buffer
  184. ~~~~~~~~~~~~~~~~~~~~
  185. SPV_NV_ray_tracing exposes user managed buffer in shader binding table by
  186. using storage class ShaderRecordBufferNV. ConstantBuffer or cbuffer blocks
  187. can now be mapped to this storage class under HLSL by using
  188. ``[[vk::shader_record_nv]]`` annotation. It is applicable only on ConstantBuffer
  189. and cbuffer declarations.
  190. Please note as per the requirements of VK_NV_ray_tracing, "there must be no
  191. more than one shader_record_nv block statically used per shader entry point
  192. otherwise results are undefined."
  193. Builtin variables
  194. ~~~~~~~~~~~~~~~~~
  195. Some of the Vulkan builtin variables have no equivalents in native HLSL
  196. language. To support them, ``[[vk::builtin("<builtin>")]]`` is introduced.
  197. Right now the following ``<builtin>`` are supported:
  198. * ``PointSize``: The GLSL equivalent is ``gl_PointSize``.
  199. * ``HelperInvocation``: The GLSL equivalent is ``gl_HelperInvocation``.
  200. * ``BaseVertex``: The GLSL equivalent is ``gl_BaseVertexARB``.
  201. Need ``SPV_KHR_shader_draw_parameters`` extension.
  202. * ``BaseInstance``: The GLSL equivalent is ``gl_BaseInstanceARB``.
  203. Need ``SPV_KHR_shader_draw_parameters`` extension.
  204. * ``DrawIndex``: The GLSL equivalent is ``gl_DrawIDARB``.
  205. Need ``SPV_KHR_shader_draw_parameters`` extension.
  206. * ``DeviceIndex``: The GLSL equivalent is ``gl_DeviceIndex``.
  207. Need ``SPV_KHR_device_group`` extension.
  208. * ``ViewportMaskNV``: The GLSL equivalent is ``gl_ViewportMask``.
  209. Please see Vulkan spec. `14.6. Built-In Variables <https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#interfaces-builtin-variables>`_
  210. for detailed explanation of these builtins.
  211. Supported extensions
  212. ~~~~~~~~~~~~~~~~~~~~
  213. * SPV_KHR_16bit_storage
  214. * SPV_KHR_device_group
  215. * SPV_KHR_multivew
  216. * SPV_KHR_post_depth_coverage
  217. * SPV_KHR_shader_draw_parameters
  218. * SPV_EXT_descriptor_indexing
  219. * SPV_EXT_fragment_fully_covered
  220. * SPV_EXT_fragment_invocation_density
  221. * SPV_EXT_shader_stencil_support
  222. * SPV_AMD_shader_explicit_vertex_parameter
  223. * SPV_GOOGLE_hlsl_functionality1
  224. * SPV_NV_mesh_shader
  225. Vulkan specific attributes
  226. --------------------------
  227. `C++ attribute specifier sequence <http://en.cppreference.com/w/cpp/language/attributes>`_
  228. is a non-intrusive way of providing Vulkan specific information in HLSL.
  229. The namespace ``vk`` will be used for all Vulkan attributes:
  230. - ``location(X)``: For specifying the location (``X``) numbers for stage
  231. input/output variables. Allowed on function parameters, function returns,
  232. and struct fields.
  233. - ``binding(X[, Y])``: For specifying the descriptor set (``Y``) and binding
  234. (``X``) numbers for resource variables. The descriptor set (``Y``) is
  235. optional; if missing, it will be set to 0. Allowed on global variables.
  236. - ``counter_binding(X)``: For specifying the binding number (``X``) for the
  237. associated counter for RW/Append/Consume structured buffer. The descriptor
  238. set number for the associated counter is always the same as the main resource.
  239. - ``push_constant``: For marking a variable as the push constant block. Allowed
  240. on global variables of struct type. At most one variable can be marked as
  241. ``push_constant`` in a shader.
  242. - ``offset(X)``: For manually layout struct members. Annotating a struct member
  243. with this attribute will force the compiler to put the member at offset ``X``
  244. w.r.t. the beginning of the struct. Only allowed on struct members.
  245. - ``constant_id(X)``: For marking a global constant as a specialization constant.
  246. Allowed on global variables of boolean/integer/float types.
  247. - ``input_attachment_index(X)``: To associate the Xth entry in the input pass
  248. list to the annotated object. Only allowed on objects whose type are
  249. ``SubpassInput`` or ``SubpassInputMS``.
  250. - ``builtin("X")``: For specifying an entity should be translated into a certain
  251. Vulkan builtin variable. Allowed on function parameters, function returns,
  252. and struct fields.
  253. - ``index(X)``: For specifying the index at a specific pixel shader output
  254. location. Used for dual-source blending.
  255. - ``post_depth_coverage``: The input variable decorated with SampleMask will
  256. reflect the result of the EarlyFragmentTests. Only valid on pixel shader entry points.
  257. Only ``vk::`` attributes in the above list are supported. Other attributes will
  258. result in warnings and be ignored by the compiler. All C++11 attributes will
  259. only trigger warnings and be ignored if not compiling towards SPIR-V.
  260. For example, to specify the layout of resource variables and the location of
  261. interface variables:
  262. .. code:: hlsl
  263. struct S { ... };
  264. [[vk::binding(X, Y), vk::counter_binding(Z)]]
  265. RWStructuredBuffer<S> mySBuffer;
  266. [[vk::location(M)]] float4
  267. main([[vk::location(N)]] float4 input: A) : B
  268. { ... }
  269. SPIR-V version and extension
  270. ----------------------------
  271. SPIR-V CodeGen provides two command-line options for fine-grained SPIR-V target
  272. environment (hence SPIR-V version) and SPIR-V extension control:
  273. - ``-fspv-target-env=``: for specifying SPIR-V target environment
  274. - ``-fspv-extension=``: for specifying allowed SPIR-V extensions
  275. ``-fspv-target-env=`` only accepts ``vulkan1.0`` and ``vulkan1.1`` right now.
  276. If such an option is not given, the CodeGen defaults to ``vulkan1.0``. When
  277. targeting ``vulkan1.0``, trying to use features that are only available
  278. in Vulkan 1.1 (SPIR-V 1.3), like `Shader Model 6.0 wave intrinsics`_, will
  279. trigger a compiler error.
  280. If ``-fspv-extension=`` is not specified, the CodeGen will select suitable
  281. SPIR-V extensions to translate the source code. Otherwise, only extensions
  282. supplied via ``-fspv-extension=`` will be used. If that does not suffice, errors
  283. will be emitted explaining what additional extensions are required to translate
  284. what specific feature in the source code. If you want to allow all KHR
  285. extensions, you can use ``-fspv-extension=KHR``.
  286. Legalization, optimization, validation
  287. --------------------------------------
  288. After initial translation of the HLSL source code, SPIR-V CodeGen will further
  289. conduct legalization (if needed), optimization (if requested), and validation
  290. (if not turned off). All these three stages are outsourced to `SPIRV-Tools <https://github.com/KhronosGroup/SPIRV-Tools>`_.
  291. Here are the options controlling these stages:
  292. * ``-fcgl``: turn off legalization and optimization
  293. * ``-Od``: turn off optimization
  294. * ``-Vd``: turn off validation
  295. Legalization
  296. ~~~~~~~~~~~~
  297. HLSL is a fairly permissive language considering the flexibility it provides for
  298. manipulating resource objects. The developer can create local copies, pass
  299. them around as function parameters and return values, as long as after certain
  300. transformations (function inlining, constant evaluation and propagating, dead
  301. code elimination, etc.), the compiler can remove all temporary copies and
  302. pinpoint all uses to unique global resource objects.
  303. Resulting from the above property of HLSL, if we translate into SPIR-V for
  304. Vulkan literally from the input HLSL source code, we will sometimes generate
  305. illegal SPIR-V. Certain transformations are needed to legalize the literally
  306. translated SPIR-V. Performing such transformations at the frontend AST level
  307. is cumbersome or impossible (e.g., function inlining). They are better to be
  308. conducted at SPIR-V level. Therefore, legalization is delegated to SPIRV-Tools.
  309. Specifically, we need to legalize the following HLSL source code patterns:
  310. * Using resource types in struct types
  311. * Creating aliases of global resource objects
  312. * Control flows invovling the above cases
  313. Legalization transformations will not run unless the above patterns are
  314. encountered in the source code.
  315. For more details, please see the `SPIR-V cookbook <https://github.com/Microsoft/DirectXShaderCompiler/tree/master/docs/SPIRV-Cookbook.rst>`_,
  316. which contains examples of what HLSL code patterns will be accepted and
  317. generate valid SPIR-V for Vulkan.
  318. Optimization
  319. ~~~~~~~~~~~~
  320. Optimization is also delegated to SPIRV-Tools. Right now there are no difference
  321. between optimization levels greater than zero; they will all invoke the same
  322. optimization recipe. That is, the recipe behind ``spirv-opt -O``. If you want to
  323. run a custom optimization recipe, you can do so using the command line option
  324. ``-Oconfig=`` and specifying a comma-separated list of your desired passes.
  325. The passes are invoked in the specified order.
  326. For example, you can specify ``-Oconfig=--loop-unroll,--scalar-replacement=300,--eliminate-dead-code-aggressive``
  327. to firstly invoke loop unrolling, then invoke scalar replacement of aggregates,
  328. lastly invoke aggressive dead code elimination. All valid options to
  329. ``spirv-opt`` are accepted as components to the comma-separated list.
  330. Here are the typical passes in alphabetical order:
  331. * ``--ccp``
  332. * ``--cfg-cleanup``
  333. * ``--convert-local-access-chains``
  334. * ``--copy-propagate-arrays``
  335. * ``--eliminate-dead-branches``
  336. * ``--eliminate-dead-code-aggressive``
  337. * ``--eliminate-dead-functions``
  338. * ``--eliminate-local-multi-store``
  339. * ``--eliminate-local-single-block``
  340. * ``--eliminate-local-single-store``
  341. * ``--flatten-decorations``
  342. * ``--if-conversion``
  343. * ``--inline-entry-points-exhaustive``
  344. * ``--local-redundancy-elimination``
  345. * ``--loop-fission``
  346. * ``--loop-fusion``
  347. * ``--loop-unroll``
  348. * ``--loop-unroll-partial=[<n>]``
  349. * ``--loop-peeling`` (requires ``--loop-peeling-threshold``)
  350. * ``--merge-blocks``
  351. * ``--merge-return``
  352. * ``--loop-unswitch``
  353. * ``--private-to-local``
  354. * ``--reduce-load-size``
  355. * ``--redundancy-elimination``
  356. * ``--remove-duplicates``
  357. * ``--replace-invalid-opcode``
  358. * ``--ssa-rewrite``
  359. * ``--scalar-replacement[=<n>]``
  360. * ``--simplify-instructions``
  361. * ``--vector-dce``
  362. Besides, there are two special batch options; each stands for a recommended
  363. recipe by itself:
  364. * ``-O``: A bunch of passes in an appropriate order that attempt to improve
  365. performance of generated code. Same as ``spirv-opt -O``. Also same as SPIR-V
  366. CodeGen's default recipe.
  367. * ``-Os``: A bunch of passes in an appropriate order that attempt to reduce the
  368. size of the generated code. Same as ``spirv-opt -Os``.
  369. So if you want to run loop unrolling additionally after the default optimization
  370. recipe, you can specify ``-Oconfig=-O,--loop-unroll``.
  371. For the whole list of accepted passes and details about each one, please see
  372. ``spirv-opt``'s help manual (``spirv-opt --help``), or the SPIRV-Tools `optimizer header file <https://github.com/KhronosGroup/SPIRV-Tools/blob/master/include/spirv-tools/optimizer.hpp>`_.
  373. Validation
  374. ~~~~~~~~~~
  375. Validation is turned on by default as the last stage of SPIR-V CodeGen. Failing
  376. validation, which indicates there is a CodeGen bug, will trigger a fatal error.
  377. Please file an issue if you see that.
  378. Debugging
  379. ---------
  380. By default, the compiler will only emit names for types and variables as debug
  381. information, to aid reading of the generated SPIR-V. The ``-Zi`` option will
  382. let the compiler emit the following additional debug information:
  383. * Full path of the main source file using ``OpSource``
  384. * Preprocessed source code using ``OpSource`` and ``OpSourceContinued``
  385. * Line information for certain instructions using ``OpLine`` (WIP)
  386. * DXC Git commit hash using ``OpModuleProcessed`` (requires Vulkan 1.1)
  387. * DXC command-line options used to compile the shader using ``OpModuleProcessed``
  388. (requires Vulkan 1.1)
  389. We chose to embed preprocessed source code instead of original source code to
  390. avoid pulling in lots of contents unrelated to the current entry point, and
  391. boilerplate contents generated by engines. We may add a mode for selecting
  392. between preprocessed single source code and original separated source code in
  393. the future.
  394. One thing to note is that to keep the line numbers in consistent with the
  395. embedded source, the compiler is invoked twice; the first time is for
  396. preprocessing the source code, and the second time is for feeding the
  397. preprocessed source code as input for a whole compilation. So using ``-Zi``
  398. means performance penality.
  399. If you want to have fine-grained control over the categories of emitted debug
  400. information, you can use ``-fspv-debug=``. It accepts:
  401. * ``file``: for emitting full path of the main source file
  402. * ``source``: for emitting preprocessed source code (turns on ``file`` implicitly)
  403. * ``line``: for emitting line information (turns on ``source`` implicitly)
  404. * ``tool``: for emitting DXC Git commit hash and command-line options
  405. ``-fspv-debug=`` overrules ``-Zi``. And you can provide multiple instances of
  406. ``-fspv-debug=``. For example, you can use ``-fspv-debug=file -fspv-debug=tool``
  407. to turn on emitting file path and DXC information; source code and line
  408. information will not be emitted.
  409. Reflection
  410. ----------
  411. Making reflection easier is one of the goals of SPIR-V CodeGen. This section
  412. provides guidelines about how to reflect on certain facts.
  413. Note that we generate ``OpName``/``OpMemberName`` instructions for various
  414. types/variables both explicitly defined in the source code and interally created
  415. by the compiler. These names are primarily for debugging purposes in the
  416. compiler. They have "no semantic impact and can safely be removed" according
  417. to the SPIR-V spec. And they are subject to changes without notice. So we do
  418. not suggest to use them for reflection.
  419. Source code shader profile
  420. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  421. The source code shader profile version can be re-discovered by the "Version"
  422. operand in ``OpSource`` instruction. For ``*s_<major>_<minor>``, the "Verison"
  423. operand in ``OpSource`` will be set as ``<major>`` * 100 + ``<minor>`` * 10.
  424. For example, ``vs_5_1`` will have 510, ``ps_6_2`` will have 620.
  425. HLSL Semantic
  426. ~~~~~~~~~~~~~
  427. HLSL semantic strings are by default not emitted into the SPIR-V binary module.
  428. If you need them, by specifying ``-fspv-reflect``, the compiler will use
  429. the ``Op*DecorateStringGOOGLE`` instruction in `SPV_GOOGLE_hlsl_funtionality1 <https://github.com/KhronosGroup/SPIRV-Registry/blob/master/extensions/GOOGLE/SPV_GOOGLE_hlsl_functionality1.asciidoc>`_
  430. extension to emit them.
  431. Counter buffers for RW/Append/Consume StructuredBuffer
  432. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  433. The association between a counter buffer and its main RW/Append/Consume
  434. StructuredBuffer is conveyed by ``OpDecorateId <structured-buffer-id>
  435. HLSLCounterBufferGOOGLE <counter-buffer-id>`` instruction from the
  436. `SPV_GOOGLE_hlsl_funtionality1 <https://github.com/KhronosGroup/SPIRV-Registry/blob/master/extensions/GOOGLE/SPV_GOOGLE_hlsl_functionality1.asciidoc>`_
  437. extension. This information is by default missing; you need to specify
  438. ``-fspv-reflect`` to direct the compiler to emit them.
  439. Read-only vs. read-write resource types
  440. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  441. There are no clear and consistent decorations in the SPIR-V to show whether a
  442. resource type is translated from a read-only (RO) or read-write (RW) HLSL
  443. resource type. Instead, you need to use different checks for reflecting different
  444. resource types:
  445. * HLSL samplers: RO.
  446. * HLSL ``Buffer``/``RWBuffer``/``Texture*``/``RWTexture*``: Check the "Sampled"
  447. operand in the ``OpTypeImage`` instruction they translated into. "2" means RW,
  448. "1" means RO.
  449. * HLSL constant/texture/structured/byte buffers: Check both ``Block``/``BufferBlock``
  450. and ``NonWritable`` decoration. If decorated with ``Block`` (``cbuffer`` &
  451. ``ConstantBuffer``), then RO; if decorated with ``BufferBlock`` and ``NonWritable``
  452. (``tbuffer``, ``TextureBuffer``, ``StructuredBuffer``), then RO; Otherwise, RW.
  453. HLSL Types
  454. ==========
  455. This section lists how various HLSL types are mapped.
  456. Normal scalar types
  457. -------------------
  458. `Normal scalar types <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509646(v=vs.85).aspx>`_
  459. in HLSL are relatively easy to handle and can be mapped directly to SPIR-V
  460. type instructions:
  461. ============================== ======================= ================== =========== =================================
  462. HLSL Command Line Option SPIR-V Capability Extension
  463. ============================== ======================= ================== =========== =================================
  464. ``bool`` ``OpTypeBool``
  465. ``int``/``int32_t`` ``OpTypeInt 32 1``
  466. ``int16_t`` ``-enable-16bit-types`` ``OpTypeInt 16 1`` ``Int16``
  467. ``uint``/``dword``/``uin32_t`` ``OpTypeInt 32 0``
  468. ``uint16_t`` ``-enable-16bit-types`` ``OpTypeInt 16 0`` ``Int16``
  469. ``half`` ``OpTypeFloat 32``
  470. ``half``/``float16_t`` ``-enable-16bit-types`` ``OpTypeFloat 16`` ``SPV_AMD_gpu_shader_half_float``
  471. ``float``/``float32_t`` ``OpTypeFloat 32``
  472. ``snorm float`` ``OpTypeFloat 32``
  473. ``unorm float`` ``OpTypeFloat 32``
  474. ``double``/``float64_t`` ``OpTypeFloat 64`` ``Float64``
  475. ============================== ======================= ================== =========== =================================
  476. Please note that ``half`` is translated into 32-bit floating point numbers
  477. if without ``-enable-16bit-types`` because MSDN says that "this data type
  478. is provided only for language compatibility. Direct3D 10 shader targets map
  479. all ``half`` data types to ``float`` data types."
  480. Minimal precision scalar types
  481. ------------------------------
  482. HLSL also supports various
  483. `minimal precision scalar types <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509646(v=vs.85).aspx>`_,
  484. which graphics drivers can implement by using any precision greater than or
  485. equal to their specified bit precision.
  486. There are no direct mappings in SPIR-V for these types. We translate them into
  487. the corresponding 16-bit or 32-bit scalar types with the ``RelaxedPrecision`` decoration.
  488. We use the 16-bit variants if '-enable-16bit-types' command line option is present.
  489. For more information on these types, please refer to:
  490. https://github.com/Microsoft/DirectXShaderCompiler/wiki/16-Bit-Scalar-Types
  491. ============== ======================= ================== ==================== ============ =================================
  492. HLSL Command Line Option SPIR-V Decoration Capability Extension
  493. ============== ======================= ================== ==================== ============ =================================
  494. ``min16float`` ``OpTypeFloat 32`` ``RelaxedPrecision``
  495. ``min10float`` ``OpTypeFloat 32`` ``RelaxedPrecision``
  496. ``min16int`` ``OpTypeInt 32 1`` ``RelaxedPrecision``
  497. ``min12int`` ``OpTypeInt 32 1`` ``RelaxedPrecision``
  498. ``min16uint`` ``OpTypeInt 32 0`` ``RelaxedPrecision``
  499. ``min16float`` ``-enable-16bit-types`` ``OpTypeFloat 16`` ``SPV_AMD_gpu_shader_half_float``
  500. ``min10float`` ``-enable-16bit-types`` ``OpTypeFloat 16`` ``SPV_AMD_gpu_shader_half_float``
  501. ``min16int`` ``-enable-16bit-types`` ``OpTypeInt 16 1`` ``Int16``
  502. ``min12int`` ``-enable-16bit-types`` ``OpTypeInt 16 1`` ``Int16``
  503. ``min16uint`` ``-enable-16bit-types`` ``OpTypeInt 16 0`` ``Int16``
  504. ============== ======================= ================== ==================== ============ =================================
  505. Vectors and matrices
  506. --------------------
  507. `Vectors <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509707(v=vs.85).aspx>`_
  508. and `matrices <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509623(v=vs.85).aspx>`_
  509. are translated into:
  510. ==================================== ====================================================
  511. HLSL SPIR-V
  512. ==================================== ====================================================
  513. ``|type|N`` (``N`` > 1) ``OpTypeVector |type| N``
  514. ``|type|1`` The scalar type for ``|type|``
  515. ``|type|MxN`` (``M`` > 1, ``N`` > 1) ``%v = OpTypeVector |type| N`` ``OpTypeMatrix %v M``
  516. ``|type|Mx1`` (``M`` > 1) ``OpTypeVector |type| M``
  517. ``|type|1xN`` (``N`` > 1) ``OpTypeVector |type| N``
  518. ``|type|1x1`` The scalar type for ``|type|``
  519. ==================================== ====================================================
  520. The above table is for float matrices.
  521. A MxN HLSL float matrix is translated into a SPIR-V matrix with M vectors, each with
  522. N elements. Conceptually HLSL matrices are row-major while SPIR-V matrices are
  523. column-major, thus all HLSL matrices are represented by their transposes.
  524. Doing so may require special handling of certain matrix operations:
  525. - **Indexing**: no special handling required. ``matrix[m][n]`` will still access
  526. the correct element since ``m``/``n`` means the ``m``-th/``n``-th row/column
  527. in HLSL but ``m``-th/``n``-th vector/element in SPIR-V.
  528. - **Per-element operation**: no special handling required.
  529. - **Matrix multiplication**: need to swap the operands. ``mat1 x mat2`` should
  530. be translated as ``transpose(mat2) x transpose(mat1)``. Then the result is
  531. ``transpose(mat1 x mat2)``.
  532. - **Storage layout**: ``row_major``/``column_major`` will be translated into
  533. SPIR-V ``ColMajor``/``RowMajor`` decoration. This is because HLSL matrix
  534. row/column becomes SPIR-V matrix column/row. If elements in a row/column are
  535. packed together, they should be loaded into a column/row correspondingly.
  536. See `Appendix A. Matrix Representation`_ for further explanation regarding these design choices.
  537. Since the ``Shader`` capability in SPIR-V does not allow to parameterize matrix
  538. types with non-floating-point types, a non-floating-point MxN matrix is translated
  539. into an array with M elements, with each element being a vector with N elements.
  540. Structs
  541. -------
  542. `Structs <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509668(v=vs.85).aspx>`_
  543. in HLSL are defined in the a format similar to C structs. They are translated
  544. into SPIR-V ``OpTypeStruct``. Depending on the storage classes of the instances,
  545. a single struct definition may generate multiple ``OpTypeStruct`` instructions
  546. in SPIR-V. For example, for the following HLSL source code:
  547. .. code:: hlsl
  548. struct S { ... }
  549. ConstantBuffer<S> myCBuffer;
  550. StructuredBuffer<S> mySBuffer;
  551. float4 main() : A {
  552. S myLocalVar;
  553. ...
  554. }
  555. There will be three different ``OpTypeStruct`` generated, one for each variable
  556. defined in the above source code. This is because the ``OpTypeStruct`` for
  557. both ``myCBuffer`` and ``mySBuffer`` will have layout decorations (``Offset``,
  558. ``MatrixStride``, ``ArrayStride``, ``RowMajor``, ``ColMajor``). However, their
  559. layout rules are different (by default); ``myCBuffer`` will use vector-relaxed
  560. OpenGL ``std140`` while ``mySBuffer`` will use vector-relaxed OpenGL ``std430``.
  561. ``myLocalVar`` will have its ``OpTypeStruct`` without layout decorations.
  562. Read more about storage classes in the `Constant/Texture/Structured/Byte Buffers`_
  563. section.
  564. Structs used as stage inputs/outputs will have semantics attached to their
  565. members. These semantics are handled in the `entry function wrapper`_.
  566. Structs used as pixel shader inputs can have optional interpolation modifiers
  567. for their members, which will be translated according to the following table:
  568. =========================== ================= =====================
  569. HLSL Interpolation Modifier SPIR-V Decoration SPIR-V Capability
  570. =========================== ================= =====================
  571. ``linear`` <none>
  572. ``centroid`` ``Centroid``
  573. ``nointerpolation`` ``Flat``
  574. ``noperspective`` ``NoPerspective``
  575. ``sample`` ``Sample`` ``SampleRateShading``
  576. =========================== ================= =====================
  577. Arrays
  578. ------
  579. Sized (either explicitly or implicitly) arrays are translated into SPIR-V
  580. `OpTypeArray`. Unsized arrays are translated into `OpTypeRuntimeArray`.
  581. Arrays, if used for external resources (residing in SPIR-V `Uniform` or
  582. `UniformConstant` storage class), will need layout decorations like SPIR-V
  583. `ArrayStride` decoration. For arrays of opaque types, e.g., HLSL textures
  584. or samplers, we don't decorate with `ArrayStride` decorations since there is
  585. no meaningful strides. Similarly for arrays of structured/byte buffers.
  586. User-defined types
  587. ------------------
  588. `User-defined types <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509702(v=vs.85).aspx>`_
  589. are type aliases introduced by typedef. No new types are introduced and we can
  590. rely on Clang to resolve to the original types.
  591. Samplers
  592. --------
  593. All `sampler types <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509644(v=vs.85).aspx>`_
  594. will be translated into SPIR-V ``OpTypeSampler``.
  595. SPIR-V ``OpTypeSampler`` is an opaque type that cannot be parameterized;
  596. therefore state assignments on sampler types is not supported (yet).
  597. Textures
  598. --------
  599. `Texture types <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509700(v=vs.85).aspx>`_
  600. are translated into SPIR-V ``OpTypeImage``, with parameters:
  601. ======================= ==================== ===== =================== ========== ===== ======= == ======= ================ =================
  602. HLSL Vulkan SPIR-V
  603. ----------------------- -------------------------- ------------------------------------------------------------------------------------------
  604. Texture Type Descriptor Type RO/RW Storage Class Dim Depth Arrayed MS Sampled Image Format Capability
  605. ======================= ==================== ===== =================== ========== ===== ======= == ======= ================ =================
  606. ``Texture1D`` Sampled Image RO ``UniformConstant`` ``1D`` 2 0 0 1 ``Unknown``
  607. ``Texture2D`` Sampled Image RO ``UniformConstant`` ``2D`` 2 0 0 1 ``Unknown``
  608. ``Texture3D`` Sampled Image RO ``UniformConstant`` ``3D`` 2 0 0 1 ``Unknown``
  609. ``TextureCube`` Sampled Image RO ``UniformConstant`` ``Cube`` 2 0 0 1 ``Unknown``
  610. ``Texture1DArray`` Sampled Image RO ``UniformConstant`` ``1D`` 2 1 0 1 ``Unknown``
  611. ``Texture2DArray`` Sampled Image RO ``UniformConstant`` ``2D`` 2 1 0 1 ``Unknown``
  612. ``Texture2DMS`` Sampled Image RO ``UniformConstant`` ``2D`` 2 0 1 1 ``Unknown``
  613. ``Texture2DMSArray`` Sampled Image RO ``UniformConstant`` ``2D`` 2 1 1 1 ``Unknown`` ``ImageMSArray``
  614. ``TextureCubeArray`` Sampled Image RO ``UniformConstant`` ``3D`` 2 1 0 1 ``Unknown``
  615. ``Buffer<T>`` Uniform Texel Buffer RO ``UniformConstant`` ``Buffer`` 2 0 0 1 Depends on ``T`` ``SampledBuffer``
  616. ``RWBuffer<T>`` Storage Texel Buffer RW ``UniformConstant`` ``Buffer`` 2 0 0 2 Depends on ``T`` ``SampledBuffer``
  617. ``RWTexture1D<T>`` Storage Image RW ``UniformConstant`` ``1D`` 2 0 0 2 Depends on ``T``
  618. ``RWTexture2D<T>`` Storage Image RW ``UniformConstant`` ``2D`` 2 0 0 2 Depends on ``T``
  619. ``RWTexture3D<T>`` Storage Image RW ``UniformConstant`` ``3D`` 2 0 0 2 Depends on ``T``
  620. ``RWTexture1DArray<T>`` Storage Image RW ``UniformConstant`` ``1D`` 2 1 0 2 Depends on ``T``
  621. ``RWTexture2DArray<T>`` Storage Image RW ``UniformConstant`` ``2D`` 2 1 0 2 Depends on ``T``
  622. ======================= ==================== ===== =================== ========== ===== ======= == ======= ================ =================
  623. The meanings of the headers in the above table is explained in ``OpTypeImage``
  624. of the SPIR-V spec.
  625. Constant/Texture/Structured/Byte Buffers
  626. ----------------------------------------
  627. There are serveral buffer types in HLSL:
  628. - ``cbuffer`` and ``ConstantBuffer``
  629. - ``tbuffer`` and ``TextureBuffer``
  630. - ``StructuredBuffer`` and ``RWStructuredBuffer``
  631. - ``AppendStructuredBuffer`` and ``ConsumeStructuredBuffer``
  632. - ``ByteAddressBuffer`` and ``RWByteAddressBuffer``
  633. Note that ``Buffer`` and ``RWBuffer`` are considered as texture object in HLSL.
  634. They are listed in the above section.
  635. Please see the following sections for the details of each type. As a summary:
  636. =========================== ================== ================================ ==================== =================
  637. HLSL Type Vulkan Buffer Type Default Memory Layout Rule SPIR-V Storage Class SPIR-V Decoration
  638. =========================== ================== ================================ ==================== =================
  639. ``cbuffer`` Uniform Buffer Vector-relaxed OpenGL ``std140`` ``Uniform`` ``Block``
  640. ``ConstantBuffer`` Uniform Buffer Vector-relaxed OpenGL ``std140`` ``Uniform`` ``Block``
  641. ``tbuffer`` Storage Buffer Vector-relaxed OpenGL ``std430`` ``Uniform`` ``BufferBlock``
  642. ``TextureBuffer`` Storage Buffer Vector-relaxed OpenGL ``std430`` ``Uniform`` ``BufferBlock``
  643. ``StructuredBuffer`` Storage Buffer Vector-relaxed OpenGL ``std430`` ``Uniform`` ``BufferBlock``
  644. ``RWStructuredBuffer`` Storage Buffer Vector-relaxed OpenGL ``std430`` ``Uniform`` ``BufferBlock``
  645. ``AppendStructuredBuffer`` Storage Buffer Vector-relaxed OpenGL ``std430`` ``Uniform`` ``BufferBlock``
  646. ``ConsumeStructuredBuffer`` Storage Buffer Vector-relaxed OpenGL ``std430`` ``Uniform`` ``BufferBlock``
  647. ``ByteAddressBuffer`` Storage Buffer Vector-relaxed OpenGL ``std430`` ``Uniform`` ``BufferBlock``
  648. ``RWByteAddressBuffer`` Storage Buffer Vector-relaxed OpenGL ``std430`` ``Uniform`` ``BufferBlock``
  649. =========================== ================== ================================ ==================== =================
  650. To know more about the Vulkan buffer types, please refer to the Vulkan spec
  651. `13.1 Descriptor Types <https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#descriptorsets-types>`_.
  652. Memory layout rules
  653. ~~~~~~~~~~~~~~~~~~~
  654. SPIR-V CodeGen supports four sets of memory layout rules for buffer resources
  655. right now:
  656. 1. Vector-relaxed OpenGL ``std140`` for uniform buffers and vector-relaxed
  657. OpenGL ``std430`` for storage buffers: these rules satisfy Vulkan `"Standard
  658. Uniform Buffer Layout" and "Standard Storage Buffer Layout" <https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#interfaces-resources-layout>`_,
  659. respectively.
  660. They are the default.
  661. 2. DirectX memory layout rules for uniform buffers and storage buffers:
  662. they allow packing data on the application side that can be shared with
  663. DirectX. They can be enabled by ``-fvk-use-dx-layout``.
  664. 3. Strict OpenGL ``std140`` for uniform buffers and strict OpenGL ``std430``
  665. for storage buffers: they allow packing data on the application side that
  666. can be shared with OpenGL. They can be enabled by ``-fvk-use-gl-layout``.
  667. 4. Scalar layout rules introduced via `VK_EXT_scalar_block_layout`, which
  668. basically aligns all aggregrate types according to their elements'
  669. natural alignment. They can be enabled by ``-fvk-use-scalar-layout``.
  670. To use scalar layout, the application side need to request
  671. ``VK_EXT_scalar_block_layout``. This is also true for using DirectX memory
  672. layout since there is no dedicated DirectX layout extension for Vulkan
  673. (at least for now). So we must request something more permissive.
  674. In the above, "vector-relaxed OpenGL ``std140``/``std430``" rules mean OpenGL
  675. ``std140``/``std430`` rules with the following modification for vector type
  676. alignment:
  677. 1. The alignment of a vector type is set to be the alignment of its element type
  678. 2. If the above causes an `improper straddle <https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#interfaces-resources-layout>`_,
  679. the alignment will be set to 16 bytes.
  680. As an exmaple, for the following HLSL definition:
  681. .. code:: hlsl
  682. struct S {
  683. float3 f;
  684. };
  685. struct T {
  686. float a_float;
  687. float3 b_float3;
  688. S c_S_float3;
  689. float2x3 d_float2x3;
  690. row_major float2x3 e_float2x3;
  691. int f_int_3[3];
  692. float2 g_float2_2[2];
  693. };
  694. We will have the following offsets for each member:
  695. ============== ====== ====== ====== ========== ====== ====== ====== ==========
  696. HLSL Uniform Buffer Storage Buffer
  697. -------------- ------------------------------- -------------------------------
  698. Member 1 (VK) 2 (DX) 3 (GL) 4 (Scalar) 1 (VK) 2 (DX) 3 (GL) 4 (Scalar)
  699. ============== ====== ====== ====== ========== ====== ====== ====== ==========
  700. ``a_float`` 0 0 0 0 0 0 0 0
  701. ``b_float3`` 4 4 16 4 4 4 16 4
  702. ``c_S_float3`` 16 16 32 16 16 16 32 16
  703. ``d_float2x3`` 32 32 48 28 32 28 48 28
  704. ``e_float2x3`` 80 80 96 52 64 52 80 52
  705. ``f_int_3`` 112 112 128 76 96 76 112 76
  706. ``g_float2_2`` 160 160 176 88 112 88 128 88
  707. ============== ====== ====== ====== ========== ====== ====== ====== ==========
  708. If the above layout rules do not satisfy your needs and you want to manually
  709. control the layout of struct members, you can use either
  710. * The native HLSL ``:packoffset()`` attribute: only available for cbuffers; or
  711. * The Vulkan-specific ``[[vk::offset()]]`` attribute: applies to all resources.
  712. ``[[vk::offset]]`` overrules ``:packoffset``. Attaching ``[[vk::offset]]``
  713. to a struct memeber affects all variables of the struct type in question. So
  714. sharing the same struct definition having ``[[vk::offset]]`` annotations means
  715. also sharing the layout.
  716. For global variables (which are collected into the ``$Globals`` cbuffer), you
  717. can use the native HLSL ``:register(c#)`` attribute. Note that ``[[vk::offset]]``
  718. and ``:packoffset`` cannot be applied to these variables.
  719. If ``register(cX)`` is used on any global variable, the offset for that variable
  720. is set to ``X * 16``, and the offset for all other global variables without the
  721. ``register(c#)`` annotation will be set to the next available address after
  722. the highest explicit address. For example:
  723. .. code:: hlsl
  724. float x : register(c10); // Offset = 160 (10 * 16)
  725. float y; // Offset = 164 (160 + 4)
  726. float z: register(c1); // Offset = 16 (1 * 16)
  727. These attributes give great flexibility but also responsibility to the
  728. developer; the compiler will just take in what is specified in the source code
  729. and emit it to SPIR-V with no error checking.
  730. ``cbuffer`` and ``ConstantBuffer``
  731. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  732. These two buffer types are treated as uniform buffers using Vulkan's
  733. terminology. They are translated into an ``OpTypeStruct`` with the
  734. necessary layout decorations (``Offset``, ``ArrayStride``, ``MatrixStride``,
  735. ``RowMajor``, ``ColMajor``) and the ``Block`` decoration. The layout rule
  736. used is vector-relaxed OpenGL ``std140`` (by default). A variable declared as
  737. one of these types will be placed in the ``Uniform`` storage class.
  738. For example, for the following HLSL source code:
  739. .. code:: hlsl
  740. struct T {
  741. float a;
  742. float3 b;
  743. };
  744. ConstantBuffer<T> myCBuffer;
  745. will be translated into
  746. .. code:: spirv
  747. ; Layout decoration
  748. OpMemberDecorate %type_ConstantBuffer_T 0 Offset 0
  749. OpMemberDecorate %type_ConstantBuffer_T 0 Offset 4
  750. ; Block decoration
  751. OpDecorate %type_ConstantBuffer_T Block
  752. ; Types
  753. %type_ConstantBuffer_T = OpTypeStruct %float %v3float
  754. %_ptr_Uniform_type_ConstantBuffer_T = OpTypePointer Uniform %type_ConstantBuffer_T
  755. ; Variable
  756. %myCbuffer = OpVariable %_ptr_Uniform_type_ConstantBuffer_T Uniform
  757. ``tbuffer`` and ``TextureBuffer``
  758. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  759. These two buffer types are treated as storage buffers using Vulkan's
  760. terminology. They are translated into an ``OpTypeStruct`` with the
  761. necessary layout decorations (``Offset``, ``ArrayStride``, ``MatrixStride``,
  762. ``RowMajor``, ``ColMajor``) and the ``BufferBlock`` decoration. All the struct
  763. members are also decorated with ``NonWritable`` decoration. The layout rule
  764. used is vector-relaxed OpenGL ``std430`` (by default). A variable declared as
  765. one of these types will be placed in the ``Uniform`` storage class.
  766. ``StructuredBuffer`` and ``RWStructuredBuffer``
  767. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  768. ``StructuredBuffer<T>``/``RWStructuredBuffer<T>`` is treated as storage buffer
  769. using Vulkan's terminology. It is translated into an ``OpTypeStruct`` containing
  770. an ``OpTypeRuntimeArray`` of type ``T``, with necessary layout decorations
  771. (``Offset``, ``ArrayStride``, ``MatrixStride``, ``RowMajor``, ``ColMajor``) and
  772. the ``BufferBlock`` decoration. The default layout rule used is vector-relaxed
  773. OpenGL ``std430``. A variable declared as one of these types will be placed in
  774. the ``Uniform`` storage class.
  775. For ``RWStructuredBuffer<T>``, each variable will have an associated counter
  776. variable generated. The counter variable will be of ``OpTypeStruct`` type, which
  777. only contains a 32-bit integer. The counter variable takes its own binding
  778. number. ``.IncrementCounter()``/``.DecrementCounter()`` will modify this counter
  779. variable.
  780. For example, for the following HLSL source code:
  781. .. code:: hlsl
  782. struct T {
  783. float a;
  784. float3 b;
  785. };
  786. StructuredBuffer<T> mySBuffer;
  787. will be translated into
  788. .. code:: spirv
  789. ; Layout decoration
  790. OpMemberDecorate %T 0 Offset 0
  791. OpMemberDecorate %T 1 Offset 4
  792. OpDecorate %_runtimearr_T ArrayStride 16
  793. OpMemberDecorate %type_StructuredBuffer_T 0 Offset 0
  794. OpMemberDecorate %type_StructuredBuffer_T 0 NoWritable
  795. ; BufferBlock decoration
  796. OpDecorate %type_StructuredBuffer_T BufferBlock
  797. ; Types
  798. %T = OpTypeStruct %float %v3float
  799. %_runtimearr_T = OpTypeRuntimeArray %T
  800. %type_StructuredBuffer_T = OpTypeStruct %_runtimearr_T
  801. %_ptr_Uniform_type_StructuredBuffer_T = OpTypePointer Uniform %type_StructuredBuffer_T
  802. ; Variable
  803. %myCbuffer = OpVariable %_ptr_Uniform_type_ConstantBuffer_T Uniform
  804. ``AppendStructuredBuffer`` and ``ConsumeStructuredBuffer``
  805. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  806. ``AppendStructuredBuffer<T>``/``ConsumeStructuredBuffer<T>`` is treated as
  807. storage buffer using Vulkan's terminology. It is translated into an
  808. ``OpTypeStruct`` containing an ``OpTypeRuntimeArray`` of type ``T``, with
  809. necessary layout decorations (``Offset``, ``ArrayStride``, ``MatrixStride``,
  810. ``RowMajor``, ``ColMajor``) and the ``BufferBlock`` decoration. The default
  811. layout rule used is vector-relaxed OpenGL ``std430``.
  812. A variable declared as one of these types will be placed in the ``Uniform``
  813. storage class. Besides, each variable will have an associated counter variable
  814. generated. The counter variable will be of ``OpTypeStruct`` type, which only
  815. contains a 32-bit integer. The integer is the total number of elements in the
  816. buffer. The counter variable takes its own binding number.
  817. ``.Append()``/``.Consume()`` will use the counter variable as the index and
  818. adjust it accordingly.
  819. For example, for the following HLSL source code:
  820. .. code:: hlsl
  821. struct T {
  822. float a;
  823. float3 b;
  824. };
  825. AppendStructuredBuffer<T> mySBuffer;
  826. will be translated into
  827. .. code:: spirv
  828. ; Layout decorations
  829. OpMemberDecorate %T 0 Offset 0
  830. OpMemberDecorate %T 1 Offset 4
  831. OpDecorate %_runtimearr_T ArrayStride 16
  832. OpMemberDecorate %type_AppendStructuredBuffer_T 0 Offset 0
  833. OpDecorate %type_AppendStructuredBuffer_T BufferBlock
  834. OpMemberDecorate %type_ACSBuffer_counter 0 Offset 0
  835. OpDecorate %type_ACSBuffer_counter BufferBlock
  836. ; Binding numbers
  837. OpDecorate %myASbuffer DescriptorSet 0
  838. OpDecorate %myASbuffer Binding 0
  839. OpDecorate %counter_var_myASbuffer DescriptorSet 0
  840. OpDecorate %counter_var_myASbuffer Binding 1
  841. ; Types
  842. %T = OpTypeStruct %float %v3float
  843. %_runtimearr_T = OpTypeRuntimeArray %T
  844. %type_AppendStructuredBuffer_T = OpTypeStruct %_runtimearr_T
  845. %_ptr_Uniform_type_AppendStructuredBuffer_T = OpTypePointer Uniform %type_AppendStructuredBuffer_T
  846. %type_ACSBuffer_counter = OpTypeStruct %int
  847. %_ptr_Uniform_type_ACSBuffer_counter = OpTypePointer Uniform %type_ACSBuffer_counter
  848. ; Variables
  849. %myASbuffer = OpVariable %_ptr_Uniform_type_AppendStructuredBuffer_T Uniform
  850. %counter_var_myASbuffer = OpVariable %_ptr_Uniform_type_ACSBuffer_counter Uniform
  851. ``ByteAddressBuffer`` and ``RWByteAddressBuffer``
  852. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  853. ``ByteAddressBuffer``/``RWByteAddressBuffer`` is treated as storage buffer using
  854. Vulkan's terminology. It is translated into an ``OpTypeStruct`` containing an
  855. ``OpTypeRuntimeArray`` of 32-bit unsigned integers, with ``BufferBlock``
  856. decoration.
  857. A variable declared as one of these types will be placed in the ``Uniform``
  858. storage class.
  859. For example, for the following HLSL source code:
  860. .. code:: hlsl
  861. ByteAddressBuffer myBuffer1;
  862. RWByteAddressBuffer myBuffer2;
  863. will be translated into
  864. .. code:: spirv
  865. ; Layout decorations
  866. OpDecorate %_runtimearr_uint ArrayStride 4
  867. OpDecorate %type_ByteAddressBuffer BufferBlock
  868. OpMemberDecorate %type_ByteAddressBuffer 0 Offset 0
  869. OpMemberDecorate %type_ByteAddressBuffer 0 NonWritable
  870. OpDecorate %type_RWByteAddressBuffer BufferBlock
  871. OpMemberDecorate %type_RWByteAddressBuffer 0 Offset 0
  872. ; Types
  873. %_runtimearr_uint = OpTypeRuntimeArray %uint
  874. %type_ByteAddressBuffer = OpTypeStruct %_runtimearr_uint
  875. %_ptr_Uniform_type_ByteAddressBuffer = OpTypePointer Uniform %type_ByteAddressBuffer
  876. %type_RWByteAddressBuffer = OpTypeStruct %_runtimearr_uint
  877. %_ptr_Uniform_type_RWByteAddressBuffer = OpTypePointer Uniform %type_RWByteAddressBuffer
  878. ; Variables
  879. %myBuffer1 = OpVariable %_ptr_Uniform_type_ByteAddressBuffer Uniform
  880. %myBuffer2 = OpVariable %_ptr_Uniform_type_RWByteAddressBuffer Uniform
  881. HLSL Variables and Resources
  882. ============================
  883. This section lists how various HLSL variables and resources are mapped.
  884. According to `Shader Constants <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509581(v=vs.85).aspx>`_,
  885. There are two default constant buffers available, $Global and $Param. Variables
  886. that are placed in the global scope are added implicitly to the $Global cbuffer,
  887. using the same packing method that is used for cbuffers. Uniform parameters in
  888. the parameter list of a function appear in the $Param constant buffer when a
  889. shader is compiled outside of the effects framework.
  890. So all global externally-visible non-resource-type stand-alone variables will
  891. be collected into a cbuffer named as ``$Globals``, no matter whether they are
  892. statically referenced by the entry point or not. The ``$Globals`` cbuffer
  893. follows the layout rules like normal cbuffer.
  894. Storage class
  895. -------------
  896. Normal local variables (without any modifier) will be placed in the ``Function``
  897. SPIR-V storage class. Normal global variables (without any modifer) will be
  898. placed in the ``Uniform`` or ``UniformConstant`` storage class.
  899. - ``static``
  900. - Global variables with ``static`` modifier will be placed in the ``Private``
  901. SPIR-V storage class. Initalizers of such global variables will be translated
  902. into SPIR-V ``OpVariable`` initializers if possible; otherwise, they will be
  903. initialized at the very beginning of the `entry function wrapper`_ using
  904. SPIR-V ``OpStore``.
  905. - Local variables with ``static`` modifier will also be placed in the
  906. ``Private`` SPIR-V storage class. initializers of such local variables will
  907. also be translated into SPIR-V ``OpVariable`` initializers if possible;
  908. otherwise, they will be initialized at the very beginning of the enclosing
  909. function. To make sure that such a local variable is only initialized once,
  910. a second boolean variable of the ``Private`` SPIR-V storage class will be
  911. generated to mark its initialization status.
  912. - ``groupshared``
  913. - Global variables with ``groupshared`` modifier will be placed in the
  914. ``Workgroup`` storage class.
  915. - Note that this modifier overrules ``static``; if both ``groupshared`` and
  916. ``static`` are applied to a variable, ``static`` will be ignored.
  917. - ``uinform``
  918. - This does not affect codegen. Variables will be treated like normal global
  919. variables.
  920. - ``extern``
  921. - This does not affect codegen. Variables will be treated like normal global
  922. variables.
  923. - ``shared``
  924. - This is a hint to the compiler. It will be ingored.
  925. - ``volatile``
  926. - This is a hint to the compiler. It will be ingored.
  927. HLSL semantic and Vulkan ``Location``
  928. -------------------------------------
  929. Direct3D uses HLSL "`semantics <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx>`_"
  930. to compose and match the interfaces between subsequent stages. These semantic
  931. strings can appear after struct members, function parameters and return
  932. values. E.g.,
  933. .. code:: hlsl
  934. struct VSInput {
  935. float4 pos : POSITION;
  936. float3 norm : NORMAL;
  937. };
  938. float4 VSMain(in VSInput input,
  939. in float4 tex : TEXCOORD,
  940. out float4 pos : SV_Position) : TEXCOORD {
  941. pos = input.pos;
  942. return tex;
  943. }
  944. In contrary, Vulkan stage input and output interface matching is via explicit
  945. ``Location`` numbers. Details can be found `here <https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#interfaces-iointerfaces>`_.
  946. To translate HLSL to SPIR-V for Vulkan, semantic strings need to be mapped to
  947. Vulkan ``Location`` numbers properly. This can be done either explicitly via
  948. information provided by the developer or implicitly by the compiler.
  949. Explicit ``Location`` number assignment
  950. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  951. ``[[vk::location(X)]]`` can be attached to the entities where semantic are
  952. allowed to attach (struct fields, function parameters, and function returns).
  953. For the above exmaple we can have:
  954. .. code:: hlsl
  955. struct VSInput {
  956. [[vk::location(0)]] float4 pos : POSITION;
  957. [[vk::location(1)]] float3 norm : NORMAL;
  958. };
  959. [[vk::location(1)]]
  960. float4 VSMain(in VSInput input,
  961. [[vk::location(2)]]
  962. in float4 tex : TEXCOORD,
  963. out float4 pos : SV_Position) : TEXCOORD {
  964. pos = input.pos;
  965. return tex;
  966. }
  967. In the above, input ``POSITION``, ``NORMAL``, and ``TEXCOORD`` will be mapped to
  968. ``Location`` 0, 1, and 2, respectively, and output ``TEXCOORD`` will be mapped
  969. to ``Location`` 1.
  970. [TODO] Another explicit way: using command-line options
  971. Please note that the compiler prohibits mixing the explicit and implicit
  972. approach for the same SigPoint to avoid complexity and fallibility. However,
  973. for a certain shader stage, one SigPoint using the explicit approach while the
  974. other adopting the implicit approach is permitted.
  975. Implicit ``Location`` number assignment
  976. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  977. Without hints from the developer, the compiler will try its best to map
  978. semantics to ``Location`` numbers. However, there is no single rule for this
  979. mapping; semantic strings should be handled case by case.
  980. Firstly, under certain `SigPoints <https://github.com/Microsoft/DirectXShaderCompiler/blob/master/docs/DXIL.rst#hlsl-signatures-and-semantics>`_,
  981. some system-value (SV) semantic strings will be translated into SPIR-V
  982. ``BuiltIn`` decorations:
  983. .. table:: Mapping from HLSL SV semantic to SPIR-V builtin and execution mode
  984. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  985. | HLSL Semantic | SigPoint | SPIR-V ``BuiltIn`` | SPIR-V Execution Mode | SPIR-V Capability |
  986. +===========================+=============+==========================+=======================+=============================+
  987. | | VSOut | ``Position`` | N/A | ``Shader`` |
  988. | +-------------+--------------------------+-----------------------+-----------------------------+
  989. | | HSCPIn | ``Position`` | N/A | ``Shader`` |
  990. | +-------------+--------------------------+-----------------------+-----------------------------+
  991. | | HSCPOut | ``Position`` | N/A | ``Shader`` |
  992. | +-------------+--------------------------+-----------------------+-----------------------------+
  993. | | DSCPIn | ``Position`` | N/A | ``Shader`` |
  994. | +-------------+--------------------------+-----------------------+-----------------------------+
  995. | SV_Position | DSOut | ``Position`` | N/A | ``Shader`` |
  996. | +-------------+--------------------------+-----------------------+-----------------------------+
  997. | | GSVIn | ``Position`` | N/A | ``Shader`` |
  998. | +-------------+--------------------------+-----------------------+-----------------------------+
  999. | | GSOut | ``Position`` | N/A | ``Shader`` |
  1000. | +-------------+--------------------------+-----------------------+-----------------------------+
  1001. | | PSIn | ``FragCoord`` | N/A | ``Shader`` |
  1002. | +-------------+--------------------------+-----------------------+-----------------------------+
  1003. | | MSOut | ``Position`` | N/A | ``Shader`` |
  1004. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1005. | | VSOut | ``ClipDistance`` | N/A | ``ClipDistance`` |
  1006. | +-------------+--------------------------+-----------------------+-----------------------------+
  1007. | | HSCPIn | ``ClipDistance`` | N/A | ``ClipDistance`` |
  1008. | +-------------+--------------------------+-----------------------+-----------------------------+
  1009. | | HSCPOut | ``ClipDistance`` | N/A | ``ClipDistance`` |
  1010. | +-------------+--------------------------+-----------------------+-----------------------------+
  1011. | | DSCPIn | ``ClipDistance`` | N/A | ``ClipDistance`` |
  1012. | +-------------+--------------------------+-----------------------+-----------------------------+
  1013. | SV_ClipDistance | DSOut | ``ClipDistance`` | N/A | ``ClipDistance`` |
  1014. | +-------------+--------------------------+-----------------------+-----------------------------+
  1015. | | GSVIn | ``ClipDistance`` | N/A | ``ClipDistance`` |
  1016. | +-------------+--------------------------+-----------------------+-----------------------------+
  1017. | | GSOut | ``ClipDistance`` | N/A | ``ClipDistance`` |
  1018. | +-------------+--------------------------+-----------------------+-----------------------------+
  1019. | | PSIn | ``ClipDistance`` | N/A | ``ClipDistance`` |
  1020. | +-------------+--------------------------+-----------------------+-----------------------------+
  1021. | | MSOut | ``ClipDistance`` | N/A | ``ClipDistance`` |
  1022. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1023. | | VSOut | ``CullDistance`` | N/A | ``CullDistance`` |
  1024. | +-------------+--------------------------+-----------------------+-----------------------------+
  1025. | | HSCPIn | ``CullDistance`` | N/A | ``CullDistance`` |
  1026. | +-------------+--------------------------+-----------------------+-----------------------------+
  1027. | | HSCPOut | ``CullDistance`` | N/A | ``CullDistance`` |
  1028. | +-------------+--------------------------+-----------------------+-----------------------------+
  1029. | | DSCPIn | ``CullDistance`` | N/A | ``CullDistance`` |
  1030. | +-------------+--------------------------+-----------------------+-----------------------------+
  1031. | SV_CullDistance | DSOut | ``CullDistance`` | N/A | ``CullDistance`` |
  1032. | +-------------+--------------------------+-----------------------+-----------------------------+
  1033. | | GSVIn | ``CullDistance`` | N/A | ``CullDistance`` |
  1034. | +-------------+--------------------------+-----------------------+-----------------------------+
  1035. | | GSOut | ``CullDistance`` | N/A | ``CullDistance`` |
  1036. | +-------------+--------------------------+-----------------------+-----------------------------+
  1037. | | PSIn | ``CullDistance`` | N/A | ``CullDistance`` |
  1038. | +-------------+--------------------------+-----------------------+-----------------------------+
  1039. | | MSOut | ``CullDistance`` | N/A | ``CullDistance`` |
  1040. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1041. | SV_VertexID | VSIn | ``VertexIndex`` | N/A | ``Shader`` |
  1042. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1043. | SV_InstanceID | VSIn | ``InstanceIndex`` | N/A | ``Shader`` |
  1044. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1045. | SV_Depth | PSOut | ``FragDepth`` | N/A | ``Shader`` |
  1046. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1047. | SV_DepthGreaterEqual | PSOut | ``FragDepth`` | ``DepthGreater`` | ``Shader`` |
  1048. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1049. | SV_DepthLessEqual | PSOut | ``FragDepth`` | ``DepthLess`` | ``Shader`` |
  1050. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1051. | SV_IsFrontFace | PSIn | ``FrontFacing`` | N/A | ``Shader`` |
  1052. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1053. | | CSIn | ``GlobalInvocationId`` | N/A | ``Shader`` |
  1054. | +-------------+--------------------------+-----------------------+-----------------------------+
  1055. | SV_DispatchThreadID | MSIn | ``GlobalInvocationId`` | N/A | ``Shader`` |
  1056. | +-------------+--------------------------+-----------------------+-----------------------------+
  1057. | | ASIn | ``GlobalInvocationId`` | N/A | ``Shader`` |
  1058. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1059. | | CSIn | ``WorkgroupId`` | N/A | ``Shader`` |
  1060. | +-------------+--------------------------+-----------------------+-----------------------------+
  1061. | SV_GroupID | MSIn | ``WorkgroupId`` | N/A | ``Shader`` |
  1062. | +-------------+--------------------------+-----------------------+-----------------------------+
  1063. | | ASIn | ``WorkgroupId`` | N/A | ``Shader`` |
  1064. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1065. | | CSIn | ``LocalInvocationId`` | N/A | ``Shader`` |
  1066. | +-------------+--------------------------+-----------------------+-----------------------------+
  1067. | SV_GroupThreadID | MSIn | ``LocalInvocationId`` | N/A | ``Shader`` |
  1068. | +-------------+--------------------------+-----------------------+-----------------------------+
  1069. | | ASIn | ``LocalInvocationId`` | N/A | ``Shader`` |
  1070. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1071. | | CSIn | ``LocalInvocationIndex`` | N/A | ``Shader`` |
  1072. | +-------------+--------------------------+-----------------------+-----------------------------+
  1073. | SV_GroupIndex | MSIn | ``LocalInvocationIndex`` | N/A | ``Shader`` |
  1074. | +-------------+--------------------------+-----------------------+-----------------------------+
  1075. | | ASIn | ``LocalInvocationIndex`` | N/A | ``Shader`` |
  1076. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1077. | SV_OutputControlPointID | HSIn | ``InvocationId`` | N/A | ``Tessellation`` |
  1078. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1079. | SV_GSInstanceID | GSIn | ``InvocationId`` | N/A | ``Geometry`` |
  1080. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1081. | SV_DomainLocation | DSIn | ``TessCoord`` | N/A | ``Tessellation`` |
  1082. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1083. | | HSIn | ``PrimitiveId`` | N/A | ``Tessellation`` |
  1084. | +-------------+--------------------------+-----------------------+-----------------------------+
  1085. | | PCIn | ``PrimitiveId`` | N/A | ``Tessellation`` |
  1086. | +-------------+--------------------------+-----------------------+-----------------------------+
  1087. | | DsIn | ``PrimitiveId`` | N/A | ``Tessellation`` |
  1088. | +-------------+--------------------------+-----------------------+-----------------------------+
  1089. | SV_PrimitiveID | GSIn | ``PrimitiveId`` | N/A | ``Geometry`` |
  1090. | +-------------+--------------------------+-----------------------+-----------------------------+
  1091. | | GSOut | ``PrimitiveId`` | N/A | ``Geometry`` |
  1092. | +-------------+--------------------------+-----------------------+-----------------------------+
  1093. | | PSIn | ``PrimitiveId`` | N/A | ``Geometry`` |
  1094. | +-------------+--------------------------+-----------------------+-----------------------------+
  1095. | | MSOut | ``PrimitiveId`` | N/A | ``MeshShadingNV`` |
  1096. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1097. | | PCOut | ``TessLevelOuter`` | N/A | ``Tessellation`` |
  1098. | SV_TessFactor +-------------+--------------------------+-----------------------+-----------------------------+
  1099. | | DSIn | ``TessLevelOuter`` | N/A | ``Tessellation`` |
  1100. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1101. | | PCOut | ``TessLevelInner`` | N/A | ``Tessellation`` |
  1102. | SV_InsideTessFactor +-------------+--------------------------+-----------------------+-----------------------------+
  1103. | | DSIn | ``TessLevelInner`` | N/A | ``Tessellation`` |
  1104. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1105. | SV_SampleIndex | PSIn | ``SampleId`` | N/A | ``SampleRateShading`` |
  1106. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1107. | SV_StencilRef | PSOut | ``FragStencilRefEXT`` | N/A | ``StencilExportEXT`` |
  1108. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1109. | SV_Barycentrics | PSIn | ``BaryCoord*AMD`` | N/A | ``Shader`` |
  1110. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1111. | | GSOut | ``Layer`` | N/A | ``Geometry`` |
  1112. | +-------------+--------------------------+-----------------------+-----------------------------+
  1113. | SV_RenderTargetArrayIndex | PSIn | ``Layer`` | N/A | ``Geometry`` |
  1114. | +-------------+--------------------------+-----------------------+-----------------------------+
  1115. | | MSOut | ``Layer`` | N/A | ``MeshShadingNV`` |
  1116. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1117. | | GSOut | ``ViewportIndex`` | N/A | ``MultiViewport`` |
  1118. | +-------------+--------------------------+-----------------------+-----------------------------+
  1119. | SV_ViewportArrayIndex | PSIn | ``ViewportIndex`` | N/A | ``MultiViewport`` |
  1120. | +-------------+--------------------------+-----------------------+-----------------------------+
  1121. | | MSOut | ``ViewportIndex`` | N/A | ``MeshShadingNV`` |
  1122. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1123. | | PSIn | ``SampleMask`` | N/A | ``Shader`` |
  1124. | SV_Coverage +-------------+--------------------------+-----------------------+-----------------------------+
  1125. | | PSOut | ``SampleMask`` | N/A | ``Shader`` |
  1126. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1127. | SV_InnerCoverage | PSIn | ``FullyCoveredEXT`` | N/A | ``FragmentFullyCoveredEXT`` |
  1128. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1129. | | VSIn | ``ViewIndex`` | N/A | ``MultiView`` |
  1130. | +-------------+--------------------------+-----------------------+-----------------------------+
  1131. | | HSIn | ``ViewIndex`` | N/A | ``MultiView`` |
  1132. | +-------------+--------------------------+-----------------------+-----------------------------+
  1133. | | DSIn | ``ViewIndex`` | N/A | ``MultiView`` |
  1134. | SV_ViewID +-------------+--------------------------+-----------------------+-----------------------------+
  1135. | | GSIn | ``ViewIndex`` | N/A | ``MultiView`` |
  1136. | +-------------+--------------------------+-----------------------+-----------------------------+
  1137. | | PSIn | ``ViewIndex`` | N/A | ``MultiView`` |
  1138. | +-------------+--------------------------+-----------------------+-----------------------------+
  1139. | | MSIn | ``ViewIndex`` | N/A | ``MultiView`` |
  1140. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1141. | SV_ShadingRate | PSIn | ``FragSizeEXT`` | N/A | ``FragmentDensityEXT`` |
  1142. +---------------------------+-------------+--------------------------+-----------------------+-----------------------------+
  1143. For entities (function parameters, function return values, struct fields) with
  1144. the above SV semantic strings attached, SPIR-V variables of the
  1145. ``Input``/``Output`` storage class will be created. They will have the
  1146. corresponding SPIR-V ``Builtin`` decorations according to the above table.
  1147. SV semantic strings not translated into SPIR-V ``BuiltIn`` decorations will be
  1148. handled similarly as non-SV (arbitrary) semantic strings: a SPIR-V variable
  1149. of the ``Input``/``Output`` storage class will be created for each entity with
  1150. such semantic string. Then sort all semantic strings according to declaration
  1151. (the default, or if ``-fvk-stage-io-order=decl`` is given) or alphabetical
  1152. (if ``-fvk-stage-io-order=alpha`` is given) order, and assign ``Location``
  1153. numbers sequentially to the corresponding SPIR-V variables. Note that this means
  1154. flattening all structs if structs are used as function parameters or returns.
  1155. There is an exception to the above rule for SV_Target[N]. It will always be
  1156. mapped to ``Location`` number N.
  1157. ``ClipDistance & CullDistance``
  1158. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1159. Variables decorated with ``SV_ClipDistanceX`` can be float or vector of float
  1160. type. To map them into one float array in the struct, we firstly sort them
  1161. asecendingly according to ``X``, and then concatenate them tightly. For example,
  1162. .. code:: hlsl
  1163. struct T {
  1164. float clip0: SV_ClipDistance0,
  1165. };
  1166. struct S {
  1167. float3 clip5: SV_ClipDistance5;
  1168. ...
  1169. };
  1170. void main(T t, S s, float2 clip2 : SV_ClipDistance2) { ... }
  1171. Then we have an float array of size (1 + 2 + 3 =) 6 for ``ClipDistance``, with
  1172. ``clip0`` at offset 0, ``clip2`` at offset 1, ``clip5`` at offset 3.
  1173. Decorating a variable or struct member with the ``ClipDistance`` builtin but not
  1174. requiring the ``ClipDistance`` capability is legal as long as we don't read or
  1175. write the variable or struct member. But as per the way we handle `shader entry
  1176. function`_, this is not satisfied because we need to read their contents to
  1177. prepare for the source code entry function call or write back them after the
  1178. call. So annotating a variable or struct member with ``SV_ClipDistanceX`` means
  1179. requiring the ``ClipDistance`` capability in the generated SPIR-V.
  1180. Variables decorated with ``SV_CullDistanceX`` are mapped similarly as above.
  1181. HLSL register and Vulkan binding
  1182. --------------------------------
  1183. In shaders for DirectX, resources are accessed via registers; while in shaders
  1184. for Vulkan, it is done via descriptor set and binding numbers. The developer
  1185. can explicitly annotate variables in HLSL to specify descriptor set and binding
  1186. numbers, or leave it to the compiler to derive implicitly from registers.
  1187. Explicit binding number assignment
  1188. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1189. ``[[vk::binding(X[, Y])]]`` can be attached to global variables to specify the
  1190. descriptor set as ``Y`` and binding number as ``X``. The descriptor set number
  1191. is optional; if missing, it will be zero (If ``-auto-binding-space N`` command
  1192. line option is used, then descriptor set #N will be used instead of descriptor
  1193. set #0). RW/append/consume structured buffers have associated counters, which
  1194. will occupy their own Vulkan descriptors. ``[vk::counter_binding(Z)]`` can be
  1195. attached to a RW/append/consume structured buffers to specify the binding number
  1196. for the associated counter to ``Z``. Note that the set number of the counter is
  1197. always the same as the main buffer.
  1198. Implicit binding number assignment
  1199. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1200. Without explicit annotations, the compiler will try to deduce descriptor sets
  1201. and binding numbers in the following way:
  1202. If there is ``:register(xX, spaceY)`` specified for the given global variable,
  1203. the corresponding resource will be assigned to descriptor set ``Y`` and binding
  1204. number ``X``, regardless of the register type ``x``. Note that this will cause
  1205. binding number collision if, say, two resources are of different register
  1206. type but the same register number. To solve this problem, four command-line
  1207. options, ``-fvk-b-shift N M``, ``-fvk-s-shift N M``, ``-fvk-t-shift N M``, and
  1208. ``-fvk-u-shift N M``, are provided to shift by ``N`` all binding numbers
  1209. inferred for register type ``b``, ``s``, ``t``, and ``u`` in space ``M``,
  1210. respectively.
  1211. If there is no register specification, the corresponding resource will be
  1212. assigned to the next available binding number, starting from 0, in descriptor
  1213. set #0 (If ``-auto-binding-space N`` command line option is used, then
  1214. descriptor set #N will be used instead of descriptor set #0).
  1215. Summary
  1216. ~~~~~~~
  1217. In summary, the compiler essentially assigns binding numbers in three passes.
  1218. - Firstly it handles all declarations with explicit ``[[vk::binding(X[, Y])]]``
  1219. annotation.
  1220. - Then the compiler processes all remaining declarations with
  1221. ``:register(xX, spaceY)`` annotation, by applying the shift passed in using
  1222. command-line option ``-fvk-{b|s|t|u}-shift N M``, if provided.
  1223. - Finally, the compiler assigns next available binding numbers to the rest in
  1224. the declaration order.
  1225. As an example, for the following code:
  1226. .. code:: hlsl
  1227. struct S { ... };
  1228. ConstantBuffer<S> cbuffer1 : register(b0);
  1229. Texture2D<float4> texture1 : register(t0);
  1230. Texture2D<float4> texture2 : register(t1, space1);
  1231. SamplerState sampler1;
  1232. [[vk::binding(3)]]
  1233. RWBuffer<float4> rwbuffer1 : register(u5, space2);
  1234. If we compile with ``-fvk-t-shift 10 0 -fvk-t-shift 20 1``:
  1235. - ``rwbuffer1`` will take binding #3 in set #0, since explicit binding
  1236. assignment has precedence over the rest.
  1237. - ``cbuffer1`` will take binding #0 in set #0, since that's what deduced from
  1238. the register assignment, and there is no shift requested from command line.
  1239. - ``texture1`` will take binding #10 in set #0, and ``texture2`` will take
  1240. binding #21 in set #1, since we requested an 10 shift on t-type registers.
  1241. - ``sampler1`` will take binding 1 in set #0, since that's the next available
  1242. binding number in set #0.
  1243. HLSL global variables and Vulkan binding
  1244. ----------------------------------------
  1245. As mentioned above, all global externally-visible non-resource-type stand-alone
  1246. variables will be collected into a cbuffer named ``$Globals``. By default,
  1247. the ``$Globals`` cbuffer is placed in descriptor set #0, and the binding number
  1248. would be the next available binding number in that set. Meaning, the binding number
  1249. depends on where the very first global variable is in the code.
  1250. Example 1:
  1251. .. code:: hlsl
  1252. float4 someColors;
  1253. // $Globals cbuffer placed at DescriptorSet #0, Binding #0
  1254. Texture2D<float4> texture1;
  1255. // texture1 placed at DescriptorSet #0, Binding #1
  1256. Example 2:
  1257. .. code:: hlsl
  1258. Texture2D<float4> texture1;
  1259. // texture1 placed at DescriptorSet #0, Binding #0
  1260. float4 someColors;
  1261. // $Globals cbuffer placed at DescriptorSet #0, Binding #1
  1262. In order provide more control over the descriptor set and binding number of the
  1263. ``$Globals`` cbuffer, you can use the ``-fvk-bind-globals B S`` command line
  1264. option, which will place this cbuffer at descriptor set ``S``, and binding number ``B``.
  1265. Example 3: (compiled with ``-fvk-bind-globals 2 1``)
  1266. .. code:: hlsl
  1267. Texture2D<float4> texture1;
  1268. // texture1 placed at DescriptorSet #0, Binding #0
  1269. float4 someColors;
  1270. // $Globals cbuffer placed at DescriptorSet #1, Binding #2
  1271. Note that if the developer chooses to use this command line option, it is their
  1272. responsibility to provide proper numbers and avoid binding overlaps.
  1273. HLSL Expressions
  1274. ================
  1275. Unless explicitly noted, matrix per-element operations will be conducted on
  1276. each component vector and then collected into the result matrix. The following
  1277. sections lists the SPIR-V opcodes for scalars and vectors.
  1278. Arithmetic operators
  1279. --------------------
  1280. `Arithmetic operators <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509631(v=vs.85).aspx#Additive_and_Multiplicative_Operators>`_
  1281. (``+``, ``-``, ``*``, ``/``, ``%``) are translated into their corresponding
  1282. SPIR-V opcodes according to the following table.
  1283. +-------+-----------------------------+-------------------------------+--------------------+
  1284. | | (Vector of) Signed Integers | (Vector of) Unsigned Integers | (Vector of) Floats |
  1285. +=======+=============================+===============================+====================+
  1286. | ``+`` | ``OpIAdd`` | ``OpFAdd`` |
  1287. +-------+-------------------------------------------------------------+--------------------+
  1288. | ``-`` | ``OpISub`` | ``OpFSub`` |
  1289. +-------+-------------------------------------------------------------+--------------------+
  1290. | ``*`` | ``OpIMul`` | ``OpFMul`` |
  1291. +-------+-----------------------------+-------------------------------+--------------------+
  1292. | ``/`` | ``OpSDiv`` | ``OpUDiv`` | ``OpFDiv`` |
  1293. +-------+-----------------------------+-------------------------------+--------------------+
  1294. | ``%`` | ``OpSRem`` | ``OpUMod`` | ``OpFRem`` |
  1295. +-------+-----------------------------+-------------------------------+--------------------+
  1296. Note that for modulo operation, SPIR-V has two sets of instructions: ``Op*Rem``
  1297. and ``Op*Mod``. For ``Op*Rem``, the sign of a non-0 result comes from the first
  1298. operand; while for ``Op*Mod``, the sign of a non-0 result comes from the second
  1299. operand. HLSL doc does not mandate which set of instructions modulo operations
  1300. should be translated into; it only says "the % operator is defined only in cases
  1301. where either both sides are positive or both sides are negative." So technically
  1302. it's undefined behavior to use the modulo operation with operands of different
  1303. signs. But considering HLSL's C heritage and the behavior of Clang frontend, we
  1304. translate modulo operators into ``Op*Rem`` (there is no ``OpURem``).
  1305. For multiplications of float vectors and float scalars, the dedicated SPIR-V
  1306. operation ``OpVectorTimesScalar`` will be used. Similarly, for multiplications
  1307. of float matrices and float scalars, ``OpMatrixTimesScalar`` will be generated.
  1308. Bitwise operators
  1309. -----------------
  1310. `Bitwise operators <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509631(v=vs.85).aspx#Bitwise_Operators>`_
  1311. (``~``, ``&``, ``|``, ``^``, ``<<``, ``>>``) are translated into their
  1312. corresponding SPIR-V opcodes according to the following table.
  1313. +--------+-----------------------------+-------------------------------+
  1314. | | (Vector of) Signed Integers | (Vector of) Unsigned Integers |
  1315. +========+=============================+===============================+
  1316. | ``~`` | ``OpNot`` |
  1317. +--------+-------------------------------------------------------------+
  1318. | ``&`` | ``OpBitwiseAnd`` |
  1319. +--------+-------------------------------------------------------------+
  1320. | ``|`` | ``OpBitwiseOr`` |
  1321. +--------+-----------------------------+-------------------------------+
  1322. | ``^`` | ``OpBitwiseXor`` |
  1323. +--------+-----------------------------+-------------------------------+
  1324. | ``<<`` | ``OpShiftLeftLogical`` |
  1325. +--------+-----------------------------+-------------------------------+
  1326. | ``>>`` | ``OpShiftRightArithmetic`` | ``OpShiftRightLogical`` |
  1327. +--------+-----------------------------+-------------------------------+
  1328. Note that for ``<<``/``>>``, the right hand side will be culled: only the ``n``
  1329. - 1 least significant bits are considered, where ``n`` is the bitwidth of the
  1330. left hand side.
  1331. Comparison operators
  1332. --------------------
  1333. `Comparison operators <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509631(v=vs.85).aspx#Comparison_Operators>`_
  1334. (``<``, ``<=``, ``>``, ``>=``, ``==``, ``!=``) are translated into their
  1335. corresponding SPIR-V opcodes according to the following table.
  1336. +--------+-----------------------------+-------------------------------+------------------------------+
  1337. | | (Vector of) Signed Integers | (Vector of) Unsigned Integers | (Vector of) Floats |
  1338. +========+=============================+===============================+==============================+
  1339. | ``<`` | ``OpSLessThan`` | ``OpULessThan`` | ``OpFOrdLessThan`` |
  1340. +--------+-----------------------------+-------------------------------+------------------------------+
  1341. | ``<=`` | ``OpSLessThanEqual`` | ``OpULessThanEqual`` | ``OpFOrdLessThanEqual`` |
  1342. +--------+-----------------------------+-------------------------------+------------------------------+
  1343. | ``>`` | ``OpSGreaterThan`` | ``OpUGreaterThan`` | ``OpFOrdGreaterThan`` |
  1344. +--------+-----------------------------+-------------------------------+------------------------------+
  1345. | ``>=`` | ``OpSGreaterThanEqual`` | ``OpUGreaterThanEqual`` | ``OpFOrdGreaterThanEqual`` |
  1346. +--------+-----------------------------+-------------------------------+------------------------------+
  1347. | ``==`` | ``OpIEqual`` | ``OpFOrdEqual`` |
  1348. +--------+-------------------------------------------------------------+------------------------------+
  1349. | ``!=`` | ``OpINotEqual`` | ``OpFOrdNotEqual`` |
  1350. +--------+-------------------------------------------------------------+------------------------------+
  1351. Note that for comparison of (vectors of) floats, SPIR-V has two sets of
  1352. instructions: ``OpFOrd*``, ``OpFUnord*``. We translate into ``OpFOrd*`` ones.
  1353. Boolean math operators
  1354. ----------------------
  1355. `Boolean match operators <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509631(v=vs.85).aspx#Boolean_Math_Operators>`_
  1356. (``&&``, ``||``, ``?:``) are translated into their corresponding SPIR-V opcodes
  1357. according to the following table.
  1358. +--------+----------------------+
  1359. | | (Vector of) Booleans |
  1360. +========+======================+
  1361. | ``&&`` | ``OpLogicalAnd`` |
  1362. +--------+----------------------+
  1363. | ``||`` | ``OpLogicalOr`` |
  1364. +--------+----------------------+
  1365. | ``?:`` | ``OpSelect`` |
  1366. +--------+----------------------+
  1367. Please note that "unlike short-circuit evaluation of ``&&``, ``||``, and ``?:``
  1368. in C, HLSL expressions never short-circuit an evaluation because they are vector
  1369. operations. All sides of the expression are always evaluated."
  1370. Unary operators
  1371. ---------------
  1372. For `unary operators <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509631(v=vs.85).aspx#Unary_Operators>`_:
  1373. - ``!`` is translated into ``OpLogicalNot``. Parsing will gurantee the operands
  1374. are of boolean types by inserting necessary casts.
  1375. - ``+`` requires no additional SPIR-V instructions.
  1376. - ``-`` is translated into ``OpSNegate`` and ``OpFNegate`` for (vectors of)
  1377. integers and floats, respectively.
  1378. Casts
  1379. -----
  1380. Casting between (vectors) of scalar types is translated according to the following table:
  1381. +------------+-------------------+-------------------+-------------------+-------------------+
  1382. | From \\ To | Bool | SInt | UInt | Float |
  1383. +============+===================+===================+===================+===================+
  1384. | Bool | no-op | select between one and zero |
  1385. +------------+-------------------+-------------------+-------------------+-------------------+
  1386. | SInt | | no-op | ``OpBitcast`` | ``OpConvertSToF`` |
  1387. +------------+ +-------------------+-------------------+-------------------+
  1388. | UInt | compare with zero | ``OpBitcast`` | no-op | ``OpConvertUToF`` |
  1389. +------------+ +-------------------+-------------------+-------------------+
  1390. | Float | | ``OpConvertFToS`` | ``OpConvertFToU`` | no-op |
  1391. +------------+-------------------+-------------------+-------------------+-------------------+
  1392. It is also feasible in HLSL to cast a float matrix to another float matrix with a smaller size.
  1393. This is known as matrix truncation cast. For instance, the following code casts a 3x4 matrix
  1394. into a 2x3 matrix.
  1395. .. code:: hlsl
  1396. float3x4 m = { 1, 2, 3, 4,
  1397. 5, 6, 7, 8,
  1398. 9, 10, 11, 12 };
  1399. float2x3 a = (float2x3)m;
  1400. Such casting takes the upper-left most corner of the original matrix to generate the result.
  1401. In the above example, matrix ``a`` will have 2 rows, with 3 columns each. First row will be
  1402. ``1, 2, 3`` and the second row will be ``5, 6, 7``.
  1403. Indexing operator
  1404. -----------------
  1405. The ``[]`` operator can also be used to access elements in a matrix or vector.
  1406. A matrix whose row and/or column count is 1 will be translated into a vector or
  1407. scalar. If a variable is used as the index for the dimension whose count is 1,
  1408. that variable will be ignored in the generated SPIR-V code. This is because
  1409. out-of-bound indexing triggers undefined behavior anyway. For example, for a
  1410. 1xN matrix ``mat``, ``mat[index][0]`` will be translated into
  1411. ``OpAccessChain ... %mat %uint_0``. Similarly, variable index into a size 1
  1412. vector will also be ignored and the only element will be always returned.
  1413. Assignment operators
  1414. --------------------
  1415. Assigning to struct object may involve decomposing the source struct object and
  1416. assign each element separately and recursively. This happens when the source
  1417. struct object is of different memory layout from the destination struct object.
  1418. For example, for the following source code:
  1419. .. code:: hlsl
  1420. struct S {
  1421. float a;
  1422. float2 b;
  1423. float2x3 c;
  1424. };
  1425. ConstantBuffer<S> cbuf;
  1426. RWStructuredBuffer<S> sbuf;
  1427. ...
  1428. sbuf[0] = cbuf[0];
  1429. ...
  1430. We need to assign each element because ``ConstantBuffer`` and
  1431. ``RWStructuredBuffer`` has different memory layout.
  1432. HLSL Control Flows
  1433. ==================
  1434. This section lists how various HLSL control flows are mapped.
  1435. Switch statement
  1436. ----------------
  1437. HLSL `switch statements <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509669(v=vs.85).aspx>`_
  1438. are translated into SPIR-V using:
  1439. - **OpSwitch**: if (all case values are integer literals or constant integer
  1440. variables) and (no attribute or the ``forcecase`` attribute is specified)
  1441. - **A series of if statements**: for all other scenarios (e.g., when
  1442. ``flatten``, ``branch``, or ``call`` attribute is specified)
  1443. Loops (for, while, do)
  1444. ----------------------
  1445. HLSL `for statements <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509602(v=vs.85).aspx>`_,
  1446. `while statements <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509708(v=vs.85).aspx>`_,
  1447. and `do statements <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509593(v=vs.85).aspx>`_
  1448. are translated into SPIR-V by constructing all necessary basic blocks and using
  1449. ``OpLoopMerge`` to organize as structured loops.
  1450. The HLSL attributes for these statements are translated into SPIR-V loop control
  1451. masks according to the following table:
  1452. +-------------------------+--------------------------------------------------+
  1453. | HLSL loop attribute | SPIR-V Loop Control Mask |
  1454. +=========================+==================================================+
  1455. | ``unroll(x)`` | ``Unroll`` |
  1456. +-------------------------+--------------------------------------------------+
  1457. | ``loop`` | ``DontUnroll`` |
  1458. +-------------------------+--------------------------------------------------+
  1459. | ``fastopt`` | ``DontUnroll`` |
  1460. +-------------------------+--------------------------------------------------+
  1461. | ``allow_uav_condition`` | Currently Unimplemented |
  1462. +-------------------------+--------------------------------------------------+
  1463. HLSL Functions
  1464. ==============
  1465. All functions reachable from the entry-point function will be translated into
  1466. SPIR-V code. Functions not reachable from the entry-point function will be
  1467. ignored.
  1468. Entry function wrapper
  1469. ----------------------
  1470. HLSL entry functions takes in parameters and returns values. These parameters
  1471. and return values can have semantics attached or if they are struct type,
  1472. the struct fields can have semantics attached. However, in Vulkan, the entry
  1473. function must be of the ``void(void)`` signature. To handle this difference,
  1474. for a given entry function ``main``, we will emit a wrapper function for it.
  1475. The wrapper function will take the name of the source code entry function,
  1476. while the source code entry function will have its name prefixed with "src.".
  1477. The wrapper function reads in stage input/builtin variables created according
  1478. to semantics and groups them into composites meeting the requirements of the
  1479. source code entry point. Then the wrapper calls the source code entry point.
  1480. The return value is extracted and components of it will be written to stage
  1481. output/builtin variables created according to semantics. For example:
  1482. .. code:: hlsl
  1483. // HLSL source code
  1484. struct S {
  1485. bool a : A;
  1486. uint2 b: B;
  1487. float2x3 c: C;
  1488. };
  1489. struct T {
  1490. S x;
  1491. int y: D;
  1492. };
  1493. T main(T input) {
  1494. return input;
  1495. }
  1496. .. code:: spirv
  1497. ; SPIR-V code
  1498. %in_var_A = OpVariable %_ptr_Input_bool Input
  1499. %in_var_B = OpVariable %_ptr_Input_v2uint Input
  1500. %in_var_C = OpVariable %_ptr_Input_mat2v3float Input
  1501. %in_var_D = OpVariable %_ptr_Input_int Input
  1502. %out_var_A = OpVariable %_ptr_Output_bool Output
  1503. %out_var_B = OpVariable %_ptr_Output_v2uint Output
  1504. %out_var_C = OpVariable %_ptr_Output_mat2v3float Output
  1505. %out_var_D = OpVariable %_ptr_Output_int Output
  1506. ; Wrapper function starts
  1507. %main = OpFunction %void None ...
  1508. ... = OpLabel
  1509. %param_var_input = OpVariable %_ptr_Function_T Function
  1510. ; Load stage input variables and group into the expected composite
  1511. %inA = OpLoad %bool %in_var_A
  1512. %inB = OpLoad %v2uint %in_var_B
  1513. %inC = OpLoad %mat2v3float %in_var_C
  1514. %inS = OpCompositeConstruct %S %inA %inB %inC
  1515. %inD = OpLoad %int %in_var_D
  1516. %inT = OpCompositeConstruct %T %inS %inD
  1517. OpStore %param_var_input %inT
  1518. %ret = OpFunctionCall %T %src_main %param_var_input
  1519. ; Extract component values from the composite and store into stage output variables
  1520. %outS = OpCompositeExtract %S %ret 0
  1521. %outA = OpCompositeExtract %bool %outS 0
  1522. OpStore %out_var_A %outA
  1523. %outB = OpCompositeExtract %v2uint %outS 1
  1524. OpStore %out_var_B %outB
  1525. %outC = OpCompositeExtract %mat2v3float %outS 2
  1526. OpStore %out_var_C %outC
  1527. %outD = OpCompositeExtract %int %ret 1
  1528. OpStore %out_var_D %outD
  1529. OpReturn
  1530. OpFunctionEnd
  1531. ; Source code entry point starts
  1532. %src_main = OpFunction %T None ...
  1533. In this way, we can concentrate all stage input/output/builtin variable
  1534. manipulation in the wrapper function and handle the source code entry function
  1535. just like other nomal functions.
  1536. Function parameter
  1537. ------------------
  1538. For a function ``f`` which has a parameter of type ``T``, the generated SPIR-V
  1539. signature will use type ``T*`` for the parameter. At every call site of ``f``,
  1540. additional local variables will be allocated to hold the actual arguments.
  1541. The local variables are passed in as direct function arguments. For example:
  1542. .. code:: hlsl
  1543. // HLSL source code
  1544. float4 f(float a, int b) { ... }
  1545. void caller(...) {
  1546. ...
  1547. float4 result = f(...);
  1548. ...
  1549. }
  1550. .. code:: spirv
  1551. ; SPIR-V code
  1552. ...
  1553. %i32PtrType = OpTypePointer Function %int
  1554. %f32PtrType = OpTypePointer Function %float
  1555. %fnType = OpTypeFunction %v4float %f32PtrType %i32PtrType
  1556. ...
  1557. %f = OpFunction %v4float None %fnType
  1558. %a = OpFunctionParameter %f32PtrType
  1559. %b = OpFunctionParameter %i32PtrType
  1560. ...
  1561. %caller = OpFunction ...
  1562. ...
  1563. %aAlloca = OpVariable %_ptr_Function_float Function
  1564. %bAlloca = OpVariable %_ptr_Function_int Function
  1565. ...
  1566. OpStore %aAlloca ...
  1567. OpStore %bAlloca ...
  1568. %result = OpFunctioncall %v4float %f %aAlloca %bAlloca
  1569. ...
  1570. This approach gives us unified handling of function parameters and local
  1571. variables: both of them are accessed via load/store instructions.
  1572. Intrinsic functions
  1573. -------------------
  1574. The following intrinsic HLSL functions have no direct SPIR-V opcode or GLSL
  1575. extended instruction mapping, so they are handled with additional steps:
  1576. - ``dot`` : performs dot product of two vectors, each containing floats or
  1577. integers. If the two parameters are vectors of floats, we use SPIR-V's
  1578. ``OpDot`` instruction to perform the translation. If the two parameters are
  1579. vectors of integers, we multiply corresponding vector elements using
  1580. ``OpIMul`` and accumulate the results using ``OpIAdd`` to compute the dot
  1581. product.
  1582. - ``mul``: performs multiplications. Each argument may be a scalar, vector,
  1583. or matrix. Depending on the argument type, this will be translated into
  1584. one of the multiplication instructions.
  1585. - ``all``: returns true if all components of the given scalar, vector, or
  1586. matrix are true. Performs conversions to boolean where necessary. Uses SPIR-V
  1587. ``OpAll`` for scalar arguments and vector arguments. For matrix arguments,
  1588. performs ``OpAll`` on each row, and then again on the vector containing the
  1589. results of all rows.
  1590. - ``any``: returns true if any component of the given scalar, vector, or matrix
  1591. is true. Performs conversions to boolean where necessary. Uses SPIR-V
  1592. ``OpAny`` for scalar arguments and vector arguments. For matrix arguments,
  1593. performs ``OpAny`` on each row, and then again on the vector containing the
  1594. results of all rows.
  1595. - ``asfloat``: converts the component type of a scalar/vector/matrix from float,
  1596. uint, or int into float. Uses ``OpBitcast``. This method currently does not
  1597. support taking non-float matrix arguments.
  1598. - ``asint``: converts the component type of a scalar/vector/matrix from float or
  1599. uint into int. Uses ``OpBitcast``. This method currently does not support
  1600. conversion into integer matrices.
  1601. - ``asuint``: converts the component type of a scalar/vector/matrix from float
  1602. or int into uint. Uses ``OpBitcast``. This method currently does not support
  1603. - ``asuint``: Converts a double into two 32-bit unsigned integers. Uses SPIR-V ``OpBitCast``.
  1604. - ``asdouble``: Converts two 32-bit unsigned integers into a double, or four 32-bit unsigned
  1605. integers into two doubles. Uses SPIR-V ``OpVectorShuffle`` and ``OpBitCast``.
  1606. conversion into unsigned integer matrices.
  1607. - ``isfinite`` : Determines if the specified value is finite. Since ``OpIsFinite``
  1608. requires the ``Kernel`` capability, translation is done using ``OpIsNan`` and
  1609. ``OpIsInf``. A given value is finite iff it is not NaN and not infinite.
  1610. - ``clip``: Discards the current pixel if the specified value is less than zero.
  1611. Uses conditional control flow as well as SPIR-V ``OpKill``.
  1612. - ``rcp``: Calculates a fast, approximate, per-component reciprocal.
  1613. Uses SIR-V ``OpFDiv``.
  1614. - ``lit``: Returns a lighting coefficient vector. This vector is a float4 with
  1615. components of (ambient, diffuse, specular, 1). How ``diffuse`` and ``specular``
  1616. are calculated are explained `here <https://msdn.microsoft.com/en-us/library/windows/desktop/bb509619(v=vs.85).aspx>`_.
  1617. - ``D3DCOLORtoUBYTE4``: Converts a floating-point, 4D vector set by a D3DCOLOR to a UBYTE4.
  1618. This is achieved by performing ``int4(input.zyxw * 255.002)`` using SPIR-V ``OpVectorShuffle``,
  1619. ``OpVectorTimesScalar``, and ``OpConvertFToS``, respectively.
  1620. - ``dst``: Calculates a distance vector. The resulting vector, ``dest``, has the following specifications:
  1621. ``dest.x = 1.0``, ``dest.y = src0.y * src1.y``, ``dest.z = src0.z``, and ``dest.w = src1.w``.
  1622. Uses SPIR-V ``OpCompositeExtract`` and ``OpFMul``.
  1623. Using SPIR-V opcode
  1624. ~~~~~~~~~~~~~~~~~~~
  1625. The following intrinsic HLSL functions have direct SPIR-V opcodes for them:
  1626. ==================================== =================================
  1627. HLSL Intrinsic Function SPIR-V Opcode
  1628. ==================================== =================================
  1629. ``AllMemoryBarrier`` ``OpMemoryBarrier``
  1630. ``AllMemoryBarrierWithGroupSync`` ``OpControlBarrier``
  1631. ``countbits`` ``OpBitCount``
  1632. ``DeviceMemoryBarrier`` ``OpMemoryBarrier``
  1633. ``DeviceMemoryBarrierWithGroupSync`` ``OpControlBarrier``
  1634. ``ddx`` ``OpDPdx``
  1635. ``ddy`` ``OpDPdy``
  1636. ``ddx_coarse`` ``OpDPdxCoarse``
  1637. ``ddy_coarse`` ``OpDPdyCoarse``
  1638. ``ddx_fine`` ``OpDPdxFine``
  1639. ``ddy_fine`` ``OpDPdyFine``
  1640. ``fmod`` ``OpFMod``
  1641. ``fwidth`` ``OpFwidth``
  1642. ``GroupMemoryBarrier`` ``OpMemoryBarrier``
  1643. ``GroupMemoryBarrierWithGroupSync`` ``OpControlBarrier``
  1644. ``InterlockedAdd`` ``OpAtomicIAdd``
  1645. ``InterlockedAnd`` ``OpAtomicAnd``
  1646. ``InterlockedOr`` ``OpAtomicOr``
  1647. ``InterlockedXor`` ``OpAtomicXor``
  1648. ``InterlockedMin`` ``OpAtomicUMin``/``OpAtomicSMin``
  1649. ``InterlockedMax`` ``OpAtomicUMax``/``OpAtomicSMax``
  1650. ``InterlockedExchange`` ``OpAtomicExchange``
  1651. ``InterlockedCompareExchange`` ``OpAtomicCompareExchange``
  1652. ``InterlockedCompareStore`` ``OpAtomicCompareExchange``
  1653. ``isnan`` ``OpIsNan``
  1654. ``isInf`` ``OpIsInf``
  1655. ``reversebits`` ``OpBitReverse``
  1656. ``transpose`` ``OpTranspose``
  1657. ``CheckAccessFullyMapped`` ``OpImageSparseTexelsResident``
  1658. ==================================== =================================
  1659. Using GLSL extended instructions
  1660. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1661. The following intrinsic HLSL functions are translated using their equivalent
  1662. instruction in the `GLSL extended instruction set <https://www.khronos.org/registry/spir-v/specs/1.0/GLSL.std.450.html>`_.
  1663. ======================= ===================================
  1664. HLSL Intrinsic Function GLSL Extended Instruction
  1665. ======================= ===================================
  1666. ``abs`` ``SAbs``/``FAbs``
  1667. ``acos`` ``Acos``
  1668. ``asin`` ``Asin``
  1669. ``atan`` ``Atan``
  1670. ``atan2`` ``Atan2``
  1671. ``ceil`` ``Ceil``
  1672. ``clamp`` ``SClamp``/``UClamp``/``FClamp``
  1673. ``cos`` ``Cos``
  1674. ``cosh`` ``Cosh``
  1675. ``cross`` ``Cross``
  1676. ``degrees`` ``Degrees``
  1677. ``distance`` ``Distance``
  1678. ``radians`` ``Radian``
  1679. ``determinant`` ``Determinant``
  1680. ``exp`` ``Exp``
  1681. ``exp2`` ``exp2``
  1682. ``f16tof32`` ``UnpackHalf2x16``
  1683. ``f32tof16`` ``PackHalf2x16``
  1684. ``faceforward`` ``FaceForward``
  1685. ``firstbithigh`` ``FindSMsb`` / ``FindUMsb``
  1686. ``firstbitlow`` ``FindILsb``
  1687. ``floor`` ``Floor``
  1688. ``fma`` ``Fma``
  1689. ``frac`` ``Fract``
  1690. ``frexp`` ``FrexpStruct``
  1691. ``ldexp`` ``Ldexp``
  1692. ``length`` ``Length``
  1693. ``lerp`` ``FMix``
  1694. ``log`` ``Log``
  1695. ``log10`` ``Log2`` (scaled by ``1/log2(10)``)
  1696. ``log2`` ``Log2``
  1697. ``mad`` ``Fma``
  1698. ``max`` ``SMax``/``UMax``/``FMax``
  1699. ``min`` ``SMin``/``UMin``/``FMin``
  1700. ``modf`` ``ModfStruct``
  1701. ``normalize`` ``Normalize``
  1702. ``pow`` ``Pow``
  1703. ``reflect`` ``Reflect``
  1704. ``refract`` ``Refract``
  1705. ``round`` ``Round``
  1706. ``rsqrt`` ``InverseSqrt``
  1707. ``saturate`` ``FClamp``
  1708. ``sign`` ``SSign``/``FSign``
  1709. ``sin`` ``Sin``
  1710. ``sincos`` ``Sin`` and ``Cos``
  1711. ``sinh`` ``Sinh``
  1712. ``smoothstep`` ``SmoothStep``
  1713. ``sqrt`` ``Sqrt``
  1714. ``step`` ``Step``
  1715. ``tan`` ``Tan``
  1716. ``tanh`` ``Tanh``
  1717. ``trunc`` ``Trunc``
  1718. ======================= ===================================
  1719. Synchronization intrinsics
  1720. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  1721. Synchronization intrinsics are translated into ``OpMemoryBarrier`` (for those
  1722. non-``WithGroupSync`` variants) or ``OpControlBarrier`` (for those ``WithGroupSync``
  1723. variants) instructions with parameters:
  1724. ======================= ============ ===== ======= ========= ==============
  1725. HLSL SPIR-V SPIR-V Memory Semantics
  1726. ----------------------- ------------ --------------------------------------
  1727. Intrinsic Memory Scope Image Uniform Workgroup AcquireRelease
  1728. ======================= ============ ===== ======= ========= ==============
  1729. ``AllMemoryBarrier`` Device ✓ ✓ ✓ ✓
  1730. ``DeviceMemoryBarrier`` Device ✓ ✓ ✓
  1731. ``GroupMemoryBarrier`` Workgroup ✓ ✓
  1732. ======================= ============ ===== ======= ========= ==============
  1733. For the ``*WithGroupSync`` intrinsics, SPIR-V memory scope and semantics are the
  1734. same as their counterparts in the above. They have an additional execution
  1735. scope:
  1736. ==================================== ======================
  1737. HLSL Intrinsic SPIR-V Execution Scope
  1738. ==================================== ======================
  1739. ``AllMemoryBarrierWithGroupSync`` Workgroup
  1740. ``DeviceMemoryBarrierWithGroupSync`` Workgroup
  1741. ``GroupMemoryBarrierWithGroupSync`` Workgroup
  1742. ==================================== ======================
  1743. HLSL OO features
  1744. ================
  1745. A HLSL struct/class member method is translated into a normal SPIR-V function,
  1746. whose signature has an additional first parameter for the struct/class called
  1747. upon. Every calling site of the method is generated to pass in the object as
  1748. the first argument.
  1749. HLSL struct/class static member variables are translated into SPIR-V variables
  1750. in the ``Private`` storage class.
  1751. HLSL Methods
  1752. ============
  1753. This section lists how various HLSL methods are mapped.
  1754. Buffers
  1755. -------
  1756. ``Buffer``
  1757. ~~~~~~~~~~
  1758. ``.Load()``
  1759. +++++++++++
  1760. Since Buffers are represented as ``OpTypeImage`` with ``Sampled`` set to 1
  1761. (meaning to be used with a sampler), ``OpImageFetch`` is used to perform this
  1762. operation. The return value of ``OpImageFetch`` is always a four-component
  1763. vector; so proper additional instructions are generated to truncate the vector
  1764. and return the desired number of elements.
  1765. If an output unsigned integer ``status`` argument is present, ``OpImageSparseFetch``
  1766. is used instead. The resulting SPIR-V ``Residency Code`` will be written to ``status``.
  1767. ``operator[]``
  1768. ++++++++++++++
  1769. Handled similarly as ``.Load()``.
  1770. ``.GetDimensions()``
  1771. ++++++++++++++++++++
  1772. Since Buffers are represented as ``OpTypeImage`` with dimension of ``Buffer``,
  1773. ``OpImageQuerySize`` is used to perform this operation.
  1774. ``RWBuffer``
  1775. ~~~~~~~~~~~~
  1776. ``.Load()``
  1777. +++++++++++
  1778. Since RWBuffers are represented as ``OpTypeImage`` with ``Sampled`` set to 2
  1779. (meaning to be used without a sampler), ``OpImageRead`` is used to perform this
  1780. operation. If an output unsigned integer ``status`` argument is present, ``OpImageSparseRead``
  1781. is used instead. The resulting SPIR-V ``Residency Code`` will be written to ``status``.
  1782. ``operator[]``
  1783. ++++++++++++++
  1784. Using ``operator[]`` for reading is handled similarly as ``.Load()``, while for
  1785. writing, the ``OpImageWrite`` instruction is generated.
  1786. ``.GetDimensions()``
  1787. ++++++++++++++++++++
  1788. Since RWBuffers are represented as ``OpTypeImage`` with dimension of ``Buffer``,
  1789. ``OpImageQuerySize`` is used to perform this operation.
  1790. ``StructuredBuffer`` and ``RWStructuredBuffer``
  1791. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1792. ``.GetDimensions()``
  1793. ++++++++++++++++++++
  1794. Since StructuredBuffers/RWStructuredBuffers are represented as a struct with one
  1795. member that is a runtime array of structures, ``OpArrayLength`` is invoked on
  1796. the runtime array in order to find the dimension.
  1797. ``ByteAddressBuffer``
  1798. ~~~~~~~~~~~~~~~~~~~~~
  1799. ``.GetDimensions()``
  1800. ++++++++++++++++++++
  1801. Since ByteAddressBuffers are represented as a struct with one member that is a
  1802. runtime array of unsigned integers, ``OpArrayLength`` is invoked on the runtime array
  1803. in order to find the number of unsigned integers. This is then multiplied by 4 to find
  1804. the number of bytes.
  1805. ``.Load()``, ``.Load2()``, ``.Load3()``, ``.Load4()``
  1806. +++++++++++++++++++++++++++++++++++++++++++++++++++++
  1807. ByteAddressBuffers are represented as a struct with one member that is a runtime array of
  1808. unsigned integers. The ``address`` argument passed to the function is first divided by 4
  1809. in order to find the offset into the array (because each array element is 4 bytes). The
  1810. SPIR-V ``OpAccessChain`` instruction is then used to access that offset, and ``OpLoad`` is
  1811. used to load a 32-bit unsigned integer. For ``Load2``, ``Load3``, and ``Load4``, this is
  1812. done 2, 3, and 4 times, respectively. Each time the word offset is incremented by 1 before
  1813. performing ``OpAccessChain``. After all ``OpLoad`` operations are performed, a vector is
  1814. constructed with all the resulting values.
  1815. ``RWByteAddressBuffer``
  1816. ~~~~~~~~~~~~~~~~~~~~~~~
  1817. ``.GetDimensions()``
  1818. ++++++++++++++++++++
  1819. Since RWByteAddressBuffers are represented as a struct with one member that is a
  1820. runtime array of unsigned integers, ``OpArrayLength`` is invoked on the runtime array
  1821. in order to find the number of unsigned integers. This is then multiplied by 4 to find
  1822. the number of bytes.
  1823. ``.Load()``, ``.Load2()``, ``.Load3()``, ``.Load4()``
  1824. +++++++++++++++++++++++++++++++++++++++++++++++++++++
  1825. RWByteAddressBuffers are represented as a struct with one member that is a runtime array of
  1826. unsigned integers. The ``address`` argument passed to the function is first divided by 4
  1827. in order to find the offset into the array (because each array element is 4 bytes). The
  1828. SPIR-V ``OpAccessChain`` instruction is then used to access that offset, and ``OpLoad`` is
  1829. used to load a 32-bit unsigned integer. For ``Load2``, ``Load3``, and ``Load4``, this is
  1830. done 2, 3, and 4 times, respectively. Each time the word offset is incremented by 1 before
  1831. performing ``OpAccessChain``. After all ``OpLoad`` operations are performed, a vector is
  1832. constructed with all the resulting values.
  1833. ``.Store()``, ``.Store2()``, ``.Store3()``, ``.Store4()``
  1834. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1835. RWByteAddressBuffers are represented as a struct with one member that is a runtime array of
  1836. unsigned integers. The ``address`` argument passed to the function is first divided by 4
  1837. in order to find the offset into the array (because each array element is 4 bytes). The
  1838. SPIR-V ``OpAccessChain`` instruction is then used to access that offset, and ``OpStore`` is
  1839. used to store a 32-bit unsigned integer. For ``Store2``, ``Store3``, and ``Store4``, this is
  1840. done 2, 3, and 4 times, respectively. Each time the word offset is incremented by 1 before
  1841. performing ``OpAccessChain``.
  1842. ``.Interlocked*()``
  1843. +++++++++++++++++++
  1844. ================================= =================================
  1845. HLSL Intrinsic Method SPIR-V Opcode
  1846. ================================= =================================
  1847. ``.InterlockedAdd()`` ``OpAtomicIAdd``
  1848. ``.InterlockedAnd()`` ``OpAtomicAnd``
  1849. ``.InterlockedOr()`` ``OpAtomicOr``
  1850. ``.InterlockedXor()`` ``OpAtomicXor``
  1851. ``.InterlockedMin()`` ``OpAtomicUMin``/``OpAtomicSMin``
  1852. ``.InterlockedMax()`` ``OpAtomicUMax``/``OpAtomicSMax``
  1853. ``.InterlockedExchange()`` ``OpAtomicExchange``
  1854. ``.InterlockedCompareExchange()`` ``OpAtomicCompareExchange``
  1855. ``.InterlockedCompareStore()`` ``OpAtomicCompareExchange``
  1856. ================================= =================================
  1857. ``AppendStructuredBuffer``
  1858. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  1859. ``.Append()``
  1860. +++++++++++++
  1861. The associated counter number will be increased by 1 using ``OpAtomicIAdd``.
  1862. The return value of ``OpAtomicIAdd``, which is the original count number, will
  1863. be used as the index for storing the new element. E.g., for ``buf.Append(vec)``:
  1864. .. code:: spirv
  1865. %counter = OpAccessChain %_ptr_Uniform_int %counter_var_buf %uint_0
  1866. %index = OpAtomicIAdd %uint %counter %uint_1 %uint_0 %uint_1
  1867. %ptr = OpAccessChain %_ptr_Uniform_v4float %buf %uint_0 %index
  1868. %val = OpLoad %v4float %vec
  1869. OpStore %ptr %val
  1870. ``.GetDimensions()``
  1871. ++++++++++++++++++++
  1872. Since AppendStructuredBuffers are represented as a struct with one member that
  1873. is a runtime array, ``OpArrayLength`` is invoked on the runtime array in order
  1874. to find the number of elements. The stride is also calculated based on GLSL
  1875. ``std430`` as explained above.
  1876. ``ConsumeStructuredBuffer``
  1877. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1878. ``.Consume()``
  1879. ++++++++++++++
  1880. The associated counter number will be decreased by 1 using ``OpAtomicISub``.
  1881. The return value of ``OpAtomicISub`` minus 1, which is the new count number,
  1882. will be used as the index for reading the new element. E.g., for
  1883. ``buf.Consume(vec)``:
  1884. .. code:: spirv
  1885. %counter = OpAccessChain %_ptr_Uniform_int %counter_var_buf %uint_0
  1886. %prev = OpAtomicISub %uint %counter %uint_1 %uint_0 %uint_1
  1887. %index = OpISub %uint %prev %uint_1
  1888. %ptr = OpAccessChain %_ptr_Uniform_v4float %buf %uint_0 %index
  1889. %val = OpLoad %v4float %vec
  1890. OpStore %ptr %val
  1891. ``.GetDimensions()``
  1892. ++++++++++++++++++++
  1893. Since ConsumeStructuredBuffers are represented as a struct with one member that
  1894. is a runtime array, ``OpArrayLength`` is invoked on the runtime array in order
  1895. to find the number of elements. The stride is also calculated based on GLSL
  1896. ``std430`` as explained above.
  1897. Read-only textures
  1898. ------------------
  1899. Methods common to all texture types are explained in the "common texture methods"
  1900. section. Methods unique to a specific texture type is explained in the section
  1901. for that texture type.
  1902. Common texture methods
  1903. ~~~~~~~~~~~~~~~~~~~~~~
  1904. ``.Sample(sampler, location[, offset][, clamp][, Status])``
  1905. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1906. Not available to ``Texture2DMS`` and ``Texture2DMSArray``.
  1907. The ``OpImageSampleImplicitLod`` instruction is used to translate ``.Sample()``
  1908. since texture types are represented as ``OpTypeImage``. An ``OpSampledImage`` is
  1909. created based on the ``sampler`` passed to the function. The resulting sampled
  1910. image and the ``location`` passed to the function are used as arguments to
  1911. ``OpImageSampleImplicitLod``, with the optional ``offset`` tranlated into
  1912. addtional SPIR-V image operands ``ConstOffset`` or ``Offset`` on it. The optional
  1913. ``clamp`` argument will be translated to the ``MinLod`` image operand.
  1914. If an output unsigned integer ``status`` argument is present,
  1915. ``OpImageSparseSampleImplicitLod`` is used instead. The resulting SPIR-V
  1916. ``Residency Code`` will be written to ``status``.
  1917. ``.SampleLevel(sampler, location, lod[, offset][, Status])``
  1918. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1919. Not available to ``Texture2DMS`` and ``Texture2DMSArray``.
  1920. The ``OpImageSampleExplicitLod`` instruction is used to translate this method.
  1921. An ``OpSampledImage`` is created based on the ``sampler`` passed to the function.
  1922. The resulting sampled image and the ``location`` passed to the function are used
  1923. as arguments to ``OpImageSampleExplicitLod``. The ``lod`` passed to the function
  1924. is attached to the instruction as an SPIR-V image operands ``Lod``. The optional
  1925. ``offset`` is also tranlated into addtional SPIR-V image operands ``ConstOffset``
  1926. or ``Offset`` on it.
  1927. If an output unsigned integer ``status`` argument is present,
  1928. ``OpImageSparseSampleExplicitLod`` is used instead. The resulting SPIR-V
  1929. ``Residency Code`` will be written to ``status``.
  1930. ``.SampleGrad(sampler, location, ddx, ddy[, offset][, clamp][, Status])``
  1931. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1932. Not available to ``Texture2DMS`` and ``Texture2DMSArray``.
  1933. Similarly to ``.SampleLevel``, the ``ddx`` and ``ddy`` parameter are attached to
  1934. the ``OpImageSampleExplicitLod`` instruction as an SPIR-V image operands
  1935. ``Grad``. The optional ``clamp`` argument will be translated into the ``MinLod``
  1936. image operand.
  1937. If an output unsigned integer ``status`` argument is present,
  1938. ``OpImageSparseSampleExplicitLod`` is used instead. The resulting SPIR-V
  1939. ``Residency Code`` will be written to ``status``.
  1940. ``.SampleBias(sampler, location, bias[, offset][, clamp][, Status])``
  1941. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1942. Not available to ``Texture2DMS`` and ``Texture2DMSArray``.
  1943. The translation is similar to ``.Sample()``, with the ``bias`` parameter
  1944. attached to the ``OpImageSampleImplicitLod`` instruction as an SPIR-V image
  1945. operands ``Bias``.
  1946. If an output unsigned integer ``status`` argument is present,
  1947. ``OpImageSparseSampleImplicitLod`` is used instead. The resulting SPIR-V
  1948. ``Residency Code`` will be written to ``status``.
  1949. ``.SampleCmp(sampler, location, comparator[, offset][, clamp][, Status])``
  1950. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1951. Not available to ``Texture3D``, ``Texture2DMS``, and ``Texture2DMSArray``.
  1952. The translation is similar to ``.Sample()``, but the
  1953. ``OpImageSampleDrefImplicitLod`` instruction are used.
  1954. If an output unsigned integer ``status`` argument is present,
  1955. ``OpImageSparseSampleDrefImplicitLod`` is used instead. The resulting SPIR-V
  1956. ``Residency Code`` will be written to ``status``.
  1957. ``.SampleCmpLevelZero(sampler, location, comparator[, offset][, Status])``
  1958. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1959. Not available to ``Texture3D``, ``Texture2DMS``, and ``Texture2DMSArray``.
  1960. The translation is similar to ``.Sample()``, but the
  1961. ``OpImageSampleDrefExplicitLod`` instruction are used, with the additional
  1962. ``Lod`` image operands set to 0.0.
  1963. If an output unsigned integer ``status`` argument is present,
  1964. ``OpImageSparseSampleDrefExplicitLod`` is used instead. The resulting SPIR-V
  1965. ``Residency Code`` will be written to ``status``.
  1966. ``.Gather()``
  1967. +++++++++++++
  1968. Available to ``Texture2D``, ``Texture2DArray``, ``TextureCube``, and
  1969. ``TextureCubeArray``.
  1970. The translation is similar to ``.Sample()``, but the ``OpImageGather``
  1971. instruction is used, with component setting to 0.
  1972. If an output unsigned integer ``status`` argument is present,
  1973. ``OpImageSparseGather`` is used instead. The resulting SPIR-V
  1974. ``Residency Code`` will be written to ``status``.
  1975. ``.GatherRed()``, ``.GatherGreen()``, ``.GatherBlue()``, ``.GatherAlpha()``
  1976. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1977. Available to ``Texture2D``, ``Texture2DArray``, ``TextureCube``, and
  1978. ``TextureCubeArray``.
  1979. The ``OpImageGather`` instruction is used to translate these functions, with
  1980. component setting to 0, 1, 2, and 3 respectively.
  1981. There are a few overloads for these functions:
  1982. - For those overloads taking 4 offset parameters, those offset parameters will
  1983. be conveyed as an additional ``ConstOffsets`` image operands to the
  1984. instruction if those offset parameters are all constants. Otherwise,
  1985. 4 separate ``OpImageGather`` instructions will be emitted to get each texel
  1986. from each offset, using the ``Offset`` image operands.
  1987. - For those overloads with the ``status`` parameter, ``OpImageSparseGather``
  1988. is used instead, and the resulting SPIR-V ``Residency Code`` will be
  1989. written to ``status``.
  1990. ``.GatherCmp()``
  1991. ++++++++++++++++
  1992. Available to ``Texture2D``, ``Texture2DArray``, ``TextureCube``, and
  1993. ``TextureCubeArray``.
  1994. The translation is similar to ``.Sample()``, but the ``OpImageDrefGather``
  1995. instruction is used.
  1996. For the overload with the output unsigned integer ``status`` argument,
  1997. ``OpImageSparseDrefGather`` is used instead. The resulting SPIR-V
  1998. ``Residency Code`` will be written to ``status``.
  1999. ``.GatherCmpRed()``
  2000. +++++++++++++++++++
  2001. Available to ``Texture2D``, ``Texture2DArray``, ``TextureCube``, and
  2002. ``TextureCubeArray``.
  2003. The translation is the same as ``.GatherCmp()``.
  2004. ``.Load(location[, sampleIndex][, offset])``
  2005. ++++++++++++++++++++++++++++++++++++++++++++
  2006. The ``OpImageFetch`` instruction is used for translation because texture types
  2007. are represented as ``OpTypeImage``. The last element in the ``location``
  2008. parameter will be used as arguments to the ``Lod`` SPIR-V image operand attached
  2009. to the ``OpImageFetch`` instruction, and the rest are used as the coordinate
  2010. argument to the instruction. ``offset`` is handled similarly to ``.Sample()``.
  2011. The return value of ``OpImageFetch`` is always a four-component vector; so
  2012. proper additional instructions are generated to truncate the vector and return
  2013. the desired number of elements.
  2014. For the overload with the output unsigned integer ``status`` argument,
  2015. ``OpImageSparseFetch`` is used instead. The resulting SPIR-V
  2016. ``Residency Code`` will be written to ``status``.
  2017. ``operator[]``
  2018. ++++++++++++++
  2019. Handled similarly as ``.Load()``.
  2020. ``.mips[lod][position]``
  2021. ++++++++++++++++++++++++
  2022. Not available to ``TextureCube``, ``TextureCubeArray``, ``Texture2DMS``, and
  2023. ``Texture2DMSArray``.
  2024. This method is translated into the ``OpImageFetch`` instruction. The ``lod``
  2025. parameter is attached to the instruction as the parameter to the ``Lod`` SPIR-V
  2026. image operands. The ``position`` parameter are used as the coordinate to the
  2027. instruction directly.
  2028. ``.CalculateLevelOfDetail()`` and ``.CalculateLevelOfDetailUnclamped()``
  2029. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2030. Not available to ``Texture2DMS`` and ``Texture2DMSArray``.
  2031. Since texture types are represented as ``OpTypeImage``, the ``OpImageQueryLod``
  2032. instruction is used for translation. An ``OpSampledImage`` is created based on
  2033. the ``SamplerState`` passed to the function. The resulting sampled image and
  2034. the coordinate passed to the function are used to invoke ``OpImageQueryLod``.
  2035. The result of ``OpImageQueryLod`` is a ``float2``. The first element contains
  2036. the mipmap array layer. The second element contains the unclamped level of detail.
  2037. ``Texture1D``
  2038. ~~~~~~~~~~~~~
  2039. ``.GetDimensions(width)`` or ``.GetDimensions(MipLevel, width, NumLevels)``
  2040. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2041. Since Texture1D is represented as ``OpTypeImage``, the ``OpImageQuerySizeLod`` instruction
  2042. is used for translation. If a ``MipLevel`` argument is passed to ``GetDimensions``, it will
  2043. be used as the ``Lod`` parameter of the query instruction. Otherwise, ``Lod`` of ``0`` be used.
  2044. ``Texture1DArray``
  2045. ~~~~~~~~~~~~~~~~~~
  2046. ``.GetDimensions(width, elements)`` or ``.GetDimensions(MipLevel, width, elements, NumLevels)``
  2047. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2048. Since Texture1DArray is represented as ``OpTypeImage``, the ``OpImageQuerySizeLod`` instruction
  2049. is used for translation. If a ``MipLevel`` argument is present, it will be used as the
  2050. ``Lod`` parameter of the query instruction. Otherwise, ``Lod`` of ``0`` be used.
  2051. ``Texture2D``
  2052. ~~~~~~~~~~~~~
  2053. ``.GetDimensions(width, height)`` or ``.GetDimensions(MipLevel, width, height, NumLevels)``
  2054. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2055. Since Texture2D is represented as ``OpTypeImage``, the ``OpImageQuerySizeLod`` instruction
  2056. is used for translation. If a ``MipLevel`` argument is present, it will be used as the
  2057. ``Lod`` parameter of the query instruction. Otherwise, ``Lod`` of ``0`` be used.
  2058. ``Texture2DArray``
  2059. ~~~~~~~~~~~~~~~~~~
  2060. ``.GetDimensions(width, height, elements)`` or ``.GetDimensions(MipLevel, width, height, elements, NumLevels)``
  2061. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2062. Since Texture2DArray is represented as ``OpTypeImage``, the ``OpImageQuerySizeLod`` instruction
  2063. is used for translation. If a ``MipLevel`` argument is present, it will be used as the
  2064. ``Lod`` parameter of the query instruction. Otherwise, ``Lod`` of ``0`` be used.
  2065. ``Texture3D``
  2066. ~~~~~~~~~~~~~
  2067. ``.GetDimensions(width, height, depth)`` or ``.GetDimensions(MipLevel, width, height, depth, NumLevels)``
  2068. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2069. Since Texture3D is represented as ``OpTypeImage``, the ``OpImageQuerySizeLod`` instruction
  2070. is used for translation. If a ``MipLevel`` argument is present, it will be used as the
  2071. ``Lod`` parameter of the query instruction. Otherwise, ``Lod`` of ``0`` be used.
  2072. ``Texture2DMS``
  2073. ~~~~~~~~~~~~~~~
  2074. ``.sample[sample][position]``
  2075. +++++++++++++++++++++++++++++
  2076. This method is translated into the ``OpImageFetch`` instruction. The ``sample``
  2077. parameter is attached to the instruction as the parameter to the ``Sample``
  2078. SPIR-V image operands. The ``position`` parameter are used as the coordinate to
  2079. the instruction directly.
  2080. ``.GetDimensions(width, height, numSamples)``
  2081. +++++++++++++++++++++++++++++++++++++++++++++
  2082. Since Texture2DMS is represented as ``OpTypeImage`` with ``MS`` of ``1``, the ``OpImageQuerySize`` instruction
  2083. is used to get the width and the height. Furthermore, ``OpImageQuerySamples`` is used to get the numSamples.
  2084. ``.GetSamplePosition(index)``
  2085. +++++++++++++++++++++++++++++
  2086. There are no direct mapping SPIR-V instructions for this method. Right now, it
  2087. is translated into the SPIR-V code for the following HLSL source code:
  2088. .. code:: hlsl
  2089. // count is the number of samples in the Texture2DMS(Array)
  2090. // index is the index of the sample we are trying to get the position
  2091. static const float2 pos2[] = {
  2092. { 4.0/16.0, 4.0/16.0 }, {-4.0/16.0, -4.0/16.0 },
  2093. };
  2094. static const float2 pos4[] = {
  2095. {-2.0/16.0, -6.0/16.0 }, { 6.0/16.0, -2.0/16.0 }, {-6.0/16.0, 2.0/16.0 }, { 2.0/16.0, 6.0/16.0 },
  2096. };
  2097. static const float2 pos8[] = {
  2098. { 1.0/16.0, -3.0/16.0 }, {-1.0/16.0, 3.0/16.0 }, { 5.0/16.0, 1.0/16.0 }, {-3.0/16.0, -5.0/16.0 },
  2099. {-5.0/16.0, 5.0/16.0 }, {-7.0/16.0, -1.0/16.0 }, { 3.0/16.0, 7.0/16.0 }, { 7.0/16.0, -7.0/16.0 },
  2100. };
  2101. static const float2 pos16[] = {
  2102. { 1.0/16.0, 1.0/16.0 }, {-1.0/16.0, -3.0/16.0 }, {-3.0/16.0, 2.0/16.0 }, { 4.0/16.0, -1.0/16.0 },
  2103. {-5.0/16.0, -2.0/16.0 }, { 2.0/16.0, 5.0/16.0 }, { 5.0/16.0, 3.0/16.0 }, { 3.0/16.0, -5.0/16.0 },
  2104. {-2.0/16.0, 6.0/16.0 }, { 0.0/16.0, -7.0/16.0 }, {-4.0/16.0, -6.0/16.0 }, {-6.0/16.0, 4.0/16.0 },
  2105. {-8.0/16.0, 0.0/16.0 }, { 7.0/16.0, -4.0/16.0 }, { 6.0/16.0, 7.0/16.0 }, {-7.0/16.0, -8.0/16.0 },
  2106. };
  2107. float2 position = float2(0.0f, 0.0f);
  2108. if (count == 2) {
  2109. position = pos2[index];
  2110. } else if (count == 4) {
  2111. position = pos4[index];
  2112. } else if (count == 8) {
  2113. position = pos8[index];
  2114. } else if (count == 16) {
  2115. position = pos16[index];
  2116. }
  2117. From the above, it's clear that the current implementation only supports standard
  2118. sample settings, i.e., with 1, 2, 4, 8, or 16 samples. For other cases, the
  2119. implementation will just return `(float2)0`.
  2120. ``Texture2DMSArray``
  2121. ~~~~~~~~~~~~~~~~~~~~
  2122. ``.sample[sample][position]``
  2123. +++++++++++++++++++++++++++++
  2124. This method is translated into the ``OpImageFetch`` instruction. The ``sample``
  2125. parameter is attached to the instruction as the parameter to the ``Sample``
  2126. SPIR-V image operands. The ``position`` parameter are used as the coordinate to
  2127. the instruction directly.
  2128. ``.GetDimensions(width, height, elements, numSamples)``
  2129. +++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2130. Since Texture2DMS is represented as ``OpTypeImage`` with ``MS`` of ``1``, the ``OpImageQuerySize`` instruction
  2131. is used to get the width, the height, and the elements. Furthermore, ``OpImageQuerySamples`` is used to get the numSamples.
  2132. ``.GetSamplePosition(index)``
  2133. +++++++++++++++++++++++++++++
  2134. Similar to Texture2D.
  2135. ``TextureCube``
  2136. ~~~~~~~~~~~~~~~
  2137. ``TextureCubeArray``
  2138. ~~~~~~~~~~~~~~~~~~~~
  2139. Read-write textures
  2140. -------------------
  2141. Methods common to all texture types are explained in the "common texture methods"
  2142. section. Methods unique to a specific texture type is explained in the section
  2143. for that texture type.
  2144. Common texture methods
  2145. ~~~~~~~~~~~~~~~~~~~~~~
  2146. ``.Load()``
  2147. +++++++++++
  2148. Since read-write texture types are represented as ``OpTypeImage`` with
  2149. ``Sampled`` set to 2 (meaning to be used without a sampler), ``OpImageRead`` is
  2150. used to perform this operation.
  2151. For the overload with the output unsigned integer ``status`` argument,
  2152. ``OpImageSparseRead`` is used instead. The resulting SPIR-V
  2153. ``Residency Code`` will be written to ``status``.
  2154. ``operator[]``
  2155. ++++++++++++++
  2156. Using ``operator[]`` for reading is handled similarly as ``.Load()``, while for
  2157. writing, the ``OpImageWrite`` instruction is generated.
  2158. ``RWTexture1D``
  2159. ~~~~~~~~~~~~~~~
  2160. ``.GetDimensions(width)``
  2161. +++++++++++++++++++++++++
  2162. The ``OpImageQuerySize`` instruction is used to find the width.
  2163. ``RWTexture1DArray``
  2164. ~~~~~~~~~~~~~~~~~~~~
  2165. ``.GetDimensions(width, elements)``
  2166. +++++++++++++++++++++++++++++++++++
  2167. The ``OpImageQuerySize`` instruction is used to get a uint2. The first element
  2168. is the width, and the second is the elements.
  2169. ``RWTexture2D``
  2170. ~~~~~~~~~~~~~~~
  2171. ``.GetDimensions(width, height)``
  2172. +++++++++++++++++++++++++++++++++
  2173. The ``OpImageQuerySize`` instruction is used to get a uint2. The first element is the width, and the second
  2174. element is the height.
  2175. ``RWTexture2DArray``
  2176. ~~~~~~~~~~~~~~~~~~~~
  2177. ``.GetDimensions(width, height, elements)``
  2178. +++++++++++++++++++++++++++++++++++++++++++
  2179. The ``OpImageQuerySize`` instruction is used to get a uint3. The first element is the width, the second
  2180. element is the height, and the third is the elements.
  2181. ``RWTexture3D``
  2182. ~~~~~~~~~~~~~~~
  2183. ``.GetDimensions(width, height, depth)``
  2184. ++++++++++++++++++++++++++++++++++++++++
  2185. The ``OpImageQuerySize`` instruction is used to get a uint3. The first element is the width, the second
  2186. element is the height, and the third element is the depth.
  2187. HLSL Shader Stages
  2188. ==================
  2189. Hull Shaders
  2190. ------------
  2191. Hull shaders corresponds to Tessellation Control Shaders (TCS) in Vulkan.
  2192. This section describes how Hull shaders are translated to SPIR-V for Vulkan.
  2193. Hull Entry Point Attributes
  2194. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2195. The following HLSL attributes are attached to the main entry point of hull shaders
  2196. and are translated to SPIR-V execution modes according to the table below:
  2197. .. table:: Mapping from HLSL attribute to SPIR-V execution mode
  2198. +-------------------------+---------------------+--------------------------+
  2199. | HLSL Attribute | value | SPIR-V Execution Mode |
  2200. +=========================+=====================+==========================+
  2201. | | ``quad`` | ``Quads`` |
  2202. | +---------------------+--------------------------+
  2203. | ``domain`` | ``tri`` | ``Triangles`` |
  2204. | +---------------------+--------------------------+
  2205. | | ``isoline`` | ``Isoline`` |
  2206. +-------------------------+---------------------+--------------------------+
  2207. | | ``integer`` | ``SpacingEqual`` |
  2208. | +---------------------+--------------------------+
  2209. | | ``fractional_even`` | ``SpacingFractionalEven``|
  2210. | ``partitioning`` +---------------------+--------------------------+
  2211. | | ``fractional_odd`` | ``SpacingFractionalOdd`` |
  2212. | +---------------------+--------------------------+
  2213. | | ``pow2`` | N/A |
  2214. +-------------------------+---------------------+--------------------------+
  2215. | | ``point`` | ``PointMode`` |
  2216. | +---------------------+--------------------------+
  2217. | | ``line`` | N/A |
  2218. | ``outputtopology`` +---------------------+--------------------------+
  2219. | | ``triangle_cw`` | ``VertexOrderCw`` |
  2220. | +---------------------+--------------------------+
  2221. | | ``triangle_ccw`` | ``VertexOrderCcw`` |
  2222. +-------------------------+---------------------+--------------------------+
  2223. |``outputcontrolpoints`` | ``n`` | ``OutputVertices n`` |
  2224. +-------------------------+---------------------+--------------------------+
  2225. The ``patchconstfunc`` attribute does not have a direct equivalent in SPIR-V.
  2226. It specifies the name of the Patch Constant Function. This function is run only
  2227. once per patch. This is further described below.
  2228. InputPatch and OutputPatch
  2229. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  2230. Both of ``InputPatch<T, N>`` and ``OutputPatch<T, N>`` are translated to an array
  2231. of constant size ``N`` where each element is of type ``T``.
  2232. InputPatch can be passed to the Hull shader main entry function as well as the
  2233. patch constant function. This would include information about each of the ``N``
  2234. vertices that are input to the tessellation control shader.
  2235. OutputPatch is an array containing ``N`` elements (where ``N`` is the number of
  2236. output vertices). Each element of the array contains information about an
  2237. output vertex. OutputPatch may also be passed to the patch constant function.
  2238. The SPIR-V ``InvocationID`` (``SV_OutputControlPointID`` in HLSL) is used to index
  2239. into the InputPatch and OutputPatch arrays to read/write information for the given
  2240. vertex.
  2241. The hull main entry function in HLSL returns only one value (say, of type ``T``), but
  2242. that function is in fact executed once for each control point. The Vulkan spec requires that
  2243. "Tessellation control shader per-vertex output variables and blocks, and tessellation control,
  2244. tessellation evaluation, and geometry shader per-vertex input variables and blocks are required
  2245. to be declared as arrays, with each element representing input or output values for a single vertex
  2246. of a multi-vertex primitive". Therefore, we need to create a stage output variable that is an array
  2247. with elements of type ``T``. The number of elements of the array is equal to the number of
  2248. output control points. Each final output control point is written into the corresponding element in
  2249. the array using SV_OutputControlPointID as the index.
  2250. Patch Constant Function
  2251. ~~~~~~~~~~~~~~~~~~~~~~~
  2252. As mentioned above, the patch constant function is to be invoked only once per patch.
  2253. As a result, in the SPIR-V module, the `entry function wrapper`_ will first invoke the
  2254. main entry function, and then use an ``OpControlBarrier`` to wait for all vertex
  2255. processing to finish. After the barrier, *only* the first thread (with InvocationID of 0)
  2256. will invoke the patch constant function.
  2257. The information resulting from the patch constant function will also be returned
  2258. as stage output variables. The output struct of the patch constant function must include
  2259. ``SV_TessFactor`` and ``SV_InsideTessFactor`` fields which will translate to
  2260. ``TessLevelOuter`` and ``TessLevelInner`` builtin variables, respectively. And the rest
  2261. will be flattened and translated into normal stage output variables, one for each field.
  2262. Geometry Shaders
  2263. ----------------
  2264. This section describes how geometry shaders are translated to SPIR-V for Vulkan.
  2265. Geometry Shader Entry Point Attributes
  2266. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2267. The following HLSL attribute is attached to the main entry point of geometry shaders
  2268. and is translated to SPIR-V execution mode as follows:
  2269. .. table:: Mapping from geometry shader HLSL attribute to SPIR-V execution mode
  2270. +-------------------------+---------------------+--------------------------+
  2271. | HLSL Attribute | value | SPIR-V Execution Mode |
  2272. +=========================+=====================+==========================+
  2273. |``maxvertexcount`` | ``n`` | ``OutputVertices n`` |
  2274. +-------------------------+---------------------+--------------------------+
  2275. |``instance`` | ``n`` | ``Invocations n`` |
  2276. +-------------------------+---------------------+--------------------------+
  2277. Translation for Primitive Types
  2278. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2279. Geometry shader vertex inputs may be qualified with primitive types. Only one primitive type
  2280. is allowed to be used in a given geometry shader. The following table shows the SPIR-V execution
  2281. mode that is used in order to represent the given primitive type.
  2282. .. table:: Mapping from geometry shader primitive type to SPIR-V execution mode
  2283. +---------------------+-----------------------------+
  2284. | HLSL Primitive Type | SPIR-V Execution Mode |
  2285. +=====================+=============================+
  2286. |``point`` | ``InputPoints`` |
  2287. +---------------------+-----------------------------+
  2288. |``line`` | ``InputLines`` |
  2289. +---------------------+-----------------------------+
  2290. |``triangle`` | ``Triangles`` |
  2291. +---------------------+-----------------------------+
  2292. |``lineadj`` | ``InputLinesAdjacency`` |
  2293. +---------------------+-----------------------------+
  2294. |``triangleadj`` | ``InputTrianglesAdjacency`` |
  2295. +---------------------+-----------------------------+
  2296. Translation of Output Stream Types
  2297. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2298. Supported output stream types in geometry shaders are: ``PointStream<T>``,
  2299. ``LineStream<T>``, and ``TriangleStream<T>``. These types are translated as the underlying
  2300. type ``T``, which is recursively flattened into stand-alone variables for each field.
  2301. Furthermore, output stream objects passed to geometry shader entry points are
  2302. required to be annotated with ``inout``, but the generated SPIR-V only contains
  2303. stage output variables for them.
  2304. The following table shows the SPIR-V execution mode that is used in order to represent the
  2305. given output stream.
  2306. .. table:: Mapping from geometry shader output stream type to SPIR-V execution mode
  2307. +---------------------+-----------------------------+
  2308. | HLSL Output Stream | SPIR-V Execution Mode |
  2309. +=====================+=============================+
  2310. |``PointStream`` | ``OutputPoints`` |
  2311. +---------------------+-----------------------------+
  2312. |``LineStream`` | ``OutputLineStrip`` |
  2313. +---------------------+-----------------------------+
  2314. |``TriangleStream`` | ``OutputTriangleStrip`` |
  2315. +---------------------+-----------------------------+
  2316. In other shader stages, stage output variables are only written in the `entry
  2317. function wrapper`_ after calling the source code entry function. However,
  2318. geometry shaders can output as many vertices as they wish, by calling the
  2319. ``.Append()`` method on the output stream object. Therefore, it is incorrect to
  2320. have only one flush in the entry function wrapper like other stages. Instead,
  2321. each time a ``*Stream<T>::Append()`` is encountered, all stage output variables
  2322. behind ``T`` will be flushed before SPIR-V ``OpEmitVertex`` instruction is
  2323. generated. ``.RestartStrip()`` method calls will be translated into the SPIR-V
  2324. ``OpEndPrimitive`` instruction.
  2325. Raytracing Shader Stages
  2326. ------------------------
  2327. DirectX Raytracing adds six new shader stages for raytracing namely ray generation, intersection, closest-hit,
  2328. any-hit, miss and callable.
  2329. | Refer to following pages for details:
  2330. | https://docs.microsoft.com/en-us/windows/desktop/direct3d12/direct3d-12-raytracing
  2331. | https://docs.microsoft.com/en-us/windows/desktop/direct3d12/direct3d-12-raytracing-hlsl-reference
  2332. Flow chart for various stages in a raytracing pipeline is as follows:
  2333. ::
  2334. +---------------------+
  2335. | Ray generation |
  2336. +---------------------+
  2337. |
  2338. TraceRay() | +--------------+
  2339. | _ _ _ _ _ _ _ _ | Any Hit |
  2340. | | +--------------+
  2341. V V ^
  2342. +---------------------+ |
  2343. | Acceleration | +--------------+
  2344. | Structure | | Intersection |
  2345. | Traversal | +--------------+
  2346. +---------------------+ ^
  2347. | | |
  2348. | |_ _ _ _ _ _ _ _ _ _ _|
  2349. |
  2350. |
  2351. V
  2352. +--------------------+ +-------------+
  2353. | Is Hit ? | | Callable |
  2354. +--------------------+ +-------------+
  2355. | |
  2356. Yes | | No
  2357. V V
  2358. +---------+ +------+
  2359. | Closest | | Miss |
  2360. | Hit | | |
  2361. +---------+ +------+
  2362. | *Note : DXC does not add special shader profiles for raytracing under -T option.*
  2363. | *All raytracing shaders must be compiled as library using lib_6_3/lib_6_4 profile option.*
  2364. | *Note : DXC now targets SPV_KHR_ray_tracing extension by default.*
  2365. | *This extension is provisional and subject to change*.
  2366. | *To compile for NV extension use -fspv-extension=SPV_NV_ray_tracing.*
  2367. Ray Generation Stage
  2368. ~~~~~~~~~~~~~~~~~~~~
  2369. | Ray generation shaders start ray tracing work and work on a compute-like 3D grid of threads.
  2370. | Entry functions of this stage type are annotated with **[shader("raygeneration")]** in HLSL source.
  2371. | Such entry functions must return void and do not accept any arguments.
  2372. | For example:
  2373. .. code:: hlsl
  2374. RaytracingAccelerationStructure rs;
  2375. struct Payload
  2376. {
  2377. float4 color;
  2378. };
  2379. [shader("raygeneration")]
  2380. void main() {
  2381. Payload myPayload = { float4(0.0f,0.0f,0.0f,0.0f) };
  2382. RayDesc rayDesc;
  2383. rayDesc.Origin = float3(0.0f, 0.0f, 0.0f);
  2384. rayDesc.Direction = float3(0.0f, 0.0f, -1.0f);
  2385. rayDesc.TMin = 0.0f;
  2386. rayDesc.TMax = 1000.0f;
  2387. TraceRay(rs, 0x0, 0xff, 0, 1, 0, rayDesc, myPayload);
  2388. }
  2389. Intersection Stage
  2390. ~~~~~~~~~~~~~~~~~~
  2391. | Intersection shader stage is used to implement arbitrary ray-primitive intersections such spheres or axis-aligned bounding boxes (AABB). Triangle primitives do not require a custom intersection shader.
  2392. | Entry functions of this stage are annotated with **[shader("intersection")]** in HLSL source.
  2393. | Such entry functions must return void and do not accept any arguments.
  2394. | For example:
  2395. .. code:: hlsl
  2396. struct Attribute
  2397. {
  2398. float2 bary;
  2399. };
  2400. [shader("intersection")]
  2401. void main() {
  2402. Attribute myHitAttribute = { float2(0.0f,0.0f) };
  2403. ReportHit(0.0f, 0U, myHitAttribute);
  2404. }
  2405. Closest-Hit Stage
  2406. ~~~~~~~~~~~~~~~~~
  2407. | Hit shaders are invoked when a ray primitive intersection is found. A closest-hit shader
  2408. | is invoked for the closest intersection point along a ray and can be used to compute interactions
  2409. | at intersection point or spawn secondary rays.
  2410. | Entry functions of this stage are annotated with **[shader("closesthit")]** in HLSL source.
  2411. | Such entry functions must return void and accept exactly two arguments. First argument must be an inout
  2412. | variable of user defined structure type and second argument must be a in variable of user defined structure type.
  2413. | For example:
  2414. .. code:: hlsl
  2415. struct Attribute
  2416. {
  2417. float2 bary;
  2418. };
  2419. struct Payload {
  2420. float4 color;
  2421. };
  2422. [shader("closesthit")]
  2423. void main(inout Payload a, in Attribute b) {
  2424. a.color = float4(0.0f,1.0f,0.0f,0.0f);
  2425. }
  2426. Any-Hit Stage
  2427. ~~~~~~~~~~~~~~~~~
  2428. | Hit shaders are invoked when a ray primitive intersection is found. An any-hit shader
  2429. | is invoked for all intersections along a ray with a primitive.
  2430. | Entry functions of this stage are annotated with **[shader("anyhit")]** in HLSL source.
  2431. | Such entry functions must return void and accept exactly two arguments. First argument must be an inout
  2432. | variable of user defined structure type and second argument must be an in variable of user defined structure type.
  2433. | For example:
  2434. .. code:: hlsl
  2435. struct Attribute
  2436. {
  2437. float2 bary;
  2438. };
  2439. struct Payload {
  2440. float4 color;
  2441. };
  2442. [shader("anyhit")]
  2443. void main(inout Payload a, in Attribute b) {
  2444. a.color = float4(0.0f,1.0f,0.0f,0.0f);
  2445. }
  2446. Miss Stage
  2447. ~~~~~~~~~~
  2448. | Miss shaders are invoked when no intersection is found.
  2449. | Entry functions of this stage are annotated with **[shader("miss")]** in HLSL source.
  2450. | Such entry functions return void and accept exactly one argument. First argument must be an inout variable of user defined structure type.
  2451. | For example:
  2452. .. code:: hlsl
  2453. struct Payload {
  2454. float4 color;
  2455. };
  2456. [shader("miss")]
  2457. void main(inout Payload a) {
  2458. a.color = float4(0.0f,1.0f,0.0f,0.0f);
  2459. }
  2460. Callable Stage
  2461. ~~~~~~~~~~~~~~
  2462. | Callables are generic function calls which can be invoked from either raygeneration, closest-hit,
  2463. | miss or callable shader stages.
  2464. | Entry functions of this stage are annotated with **[shader("callable")]** in HLSL source.
  2465. | Such entry functions must return void and accept exactly one argument. First argument must be an inout
  2466. | variable of user defined structure type.
  2467. | For example:
  2468. .. code:: hlsl
  2469. struct CallData {
  2470. float4 data;
  2471. };
  2472. [shader("callable")]
  2473. void main(inout CallData a) {
  2474. a.color = float4(0.0f,1.0f,0.0f,0.0f);
  2475. }
  2476. Mesh and Amplification Shaders
  2477. ------------------------------
  2478. | DirectX adds 2 new shader stages for using MeshShading pipeline namely Mesh and Amplification.
  2479. | Amplification shaders corresponds to Task Shaders in Vulkan.
  2480. |
  2481. | Refer to following HLSL and SPIR-V specs for details:
  2482. | https://docs.microsoft.com/<TBD>
  2483. | https://github.com/KhronosGroup/SPIRV-Registry/blob/master/extensions/NV/SPV_NV_mesh_shader.asciidoc
  2484. |
  2485. | This section describes how Mesh and Amplification shaders are translated to SPIR-V for Vulkan.
  2486. Entry Point Attributes
  2487. ~~~~~~~~~~~~~~~~~~~~~~
  2488. The following HLSL attributes are attached to the main entry point of Mesh and/or Amplification
  2489. shaders and are translated to SPIR-V execution modes according to the table below:
  2490. .. table:: Mapping from HLSL attribute to SPIR-V execution mode
  2491. +-------------------+--------------------+-------------------------+
  2492. | HLSL Attribute | Value | SPIR-V Execution Mode |
  2493. +===================+====================+=========================+
  2494. |``outputtopology`` | ``point`` | ``OutputPoints`` |
  2495. | +--------------------+-------------------------+
  2496. |``(Mesh shader)`` | ``line`` | ``OutputLinesNV`` |
  2497. | +--------------------+-------------------------+
  2498. | | ``triangle`` | ``OutputTrianglesNV`` |
  2499. +-------------------+--------------------+-------------------------+
  2500. | ``numthreads`` | ``X, Y, Z`` | ``LocalSize X, Y, Z`` |
  2501. | | | |
  2502. | | ``(X*Y*Z <= 128)`` | |
  2503. +-------------------+--------------------+-------------------------+
  2504. Intrinsics
  2505. ~~~~~~~~~~
  2506. The following HLSL intrinsics are used in Mesh or Amplification shaders
  2507. and are translated to SPIR-V intrinsics according to the table below:
  2508. .. table:: Mapping from HLSL intrinsics to SPIR-V intrinsics
  2509. +---------------------------+--------------------+-----------------------------------------+
  2510. | HLSL Intrinsic | Parameters | SPIR-V Intrinsic |
  2511. +===========================+====================+=========================================+
  2512. | ``SetMeshOutputCounts`` | ``numVertices`` | ``PrimitiveCountNV numPrimitives`` |
  2513. | | | |
  2514. | ``(Mesh shader)`` | ``numPrimitives`` | |
  2515. +---------------------------+--------------------+-----------------------------------------+
  2516. | ``DispatchMesh`` | ``ThreadX`` | ``OpControlBarrier`` |
  2517. | | | |
  2518. | ``(Amplification shader)``| ``ThreadY`` | ``TaskCountNV ThreadX*ThreadY*ThreadZ`` |
  2519. | | | |
  2520. | | ``ThreadZ`` | |
  2521. | | | |
  2522. | | ``MeshPayload`` | |
  2523. +---------------------------+--------------------+-----------------------------------------+
  2524. | Note : For ``DispatchMesh`` intrinsic, we also emit ``MeshPayload`` as output block with ``PerTaskNV`` decoration
  2525. Mesh Interface Variables
  2526. ~~~~~~~~~~~~~~~~~~~~~~~~
  2527. | Interface variables are defined for Mesh shaders using HLSL modifiers.
  2528. | Following table gives high level overview of the mapping:
  2529. |
  2530. .. table:: Mapping from HLSL modifiers to SPIR-V definitions
  2531. +-----------------+-------------------------------------------------------------------------+
  2532. | HLSL modifier | SPIR-V definition |
  2533. +=================+=========================================================================+
  2534. | ``indices`` | Maps to SPIR-V intrinsic ``PrimitiveIndicesNV`` |
  2535. | | |
  2536. | | Defines SPIR-V Execution Mode ``OutputPrimitivesNV <array-size>`` |
  2537. +-----------------+-------------------------------------------------------------------------+
  2538. | ``vertices`` | Maps to per-vertex out attributes |
  2539. | | |
  2540. | | Defines existing SPIR-V Execution Mode ``OutputVertices <array-size>`` |
  2541. +-----------------+-------------------------------------------------------------------------+
  2542. | ``primitives`` | Maps to per-primitive out attributes with ``PerPrimitiveNV`` decoration |
  2543. +-----------------+-------------------------------------------------------------------------+
  2544. | ``payload`` | Maps to per-task in attributes with ``PerTaskNV`` decoration |
  2545. +-----------------+-------------------------------------------------------------------------+
  2546. Raytracing in Vulkan and SPIRV
  2547. ==============================
  2548. | SPIR-V codegen is currently supported for NVIDIA platforms via SPV_NV_ray_tracing extension or
  2549. | on other platforms via provisional cross vendor SPV_KHR_ray_tracing extension.
  2550. | SPIR-V specification for reference:
  2551. | https://github.com/KhronosGroup/SPIRV-Registry/blob/master/extensions/NV/SPV_NV_ray_tracing.asciidoc
  2552. | https://github.com/KhronosGroup/SPIRV-Registry/blob/master/extensions/KHR/SPV_KHR_ray_tracing.asciidoc
  2553. | Vulkan ray tracing samples:
  2554. | https://developer.nvidia.com/rtx/raytracing/vkray
  2555. Raytracing Mapping to SPIR-V
  2556. ----------------------------
  2557. Intrinsics
  2558. ~~~~~~~~~~
  2559. | Following table provides mapping for system value intrinsics along with supported shader stages.
  2560. ============================ =============================== ====== ============ =========== ======= ======== ========
  2561. HLSL SPIR-V HLSL Shader Stage
  2562. ---------------------------- ------------------------------- ---------------------------------------------------------
  2563. System Value Intrinsic Builtin Raygen Intersection Closest Hit Any Hit Miss Callable
  2564. ============================ =============================== ====== ============ =========== ======= ======== ========
  2565. ``DispatchRaysIndex()`` ``LaunchId{NV/KHR}`` ✓ ✓ ✓ ✓ ✓ ✓
  2566. ``DispatchRaysDimensions()`` ``LaunchSize{NV/KHR}`` ✓ ✓ ✓ ✓ ✓ ✓
  2567. ``WorldRayOrigin()`` ``WorldRayOrigin{NV/KHR}`` ✓ ✓ ✓ ✓
  2568. ``WorldRayDirection()`` ``WorldRayDirection{NV/KHR}`` ✓ ✓ ✓ ✓
  2569. ``RayTMin()`` ``RayTmin{NV/KHR}`` ✓ ✓ ✓ ✓
  2570. ``RayTCurrent()`` ``HitT{NV/KHR}`` ✓ ✓ ✓ ✓
  2571. ``RayFlags()`` ``IncomingRayFlags{NV/KHR}`` ✓ ✓ ✓ ✓
  2572. ``InstanceIndex()`` ``InstanceId`` ✓ ✓ ✓
  2573. ``GeometryIndex()`` ``RayGeometryIndexKHR`` ✓ ✓ ✓
  2574. ``InstanceID()`` ``InstanceCustomIndex{NV/KHR}`` ✓ ✓ ✓
  2575. ``PrimitiveIndex()`` ``PrimitiveId`` ✓ ✓ ✓
  2576. ``ObjectRayOrigin()`` ``ObjectRayOrigin{NV/KHR}`` ✓ ✓ ✓
  2577. ``ObjectRayDirection()`` ``ObjectRayDirection{NV/KHR}`` ✓ ✓ ✓
  2578. ``ObjectToWorld3x4()`` ``ObjectToWorld{NV/KHR}`` ✓ ✓ ✓
  2579. ``ObjectToWorld4x3()`` ``ObjectToWorld{NV/KHR}`` ✓ ✓ ✓
  2580. ``WorldToObject3x4()`` ``WorldToObject{NV/KHR}`` ✓ ✓ ✓
  2581. ``WorldToObject4x3()`` ``WorldToObject{NV/KHR}`` ✓ ✓ ✓
  2582. ``HitKind()`` ``HitKind{NV/KHR}`` ✓ ✓ ✓
  2583. ============================ =============================== ====== ============ =========== ======= ======== ========
  2584. | *There is no separate builtin for transposed matrices ObjectToWorld3x4 and WorldToObject3x4 in SPIR-V hence we internally transpose during translation*
  2585. | *GeometryIndex() is only supported under SPV_KHR_ray_tracing extension.*
  2586. | Following table provides mapping for other intrinsics along with supported shader stages.
  2587. =========================== ================================= ====== ============ =========== ======= ===== ========
  2588. HLSL SPIR-V HLSL Shader Stage
  2589. --------------------------- --------------------------------- ------------------------------------------------------
  2590. Intrinsic Opcode Raygen Intersection Closest Hit Any Hit Miss Callable
  2591. =========================== ================================= ====== ============ =========== ======= ===== ========
  2592. ``TraceRay`` ``OpTrace{NV/KHR}`` ✓ ✓ ✓
  2593. ``ReportHit`` ``OpReportIntersection{NV/KHR}`` ✓ ✓
  2594. ``IgnoreHit`` ``OpIgnoreIntersection{NV/KHR}`` ✓ ✓
  2595. ``AcceptHitAndEndSearch`` ``OpTerminateRay{NV/KHR}`` ✓ ✓
  2596. ``CallShader`` ``OpExecuteCallable{NV/KHR}`` ✓ ✓ ✓ ✓
  2597. =========================== ================================= ====== ============ =========== ======= ===== ========
  2598. Resource Types
  2599. ~~~~~~~~~~~~~~
  2600. | Following table provides mapping for new resource types supported in all raytracing shaders.
  2601. =================================== =======================================
  2602. HLSL Type SPIR-V Opcode
  2603. ----------------------------------- ---------------------------------------
  2604. ``RaytracingAccelerationStructure`` ``OpTypeAccelerationStructure{NV/KHR}``
  2605. =================================== =======================================
  2606. Interface Variables
  2607. ~~~~~~~~~~~~~~~~~~~
  2608. | Interface variables are created for various ray tracing storage classes based on intrinsic/shader stage
  2609. | Following table gives high level overview of the mapping.
  2610. ================================= ===========================================================
  2611. SPIR-V Storage Class Created For
  2612. --------------------------------- -----------------------------------------------------------
  2613. ``RayPayload{NV/KHR}`` Last argument to TraceRay
  2614. ``IncomingRayPayload{NV/KHR}`` First argument of entry for AnyHit/ClosestHit & Miss stage
  2615. ``HitAttribute{NV/KHR}`` Last argument to ReportHit
  2616. ``CallableData{NV/KHR}`` Last argument to CallShader
  2617. ``IncomingCallableData{NV/KHR}`` First argument of entry for Callable stage
  2618. ================================= ===========================================================
  2619. RayQuery
  2620. --------
  2621. Ray Query is subfeature of the DirectX ray tracing and belongs to the DirectX ray tracing spec 1.1 (DXR 1.1).
  2622. DirectX add RayQuery object type and its member TraceRayInline() to do the TraceRay() that doesn't
  2623. use any seperate ray-tracing shader stages.
  2624. Shaders can instantiate RayQuery objects as local variables, the RayQuery object acts as a state
  2625. machine for ray query. The shader interacts with the RayQuery object's methods to advance the
  2626. query through an acceleration structure and query traversal information
  2627. Refer to following pages for details:
  2628. https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html
  2629. A flow chart for a simple ray query process
  2630. ::
  2631. +------------------------------+
  2632. | RayQuery<RAY_FLAG_NONE> q |
  2633. +------------------------------+
  2634. |
  2635. V
  2636. +------------------------------+
  2637. | q.TraceRayInline() |
  2638. +------------------------------+
  2639. | — — — — — — — — — — — — —
  2640. | | |
  2641. | | +------------------------+
  2642. | | | Your intersection code |
  2643. | | +------------------------+
  2644. | | ^
  2645. V V |
  2646. +------------------------------+ +---------------------+
  2647. | q.Proceed() // AS traversal | | q.CandidateType() |
  2648. +------------------------------+ +---------------------+
  2649. | | ^
  2650. No | | Yes |
  2651. | |_ _ _ _ _ _ _ _ _ _ _ _|
  2652. V
  2653. +------------------------------+
  2654. | q.CommittedStatus() |
  2655. +------------------------------+
  2656. |
  2657. V
  2658. +----------------------------------+
  2659. | Your Intersection/shader code |
  2660. +----------------------------------+
  2661. Example:
  2662. .. code:: hlsl
  2663. void main() {
  2664. RayQuery<RAY_FLAG_CULL_NON_OPAQUE | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH> q;
  2665. q.TraceRayInline(myAccelerationStructure, 0 , 0xff, myRay);
  2666. // Proceed() is AccelerationStructure traversal loop take places
  2667. while(q.Proceed()) {
  2668. switch(q.CandidateType()) {
  2669. // retrieve intersection information/Do the shadering
  2670. }
  2671. }
  2672. // AccelerationStructure traversal end
  2673. // Get the Committed status
  2674. switch(q.CommittedStatus()) {
  2675. // retrieve intersection information/ Do the shadering
  2676. }
  2677. }
  2678. Ray Query in SPIRV
  2679. ~~~~~~~~~~~~~~~~~~
  2680. RayQuery SPIR-V codegen is currently supported via SPV_KHR_ray_query extension
  2681. SPIR-V specification for reference:
  2682. https://github.com/KhronosGroup/SPIRV-Registry/blob/master/extensions/KHR/SPV_KHR_ray_query.asciidoc
  2683. Object Type
  2684. ~~~~~~~~~~~
  2685. RayQuery<RAY_FLAGS>
  2686. RayQuery represents the state of an inline ray tracing call into an acceleration structure.
  2687. ============ ================================
  2688. HLSL Type SPIR-V Opcode
  2689. ------------ --------------------------------
  2690. ``RayQuery`` ``OpTypeRayQueryProvisionalKHR``
  2691. ============ ================================
  2692. RayQuery Mapping to SPIR-V
  2693. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  2694. +---------------------------------------------------+-------------------------------------------------------------------------+
  2695. | HLSL RayQuery member Intrinsic | SPIR-V Opcode |
  2696. +===================================================+=========================================================================+
  2697. |``.Abort`` | ``OpRayQueryTerminateKHR`` |
  2698. +---------------------------------------------------+-------------------------------------------------------------------------+
  2699. |``.CandidateType`` | ``OpRayQueryGetIntersectionTypeKHR`` |
  2700. +---------------------------------------------------+-------------------------------------------------------------------------+
  2701. |``.CandidateProceduralPrimitiveNonOpaque`` | ``OpRayQueryGetIntersectionCandidateAABBOpaqueKHR`` |
  2702. +---------------------------------------------------+-------------------------------------------------------------------------+
  2703. |``.CandidateInstanceIndex`` | ``OpRayQueryGetIntersectionInstanceIdKHR`` |
  2704. +---------------------------------------------------+-------------------------------------------------------------------------+
  2705. |``.CandidateInstanceID`` | ``OpRayQueryGetIntersectionInstanceCustomIndexKHR`` |
  2706. +---------------------------------------------------+-------------------------------------------------------------------------+
  2707. | ``.CandidateInstanceContributionToHitGroupIndex`` | ``OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR`` |
  2708. +---------------------------------------------------+-------------------------------------------------------------------------+
  2709. |``.CandidateGeometryIndex`` | ``OpRayQueryGetIntersectionGeometryIndexKHR`` |
  2710. +---------------------------------------------------+-------------------------------------------------------------------------+
  2711. |``.CandidatePrimitiveIndex`` | ``OpRayQueryGetIntersectionPrimitiveIndexKHR`` |
  2712. +---------------------------------------------------+-------------------------------------------------------------------------+
  2713. |``.CandidateObjectRayOrigin`` | ``OpRayQueryGetIntersectionObjectRayOriginKHR`` |
  2714. +---------------------------------------------------+-------------------------------------------------------------------------+
  2715. |``.CandidateObjectRayDirection`` | ``OpRayQueryGetIntersectionObjectRayDirectionKHR`` |
  2716. +---------------------------------------------------+-------------------------------------------------------------------------+
  2717. |``.CandidateObjectToWorld3x4`` | ``OpRayQueryGetIntersectionObjectToWorldKHR`` |
  2718. +---------------------------------------------------+-------------------------------------------------------------------------+
  2719. |``.CandidateObjectToWorld4x3`` | ``OpRayQueryGetIntersectionObjectToWorldKHR`` |
  2720. +---------------------------------------------------+-------------------------------------------------------------------------+
  2721. |``.CandidateWorldToObject3x4`` | ``OpRayQueryGetIntersectionWorldToObjectKHR`` |
  2722. +---------------------------------------------------+-------------------------------------------------------------------------+
  2723. |``.CandidateWorldToObject4x3`` | ``OpRayQueryGetIntersectionWorldToObjectKHR`` |
  2724. +---------------------------------------------------+-------------------------------------------------------------------------+
  2725. |``.CandidateTriangleBarycentrics`` | ``OpRayQueryGetIntersectionBarycentricsKHR`` |
  2726. +---------------------------------------------------+-------------------------------------------------------------------------+
  2727. |``.CandidateTriangleFrontFace`` | ``OpRayQueryGetIntersectionFrontFaceKHR`` |
  2728. +---------------------------------------------------+-------------------------------------------------------------------------+
  2729. |``.CommittedStatus`` | ``OpRayQueryGetIntersectionTypeKHR`` |
  2730. +---------------------------------------------------+-------------------------------------------------------------------------+
  2731. |``.CommittedInstanceIndex`` | ``OpRayQueryGetIntersectionInstanceIdKHR`` |
  2732. +---------------------------------------------------+-------------------------------------------------------------------------+
  2733. |``.CommittedInstanceID`` | ``OpRayQueryGetIntersectionInstanceCustomIndexKHR`` |
  2734. +---------------------------------------------------+-------------------------------------------------------------------------+
  2735. | ``.CommittedInstanceContributionToHitGroupIndex`` | ``OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR`` |
  2736. +---------------------------------------------------+-------------------------------------------------------------------------+
  2737. |``.CommittedGeometryIndex`` | ``OpRayQueryGetIntersectionGeometryIndexKHR`` |
  2738. +---------------------------------------------------+-------------------------------------------------------------------------+
  2739. |``.CommittedPrimitiveIndex`` | ``OpRayQueryGetIntersectionPrimitiveIndexKHR`` |
  2740. +---------------------------------------------------+-------------------------------------------------------------------------+
  2741. |``.CommittedRayT`` | ``OpRayQueryGetIntersectionTKHR`` |
  2742. +---------------------------------------------------+-------------------------------------------------------------------------+
  2743. |``.CommittedObjectRayOrigin`` | ``OpRayQueryGetIntersectionObjectRayOriginKHR`` |
  2744. +---------------------------------------------------+-------------------------------------------------------------------------+
  2745. |``.CommittedObjectRayDirection`` | ``OpRayQueryGetIntersectionObjectRayDirectionKHR`` |
  2746. +---------------------------------------------------+-------------------------------------------------------------------------+
  2747. |``.CommittedObjectToWorld3x4`` | ``OpRayQueryGetIntersectionObjectToWorldKHR`` |
  2748. +---------------------------------------------------+-------------------------------------------------------------------------+
  2749. |``.CommittedObjectToWorld4x3`` | ``OpRayQueryGetIntersectionObjectToWorldKHR`` |
  2750. +---------------------------------------------------+-------------------------------------------------------------------------+
  2751. |``.CommittedWorldToObject3x4`` | ``OpRayQueryGetIntersectionWorldToObjectKHR`` |
  2752. +---------------------------------------------------+-------------------------------------------------------------------------+
  2753. |``.CommittedWorldToObject4x3`` | ``OpRayQueryGetIntersectionWorldToObjectKHR`` |
  2754. +---------------------------------------------------+-------------------------------------------------------------------------+
  2755. |``.CommittedTriangleBarycentrics`` | ``OpRayQueryGetIntersectionBarycentricsKHR`` |
  2756. +---------------------------------------------------+-------------------------------------------------------------------------+
  2757. |``.CommittedTriangleFrontFace`` | ``OpRayQueryGetIntersectionFrontFaceKHR`` |
  2758. +---------------------------------------------------+-------------------------------------------------------------------------+
  2759. |``.CommitNonOpaqueTriangleHit`` | ``OpRayQueryConfirmIntersectionKHR`` |
  2760. +---------------------------------------------------+-------------------------------------------------------------------------+
  2761. |``.CommitProceduralPrimitiveHit`` | ``OpRayQueryGenerateIntersectionKHR`` |
  2762. +---------------------------------------------------+-------------------------------------------------------------------------+
  2763. |``.Proceed`` | ``OpRayQueryProceedKHR`` |
  2764. +---------------------------------------------------+-------------------------------------------------------------------------+
  2765. |``.RayFlags`` | ``OpRayQueryGetRayFlagsKHR`` |
  2766. +---------------------------------------------------+-------------------------------------------------------------------------+
  2767. |``.RayTMin`` | ``OpRayQueryGetRayTMinKHR`` |
  2768. +---------------------------------------------------+-------------------------------------------------------------------------+
  2769. |``.TraceRayInline`` | ``OpRayQueryInitializeKHR`` |
  2770. +---------------------------------------------------+-------------------------------------------------------------------------+
  2771. |``.WorldRayDirection`` | ``OpRayQueryGetWorldRayDirectionKHR`` |
  2772. +---------------------------------------------------+-------------------------------------------------------------------------+
  2773. |``.WorldRayOrigin` | ``OpRayQueryGetWorldRayOriginKHR`` |
  2774. +---------------------------------------------------+-------------------------------------------------------------------------+
  2775. Shader Model 6.0 Wave Intrinsics
  2776. ================================
  2777. Note that Wave intrinsics requires SPIR-V 1.3, which is supported by Vulkan 1.1.
  2778. If you use wave intrinsics in your source code, you will need to specify
  2779. -fspv-target-env=vulkan1.1 via the command line to target Vulkan 1.1.
  2780. Shader model 6.0 introduces a set of wave operations. Apart from
  2781. ``WaveGetLaneCount()`` and ``WaveGetLaneIndex()``, which are translated into
  2782. loading from SPIR-V builtin variable ``SubgroupSize`` and
  2783. ``SubgroupLocalInvocationId`` respectively, the rest are translated into SPIR-V
  2784. group operations with ``Subgroup`` scope according to the following chart:
  2785. ============= ============================ =================================== ======================
  2786. Wave Category Wave Intrinsics SPIR-V Opcode SPIR-V Group Operation
  2787. ============= ============================ =================================== ======================
  2788. Query ``WaveIsFirstLane()`` ``OpGroupNonUniformElect``
  2789. Vote ``WaveActiveAnyTrue()`` ``OpGroupNonUniformAny``
  2790. Vote ``WaveActiveAllTrue()`` ``OpGroupNonUniformAll``
  2791. Vote ``WaveActiveBallot()`` ``OpGroupNonUniformBallot``
  2792. Reduction ``WaveActiveAllEqual()`` ``OpGroupNonUniformAllEqual`` ``Reduction``
  2793. Reduction ``WaveActiveCountBits()`` ``OpGroupNonUniformBallotBitCount`` ``Reduction``
  2794. Reduction ``WaveActiveSum()`` ``OpGroupNonUniform*Add`` ``Reduction``
  2795. Reduction ``WaveActiveProduct()`` ``OpGroupNonUniform*Mul`` ``Reduction``
  2796. Reduction ``WaveActiveBitAdd()`` ``OpGroupNonUniformBitwiseAnd`` ``Reduction``
  2797. Reduction ``WaveActiveBitOr()`` ``OpGroupNonUniformBitwiseOr`` ``Reduction``
  2798. Reduction ``WaveActiveBitXor()`` ``OpGroupNonUniformBitwiseXor`` ``Reduction``
  2799. Reduction ``WaveActiveMin()`` ``OpGroupNonUniform*Min`` ``Reduction``
  2800. Reduction ``WaveActiveMax()`` ``OpGroupNonUniform*Max`` ``Reduction``
  2801. Scan/Prefix ``WavePrefixSum()`` ``OpGroupNonUniform*Add`` ``ExclusiveScan``
  2802. Scan/Prefix ``WavePrefixProduct()`` ``OpGroupNonUniform*Mul`` ``ExclusiveScan``
  2803. Scan/Prefix ``WavePrefixCountBits()`` ``OpGroupNonUniformBallotBitCount`` ``ExclusiveScan``
  2804. Broadcast ``WaveReadLaneAt()`` ``OpGroupNonUniformBroadcast``
  2805. Broadcast ``WaveReadLaneFirst()`` ``OpGroupNonUniformBroadcastFirst``
  2806. Quad ``QuadReadAcrossX()`` ``OpGroupNonUniformQuadSwap``
  2807. Quad ``QuadReadAcrossY()`` ``OpGroupNonUniformQuadSwap``
  2808. Quad ``QuadReadAcrossDiagonal()`` ``OpGroupNonUniformQuadSwap``
  2809. Quad ``QuadReadLaneAt()`` ``OpGroupNonUniformQuadBroadcast``
  2810. ============= ============================ =================================== ======================
  2811. Supported Command-line Options
  2812. ==============================
  2813. Command-line options supported by SPIR-V CodeGen are listed below. They are
  2814. also recognized by the library API calls.
  2815. General options
  2816. ---------------
  2817. - ``-T``: specifies shader profile
  2818. - ``-E``: specifies entry point
  2819. - ``-D``: Defines macro
  2820. - ``-I``: Adds directory to include search path
  2821. - ``-O{|0|1|2|3}``: Specifies optimization level
  2822. - ``-enable-16bit-types``: enables 16-bit types and disables min precision types
  2823. - ``-Zpc``: Packs matrices in column-major order by deafult
  2824. - ``-Zpr``: Packs matrices in row-major order by deafult
  2825. - ``-Fc``: outputs SPIR-V disassembly to the given file
  2826. - ``-Fe``: outputs warnings and errors to the given file
  2827. - ``-Fo``: outputs SPIR-V code to the given file
  2828. - ``-Fh``: outputs SPIR-V code as a header file
  2829. - ``-Vn``: specifies the variable name for SPIR-V code in generated header file
  2830. - ``-Zi``: Emits more debug information (see `Debugging`_)
  2831. - ``-Cc``: colorizes SPIR-V disassembly
  2832. - ``-No``: adds instruction byte offsets to SPIR-V disassembly
  2833. - ``-H``: Shows header includes and nesting depth
  2834. - ``-Vi``: Shows details about the include process
  2835. - ``-Vd``: Disables SPIR-V verification
  2836. - ``-WX``: Treats warnings as errors
  2837. - ``-no-warnings``: Suppresses all warnings
  2838. - ``-flegacy-macro-expansion``: expands the operands before performing
  2839. token-pasting operation (fxc behavior)
  2840. Vulkan-specific options
  2841. -----------------------
  2842. The following command line options are added into ``dxc`` to support SPIR-V
  2843. codegen for Vulkan:
  2844. - ``-spirv``: Generates SPIR-V code.
  2845. - ``-fvk-b-shift N M``: Shifts by ``N`` the inferred binding numbers for all
  2846. resources in b-type registers of space ``M``. Specifically, for a resouce
  2847. attached with ``:register(bX, spaceM)`` but not ``[vk::binding(...)]``,
  2848. sets its Vulkan descriptor set to ``M`` and binding number to ``X + N``. If
  2849. you need to shift the inferred binding numbers for more than one space,
  2850. provide more than one such option. If more than one such option is provided
  2851. for the same space, the last one takes effect. If you need to shift the
  2852. inferred binding numbers for all sets, use ``all`` as ``M``.
  2853. See `HLSL register and Vulkan binding`_ for explanation and examples.
  2854. - ``-fvk-t-shift N M``, similar to ``-fvk-b-shift``, but for t-type registers.
  2855. - ``-fvk-s-shift N M``, similar to ``-fvk-b-shift``, but for s-type registers.
  2856. - ``-fvk-u-shift N M``, similar to ``-fvk-b-shift``, but for u-type registers.
  2857. - ``-fvk-bind-register xX Y N M`` (short alias: ``-vkbr``): Binds the resouce
  2858. at ``register(xX, spaceY)`` to descriptor set ``M`` and binding ``N``. This
  2859. option cannot be used together with other binding assignment options.
  2860. It requires all source code resources have ``:register()`` attribute and
  2861. all registers have corresponding Vulkan descriptors specified using this
  2862. option.
  2863. - ``-fvk-bind-globals N M``: Places the ``$Globals`` cbuffer at
  2864. descriptor set #M and binding #N. See `HLSL global variables and Vulkan binding`_
  2865. for explanation and examples.
  2866. - ``-fvk-use-gl-layout``: Uses strict OpenGL ``std140``/``std430``
  2867. layout rules for resources.
  2868. - ``-fvk-use-dx-layout``: Uses DirectX layout rules for resources.
  2869. - ``-fvk-invert-y``: Negates (additively inverts) SV_Position.y before writing
  2870. to stage output. Used to accommodate the difference between Vulkan's
  2871. coordinate system and DirectX's. Only allowed in VS/DS/GS.
  2872. - ``-fvk-use-dx-position-w``: Reciprocates (multiplicatively inverts)
  2873. SV_Position.w after reading from stage input. Used to accommodate the
  2874. difference between Vulkan DirectX: the w component of SV_Position in PS is
  2875. stored as 1/w in Vulkan. Only recognized in PS; applying to other stages
  2876. is no-op.
  2877. - ``-fvk-stage-io-order={alpha|decl}``: Assigns the stage input/output variable
  2878. location number according to alphabetical order or declaration order. See
  2879. `HLSL semantic and Vulkan Location`_ for more details.
  2880. - ``-fspv-reflect``: Emits additional SPIR-V instructions to aid reflection.
  2881. - ``-fspv-debug=<category>``: Controls what category of debug information
  2882. should be emitted. Accepted values are ``file``, ``source``, ``line``, and
  2883. ``tool``. See `Debugging`_ for more details.
  2884. - ``-fspv-extension=<extension>``: Only allows using ``<extension>`` in CodeGen.
  2885. If you want to allow multiple extensions, provide more than one such option. If you
  2886. want to allow *all* KHR extensions, use ``-fspv-extension=KHR``.
  2887. - ``-fspv-target-env=<env>``: Specifies the target environment for this compilation.
  2888. The current valid options are ``vulkan1.0`` and ``vulkan1.1``. If no target
  2889. environment is provided, ``vulkan1.0`` is used as default.
  2890. - ``-fspv-flatten-resource-arrays``: Flattens arrays of textures and samplers
  2891. into individual resources, each taking one binding number. For example, an
  2892. array of 3 textures will become 3 texture resources taking 3 binding numbers.
  2893. This makes the behavior similar to DX. Without this option, you would get 1
  2894. array object taking 1 binding number. Note that arrays of
  2895. {RW|Append|Consume}StructuredBuffers are currently not supported in the
  2896. SPIR-V backend. Also note that this requires the optimizer to be able to
  2897. resolve all array accesses with constant indeces. Therefore, all loops using
  2898. the resource arrays must be marked with ``[unroll]``.
  2899. - ``-Wno-vk-ignored-features``: Does not emit warnings on ignored features
  2900. resulting from no Vulkan support, e.g., cbuffer member initializer.
  2901. Unsupported HLSL Features
  2902. =========================
  2903. The following HLSL language features are not supported in SPIR-V codegen,
  2904. either because of no Vulkan equivalents at the moment, or because of deprecation.
  2905. * Literal/immediate sampler state: deprecated feature. The compiler will
  2906. emit a warning and ignore it.
  2907. * ``abort()`` intrinsic function: no Vulkan equivalent. The compiler will emit
  2908. an error.
  2909. * ``GetRenderTargetSampleCount()`` intrinsic function: no Vulkan equivalent.
  2910. (Its GLSL counterpart is ``gl_NumSamples``, which is not available in GLSL for
  2911. Vulkan.) The compiler will emit an error.
  2912. * ``GetRenderTargetSamplePosition()`` intrinsic function: no Vulkan equivalent.
  2913. (``gl_SamplePosition`` provides similar functionality but it's only for the
  2914. sample currently being processed.) The compiler will emit an error.
  2915. * ``tex*()`` intrinsic functions: deprecated features. The compiler will
  2916. emit errors.
  2917. * ``.GatherCmpGreen()``, ``.GatherCmpBlue()``, ``.GatherCmpAlpha()`` intrinsic
  2918. method: no Vulkan equivalent. (SPIR-V ``OpImageDrefGather`` instruction does
  2919. not take component as input.) The compiler will emit an error.
  2920. * Since ``StructuredBuffer``, ``RWStructuredBuffer``, ``ByteAddressBuffer``, and
  2921. ``RWByteAddressBuffer`` are not represented as image types in SPIR-V, using the
  2922. output unsigned integer ``status`` argument in their ``Load*`` methods is not
  2923. supported. Using these methods with the ``status`` argument will cause a compiler error.
  2924. * Applying ``row_major`` or ``column_major`` attributes to a stand-alone matrix will be
  2925. ignored by the compiler because ``RowMajor`` and ``ColMajor`` decorations in SPIR-V are
  2926. only allowed to be applied to members of structures. A warning will be issued by the compiler.
  2927. * The Hull shader ``partitioning`` attribute may not have the ``pow2`` value. The compiler
  2928. will emit an error. Other attribute values are supported and described in the
  2929. `Hull Entry Point Attributes`_ section.
  2930. * ``cbuffer``/``tbuffer`` member initializer: no Vulkan equivalent. The compiler
  2931. will emit an warning and ignore it.
  2932. Appendix
  2933. ==========
  2934. Appendix A. Matrix Representation
  2935. ---------------------------------
  2936. Consider a matrix in HLSL defined as ``float2x3 m;``. Conceptually, this is a matrix with 2 rows and 3 columns.
  2937. This means that you can access its elements via expressions such as ``m[i][j]``, where ``i`` can be ``{0, 1}`` and ``j`` can be ``{0, 1, 2}``.
  2938. Now let's look how matrices are defined in SPIR-V:
  2939. .. code:: spirv
  2940. %columnType = OpTypeVector %float <number of rows>
  2941. %matType = OpTypeMatrix %columnType <number of columns>
  2942. As you can see, SPIR-V conceptually represents matrices as a collection of vectors where each vector is a *column*.
  2943. Now, let's represent our float2x3 matrix in SPIR-V. If we choose a naive translation (3 columns, each of which is a vector of size 2), we get:
  2944. .. code:: spirv
  2945. %v2float = OpTypeVector %float 2
  2946. %mat3v2float = OpTypeMatrix %v2float 3
  2947. Now, let's use this naive translation to access into the matrix (e.g. ``m[0][2]``). This is evaluated by first finding ``n = m[0]``, and then finding ``n[2]``.
  2948. Notice that in HLSL, ``m[0]`` represents a row, which is a vector of size 3. But accessing the first dimension of the SPIR-V matrix give us
  2949. the first column which is a vector of size 2.
  2950. .. code:: spirv
  2951. ; n is a vector of size 2
  2952. %n = OpAccessChain %v2float %m %int_0
  2953. Notice that in HLSL access ``m[i][j]``, ``i`` can be ``{0, 1}`` and ``j`` can be ``{0, 1, 2}``.
  2954. But in SPIR-V OpAccessChain access, the first index (``i``) can be ``{0, 1, 2}`` and the second index (``j``) can be ``{1, 0}``.
  2955. Therefore, the naive translation does not work well with indexing.
  2956. As a result, we must translate a given HLSL float2x3 matrix (with 2 rows and 3 columns) as a SPIR-V matrix with 3 rows and 2 columns:
  2957. .. code:: spirv
  2958. %v3float = OpTypeVector %float 3
  2959. %mat2v3float = OpTypeMatrix %v3float 2
  2960. This way, all accesses into the matrix can be naturally handled correctly.
  2961. Packing
  2962. ~~~~~~~
  2963. The HLSL ``row_major`` and ``column_major`` type modifiers change the way packing is done.
  2964. The following table provides an example which should make our translation more clear:
  2965. +------------------+---------------------------+---------------------------+-----------------------------+-------------------+
  2966. | Host CPU Data | HLSL Variable | GPU (HLSL Representation) | GPU (SPIR-V Representation) | SPIR-V Decoration |
  2967. +==================+===========================+===========================+=============================+===================+
  2968. |``{1,2,3,4,5,6}`` | ``float2x3`` | ``[1 3 5]`` | ``[1 2]`` | |
  2969. | | | | | |
  2970. | | | ``[2 4 6]`` | ``[3 4]`` | ``RowMajor`` |
  2971. | | | | | |
  2972. | | | | ``[5 6]`` | |
  2973. +------------------+---------------------------+---------------------------+-----------------------------+-------------------+
  2974. |``{1,2,3,4,5,6}`` | ``column_major float2x3`` | ``[1 3 5]`` | ``[1 2]`` | |
  2975. | | | | | |
  2976. | | | ``[2 4 6]`` | ``[3 4]`` | ``RowMajor`` |
  2977. | | | | | |
  2978. | | | | ``[5 6]`` | |
  2979. +------------------+---------------------------+---------------------------+-----------------------------+-------------------+
  2980. |``{1,2,3,4,5,6}`` | ``row_major float2x3`` | ``[1 2 3]`` | ``[1 4]`` | |
  2981. | | | | | |
  2982. | | | ``[4 5 6]`` | ``[2 5]`` | ``ColMajor`` |
  2983. | | | | | |
  2984. | | | | ``[3 6]`` | |
  2985. +------------------+---------------------------+---------------------------+-----------------------------+-------------------+