mojoshader.h 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232
  1. /**
  2. * MojoShader; generate shader programs from bytecode of compiled
  3. * Direct3D shaders.
  4. *
  5. * Please see the file LICENSE.txt in the source's root directory.
  6. *
  7. * This file written by Ryan C. Gordon.
  8. */
  9. // Modified by Lasse Oorni for Urho3D
  10. #ifndef _INCL_MOJOSHADER_H_
  11. #define _INCL_MOJOSHADER_H_
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /* You can define this if you aren't generating mojoshader_version.h */
  16. // Urho3D: commented out to avoid the define
  17. /*
  18. #ifndef MOJOSHADER_NO_VERSION_INCLUDE
  19. #include "mojoshader_version.h"
  20. #endif
  21. */
  22. #ifndef MOJOSHADER_VERSION
  23. #define MOJOSHADER_VERSION -1
  24. #endif
  25. #ifndef MOJOSHADER_CHANGESET
  26. #define MOJOSHADER_CHANGESET "???"
  27. #endif
  28. /*
  29. * For determining the version of MojoShader you are using:
  30. * const int compiled_against = MOJOSHADER_VERSION;
  31. * const int linked_against = MOJOSHADER_version();
  32. *
  33. * The version is a single integer that increments, not a major/minor value.
  34. */
  35. int MOJOSHADER_version(void);
  36. /*
  37. * For determining the revision control changeset of MojoShader you are using:
  38. * const char *compiled_against = MOJOSHADER_CHANGESET;
  39. * const char *linked_against = MOJOSHADER_changeset();
  40. *
  41. * The version is an arbitrary, null-terminated ASCII string. It is probably
  42. * a hash that represents a revision control changeset, and can't be
  43. * compared to any other string to determine chronology.
  44. *
  45. * Do not attempt to free this string; it's statically allocated.
  46. */
  47. const char *MOJOSHADER_changeset(void);
  48. /*
  49. * These allocators work just like the C runtime's malloc() and free()
  50. * (in fact, they probably use malloc() and free() internally if you don't
  51. * specify your own allocator, but don't rely on that behaviour).
  52. * (data) is the pointer you supplied when specifying these allocator
  53. * callbacks, in case you need instance-specific data...it is passed through
  54. * to your allocator unmolested, and can be NULL if you like.
  55. */
  56. typedef void *(*MOJOSHADER_malloc)(int bytes, void *data);
  57. typedef void (*MOJOSHADER_free)(void *ptr, void *data);
  58. /*
  59. * These are enum values, but they also can be used in bitmasks, so we can
  60. * test if an opcode is acceptable: if (op->shader_types & ourtype) {} ...
  61. */
  62. typedef enum
  63. {
  64. MOJOSHADER_TYPE_UNKNOWN = 0,
  65. MOJOSHADER_TYPE_PIXEL = (1 << 0),
  66. MOJOSHADER_TYPE_VERTEX = (1 << 1),
  67. MOJOSHADER_TYPE_GEOMETRY = (1 << 2), /* (not supported yet.) */
  68. MOJOSHADER_TYPE_ANY = 0xFFFFFFFF /* used for bitmasks */
  69. } MOJOSHADER_shaderType;
  70. /*
  71. * Data types for vertex attribute streams.
  72. */
  73. typedef enum
  74. {
  75. MOJOSHADER_ATTRIBUTE_UNKNOWN = -1, /* housekeeping; not returned. */
  76. MOJOSHADER_ATTRIBUTE_BYTE,
  77. MOJOSHADER_ATTRIBUTE_UBYTE,
  78. MOJOSHADER_ATTRIBUTE_SHORT,
  79. MOJOSHADER_ATTRIBUTE_USHORT,
  80. MOJOSHADER_ATTRIBUTE_INT,
  81. MOJOSHADER_ATTRIBUTE_UINT,
  82. MOJOSHADER_ATTRIBUTE_FLOAT,
  83. MOJOSHADER_ATTRIBUTE_DOUBLE,
  84. MOJOSHADER_ATTRIBUTE_HALF_FLOAT, /* MAYBE available in your OpenGL! */
  85. } MOJOSHADER_attributeType;
  86. /*
  87. * Data types for uniforms. See MOJOSHADER_uniform for more information.
  88. */
  89. typedef enum
  90. {
  91. MOJOSHADER_UNIFORM_UNKNOWN = -1, /* housekeeping value; never returned. */
  92. MOJOSHADER_UNIFORM_FLOAT,
  93. MOJOSHADER_UNIFORM_INT,
  94. MOJOSHADER_UNIFORM_BOOL,
  95. } MOJOSHADER_uniformType;
  96. /*
  97. * These are the uniforms to be set for a shader. "Uniforms" are what Direct3D
  98. * calls "Constants" ... IDirect3DDevice::SetVertexShaderConstantF() would
  99. * need this data, for example. These integers are register indexes. So if
  100. * index==6 and type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a
  101. * 4-float vector to be specified for what would be register "c6" in D3D
  102. * assembly language, before drawing with the shader.
  103. * (array_count) means this is an array of uniforms...this happens in some
  104. * profiles when we see a relative address ("c0[a0.x]", not the usual "c0").
  105. * In those cases, the shader was built to set some range of constant
  106. * registers as an array. You should set this array with (array_count)
  107. * elements from the constant register file, starting at (index) instead of
  108. * just a single uniform. To be extra difficult, you'll need to fill in the
  109. * correct values from the MOJOSHADER_constant data into the appropriate
  110. * parts of the array, overriding the constant register file. Fun!
  111. * (constant) says whether this is a constant array; these need to be loaded
  112. * once at creation time, from the constant list and not ever updated from
  113. * the constant register file. This is a workaround for limitations in some
  114. * profiles.
  115. * (name) is a profile-specific variable name; it may be NULL if it isn't
  116. * applicable to the requested profile.
  117. */
  118. typedef struct MOJOSHADER_uniform
  119. {
  120. MOJOSHADER_uniformType type;
  121. int index;
  122. int array_count;
  123. int constant;
  124. const char *name;
  125. } MOJOSHADER_uniform;
  126. /*
  127. * These are the constants defined in a shader. These are data values
  128. * hardcoded in a shader (with the DEF, DEFI, DEFB instructions), which
  129. * override your Uniforms. This data is largely for informational purposes,
  130. * since they are compiled in and can't be changed, like Uniforms can be.
  131. * These integers are register indexes. So if index==6 and
  132. * type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a 4-float vector
  133. * to be specified for what would be register "c6" in D3D assembly language,
  134. * before drawing with the shader.
  135. * (value) is the value of the constant, unioned by type.
  136. */
  137. typedef struct MOJOSHADER_constant
  138. {
  139. MOJOSHADER_uniformType type;
  140. int index;
  141. union
  142. {
  143. float f[4]; /* if type==MOJOSHADER_UNIFORM_FLOAT */
  144. int i[4]; /* if type==MOJOSHADER_UNIFORM_INT */
  145. int b; /* if type==MOJOSHADER_UNIFORM_BOOL */
  146. } value;
  147. } MOJOSHADER_constant;
  148. /*
  149. * Data types for samplers. See MOJOSHADER_sampler for more information.
  150. */
  151. typedef enum
  152. {
  153. MOJOSHADER_SAMPLER_UNKNOWN = -1, /* housekeeping value; never returned. */
  154. MOJOSHADER_SAMPLER_2D,
  155. MOJOSHADER_SAMPLER_CUBE,
  156. MOJOSHADER_SAMPLER_VOLUME,
  157. } MOJOSHADER_samplerType;
  158. /*
  159. * These are the samplers to be set for a shader. ...
  160. * IDirect3DDevice::SetTexture() would need this data, for example.
  161. * These integers are the sampler "stage". So if index==6 and
  162. * type==MOJOSHADER_SAMPLER_2D, that means we'd expect a regular 2D texture
  163. * to be specified for what would be register "s6" in D3D assembly language,
  164. * before drawing with the shader.
  165. * (name) is a profile-specific variable name; it may be NULL if it isn't
  166. * applicable to the requested profile.
  167. * (texbem) will be non-zero if a TEXBEM opcode references this sampler. This
  168. * is only used in legacy shaders (ps_1_1 through ps_1_3), but it needs some
  169. * special support to work, as we have to load a magic uniform behind the
  170. * scenes to support it. Most code can ignore this field in general, and no
  171. * one has to touch it unless they really know what they're doing.
  172. */
  173. typedef struct MOJOSHADER_sampler
  174. {
  175. MOJOSHADER_samplerType type;
  176. int index;
  177. const char *name;
  178. int texbem;
  179. } MOJOSHADER_sampler;
  180. /*
  181. * This struct is used if you have to force a sampler to a specific type.
  182. * Generally, you can ignore this, but if you have, say, a ps_1_1
  183. * shader, you might need to specify what the samplers are meant to be
  184. * to get correct results, as Shader Model 1 samples textures according
  185. * to what is bound to a sampler at the moment instead of what the shader
  186. * is hardcoded to expect.
  187. */
  188. typedef struct MOJOSHADER_samplerMap
  189. {
  190. int index;
  191. MOJOSHADER_samplerType type;
  192. } MOJOSHADER_samplerMap;
  193. /*
  194. * Data types for attributes. See MOJOSHADER_attribute for more information.
  195. */
  196. typedef enum
  197. {
  198. MOJOSHADER_USAGE_UNKNOWN = -1, /* housekeeping value; never returned. */
  199. MOJOSHADER_USAGE_POSITION,
  200. MOJOSHADER_USAGE_BLENDWEIGHT,
  201. MOJOSHADER_USAGE_BLENDINDICES,
  202. MOJOSHADER_USAGE_NORMAL,
  203. MOJOSHADER_USAGE_POINTSIZE,
  204. MOJOSHADER_USAGE_TEXCOORD,
  205. MOJOSHADER_USAGE_TANGENT,
  206. MOJOSHADER_USAGE_BINORMAL,
  207. MOJOSHADER_USAGE_TESSFACTOR,
  208. MOJOSHADER_USAGE_POSITIONT,
  209. MOJOSHADER_USAGE_COLOR,
  210. MOJOSHADER_USAGE_FOG,
  211. MOJOSHADER_USAGE_DEPTH,
  212. MOJOSHADER_USAGE_SAMPLE,
  213. MOJOSHADER_USAGE_TOTAL, /* housekeeping value; never returned. */
  214. } MOJOSHADER_usage;
  215. /*
  216. * These are the attributes to be set for a shader. "Attributes" are what
  217. * Direct3D calls "Vertex Declarations Usages" ...
  218. * IDirect3DDevice::CreateVertexDeclaration() would need this data, for
  219. * example. Each attribute is associated with an array of data that uses one
  220. * element per-vertex. So if usage==MOJOSHADER_USAGE_COLOR and index==1, that
  221. * means we'd expect a secondary color array to be bound to this shader
  222. * before drawing.
  223. * (name) is a profile-specific variable name; it may be NULL if it isn't
  224. * applicable to the requested profile.
  225. */
  226. typedef struct MOJOSHADER_attribute
  227. {
  228. MOJOSHADER_usage usage;
  229. int index;
  230. const char *name;
  231. } MOJOSHADER_attribute;
  232. /*
  233. * Use this if you want to specify newly-parsed code to swizzle incoming
  234. * data. This can be useful if you know, at parse time, that a shader
  235. * will be processing data on COLOR0 that should be RGBA, but you'll
  236. * be passing it a vertex array full of ARGB instead.
  237. */
  238. typedef struct MOJOSHADER_swizzle
  239. {
  240. MOJOSHADER_usage usage;
  241. unsigned int index;
  242. unsigned char swizzles[4]; /* {0,1,2,3} == .xyzw, {2,2,2,2} == .zzzz */
  243. } MOJOSHADER_swizzle;
  244. /*
  245. * MOJOSHADER_symbol data.
  246. *
  247. * These are used to expose high-level information in shader bytecode.
  248. * They associate HLSL variables with registers. This data is used for both
  249. * debugging and optimization.
  250. */
  251. typedef enum
  252. {
  253. MOJOSHADER_SYMREGSET_BOOL,
  254. MOJOSHADER_SYMREGSET_INT4,
  255. MOJOSHADER_SYMREGSET_FLOAT4,
  256. MOJOSHADER_SYMREGSET_SAMPLER,
  257. } MOJOSHADER_symbolRegisterSet;
  258. typedef enum
  259. {
  260. MOJOSHADER_SYMCLASS_SCALAR,
  261. MOJOSHADER_SYMCLASS_VECTOR,
  262. MOJOSHADER_SYMCLASS_MATRIX_ROWS,
  263. MOJOSHADER_SYMCLASS_MATRIX_COLUMNS,
  264. MOJOSHADER_SYMCLASS_OBJECT,
  265. MOJOSHADER_SYMCLASS_STRUCT,
  266. } MOJOSHADER_symbolClass;
  267. typedef enum
  268. {
  269. MOJOSHADER_SYMTYPE_VOID,
  270. MOJOSHADER_SYMTYPE_BOOL,
  271. MOJOSHADER_SYMTYPE_INT,
  272. MOJOSHADER_SYMTYPE_FLOAT,
  273. MOJOSHADER_SYMTYPE_STRING,
  274. MOJOSHADER_SYMTYPE_TEXTURE,
  275. MOJOSHADER_SYMTYPE_TEXTURE1D,
  276. MOJOSHADER_SYMTYPE_TEXTURE2D,
  277. MOJOSHADER_SYMTYPE_TEXTURE3D,
  278. MOJOSHADER_SYMTYPE_TEXTURECUBE,
  279. MOJOSHADER_SYMTYPE_SAMPLER,
  280. MOJOSHADER_SYMTYPE_SAMPLER1D,
  281. MOJOSHADER_SYMTYPE_SAMPLER2D,
  282. MOJOSHADER_SYMTYPE_SAMPLER3D,
  283. MOJOSHADER_SYMTYPE_SAMPLERCUBE,
  284. MOJOSHADER_SYMTYPE_PIXELSHADER,
  285. MOJOSHADER_SYMTYPE_VERTEXSHADER,
  286. MOJOSHADER_SYMTYPE_PIXELFRAGMENT,
  287. MOJOSHADER_SYMTYPE_VERTEXFRAGMENT,
  288. MOJOSHADER_SYMTYPE_UNSUPPORTED,
  289. } MOJOSHADER_symbolType;
  290. typedef struct MOJOSHADER_symbolStructMember MOJOSHADER_symbolStructMember;
  291. typedef struct MOJOSHADER_symbolTypeInfo
  292. {
  293. MOJOSHADER_symbolClass parameter_class;
  294. MOJOSHADER_symbolType parameter_type;
  295. unsigned int rows;
  296. unsigned int columns;
  297. unsigned int elements;
  298. unsigned int member_count;
  299. MOJOSHADER_symbolStructMember *members;
  300. } MOJOSHADER_symbolTypeInfo;
  301. struct MOJOSHADER_symbolStructMember
  302. {
  303. const char *name;
  304. MOJOSHADER_symbolTypeInfo info;
  305. };
  306. typedef struct MOJOSHADER_symbol
  307. {
  308. const char *name;
  309. MOJOSHADER_symbolRegisterSet register_set;
  310. unsigned int register_index;
  311. unsigned int register_count;
  312. MOJOSHADER_symbolTypeInfo info;
  313. } MOJOSHADER_symbol;
  314. /*
  315. * These are used with MOJOSHADER_error as special case positions.
  316. */
  317. #define MOJOSHADER_POSITION_NONE (-3)
  318. #define MOJOSHADER_POSITION_BEFORE (-2)
  319. #define MOJOSHADER_POSITION_AFTER (-1)
  320. typedef struct MOJOSHADER_error
  321. {
  322. /*
  323. * Human-readable error, if there is one. Will be NULL if there was no
  324. * error. The string will be UTF-8 encoded, and English only. Most of
  325. * these shouldn't be shown to the end-user anyhow.
  326. */
  327. const char *error;
  328. /*
  329. * Filename where error happened. This can be NULL if the information
  330. * isn't available.
  331. */
  332. const char *filename;
  333. /*
  334. * Position of error, if there is one. Will be MOJOSHADER_POSITION_NONE if
  335. * there was no error, MOJOSHADER_POSITION_BEFORE if there was an error
  336. * before processing started, and MOJOSHADER_POSITION_AFTER if there was
  337. * an error during final processing. If >= 0, MOJOSHADER_parse() sets
  338. * this to the byte offset (starting at zero) into the bytecode you
  339. * supplied, and MOJOSHADER_assemble(), MOJOSHADER_parseAst(), and
  340. * MOJOSHADER_compile() sets this to a a line number in the source code
  341. * you supplied (starting at one).
  342. */
  343. int error_position;
  344. } MOJOSHADER_error;
  345. /* !!! FIXME: document me. */
  346. typedef enum MOJOSHADER_preshaderOpcode
  347. {
  348. MOJOSHADER_PRESHADEROP_NOP,
  349. MOJOSHADER_PRESHADEROP_MOV,
  350. MOJOSHADER_PRESHADEROP_NEG,
  351. MOJOSHADER_PRESHADEROP_RCP,
  352. MOJOSHADER_PRESHADEROP_FRC,
  353. MOJOSHADER_PRESHADEROP_EXP,
  354. MOJOSHADER_PRESHADEROP_LOG,
  355. MOJOSHADER_PRESHADEROP_RSQ,
  356. MOJOSHADER_PRESHADEROP_SIN,
  357. MOJOSHADER_PRESHADEROP_COS,
  358. MOJOSHADER_PRESHADEROP_ASIN,
  359. MOJOSHADER_PRESHADEROP_ACOS,
  360. MOJOSHADER_PRESHADEROP_ATAN,
  361. MOJOSHADER_PRESHADEROP_MIN,
  362. MOJOSHADER_PRESHADEROP_MAX,
  363. MOJOSHADER_PRESHADEROP_LT,
  364. MOJOSHADER_PRESHADEROP_GE,
  365. MOJOSHADER_PRESHADEROP_ADD,
  366. MOJOSHADER_PRESHADEROP_MUL,
  367. MOJOSHADER_PRESHADEROP_ATAN2,
  368. MOJOSHADER_PRESHADEROP_DIV,
  369. MOJOSHADER_PRESHADEROP_CMP,
  370. MOJOSHADER_PRESHADEROP_MOVC,
  371. MOJOSHADER_PRESHADEROP_DOT,
  372. MOJOSHADER_PRESHADEROP_NOISE,
  373. MOJOSHADER_PRESHADEROP_SCALAR_OPS,
  374. MOJOSHADER_PRESHADEROP_MIN_SCALAR = MOJOSHADER_PRESHADEROP_SCALAR_OPS,
  375. MOJOSHADER_PRESHADEROP_MAX_SCALAR,
  376. MOJOSHADER_PRESHADEROP_LT_SCALAR,
  377. MOJOSHADER_PRESHADEROP_GE_SCALAR,
  378. MOJOSHADER_PRESHADEROP_ADD_SCALAR,
  379. MOJOSHADER_PRESHADEROP_MUL_SCALAR,
  380. MOJOSHADER_PRESHADEROP_ATAN2_SCALAR,
  381. MOJOSHADER_PRESHADEROP_DIV_SCALAR,
  382. MOJOSHADER_PRESHADEROP_DOT_SCALAR,
  383. MOJOSHADER_PRESHADEROP_NOISE_SCALAR,
  384. } MOJOSHADER_preshaderOpcode;
  385. typedef enum MOJOSHADER_preshaderOperandType
  386. {
  387. MOJOSHADER_PRESHADEROPERAND_INPUT,
  388. MOJOSHADER_PRESHADEROPERAND_OUTPUT,
  389. MOJOSHADER_PRESHADEROPERAND_LITERAL,
  390. MOJOSHADER_PRESHADEROPERAND_TEMP,
  391. } MOJOSHADER_preshaderOperandType;
  392. typedef struct MOJOSHADER_preshaderOperand
  393. {
  394. MOJOSHADER_preshaderOperandType type;
  395. unsigned int index;
  396. } MOJOSHADER_preshaderOperand;
  397. typedef struct MOJOSHADER_preshaderInstruction
  398. {
  399. MOJOSHADER_preshaderOpcode opcode;
  400. unsigned int element_count;
  401. unsigned int operand_count;
  402. MOJOSHADER_preshaderOperand operands[3];
  403. } MOJOSHADER_preshaderInstruction;
  404. typedef struct MOJOSHADER_preshader
  405. {
  406. unsigned int literal_count;
  407. double *literals;
  408. unsigned int temp_count; /* scalar, not vector! */
  409. unsigned int symbol_count;
  410. MOJOSHADER_symbol *symbols;
  411. unsigned int instruction_count;
  412. MOJOSHADER_preshaderInstruction *instructions;
  413. } MOJOSHADER_preshader;
  414. /*
  415. * Structure used to return data from parsing of a shader...
  416. */
  417. /* !!! FIXME: most of these ints should be unsigned. */
  418. typedef struct MOJOSHADER_parseData
  419. {
  420. /*
  421. * The number of elements pointed to by (errors).
  422. */
  423. int error_count;
  424. /*
  425. * (error_count) elements of data that specify errors that were generated
  426. * by parsing this shader.
  427. * This can be NULL if there were no errors or if (error_count) is zero.
  428. */
  429. MOJOSHADER_error *errors;
  430. /*
  431. * The name of the profile used to parse the shader. Will be NULL on error.
  432. */
  433. const char *profile;
  434. /*
  435. * Bytes of output from parsing. Most profiles produce a string of source
  436. * code, but profiles that do binary output may not be text at all.
  437. * Will be NULL on error.
  438. */
  439. const char *output;
  440. /*
  441. * Byte count for output, not counting any null terminator. Most profiles
  442. * produce an ASCII string of source code (which will be null-terminated
  443. * even though that null char isn't included in output_len), but profiles
  444. * that do binary output may not be text at all. Will be 0 on error.
  445. */
  446. int output_len;
  447. /*
  448. * Count of Direct3D instruction slots used. This is meaningless in terms
  449. * of the actual output, as the profile will probably grow or reduce
  450. * the count (or for high-level languages, not have that information at
  451. * all). Also, as with Microsoft's own assembler, this value is just a
  452. * rough estimate, as unpredicable real-world factors make the actual
  453. * value vary at least a little from this count. Still, it can give you
  454. * a rough idea of the size of your shader. Will be zero on error.
  455. */
  456. int instruction_count;
  457. /*
  458. * The type of shader we parsed. Will be MOJOSHADER_TYPE_UNKNOWN on error.
  459. */
  460. MOJOSHADER_shaderType shader_type;
  461. /*
  462. * The shader's major version. If this was a "vs_3_0", this would be 3.
  463. */
  464. int major_ver;
  465. /*
  466. * The shader's minor version. If this was a "ps_1_4", this would be 4.
  467. * Two notes: for "vs_2_x", this is 1, and for "vs_3_sw", this is 255.
  468. */
  469. int minor_ver;
  470. /*
  471. * The number of elements pointed to by (uniforms).
  472. */
  473. int uniform_count;
  474. /*
  475. * (uniform_count) elements of data that specify Uniforms to be set for
  476. * this shader. See discussion on MOJOSHADER_uniform for details.
  477. * This can be NULL on error or if (uniform_count) is zero.
  478. */
  479. MOJOSHADER_uniform *uniforms;
  480. /*
  481. * The number of elements pointed to by (constants).
  482. */
  483. int constant_count;
  484. /*
  485. * (constant_count) elements of data that specify constants used in
  486. * this shader. See discussion on MOJOSHADER_constant for details.
  487. * This can be NULL on error or if (constant_count) is zero.
  488. * This is largely informational: constants are hardcoded into a shader.
  489. * The constants that you can set like parameters are in the "uniforms"
  490. * list.
  491. */
  492. MOJOSHADER_constant *constants;
  493. /*
  494. * The number of elements pointed to by (samplers).
  495. */
  496. int sampler_count;
  497. /*
  498. * (sampler_count) elements of data that specify Samplers to be set for
  499. * this shader. See discussion on MOJOSHADER_sampler for details.
  500. * This can be NULL on error or if (sampler_count) is zero.
  501. */
  502. MOJOSHADER_sampler *samplers;
  503. /* !!! FIXME: this should probably be "input" and not "attribute" */
  504. /*
  505. * The number of elements pointed to by (attributes).
  506. */
  507. int attribute_count;
  508. /* !!! FIXME: this should probably be "input" and not "attribute" */
  509. /*
  510. * (attribute_count) elements of data that specify Attributes to be set
  511. * for this shader. See discussion on MOJOSHADER_attribute for details.
  512. * This can be NULL on error or if (attribute_count) is zero.
  513. */
  514. MOJOSHADER_attribute *attributes;
  515. /*
  516. * The number of elements pointed to by (outputs).
  517. */
  518. int output_count;
  519. /*
  520. * (output_count) elements of data that specify outputs this shader
  521. * writes to. See discussion on MOJOSHADER_attribute for details.
  522. * This can be NULL on error or if (output_count) is zero.
  523. */
  524. MOJOSHADER_attribute *outputs;
  525. /*
  526. * The number of elements pointed to by (swizzles).
  527. */
  528. int swizzle_count;
  529. /* !!! FIXME: this should probably be "input" and not "attribute" */
  530. /*
  531. * (swizzle_count) elements of data that specify swizzles the shader will
  532. * apply to incoming attributes. This is a copy of what was passed to
  533. * MOJOSHADER_parseData().
  534. * This can be NULL on error or if (swizzle_count) is zero.
  535. */
  536. MOJOSHADER_swizzle *swizzles;
  537. /*
  538. * The number of elements pointed to by (symbols).
  539. */
  540. int symbol_count;
  541. /*
  542. * (symbol_count) elements of data that specify high-level symbol data
  543. * for the shader. This will be parsed from the CTAB section
  544. * in bytecode, and will be a copy of what you provide to
  545. * MOJOSHADER_assemble(). This data is optional.
  546. * This can be NULL on error or if (symbol_count) is zero.
  547. */
  548. MOJOSHADER_symbol *symbols;
  549. /*
  550. * !!! FIXME: document me.
  551. * This can be NULL on error or if no preshader was available.
  552. */
  553. MOJOSHADER_preshader *preshader;
  554. /*
  555. * This is the malloc implementation you passed to MOJOSHADER_parse().
  556. */
  557. MOJOSHADER_malloc malloc;
  558. /*
  559. * This is the free implementation you passed to MOJOSHADER_parse().
  560. */
  561. MOJOSHADER_free free;
  562. /*
  563. * This is the pointer you passed as opaque data for your allocator.
  564. */
  565. void *malloc_data;
  566. } MOJOSHADER_parseData;
  567. /*
  568. * Profile string for Direct3D assembly language output.
  569. */
  570. #define MOJOSHADER_PROFILE_D3D "d3d"
  571. /*
  572. * Profile string for passthrough of the original bytecode, unchanged.
  573. */
  574. #define MOJOSHADER_PROFILE_BYTECODE "bytecode"
  575. /*
  576. * Profile string for GLSL: OpenGL high-level shader language output.
  577. */
  578. #define MOJOSHADER_PROFILE_GLSL "glsl"
  579. /*
  580. * Profile string for GLSL 1.20: minor improvements to base GLSL spec.
  581. */
  582. #define MOJOSHADER_PROFILE_GLSL120 "glsl120"
  583. /*
  584. * Profile string for OpenGL ARB 1.0 shaders: GL_ARB_(vertex|fragment)_program.
  585. */
  586. #define MOJOSHADER_PROFILE_ARB1 "arb1"
  587. /*
  588. * Profile string for OpenGL ARB 1.0 shaders with Nvidia 2.0 extensions:
  589. * GL_NV_vertex_program2_option and GL_NV_fragment_program2
  590. */
  591. #define MOJOSHADER_PROFILE_NV2 "nv2"
  592. /*
  593. * Profile string for OpenGL ARB 1.0 shaders with Nvidia 3.0 extensions:
  594. * GL_NV_vertex_program3 and GL_NV_fragment_program2
  595. */
  596. #define MOJOSHADER_PROFILE_NV3 "nv3"
  597. /*
  598. * Profile string for OpenGL ARB 1.0 shaders with Nvidia 4.0 extensions:
  599. * GL_NV_gpu_program4
  600. */
  601. #define MOJOSHADER_PROFILE_NV4 "nv4"
  602. /*
  603. * Determine the highest supported Shader Model for a profile.
  604. */
  605. int MOJOSHADER_maxShaderModel(const char *profile);
  606. /*
  607. * Parse a compiled Direct3D shader's bytecode.
  608. *
  609. * This is your primary entry point into MojoShader. You need to pass it
  610. * a compiled D3D shader and tell it which "profile" you want to use to
  611. * convert it into useful data.
  612. *
  613. * The available profiles are the set of MOJOSHADER_PROFILE_* defines.
  614. * Note that MojoShader may be built without support for all listed
  615. * profiles (in which case using one here will return with an error).
  616. *
  617. * As parsing requires some memory to be allocated, you may provide a custom
  618. * allocator to this function, which will be used to allocate/free memory.
  619. * They function just like malloc() and free(). We do not use realloc().
  620. * If you don't care, pass NULL in for the allocator functions. If your
  621. * allocator needs instance-specific data, you may supply it with the
  622. * (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
  623. *
  624. * This function returns a MOJOSHADER_parseData.
  625. *
  626. * This function will never return NULL, even if the system is completely
  627. * out of memory upon entry (in which case, this function returns a static
  628. * MOJOSHADER_parseData object, which is still safe to pass to
  629. * MOJOSHADER_freeParseData()).
  630. *
  631. * You can tell the generated program to swizzle certain inputs. If you know
  632. * that COLOR0 should be RGBA but you're passing in ARGB, you can specify
  633. * a swizzle of { MOJOSHADER_USAGE_COLOR, 0, {1,2,3,0} } to (swiz). If the
  634. * input register in the code would produce reg.ywzx, that swizzle would
  635. * change it to reg.wzxy ... (swiz) can be NULL.
  636. *
  637. * You can force the shader to expect samplers of certain types. Generally
  638. * you don't need this, as Shader Model 2 and later always specify what they
  639. * expect samplers to be (2D, cubemap, etc). Shader Model 1, however, just
  640. * uses whatever is bound to a given sampler at draw time, but this doesn't
  641. * work in OpenGL, etc. In these cases, MojoShader will default to
  642. * 2D texture sampling (or cubemap sampling, in cases where it makes sense,
  643. * like the TEXM3X3TEX opcode), which works 75% of the time, but if you
  644. * really needed something else, you'll need to specify it here. This can
  645. * also be used, at your own risk, to override DCL opcodes in shaders: if
  646. * the shader explicit says 2D, but you want Cubemap, for example, you can
  647. * use this to override. If you aren't sure about any of this stuff, you can
  648. * (and should) almost certainly ignore it: (smap) can be NULL.
  649. *
  650. * This function is thread safe, so long as (m) and (f) are too, and that
  651. * (tokenbuf) remains intact for the duration of the call. This allows you
  652. * to parse several shaders on separate CPU cores at the same time.
  653. */
  654. const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile,
  655. const unsigned char *tokenbuf,
  656. const unsigned int bufsize,
  657. const MOJOSHADER_swizzle *swiz,
  658. const unsigned int swizcount,
  659. const MOJOSHADER_samplerMap *smap,
  660. const unsigned int smapcount,
  661. MOJOSHADER_malloc m,
  662. MOJOSHADER_free f,
  663. void *d);
  664. /*
  665. * Call this to dispose of parsing results when you are done with them.
  666. * This will call the MOJOSHADER_free function you provided to
  667. * MOJOSHADER_parse multiple times, if you provided one.
  668. * Passing a NULL here is a safe no-op.
  669. *
  670. * This function is thread safe, so long as any allocator you passed into
  671. * MOJOSHADER_parse() is, too.
  672. */
  673. void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *data);
  674. /* Effects interface... */ /* !!! FIXME: THIS API IS NOT STABLE YET! */
  675. typedef struct MOJOSHADER_effectParam
  676. {
  677. const char *name;
  678. const char *semantic;
  679. } MOJOSHADER_effectParam;
  680. typedef struct MOJOSHADER_effectState
  681. {
  682. unsigned int type;
  683. } MOJOSHADER_effectState;
  684. typedef struct MOJOSHADER_effectPass
  685. {
  686. const char *name;
  687. unsigned int state_count;
  688. MOJOSHADER_effectState *states;
  689. } MOJOSHADER_effectPass;
  690. typedef struct MOJOSHADER_effectTechnique
  691. {
  692. const char *name;
  693. unsigned int pass_count;
  694. MOJOSHADER_effectPass *passes;
  695. } MOJOSHADER_effectTechnique;
  696. typedef struct MOJOSHADER_effectTexture
  697. {
  698. unsigned int param;
  699. const char *name;
  700. } MOJOSHADER_effectTexture;
  701. typedef struct MOJOSHADER_effectShader
  702. {
  703. unsigned int technique;
  704. unsigned int pass;
  705. const MOJOSHADER_parseData *shader;
  706. } MOJOSHADER_effectShader;
  707. /*
  708. * Structure used to return data from parsing of an effect file...
  709. */
  710. /* !!! FIXME: most of these ints should be unsigned. */
  711. typedef struct MOJOSHADER_effect
  712. {
  713. /*
  714. * The number of elements pointed to by (errors).
  715. */
  716. int error_count;
  717. /*
  718. * (error_count) elements of data that specify errors that were generated
  719. * by parsing this shader.
  720. * This can be NULL if there were no errors or if (error_count) is zero.
  721. */
  722. MOJOSHADER_error *errors;
  723. /*
  724. * The name of the profile used to parse the shader. Will be NULL on error.
  725. */
  726. const char *profile;
  727. /*
  728. * The number of params pointed to by (params).
  729. */
  730. int param_count;
  731. /*
  732. * (param_count) elements of data that specify parameter bind points for
  733. * this effect.
  734. * This can be NULL on error or if (param_count) is zero.
  735. */
  736. MOJOSHADER_effectParam *params;
  737. /*
  738. * The number of elements pointed to by (techniques).
  739. */
  740. int technique_count;
  741. /*
  742. * (technique_count) elements of data that specify techniques used in
  743. * this effect. Each technique contains a series of passes, and each pass
  744. * specifies state and shaders that affect rendering.
  745. * This can be NULL on error or if (technique_count) is zero.
  746. */
  747. MOJOSHADER_effectTechnique *techniques;
  748. /*
  749. * The number of elements pointed to by (textures).
  750. */
  751. int texture_count;
  752. /*
  753. * (texture_count) elements of data that specify textures used in
  754. * this effect.
  755. * This can be NULL on error or if (texture_count) is zero.
  756. */
  757. MOJOSHADER_effectTexture *textures;
  758. /*
  759. * The number of elements pointed to by (shaders).
  760. */
  761. int shader_count;
  762. /*
  763. * (shader_count) elements of data that specify shaders used in
  764. * this effect.
  765. * This can be NULL on error or if (shader_count) is zero.
  766. */
  767. MOJOSHADER_effectShader *shaders;
  768. /*
  769. * This is the malloc implementation you passed to MOJOSHADER_parseEffect().
  770. */
  771. MOJOSHADER_malloc malloc;
  772. /*
  773. * This is the free implementation you passed to MOJOSHADER_parseEffect().
  774. */
  775. MOJOSHADER_free free;
  776. /*
  777. * This is the pointer you passed as opaque data for your allocator.
  778. */
  779. void *malloc_data;
  780. } MOJOSHADER_effect;
  781. /* !!! FIXME: document me. */
  782. const MOJOSHADER_effect *MOJOSHADER_parseEffect(const char *profile,
  783. const unsigned char *buf,
  784. const unsigned int _len,
  785. const MOJOSHADER_swizzle *swiz,
  786. const unsigned int swizcount,
  787. const MOJOSHADER_samplerMap *smap,
  788. const unsigned int smapcount,
  789. MOJOSHADER_malloc m,
  790. MOJOSHADER_free f,
  791. void *d);
  792. /* !!! FIXME: document me. */
  793. void MOJOSHADER_freeEffect(const MOJOSHADER_effect *effect);
  794. /* Preprocessor interface... */
  795. /*
  796. * Structure used to pass predefined macros. Maps to D3DXMACRO.
  797. * You can have macro arguments: set identifier to "a(b, c)" or whatever.
  798. */
  799. typedef struct MOJOSHADER_preprocessorDefine
  800. {
  801. const char *identifier;
  802. const char *definition;
  803. } MOJOSHADER_preprocessorDefine;
  804. /*
  805. * Used with the MOJOSHADER_includeOpen callback. Maps to D3DXINCLUDE_TYPE.
  806. */
  807. typedef enum
  808. {
  809. MOJOSHADER_INCLUDETYPE_LOCAL, /* local header: #include "blah.h" */
  810. MOJOSHADER_INCLUDETYPE_SYSTEM /* system header: #include <blah.h> */
  811. } MOJOSHADER_includeType;
  812. /*
  813. * Structure used to return data from preprocessing of a shader...
  814. */
  815. /* !!! FIXME: most of these ints should be unsigned. */
  816. typedef struct MOJOSHADER_preprocessData
  817. {
  818. /*
  819. * The number of elements pointed to by (errors).
  820. */
  821. int error_count;
  822. /*
  823. * (error_count) elements of data that specify errors that were generated
  824. * by parsing this shader.
  825. * This can be NULL if there were no errors or if (error_count) is zero.
  826. */
  827. MOJOSHADER_error *errors;
  828. /*
  829. * Bytes of output from preprocessing. This is a UTF-8 string. We
  830. * guarantee it to be NULL-terminated. Will be NULL on error.
  831. */
  832. const char *output;
  833. /*
  834. * Byte count for output, not counting any null terminator.
  835. * Will be 0 on error.
  836. */
  837. int output_len;
  838. /*
  839. * This is the malloc implementation you passed to MOJOSHADER_parse().
  840. */
  841. MOJOSHADER_malloc malloc;
  842. /*
  843. * This is the free implementation you passed to MOJOSHADER_parse().
  844. */
  845. MOJOSHADER_free free;
  846. /*
  847. * This is the pointer you passed as opaque data for your allocator.
  848. */
  849. void *malloc_data;
  850. } MOJOSHADER_preprocessData;
  851. /*
  852. * This callback allows an app to handle #include statements for the
  853. * preprocessor. When the preprocessor sees an #include, it will call this
  854. * function to obtain the contents of the requested file. This is optional;
  855. * the preprocessor will open files directly if no callback is supplied, but
  856. * this allows an app to retrieve data from something other than the
  857. * traditional filesystem (for example, headers packed in a .zip file or
  858. * headers generated on-the-fly).
  859. *
  860. * This function maps to ID3DXInclude::Open()
  861. *
  862. * (inctype) specifies the type of header we wish to include.
  863. * (fname) specifies the name of the file specified on the #include line.
  864. * (parent) is a string of the entire source file containing the include, in
  865. * its original, not-yet-preprocessed state. Note that this is just the
  866. * contents of the specific file, not all source code that the preprocessor
  867. * has seen through other includes, etc.
  868. * (outdata) will be set by the callback to a pointer to the included file's
  869. * contents. The callback is responsible for allocating this however they
  870. * see fit (we provide allocator functions, but you may ignore them). This
  871. * pointer must remain valid until the includeClose callback runs. This
  872. * string does not need to be NULL-terminated.
  873. * (outbytes) will be set by the callback to the number of bytes pointed to
  874. * by (outdata).
  875. * (m),(f), and (d) are the allocator details that the application passed to
  876. * MojoShader. If these were NULL, MojoShader may have replaced them with its
  877. * own internal allocators.
  878. *
  879. * The callback returns zero on error, non-zero on success.
  880. *
  881. * If you supply an includeOpen callback, you must supply includeClose, too.
  882. */
  883. typedef int (*MOJOSHADER_includeOpen)(MOJOSHADER_includeType inctype,
  884. const char *fname, const char *parent,
  885. const char **outdata, unsigned int *outbytes,
  886. MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
  887. /*
  888. * This callback allows an app to clean up the results of a previous
  889. * includeOpen callback.
  890. *
  891. * This function maps to ID3DXInclude::Close()
  892. *
  893. * (data) is the data that was returned from a previous call to includeOpen.
  894. * It is now safe to deallocate this data.
  895. * (m),(f), and (d) are the same allocator details that were passed to your
  896. * includeOpen callback.
  897. *
  898. * If you supply an includeClose callback, you must supply includeOpen, too.
  899. */
  900. typedef void (*MOJOSHADER_includeClose)(const char *data,
  901. MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
  902. /*
  903. * This function is optional. Even if you are dealing with shader source
  904. * code, you don't need to explicitly use the preprocessor, as the compiler
  905. * and assembler will use it behind the scenes. In fact, you probably never
  906. * need this function unless you are debugging a custom tool (or debugging
  907. * MojoShader itself).
  908. *
  909. * Preprocessing roughly follows the syntax of an ANSI C preprocessor, as
  910. * Microsoft's Direct3D assembler and HLSL compiler use this syntax. Please
  911. * note that we try to match the output you'd get from Direct3D's
  912. * preprocessor, which has some quirks if you're expecting output that matches
  913. * a generic C preprocessor.
  914. *
  915. * This function maps to D3DXPreprocessShader().
  916. *
  917. * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
  918. * actually access this file, as we obtain our data from (source). This
  919. * string is copied when we need to report errors while processing (source),
  920. * as opposed to errors in a file referenced via the #include directive in
  921. * (source). If this is NULL, then errors will report the filename as NULL,
  922. * too.
  923. *
  924. * (source) is an string of UTF-8 text to preprocess. It does not need to be
  925. * NULL-terminated.
  926. *
  927. * (sourcelen) is the length of the string pointed to by (source), in bytes.
  928. *
  929. * (defines) points to (define_count) preprocessor definitions, and can be
  930. * NULL. These are treated by the preprocessor as if the source code started
  931. * with one #define for each entry you pass in here.
  932. *
  933. * (include_open) and (include_close) let the app control the preprocessor's
  934. * behaviour for #include statements. Both are optional and can be NULL, but
  935. * both must be specified if either is specified.
  936. *
  937. * This will return a MOJOSHADER_preprocessorData. You should pass this
  938. * return value to MOJOSHADER_freePreprocessData() when you are done with
  939. * it.
  940. *
  941. * This function will never return NULL, even if the system is completely
  942. * out of memory upon entry (in which case, this function returns a static
  943. * MOJOSHADER_preprocessData object, which is still safe to pass to
  944. * MOJOSHADER_freePreprocessData()).
  945. *
  946. * As preprocessing requires some memory to be allocated, you may provide a
  947. * custom allocator to this function, which will be used to allocate/free
  948. * memory. They function just like malloc() and free(). We do not use
  949. * realloc(). If you don't care, pass NULL in for the allocator functions.
  950. * If your allocator needs instance-specific data, you may supply it with the
  951. * (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
  952. *
  953. * This function is thread safe, so long as the various callback functions
  954. * are, too, and that the parameters remains intact for the duration of the
  955. * call. This allows you to preprocess several shaders on separate CPU cores
  956. * at the same time.
  957. */
  958. const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename,
  959. const char *source, unsigned int sourcelen,
  960. const MOJOSHADER_preprocessorDefine *defines,
  961. unsigned int define_count,
  962. MOJOSHADER_includeOpen include_open,
  963. MOJOSHADER_includeClose include_close,
  964. MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
  965. /*
  966. * Call this to dispose of preprocessing results when you are done with them.
  967. * This will call the MOJOSHADER_free function you provided to
  968. * MOJOSHADER_preprocess() multiple times, if you provided one.
  969. * Passing a NULL here is a safe no-op.
  970. *
  971. * This function is thread safe, so long as any allocator you passed into
  972. * MOJOSHADER_preprocess() is, too.
  973. */
  974. void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *data);
  975. /* Assembler interface... */
  976. /*
  977. * This function is optional. Use this to convert Direct3D shader assembly
  978. * language into bytecode, which can be handled by MOJOSHADER_parse().
  979. *
  980. * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
  981. * actually access this file, as we obtain our data from (source). This
  982. * string is copied when we need to report errors while processing (source),
  983. * as opposed to errors in a file referenced via the #include directive in
  984. * (source). If this is NULL, then errors will report the filename as NULL,
  985. * too.
  986. *
  987. * (source) is an UTF-8 string of valid Direct3D shader assembly source code.
  988. * It does not need to be NULL-terminated.
  989. *
  990. * (sourcelen) is the length of the string pointed to by (source), in bytes.
  991. *
  992. * (comments) points to (comment_count) NULL-terminated UTF-8 strings, and
  993. * can be NULL. These strings are inserted as comments in the bytecode.
  994. *
  995. * (symbols) points to (symbol_count) symbol structs, and can be NULL. These
  996. * become a CTAB field in the bytecode. This is optional, but
  997. * MOJOSHADER_parse() needs CTAB data for all arrays used in a program, or
  998. * relative addressing will not be permitted, so you'll want to at least
  999. * provide symbol information for those. The symbol data is 100% trusted
  1000. * at this time; it will not be checked to see if it matches what was
  1001. * assembled in any way whatsoever.
  1002. *
  1003. * (defines) points to (define_count) preprocessor definitions, and can be
  1004. * NULL. These are treated by the preprocessor as if the source code started
  1005. * with one #define for each entry you pass in here.
  1006. *
  1007. * (include_open) and (include_close) let the app control the preprocessor's
  1008. * behaviour for #include statements. Both are optional and can be NULL, but
  1009. * both must be specified if either is specified.
  1010. *
  1011. * This will return a MOJOSHADER_parseData, like MOJOSHADER_parse() would,
  1012. * except the profile will be MOJOSHADER_PROFILE_BYTECODE and the output
  1013. * will be the assembled bytecode instead of some other language. This output
  1014. * can be pushed back through MOJOSHADER_parseData() with a different profile.
  1015. *
  1016. * This function will never return NULL, even if the system is completely
  1017. * out of memory upon entry (in which case, this function returns a static
  1018. * MOJOSHADER_parseData object, which is still safe to pass to
  1019. * MOJOSHADER_freeParseData()).
  1020. *
  1021. * As assembling requires some memory to be allocated, you may provide a
  1022. * custom allocator to this function, which will be used to allocate/free
  1023. * memory. They function just like malloc() and free(). We do not use
  1024. * realloc(). If you don't care, pass NULL in for the allocator functions.
  1025. * If your allocator needs instance-specific data, you may supply it with the
  1026. * (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
  1027. *
  1028. * This function is thread safe, so long as the various callback functions
  1029. * are, too, and that the parameters remains intact for the duration of the
  1030. * call. This allows you to assemble several shaders on separate CPU cores
  1031. * at the same time.
  1032. */
  1033. const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *filename,
  1034. const char *source, unsigned int sourcelen,
  1035. const char **comments, unsigned int comment_count,
  1036. const MOJOSHADER_symbol *symbols,
  1037. unsigned int symbol_count,
  1038. const MOJOSHADER_preprocessorDefine *defines,
  1039. unsigned int define_count,
  1040. MOJOSHADER_includeOpen include_open,
  1041. MOJOSHADER_includeClose include_close,
  1042. MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
  1043. /* High level shading language support... */
  1044. /*
  1045. * Source profile strings for HLSL: Direct3D High Level Shading Language.
  1046. */
  1047. #define MOJOSHADER_SRC_PROFILE_HLSL_VS_1_1 "hlsl_vs_1_1"
  1048. #define MOJOSHADER_SRC_PROFILE_HLSL_VS_2_0 "hlsl_vs_2_0"
  1049. #define MOJOSHADER_SRC_PROFILE_HLSL_VS_3_0 "hlsl_vs_3_0"
  1050. #define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_1 "hlsl_ps_1_1"
  1051. #define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_2 "hlsl_ps_1_2"
  1052. #define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_3 "hlsl_ps_1_3"
  1053. #define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_4 "hlsl_ps_1_4"
  1054. #define MOJOSHADER_SRC_PROFILE_HLSL_PS_2_0 "hlsl_ps_2_0"
  1055. #define MOJOSHADER_SRC_PROFILE_HLSL_PS_3_0 "hlsl_ps_3_0"
  1056. /* Abstract Syntax Tree interface... */
  1057. /*
  1058. * ATTENTION: This adds a lot of stuff to the API, but almost everyone can
  1059. * ignore this section. Seriously, go ahead and skip over anything that has
  1060. * "AST" in it, unless you know why you'd want to use it.
  1061. *
  1062. * ALSO: This API is still evolving! We make no promises at this time to keep
  1063. * source or binary compatibility for the AST pieces.
  1064. *
  1065. * Important notes:
  1066. * - ASTs are the result of parsing the source code: a program that fails to
  1067. * compile will often parse successfully. Undeclared variables,
  1068. * type incompatibilities, etc, aren't detected at this point.
  1069. * - Vector swizzles (the ".xyzw" part of "MyVec4.xyzw") will look like
  1070. * structure dereferences. We don't realize these are actually swizzles
  1071. * until semantic analysis.
  1072. * - MOJOSHADER_astDataType info is not reliable when returned from
  1073. * MOJOSHADER_parseAst()! Most of the datatype info will be missing or have
  1074. * inaccurate data types. We sort these out during semantic analysis, which
  1075. * happens after the AST parsing is complete. A few are filled in, or can
  1076. * be deduced fairly trivially by processing several pieces into one.
  1077. * It's enough that you can reproduce the original source code, more or
  1078. * less, from the AST.
  1079. */
  1080. /* High-level datatypes for AST nodes. */
  1081. typedef enum MOJOSHADER_astDataTypeType
  1082. {
  1083. MOJOSHADER_AST_DATATYPE_NONE,
  1084. MOJOSHADER_AST_DATATYPE_BOOL,
  1085. MOJOSHADER_AST_DATATYPE_INT,
  1086. MOJOSHADER_AST_DATATYPE_UINT,
  1087. MOJOSHADER_AST_DATATYPE_FLOAT,
  1088. MOJOSHADER_AST_DATATYPE_FLOAT_SNORM,
  1089. MOJOSHADER_AST_DATATYPE_FLOAT_UNORM,
  1090. MOJOSHADER_AST_DATATYPE_HALF,
  1091. MOJOSHADER_AST_DATATYPE_DOUBLE,
  1092. MOJOSHADER_AST_DATATYPE_STRING,
  1093. MOJOSHADER_AST_DATATYPE_SAMPLER_1D,
  1094. MOJOSHADER_AST_DATATYPE_SAMPLER_2D,
  1095. MOJOSHADER_AST_DATATYPE_SAMPLER_3D,
  1096. MOJOSHADER_AST_DATATYPE_SAMPLER_CUBE,
  1097. MOJOSHADER_AST_DATATYPE_SAMPLER_STATE,
  1098. MOJOSHADER_AST_DATATYPE_SAMPLER_COMPARISON_STATE,
  1099. MOJOSHADER_AST_DATATYPE_STRUCT,
  1100. MOJOSHADER_AST_DATATYPE_ARRAY,
  1101. MOJOSHADER_AST_DATATYPE_VECTOR,
  1102. MOJOSHADER_AST_DATATYPE_MATRIX,
  1103. MOJOSHADER_AST_DATATYPE_BUFFER,
  1104. MOJOSHADER_AST_DATATYPE_FUNCTION,
  1105. MOJOSHADER_AST_DATATYPE_USER,
  1106. } MOJOSHADER_astDataTypeType;
  1107. #define MOJOSHADER_AST_DATATYPE_CONST (1 << 31)
  1108. typedef union MOJOSHADER_astDataType MOJOSHADER_astDataType;
  1109. // This is just part of DataTypeStruct, never appears outside of it.
  1110. typedef struct MOJOSHADER_astDataTypeStructMember
  1111. {
  1112. const MOJOSHADER_astDataType *datatype;
  1113. const char *identifier;
  1114. } MOJOSHADER_astDataTypeStructMember;
  1115. typedef struct MOJOSHADER_astDataTypeStruct
  1116. {
  1117. MOJOSHADER_astDataTypeType type;
  1118. const MOJOSHADER_astDataTypeStructMember *members;
  1119. int member_count;
  1120. } MOJOSHADER_astDataTypeStruct;
  1121. typedef struct MOJOSHADER_astDataTypeArray
  1122. {
  1123. MOJOSHADER_astDataTypeType type;
  1124. const MOJOSHADER_astDataType *base;
  1125. int elements;
  1126. } MOJOSHADER_astDataTypeArray;
  1127. typedef MOJOSHADER_astDataTypeArray MOJOSHADER_astDataTypeVector;
  1128. typedef struct MOJOSHADER_astDataTypeMatrix
  1129. {
  1130. MOJOSHADER_astDataTypeType type;
  1131. const MOJOSHADER_astDataType *base;
  1132. int rows;
  1133. int columns;
  1134. } MOJOSHADER_astDataTypeMatrix;
  1135. typedef struct MOJOSHADER_astDataTypeBuffer
  1136. {
  1137. MOJOSHADER_astDataTypeType type;
  1138. const MOJOSHADER_astDataType *base;
  1139. } MOJOSHADER_astDataTypeBuffer;
  1140. typedef struct MOJOSHADER_astDataTypeFunction
  1141. {
  1142. MOJOSHADER_astDataTypeType type;
  1143. const MOJOSHADER_astDataType *retval;
  1144. const MOJOSHADER_astDataType **params;
  1145. int num_params;
  1146. int intrinsic; /* non-zero for built-in functions */
  1147. } MOJOSHADER_astDataTypeFunction;
  1148. typedef struct MOJOSHADER_astDataTypeUser
  1149. {
  1150. MOJOSHADER_astDataTypeType type;
  1151. const MOJOSHADER_astDataType *details;
  1152. const char *name;
  1153. } MOJOSHADER_astDataTypeUser;
  1154. union MOJOSHADER_astDataType
  1155. {
  1156. MOJOSHADER_astDataTypeType type;
  1157. MOJOSHADER_astDataTypeArray array;
  1158. MOJOSHADER_astDataTypeStruct structure;
  1159. MOJOSHADER_astDataTypeVector vector;
  1160. MOJOSHADER_astDataTypeMatrix matrix;
  1161. MOJOSHADER_astDataTypeBuffer buffer;
  1162. MOJOSHADER_astDataTypeUser user;
  1163. MOJOSHADER_astDataTypeFunction function;
  1164. };
  1165. /* Structures that make up the parse tree... */
  1166. typedef enum MOJOSHADER_astNodeType
  1167. {
  1168. MOJOSHADER_AST_OP_START_RANGE, /* expression operators. */
  1169. MOJOSHADER_AST_OP_START_RANGE_UNARY, /* unary operators. */
  1170. MOJOSHADER_AST_OP_PREINCREMENT,
  1171. MOJOSHADER_AST_OP_PREDECREMENT,
  1172. MOJOSHADER_AST_OP_NEGATE,
  1173. MOJOSHADER_AST_OP_COMPLEMENT,
  1174. MOJOSHADER_AST_OP_NOT,
  1175. MOJOSHADER_AST_OP_POSTINCREMENT,
  1176. MOJOSHADER_AST_OP_POSTDECREMENT,
  1177. MOJOSHADER_AST_OP_CAST,
  1178. MOJOSHADER_AST_OP_END_RANGE_UNARY,
  1179. MOJOSHADER_AST_OP_START_RANGE_BINARY, /* binary operators. */
  1180. MOJOSHADER_AST_OP_COMMA,
  1181. MOJOSHADER_AST_OP_MULTIPLY,
  1182. MOJOSHADER_AST_OP_DIVIDE,
  1183. MOJOSHADER_AST_OP_MODULO,
  1184. MOJOSHADER_AST_OP_ADD,
  1185. MOJOSHADER_AST_OP_SUBTRACT,
  1186. MOJOSHADER_AST_OP_LSHIFT,
  1187. MOJOSHADER_AST_OP_RSHIFT,
  1188. MOJOSHADER_AST_OP_LESSTHAN,
  1189. MOJOSHADER_AST_OP_GREATERTHAN,
  1190. MOJOSHADER_AST_OP_LESSTHANOREQUAL,
  1191. MOJOSHADER_AST_OP_GREATERTHANOREQUAL,
  1192. MOJOSHADER_AST_OP_EQUAL,
  1193. MOJOSHADER_AST_OP_NOTEQUAL,
  1194. MOJOSHADER_AST_OP_BINARYAND,
  1195. MOJOSHADER_AST_OP_BINARYXOR,
  1196. MOJOSHADER_AST_OP_BINARYOR,
  1197. MOJOSHADER_AST_OP_LOGICALAND,
  1198. MOJOSHADER_AST_OP_LOGICALOR,
  1199. MOJOSHADER_AST_OP_ASSIGN,
  1200. MOJOSHADER_AST_OP_MULASSIGN,
  1201. MOJOSHADER_AST_OP_DIVASSIGN,
  1202. MOJOSHADER_AST_OP_MODASSIGN,
  1203. MOJOSHADER_AST_OP_ADDASSIGN,
  1204. MOJOSHADER_AST_OP_SUBASSIGN,
  1205. MOJOSHADER_AST_OP_LSHIFTASSIGN,
  1206. MOJOSHADER_AST_OP_RSHIFTASSIGN,
  1207. MOJOSHADER_AST_OP_ANDASSIGN,
  1208. MOJOSHADER_AST_OP_XORASSIGN,
  1209. MOJOSHADER_AST_OP_ORASSIGN,
  1210. MOJOSHADER_AST_OP_DEREF_ARRAY,
  1211. MOJOSHADER_AST_OP_END_RANGE_BINARY,
  1212. MOJOSHADER_AST_OP_START_RANGE_TERNARY, /* ternary operators. */
  1213. MOJOSHADER_AST_OP_CONDITIONAL,
  1214. MOJOSHADER_AST_OP_END_RANGE_TERNARY,
  1215. MOJOSHADER_AST_OP_START_RANGE_DATA, /* expression operands. */
  1216. MOJOSHADER_AST_OP_IDENTIFIER,
  1217. MOJOSHADER_AST_OP_INT_LITERAL,
  1218. MOJOSHADER_AST_OP_FLOAT_LITERAL,
  1219. MOJOSHADER_AST_OP_STRING_LITERAL,
  1220. MOJOSHADER_AST_OP_BOOLEAN_LITERAL,
  1221. MOJOSHADER_AST_OP_END_RANGE_DATA,
  1222. MOJOSHADER_AST_OP_START_RANGE_MISC, /* other expression things. */
  1223. MOJOSHADER_AST_OP_DEREF_STRUCT,
  1224. MOJOSHADER_AST_OP_CALLFUNC,
  1225. MOJOSHADER_AST_OP_CONSTRUCTOR,
  1226. MOJOSHADER_AST_OP_END_RANGE_MISC,
  1227. MOJOSHADER_AST_OP_END_RANGE,
  1228. MOJOSHADER_AST_COMPUNIT_START_RANGE, /* things in global scope. */
  1229. MOJOSHADER_AST_COMPUNIT_FUNCTION,
  1230. MOJOSHADER_AST_COMPUNIT_TYPEDEF,
  1231. MOJOSHADER_AST_COMPUNIT_STRUCT,
  1232. MOJOSHADER_AST_COMPUNIT_VARIABLE,
  1233. MOJOSHADER_AST_COMPUNIT_END_RANGE,
  1234. MOJOSHADER_AST_STATEMENT_START_RANGE, /* statements in function scope. */
  1235. MOJOSHADER_AST_STATEMENT_EMPTY,
  1236. MOJOSHADER_AST_STATEMENT_BREAK,
  1237. MOJOSHADER_AST_STATEMENT_CONTINUE,
  1238. MOJOSHADER_AST_STATEMENT_DISCARD,
  1239. MOJOSHADER_AST_STATEMENT_BLOCK,
  1240. MOJOSHADER_AST_STATEMENT_EXPRESSION,
  1241. MOJOSHADER_AST_STATEMENT_IF,
  1242. MOJOSHADER_AST_STATEMENT_SWITCH,
  1243. MOJOSHADER_AST_STATEMENT_FOR,
  1244. MOJOSHADER_AST_STATEMENT_DO,
  1245. MOJOSHADER_AST_STATEMENT_WHILE,
  1246. MOJOSHADER_AST_STATEMENT_RETURN,
  1247. MOJOSHADER_AST_STATEMENT_TYPEDEF,
  1248. MOJOSHADER_AST_STATEMENT_STRUCT,
  1249. MOJOSHADER_AST_STATEMENT_VARDECL,
  1250. MOJOSHADER_AST_STATEMENT_END_RANGE,
  1251. MOJOSHADER_AST_MISC_START_RANGE, /* misc. syntactic glue. */
  1252. MOJOSHADER_AST_FUNCTION_PARAMS,
  1253. MOJOSHADER_AST_FUNCTION_SIGNATURE,
  1254. MOJOSHADER_AST_SCALAR_OR_ARRAY,
  1255. MOJOSHADER_AST_TYPEDEF,
  1256. MOJOSHADER_AST_PACK_OFFSET,
  1257. MOJOSHADER_AST_VARIABLE_LOWLEVEL,
  1258. MOJOSHADER_AST_ANNOTATION,
  1259. MOJOSHADER_AST_VARIABLE_DECLARATION,
  1260. MOJOSHADER_AST_STRUCT_DECLARATION,
  1261. MOJOSHADER_AST_STRUCT_MEMBER,
  1262. MOJOSHADER_AST_SWITCH_CASE,
  1263. MOJOSHADER_AST_ARGUMENTS,
  1264. MOJOSHADER_AST_MISC_END_RANGE,
  1265. MOJOSHADER_AST_END_RANGE
  1266. } MOJOSHADER_astNodeType;
  1267. typedef struct MOJOSHADER_astNodeInfo
  1268. {
  1269. MOJOSHADER_astNodeType type;
  1270. const char *filename;
  1271. unsigned int line;
  1272. } MOJOSHADER_astNodeInfo;
  1273. typedef enum MOJOSHADER_astVariableAttributes
  1274. {
  1275. MOJOSHADER_AST_VARATTR_EXTERN = (1 << 0),
  1276. MOJOSHADER_AST_VARATTR_NOINTERPOLATION = (1 << 1),
  1277. MOJOSHADER_AST_VARATTR_SHARED = (1 << 2),
  1278. MOJOSHADER_AST_VARATTR_STATIC = (1 << 3),
  1279. MOJOSHADER_AST_VARATTR_UNIFORM = (1 << 4),
  1280. MOJOSHADER_AST_VARATTR_VOLATILE = (1 << 5),
  1281. MOJOSHADER_AST_VARATTR_CONST = (1 << 6),
  1282. MOJOSHADER_AST_VARATTR_ROWMAJOR = (1 << 7),
  1283. MOJOSHADER_AST_VARATTR_COLUMNMAJOR = (1 << 8)
  1284. } MOJOSHADER_astVariableAttributes;
  1285. typedef enum MOJOSHADER_astIfAttributes
  1286. {
  1287. MOJOSHADER_AST_IFATTR_NONE,
  1288. MOJOSHADER_AST_IFATTR_BRANCH,
  1289. MOJOSHADER_AST_IFATTR_FLATTEN,
  1290. MOJOSHADER_AST_IFATTR_IFALL,
  1291. MOJOSHADER_AST_IFATTR_IFANY,
  1292. MOJOSHADER_AST_IFATTR_PREDICATE,
  1293. MOJOSHADER_AST_IFATTR_PREDICATEBLOCK,
  1294. } MOJOSHADER_astIfAttributes;
  1295. typedef enum MOJOSHADER_astSwitchAttributes
  1296. {
  1297. MOJOSHADER_AST_SWITCHATTR_NONE,
  1298. MOJOSHADER_AST_SWITCHATTR_FLATTEN,
  1299. MOJOSHADER_AST_SWITCHATTR_BRANCH,
  1300. MOJOSHADER_AST_SWITCHATTR_FORCECASE,
  1301. MOJOSHADER_AST_SWITCHATTR_CALL
  1302. } MOJOSHADER_astSwitchAttributes;
  1303. /* You can cast any AST node pointer to this. */
  1304. typedef struct MOJOSHADER_astGeneric
  1305. {
  1306. MOJOSHADER_astNodeInfo ast;
  1307. } MOJOSHADER_astGeneric;
  1308. typedef struct MOJOSHADER_astExpression
  1309. {
  1310. MOJOSHADER_astNodeInfo ast;
  1311. const MOJOSHADER_astDataType *datatype;
  1312. } MOJOSHADER_astExpression;
  1313. typedef struct MOJOSHADER_astArguments
  1314. {
  1315. MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_ARGUMENTS */
  1316. MOJOSHADER_astExpression *argument;
  1317. struct MOJOSHADER_astArguments *next;
  1318. } MOJOSHADER_astArguments;
  1319. typedef struct MOJOSHADER_astExpressionUnary
  1320. {
  1321. MOJOSHADER_astNodeInfo ast;
  1322. const MOJOSHADER_astDataType *datatype;
  1323. MOJOSHADER_astExpression *operand;
  1324. } MOJOSHADER_astExpressionUnary;
  1325. typedef struct MOJOSHADER_astExpressionBinary
  1326. {
  1327. MOJOSHADER_astNodeInfo ast;
  1328. const MOJOSHADER_astDataType *datatype;
  1329. MOJOSHADER_astExpression *left;
  1330. MOJOSHADER_astExpression *right;
  1331. } MOJOSHADER_astExpressionBinary;
  1332. typedef struct MOJOSHADER_astExpressionTernary
  1333. {
  1334. MOJOSHADER_astNodeInfo ast;
  1335. const MOJOSHADER_astDataType *datatype;
  1336. MOJOSHADER_astExpression *left;
  1337. MOJOSHADER_astExpression *center;
  1338. MOJOSHADER_astExpression *right;
  1339. } MOJOSHADER_astExpressionTernary;
  1340. /* Identifier indexes aren't available until semantic analysis phase completes.
  1341. * It provides a unique id for this identifier's variable.
  1342. * It will be negative for global scope, positive for function scope
  1343. * (global values are globally unique, function values are only
  1344. * unique within the scope of the given function). There's a different
  1345. * set of indices if this identifier is a function (positive for
  1346. * user-defined functions, negative for intrinsics).
  1347. * May be zero for various reasons (unknown identifier, etc).
  1348. */
  1349. typedef struct MOJOSHADER_astExpressionIdentifier
  1350. {
  1351. MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_IDENTIFIER */
  1352. const MOJOSHADER_astDataType *datatype;
  1353. const char *identifier;
  1354. int index;
  1355. } MOJOSHADER_astExpressionIdentifier;
  1356. typedef struct MOJOSHADER_astExpressionIntLiteral
  1357. {
  1358. MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_INT_LITERAL */
  1359. const MOJOSHADER_astDataType *datatype; /* always AST_DATATYPE_INT */
  1360. int value;
  1361. } MOJOSHADER_astExpressionIntLiteral;
  1362. typedef struct MOJOSHADER_astExpressionFloatLiteral
  1363. {
  1364. MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_FLOAT_LITERAL */
  1365. const MOJOSHADER_astDataType *datatype; /* always AST_DATATYPE_FLOAT */
  1366. double value;
  1367. } MOJOSHADER_astExpressionFloatLiteral;
  1368. typedef struct MOJOSHADER_astExpressionStringLiteral
  1369. {
  1370. MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_STRING_LITERAL */
  1371. const MOJOSHADER_astDataType *datatype; /* always AST_DATATYPE_STRING */
  1372. const char *string;
  1373. } MOJOSHADER_astExpressionStringLiteral;
  1374. typedef struct MOJOSHADER_astExpressionBooleanLiteral
  1375. {
  1376. MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_BOOLEAN_LITERAL */
  1377. const MOJOSHADER_astDataType *datatype; /* always AST_DATATYPE_BOOL */
  1378. int value; /* Always 1 or 0. */
  1379. } MOJOSHADER_astExpressionBooleanLiteral;
  1380. typedef struct MOJOSHADER_astExpressionConstructor
  1381. {
  1382. MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_CONSTRUCTOR */
  1383. const MOJOSHADER_astDataType *datatype;
  1384. MOJOSHADER_astArguments *args;
  1385. } MOJOSHADER_astExpressionConstructor;
  1386. typedef struct MOJOSHADER_astExpressionDerefStruct
  1387. {
  1388. MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_DEREF_STRUCT */
  1389. const MOJOSHADER_astDataType *datatype;
  1390. /* !!! FIXME:
  1391. * "identifier" is misnamed; this might not be an identifier at all:
  1392. * x = FunctionThatReturnsAStruct().SomeMember;
  1393. */
  1394. MOJOSHADER_astExpression *identifier;
  1395. const char *member;
  1396. int isswizzle; /* Always 1 or 0. Never set by parseAst()! */
  1397. int member_index; /* Never set by parseAst()! */
  1398. } MOJOSHADER_astExpressionDerefStruct;
  1399. typedef struct MOJOSHADER_astExpressionCallFunction
  1400. {
  1401. MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_CALLFUNC */
  1402. const MOJOSHADER_astDataType *datatype;
  1403. MOJOSHADER_astExpressionIdentifier *identifier;
  1404. MOJOSHADER_astArguments *args;
  1405. } MOJOSHADER_astExpressionCallFunction;
  1406. typedef struct MOJOSHADER_astExpressionCast
  1407. {
  1408. MOJOSHADER_astNodeInfo ast; /* Always MOJOSHADER_AST_OP_CAST */
  1409. const MOJOSHADER_astDataType *datatype;
  1410. MOJOSHADER_astExpression *operand;
  1411. } MOJOSHADER_astExpressionCast;
  1412. typedef struct MOJOSHADER_astCompilationUnit
  1413. {
  1414. MOJOSHADER_astNodeInfo ast;
  1415. struct MOJOSHADER_astCompilationUnit *next;
  1416. } MOJOSHADER_astCompilationUnit;
  1417. typedef enum MOJOSHADER_astFunctionStorageClass
  1418. {
  1419. MOJOSHADER_AST_FNSTORECLS_NONE,
  1420. MOJOSHADER_AST_FNSTORECLS_INLINE
  1421. } MOJOSHADER_astFunctionStorageClass;
  1422. typedef enum MOJOSHADER_astInputModifier
  1423. {
  1424. MOJOSHADER_AST_INPUTMOD_NONE,
  1425. MOJOSHADER_AST_INPUTMOD_IN,
  1426. MOJOSHADER_AST_INPUTMOD_OUT,
  1427. MOJOSHADER_AST_INPUTMOD_INOUT,
  1428. MOJOSHADER_AST_INPUTMOD_UNIFORM
  1429. } MOJOSHADER_astInputModifier;
  1430. typedef enum MOJOSHADER_astInterpolationModifier
  1431. {
  1432. MOJOSHADER_AST_INTERPMOD_NONE,
  1433. MOJOSHADER_AST_INTERPMOD_LINEAR,
  1434. MOJOSHADER_AST_INTERPMOD_CENTROID,
  1435. MOJOSHADER_AST_INTERPMOD_NOINTERPOLATION,
  1436. MOJOSHADER_AST_INTERPMOD_NOPERSPECTIVE,
  1437. MOJOSHADER_AST_INTERPMOD_SAMPLE
  1438. } MOJOSHADER_astInterpolationModifier;
  1439. typedef struct MOJOSHADER_astFunctionParameters
  1440. {
  1441. MOJOSHADER_astNodeInfo ast;
  1442. const MOJOSHADER_astDataType *datatype;
  1443. MOJOSHADER_astInputModifier input_modifier;
  1444. const char *identifier;
  1445. const char *semantic;
  1446. MOJOSHADER_astInterpolationModifier interpolation_modifier;
  1447. MOJOSHADER_astExpression *initializer;
  1448. struct MOJOSHADER_astFunctionParameters *next;
  1449. } MOJOSHADER_astFunctionParameters;
  1450. typedef struct MOJOSHADER_astFunctionSignature
  1451. {
  1452. MOJOSHADER_astNodeInfo ast;
  1453. const MOJOSHADER_astDataType *datatype;
  1454. const char *identifier;
  1455. MOJOSHADER_astFunctionParameters *params;
  1456. MOJOSHADER_astFunctionStorageClass storage_class;
  1457. const char *semantic;
  1458. } MOJOSHADER_astFunctionSignature;
  1459. typedef struct MOJOSHADER_astScalarOrArray
  1460. {
  1461. MOJOSHADER_astNodeInfo ast;
  1462. const char *identifier;
  1463. int isarray; /* boolean: 1 or 0 */
  1464. MOJOSHADER_astExpression *dimension;
  1465. } MOJOSHADER_astScalarOrArray;
  1466. typedef struct MOJOSHADER_astAnnotations
  1467. {
  1468. MOJOSHADER_astNodeInfo ast;
  1469. const MOJOSHADER_astDataType *datatype;
  1470. MOJOSHADER_astExpression *initializer;
  1471. struct MOJOSHADER_astAnnotations *next;
  1472. } MOJOSHADER_astAnnotations;
  1473. typedef struct MOJOSHADER_astPackOffset
  1474. {
  1475. MOJOSHADER_astNodeInfo ast;
  1476. const char *ident1; /* !!! FIXME: rename this. */
  1477. const char *ident2;
  1478. } MOJOSHADER_astPackOffset;
  1479. typedef struct MOJOSHADER_astVariableLowLevel
  1480. {
  1481. MOJOSHADER_astNodeInfo ast;
  1482. MOJOSHADER_astPackOffset *packoffset;
  1483. const char *register_name;
  1484. } MOJOSHADER_astVariableLowLevel;
  1485. typedef struct MOJOSHADER_astStructMembers
  1486. {
  1487. MOJOSHADER_astNodeInfo ast;
  1488. const MOJOSHADER_astDataType *datatype;
  1489. const char *semantic;
  1490. MOJOSHADER_astScalarOrArray *details;
  1491. MOJOSHADER_astInterpolationModifier interpolation_mod;
  1492. struct MOJOSHADER_astStructMembers *next;
  1493. } MOJOSHADER_astStructMembers;
  1494. typedef struct MOJOSHADER_astStructDeclaration
  1495. {
  1496. MOJOSHADER_astNodeInfo ast;
  1497. const MOJOSHADER_astDataType *datatype;
  1498. const char *name;
  1499. MOJOSHADER_astStructMembers *members;
  1500. } MOJOSHADER_astStructDeclaration;
  1501. typedef struct MOJOSHADER_astVariableDeclaration
  1502. {
  1503. MOJOSHADER_astNodeInfo ast;
  1504. int attributes;
  1505. const MOJOSHADER_astDataType *datatype;
  1506. MOJOSHADER_astStructDeclaration *anonymous_datatype;
  1507. MOJOSHADER_astScalarOrArray *details;
  1508. const char *semantic;
  1509. MOJOSHADER_astAnnotations *annotations;
  1510. MOJOSHADER_astExpression *initializer;
  1511. MOJOSHADER_astVariableLowLevel *lowlevel;
  1512. struct MOJOSHADER_astVariableDeclaration *next;
  1513. } MOJOSHADER_astVariableDeclaration;
  1514. typedef struct MOJOSHADER_astStatement
  1515. {
  1516. MOJOSHADER_astNodeInfo ast;
  1517. struct MOJOSHADER_astStatement *next;
  1518. } MOJOSHADER_astStatement;
  1519. typedef MOJOSHADER_astStatement MOJOSHADER_astEmptyStatement;
  1520. typedef MOJOSHADER_astStatement MOJOSHADER_astBreakStatement;
  1521. typedef MOJOSHADER_astStatement MOJOSHADER_astContinueStatement;
  1522. typedef MOJOSHADER_astStatement MOJOSHADER_astDiscardStatement;
  1523. /* something enclosed in "{}" braces. */
  1524. typedef struct MOJOSHADER_astBlockStatement
  1525. {
  1526. MOJOSHADER_astNodeInfo ast;
  1527. MOJOSHADER_astStatement *next;
  1528. MOJOSHADER_astStatement *statements; /* list of child statements. */
  1529. } MOJOSHADER_astBlockStatement;
  1530. typedef struct MOJOSHADER_astReturnStatement
  1531. {
  1532. MOJOSHADER_astNodeInfo ast;
  1533. MOJOSHADER_astStatement *next;
  1534. MOJOSHADER_astExpression *expr;
  1535. } MOJOSHADER_astReturnStatement;
  1536. typedef struct MOJOSHADER_astExpressionStatement
  1537. {
  1538. MOJOSHADER_astNodeInfo ast;
  1539. MOJOSHADER_astStatement *next;
  1540. MOJOSHADER_astExpression *expr;
  1541. } MOJOSHADER_astExpressionStatement;
  1542. typedef struct MOJOSHADER_astIfStatement
  1543. {
  1544. MOJOSHADER_astNodeInfo ast;
  1545. MOJOSHADER_astStatement *next;
  1546. int attributes;
  1547. MOJOSHADER_astExpression *expr;
  1548. MOJOSHADER_astStatement *statement;
  1549. MOJOSHADER_astStatement *else_statement;
  1550. } MOJOSHADER_astIfStatement;
  1551. typedef struct MOJOSHADER_astSwitchCases
  1552. {
  1553. MOJOSHADER_astNodeInfo ast;
  1554. MOJOSHADER_astExpression *expr;
  1555. MOJOSHADER_astStatement *statement;
  1556. struct MOJOSHADER_astSwitchCases *next;
  1557. } MOJOSHADER_astSwitchCases;
  1558. typedef struct MOJOSHADER_astSwitchStatement
  1559. {
  1560. MOJOSHADER_astNodeInfo ast;
  1561. MOJOSHADER_astStatement *next;
  1562. int attributes;
  1563. MOJOSHADER_astExpression *expr;
  1564. MOJOSHADER_astSwitchCases *cases;
  1565. } MOJOSHADER_astSwitchStatement;
  1566. typedef struct MOJOSHADER_astWhileStatement
  1567. {
  1568. MOJOSHADER_astNodeInfo ast;
  1569. MOJOSHADER_astStatement *next;
  1570. int unroll; /* # times to unroll, 0 to loop, < 0 == compiler's choice. */
  1571. MOJOSHADER_astExpression *expr;
  1572. MOJOSHADER_astStatement *statement;
  1573. } MOJOSHADER_astWhileStatement;
  1574. typedef MOJOSHADER_astWhileStatement MOJOSHADER_astDoStatement;
  1575. typedef struct MOJOSHADER_astForStatement
  1576. {
  1577. MOJOSHADER_astNodeInfo ast;
  1578. MOJOSHADER_astStatement *next;
  1579. int unroll; /* # times to unroll, 0 to loop, < 0 == compiler's choice. */
  1580. MOJOSHADER_astVariableDeclaration *var_decl; /* either this ... */
  1581. MOJOSHADER_astExpression *initializer; /* ... or this will used. */
  1582. MOJOSHADER_astExpression *looptest;
  1583. MOJOSHADER_astExpression *counter;
  1584. MOJOSHADER_astStatement *statement;
  1585. } MOJOSHADER_astForStatement;
  1586. typedef struct MOJOSHADER_astTypedef
  1587. {
  1588. MOJOSHADER_astNodeInfo ast;
  1589. const MOJOSHADER_astDataType *datatype;
  1590. int isconst; /* boolean: 1 or 0 */
  1591. MOJOSHADER_astScalarOrArray *details;
  1592. } MOJOSHADER_astTypedef;
  1593. typedef struct MOJOSHADER_astTypedefStatement
  1594. {
  1595. MOJOSHADER_astNodeInfo ast;
  1596. MOJOSHADER_astStatement *next;
  1597. MOJOSHADER_astTypedef *type_info;
  1598. } MOJOSHADER_astTypedefStatement;
  1599. typedef struct MOJOSHADER_astVarDeclStatement
  1600. {
  1601. MOJOSHADER_astNodeInfo ast;
  1602. MOJOSHADER_astStatement *next;
  1603. MOJOSHADER_astVariableDeclaration *declaration;
  1604. } MOJOSHADER_astVarDeclStatement;
  1605. typedef struct MOJOSHADER_astStructStatement
  1606. {
  1607. MOJOSHADER_astNodeInfo ast;
  1608. MOJOSHADER_astStatement *next;
  1609. MOJOSHADER_astStructDeclaration *struct_info;
  1610. } MOJOSHADER_astStructStatement;
  1611. typedef struct MOJOSHADER_astCompilationUnitFunction
  1612. {
  1613. MOJOSHADER_astNodeInfo ast;
  1614. MOJOSHADER_astCompilationUnit *next;
  1615. MOJOSHADER_astFunctionSignature *declaration;
  1616. MOJOSHADER_astStatement *definition;
  1617. int index; /* unique id. Will be 0 until semantic analysis runs. */
  1618. } MOJOSHADER_astCompilationUnitFunction;
  1619. typedef struct MOJOSHADER_astCompilationUnitTypedef
  1620. {
  1621. MOJOSHADER_astNodeInfo ast;
  1622. MOJOSHADER_astCompilationUnit *next;
  1623. MOJOSHADER_astTypedef *type_info;
  1624. } MOJOSHADER_astCompilationUnitTypedef;
  1625. typedef struct MOJOSHADER_astCompilationUnitStruct
  1626. {
  1627. MOJOSHADER_astNodeInfo ast;
  1628. MOJOSHADER_astCompilationUnit *next;
  1629. MOJOSHADER_astStructDeclaration *struct_info;
  1630. } MOJOSHADER_astCompilationUnitStruct;
  1631. typedef struct MOJOSHADER_astCompilationUnitVariable
  1632. {
  1633. MOJOSHADER_astNodeInfo ast;
  1634. MOJOSHADER_astCompilationUnit *next;
  1635. MOJOSHADER_astVariableDeclaration *declaration;
  1636. } MOJOSHADER_astCompilationUnitVariable;
  1637. /* this is way cleaner than all the nasty typecasting. */
  1638. typedef union MOJOSHADER_astNode
  1639. {
  1640. MOJOSHADER_astNodeInfo ast;
  1641. MOJOSHADER_astGeneric generic;
  1642. MOJOSHADER_astExpression expression;
  1643. MOJOSHADER_astArguments arguments;
  1644. MOJOSHADER_astExpressionUnary unary;
  1645. MOJOSHADER_astExpressionBinary binary;
  1646. MOJOSHADER_astExpressionTernary ternary;
  1647. MOJOSHADER_astExpressionIdentifier identifier;
  1648. MOJOSHADER_astExpressionIntLiteral intliteral;
  1649. MOJOSHADER_astExpressionFloatLiteral floatliteral;
  1650. MOJOSHADER_astExpressionStringLiteral stringliteral;
  1651. MOJOSHADER_astExpressionBooleanLiteral boolliteral;
  1652. MOJOSHADER_astExpressionConstructor constructor;
  1653. MOJOSHADER_astExpressionDerefStruct derefstruct;
  1654. MOJOSHADER_astExpressionCallFunction callfunc;
  1655. MOJOSHADER_astExpressionCast cast;
  1656. MOJOSHADER_astCompilationUnit compunit;
  1657. MOJOSHADER_astFunctionParameters params;
  1658. MOJOSHADER_astFunctionSignature funcsig;
  1659. MOJOSHADER_astScalarOrArray soa;
  1660. MOJOSHADER_astAnnotations annotations;
  1661. MOJOSHADER_astPackOffset packoffset;
  1662. MOJOSHADER_astVariableLowLevel varlowlevel;
  1663. MOJOSHADER_astStructMembers structmembers;
  1664. MOJOSHADER_astStructDeclaration structdecl;
  1665. MOJOSHADER_astVariableDeclaration vardecl;
  1666. MOJOSHADER_astStatement stmt;
  1667. MOJOSHADER_astEmptyStatement emptystmt;
  1668. MOJOSHADER_astBreakStatement breakstmt;
  1669. MOJOSHADER_astContinueStatement contstmt;
  1670. MOJOSHADER_astDiscardStatement discardstmt;
  1671. MOJOSHADER_astBlockStatement blockstmt;
  1672. MOJOSHADER_astReturnStatement returnstmt;
  1673. MOJOSHADER_astExpressionStatement exprstmt;
  1674. MOJOSHADER_astIfStatement ifstmt;
  1675. MOJOSHADER_astSwitchCases cases;
  1676. MOJOSHADER_astSwitchStatement switchstmt;
  1677. MOJOSHADER_astWhileStatement whilestmt;
  1678. MOJOSHADER_astDoStatement dostmt;
  1679. MOJOSHADER_astForStatement forstmt;
  1680. MOJOSHADER_astTypedef typdef;
  1681. MOJOSHADER_astTypedefStatement typedefstmt;
  1682. MOJOSHADER_astVarDeclStatement vardeclstmt;
  1683. MOJOSHADER_astStructStatement structstmt;
  1684. MOJOSHADER_astCompilationUnitFunction funcunit;
  1685. MOJOSHADER_astCompilationUnitTypedef typedefunit;
  1686. MOJOSHADER_astCompilationUnitStruct structunit;
  1687. MOJOSHADER_astCompilationUnitVariable varunit;
  1688. } MOJOSHADER_astNode;
  1689. /*
  1690. * Structure used to return data from parsing of a shader into an AST...
  1691. */
  1692. /* !!! FIXME: most of these ints should be unsigned. */
  1693. typedef struct MOJOSHADER_astData
  1694. {
  1695. /*
  1696. * The number of elements pointed to by (errors).
  1697. */
  1698. int error_count;
  1699. /*
  1700. * (error_count) elements of data that specify errors that were generated
  1701. * by parsing this shader.
  1702. * This can be NULL if there were no errors or if (error_count) is zero.
  1703. * Note that this will only produce errors for syntax problems. Most of
  1704. * the things we expect a compiler to produce errors for--incompatible
  1705. * types, unknown identifiers, etc--are not checked at all during
  1706. * initial generation of the syntax tree...bogus programs that would
  1707. * fail to compile will pass here without error, if they are syntactically
  1708. * correct!
  1709. */
  1710. MOJOSHADER_error *errors;
  1711. /*
  1712. * The name of the source profile used to parse the shader. Will be NULL
  1713. * on error.
  1714. */
  1715. const char *source_profile;
  1716. /*
  1717. * The actual syntax tree. You are responsible for walking it yourself.
  1718. * CompilationUnits are always the top of the tree (functions, typedefs,
  1719. * global variables, etc). Will be NULL on error.
  1720. */
  1721. const MOJOSHADER_astNode *ast;
  1722. /*
  1723. * This is the malloc implementation you passed to MOJOSHADER_parse().
  1724. */
  1725. MOJOSHADER_malloc malloc;
  1726. /*
  1727. * This is the free implementation you passed to MOJOSHADER_parse().
  1728. */
  1729. MOJOSHADER_free free;
  1730. /*
  1731. * This is the pointer you passed as opaque data for your allocator.
  1732. */
  1733. void *malloc_data;
  1734. /*
  1735. * This is internal data, and not for the application to touch.
  1736. */
  1737. void *opaque;
  1738. } MOJOSHADER_astData;
  1739. /*
  1740. * You almost certainly don't need this function, unless you absolutely know
  1741. * why you need it without hesitation. This is almost certainly only good for
  1742. * building code analysis tools on top of.
  1743. *
  1744. * This is intended to parse HLSL source code, turning it into an abstract
  1745. * syntax tree.
  1746. *
  1747. * (srcprofile) specifies the source language of the shader. You can specify
  1748. * a shader model with this, too. See MOJOSHADER_SRC_PROFILE_* constants.
  1749. *
  1750. * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
  1751. * actually access this file, as we obtain our data from (source). This
  1752. * string is copied when we need to report errors while processing (source),
  1753. * as opposed to errors in a file referenced via the #include directive in
  1754. * (source). If this is NULL, then errors will report the filename as NULL,
  1755. * too.
  1756. *
  1757. * (source) is an UTF-8 string of valid high-level shader source code.
  1758. * It does not need to be NULL-terminated.
  1759. *
  1760. * (sourcelen) is the length of the string pointed to by (source), in bytes.
  1761. *
  1762. * (defines) points to (define_count) preprocessor definitions, and can be
  1763. * NULL. These are treated by the preprocessor as if the source code started
  1764. * with one #define for each entry you pass in here.
  1765. *
  1766. * (include_open) and (include_close) let the app control the preprocessor's
  1767. * behaviour for #include statements. Both are optional and can be NULL, but
  1768. * both must be specified if either is specified.
  1769. *
  1770. * This will return a MOJOSHADER_astData. The data supplied here gives the
  1771. * application a tree-like structure they can walk to see the layout of
  1772. * a given program. When you are done with this data, pass it to
  1773. * MOJOSHADER_freeCompileData() to deallocate resources.
  1774. *
  1775. * This function will never return NULL, even if the system is completely
  1776. * out of memory upon entry (in which case, this function returns a static
  1777. * MOJOSHADER_astData object, which is still safe to pass to
  1778. * MOJOSHADER_freeAstData()).
  1779. *
  1780. * As parsing requires some memory to be allocated, you may provide a
  1781. * custom allocator to this function, which will be used to allocate/free
  1782. * memory. They function just like malloc() and free(). We do not use
  1783. * realloc(). If you don't care, pass NULL in for the allocator functions.
  1784. * If your allocator needs instance-specific data, you may supply it with the
  1785. * (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
  1786. *
  1787. * This function is thread safe, so long as the various callback functions
  1788. * are, too, and that the parameters remains intact for the duration of the
  1789. * call. This allows you to parse several shaders on separate CPU cores
  1790. * at the same time.
  1791. */
  1792. const MOJOSHADER_astData *MOJOSHADER_parseAst(const char *srcprofile,
  1793. const char *filename, const char *source,
  1794. unsigned int sourcelen,
  1795. const MOJOSHADER_preprocessorDefine *defs,
  1796. unsigned int define_count,
  1797. MOJOSHADER_includeOpen include_open,
  1798. MOJOSHADER_includeClose include_close,
  1799. MOJOSHADER_malloc m, MOJOSHADER_free f,
  1800. void *d);
  1801. /* !!! FIXME: expose semantic analysis to the public API? */
  1802. /*
  1803. * Call this to dispose of AST parsing results when you are done with them.
  1804. * This will call the MOJOSHADER_free function you provided to
  1805. * MOJOSHADER_parseAst() multiple times, if you provided one.
  1806. * Passing a NULL here is a safe no-op.
  1807. *
  1808. * This function is thread safe, so long as any allocator you passed into
  1809. * MOJOSHADER_parseAst() is, too.
  1810. */
  1811. void MOJOSHADER_freeAstData(const MOJOSHADER_astData *data);
  1812. /* Intermediate Representation interface... */
  1813. /* !!! FIXME: there is currently no way to access the IR via the public API. */
  1814. typedef enum MOJOSHADER_irNodeType
  1815. {
  1816. MOJOSHADER_IR_START_RANGE_EXPR,
  1817. MOJOSHADER_IR_CONSTANT,
  1818. MOJOSHADER_IR_TEMP,
  1819. MOJOSHADER_IR_BINOP,
  1820. MOJOSHADER_IR_MEMORY,
  1821. MOJOSHADER_IR_CALL,
  1822. MOJOSHADER_IR_ESEQ,
  1823. MOJOSHADER_IR_ARRAY,
  1824. MOJOSHADER_IR_CONVERT,
  1825. MOJOSHADER_IR_SWIZZLE,
  1826. MOJOSHADER_IR_CONSTRUCT,
  1827. MOJOSHADER_IR_END_RANGE_EXPR,
  1828. MOJOSHADER_IR_START_RANGE_STMT,
  1829. MOJOSHADER_IR_MOVE,
  1830. MOJOSHADER_IR_EXPR_STMT,
  1831. MOJOSHADER_IR_JUMP,
  1832. MOJOSHADER_IR_CJUMP,
  1833. MOJOSHADER_IR_SEQ,
  1834. MOJOSHADER_IR_LABEL,
  1835. MOJOSHADER_IR_DISCARD,
  1836. MOJOSHADER_IR_END_RANGE_STMT,
  1837. MOJOSHADER_IR_START_RANGE_MISC,
  1838. MOJOSHADER_IR_EXPRLIST,
  1839. MOJOSHADER_IR_END_RANGE_MISC,
  1840. MOJOSHADER_IR_END_RANGE
  1841. } MOJOSHADER_irNodeType;
  1842. typedef struct MOJOSHADER_irNodeInfo
  1843. {
  1844. MOJOSHADER_irNodeType type;
  1845. const char *filename;
  1846. unsigned int line;
  1847. } MOJOSHADER_irNodeInfo;
  1848. typedef struct MOJOSHADER_irExprList MOJOSHADER_irExprList;
  1849. /*
  1850. * IR nodes are categorized into Expressions, Statements, and Everything Else.
  1851. * You can cast any of them to MOJOSHADER_irGeneric, but this split is
  1852. * useful for slightly better type-checking (you can't cleanly assign
  1853. * something that doesn't return a value to something that wants one, etc).
  1854. * These broader categories are just unions of the simpler types, so the
  1855. * real definitions are below all the things they contain (but these
  1856. * predeclarations are because the simpler types refer to the broader
  1857. * categories).
  1858. */
  1859. typedef union MOJOSHADER_irExpression MOJOSHADER_irExpression; /* returns a value. */
  1860. typedef union MOJOSHADER_irStatement MOJOSHADER_irStatement; /* no returned value. */
  1861. typedef union MOJOSHADER_irMisc MOJOSHADER_irMisc; /* Everything Else. */
  1862. typedef union MOJOSHADER_irNode MOJOSHADER_irNode; /* Generic uber-wrapper. */
  1863. /* You can cast any IR node pointer to this. */
  1864. typedef struct MOJOSHADER_irGeneric
  1865. {
  1866. MOJOSHADER_irNodeInfo ir;
  1867. } MOJOSHADER_irGeneric;
  1868. /* These are used for MOJOSHADER_irBinOp */
  1869. typedef enum MOJOSHADER_irBinOpType
  1870. {
  1871. MOJOSHADER_IR_BINOP_ADD,
  1872. MOJOSHADER_IR_BINOP_SUBTRACT,
  1873. MOJOSHADER_IR_BINOP_MULTIPLY,
  1874. MOJOSHADER_IR_BINOP_DIVIDE,
  1875. MOJOSHADER_IR_BINOP_MODULO,
  1876. MOJOSHADER_IR_BINOP_AND,
  1877. MOJOSHADER_IR_BINOP_OR,
  1878. MOJOSHADER_IR_BINOP_XOR,
  1879. MOJOSHADER_IR_BINOP_LSHIFT,
  1880. MOJOSHADER_IR_BINOP_RSHIFT,
  1881. MOJOSHADER_IR_BINOP_UNKNOWN
  1882. } MOJOSHADER_irBinOpType;
  1883. typedef enum MOJOSHADER_irConditionType
  1884. {
  1885. MOJOSHADER_IR_COND_EQL,
  1886. MOJOSHADER_IR_COND_NEQ,
  1887. MOJOSHADER_IR_COND_LT,
  1888. MOJOSHADER_IR_COND_GT,
  1889. MOJOSHADER_IR_COND_LEQ,
  1890. MOJOSHADER_IR_COND_GEQ,
  1891. MOJOSHADER_IR_COND_UNKNOWN
  1892. } MOJOSHADER_irConditionType;
  1893. /* MOJOSHADER_irExpression types... */
  1894. typedef struct MOJOSHADER_irExprInfo
  1895. {
  1896. MOJOSHADER_irNodeInfo ir;
  1897. MOJOSHADER_astDataTypeType type;
  1898. int elements;
  1899. } MOJOSHADER_irExprInfo;
  1900. typedef struct MOJOSHADER_irConstant /* Constant value */
  1901. {
  1902. MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_CONSTANT */
  1903. union
  1904. {
  1905. int ival[16];
  1906. float fval[16];
  1907. } value;
  1908. } MOJOSHADER_irConstant;
  1909. typedef struct MOJOSHADER_irTemp /* temp value (not necessarily a register). */
  1910. {
  1911. MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_TEMP */
  1912. int index;
  1913. } MOJOSHADER_irTemp;
  1914. typedef struct MOJOSHADER_irBinOp /* binary operator (+, -, etc) */
  1915. {
  1916. MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_BINOP */
  1917. MOJOSHADER_irBinOpType op;
  1918. MOJOSHADER_irExpression *left;
  1919. MOJOSHADER_irExpression *right;
  1920. } MOJOSHADER_irBinOp;
  1921. typedef struct MOJOSHADER_irMemory
  1922. {
  1923. MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_MEMORY */
  1924. int index; /* not final addresses, just a unique identifier. */
  1925. } MOJOSHADER_irMemory;
  1926. typedef struct MOJOSHADER_irCall
  1927. {
  1928. MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_CALL */
  1929. int index;
  1930. MOJOSHADER_irExprList *args;
  1931. } MOJOSHADER_irCall;
  1932. typedef struct MOJOSHADER_irESeq /* statement with result */
  1933. {
  1934. MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_ESEQ */
  1935. MOJOSHADER_irStatement *stmt; /* execute this for side-effects, then... */
  1936. MOJOSHADER_irExpression *expr; /* ...use this for the result. */
  1937. } MOJOSHADER_irESeq;
  1938. typedef struct MOJOSHADER_irArray /* Array dereference. */
  1939. {
  1940. MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_ARRAY */
  1941. MOJOSHADER_irExpression *array;
  1942. MOJOSHADER_irExpression *element;
  1943. } MOJOSHADER_irArray;
  1944. typedef struct MOJOSHADER_irConvert /* casting between datatypes */
  1945. {
  1946. MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_CONVERT */
  1947. MOJOSHADER_irExpression *expr;
  1948. } MOJOSHADER_irConvert;
  1949. typedef struct MOJOSHADER_irSwizzle /* vector swizzle */
  1950. {
  1951. MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_SWIZZLE */
  1952. MOJOSHADER_irExpression *expr;
  1953. char channels[4];
  1954. } MOJOSHADER_irSwizzle;
  1955. typedef struct MOJOSHADER_irConstruct /* vector construct from discrete items */
  1956. {
  1957. MOJOSHADER_irExprInfo info; /* Always MOJOSHADER_IR_CONTSTRUCT */
  1958. MOJOSHADER_irExprList *args;
  1959. } MOJOSHADER_irConstruct;
  1960. /* Wrap the whole category in a union for type "safety." */
  1961. union MOJOSHADER_irExpression
  1962. {
  1963. MOJOSHADER_irNodeInfo ir;
  1964. MOJOSHADER_irExprInfo info;
  1965. MOJOSHADER_irConstant constant;
  1966. MOJOSHADER_irTemp temp;
  1967. MOJOSHADER_irBinOp binop;
  1968. MOJOSHADER_irMemory memory;
  1969. MOJOSHADER_irCall call;
  1970. MOJOSHADER_irESeq eseq;
  1971. MOJOSHADER_irArray array;
  1972. MOJOSHADER_irConvert convert;
  1973. MOJOSHADER_irSwizzle swizzle;
  1974. MOJOSHADER_irConstruct construct;
  1975. };
  1976. /* MOJOSHADER_irStatement types. */
  1977. typedef struct MOJOSHADER_irMove /* load/store. */
  1978. {
  1979. MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_MOVE */
  1980. MOJOSHADER_irExpression *dst; /* must result in a temp or mem! */
  1981. MOJOSHADER_irExpression *src;
  1982. int writemask; // for write-masking vector channels.
  1983. } MOJOSHADER_irMove;
  1984. typedef struct MOJOSHADER_irExprStmt /* evaluate expression, throw it away. */
  1985. {
  1986. MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_EXPR_STMT */
  1987. MOJOSHADER_irExpression *expr;
  1988. } MOJOSHADER_irExprStmt;
  1989. typedef struct MOJOSHADER_irJump /* unconditional jump */
  1990. {
  1991. MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_JUMP */
  1992. int label;
  1993. // !!! FIXME: possible label list, for further optimization passes.
  1994. } MOJOSHADER_irJump;
  1995. typedef struct MOJOSHADER_irCJump /* conditional jump */
  1996. {
  1997. MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_CJUMP */
  1998. MOJOSHADER_irConditionType cond;
  1999. MOJOSHADER_irExpression *left; /* if (left cond right) */
  2000. MOJOSHADER_irExpression *right;
  2001. int iftrue; /* label id for true case. */
  2002. int iffalse; /* label id for false case. */
  2003. } MOJOSHADER_irCJump;
  2004. typedef struct MOJOSHADER_irSeq /* statement without side effects */
  2005. {
  2006. MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_SEQ */
  2007. MOJOSHADER_irStatement *first;
  2008. MOJOSHADER_irStatement *next;
  2009. } MOJOSHADER_irSeq;
  2010. typedef struct MOJOSHADER_irLabel /* like a label in assembly language. */
  2011. {
  2012. MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_LABEL */
  2013. int index;
  2014. } MOJOSHADER_irLabel;
  2015. typedef MOJOSHADER_irGeneric MOJOSHADER_irDiscard; /* discard statement. */
  2016. /* Wrap the whole category in a union for type "safety." */
  2017. union MOJOSHADER_irStatement
  2018. {
  2019. MOJOSHADER_irNodeInfo ir;
  2020. MOJOSHADER_irGeneric generic;
  2021. MOJOSHADER_irMove move;
  2022. MOJOSHADER_irExprStmt expr;
  2023. MOJOSHADER_irJump jump;
  2024. MOJOSHADER_irCJump cjump;
  2025. MOJOSHADER_irSeq seq;
  2026. MOJOSHADER_irLabel label;
  2027. MOJOSHADER_irDiscard discard;
  2028. };
  2029. /* MOJOSHADER_irMisc types. */
  2030. struct MOJOSHADER_irExprList
  2031. {
  2032. MOJOSHADER_irNodeInfo ir; /* Always MOJOSHADER_IR_EXPRLIST */
  2033. MOJOSHADER_irExpression *expr;
  2034. MOJOSHADER_irExprList *next;
  2035. };
  2036. /* Wrap the whole category in a union for type "safety." */
  2037. union MOJOSHADER_irMisc
  2038. {
  2039. MOJOSHADER_irNodeInfo ir;
  2040. MOJOSHADER_irGeneric generic;
  2041. MOJOSHADER_irExprList exprlist;
  2042. };
  2043. /* This is a catchall for all your needs. :) */
  2044. union MOJOSHADER_irNode
  2045. {
  2046. MOJOSHADER_irNodeInfo ir;
  2047. MOJOSHADER_irGeneric generic;
  2048. MOJOSHADER_irExpression expr;
  2049. MOJOSHADER_irStatement stmt;
  2050. MOJOSHADER_irMisc misc;
  2051. };
  2052. /* Compiler interface... */
  2053. /*
  2054. * Structure used to return data from parsing of a shader...
  2055. */
  2056. /* !!! FIXME: most of these ints should be unsigned. */
  2057. typedef struct MOJOSHADER_compileData
  2058. {
  2059. /*
  2060. * The number of elements pointed to by (errors).
  2061. */
  2062. int error_count;
  2063. /*
  2064. * (error_count) elements of data that specify errors that were generated
  2065. * by compiling this shader.
  2066. * This can be NULL if there were no errors or if (error_count) is zero.
  2067. */
  2068. MOJOSHADER_error *errors;
  2069. /*
  2070. * The number of elements pointed to by (warnings).
  2071. */
  2072. int warning_count;
  2073. /*
  2074. * (warning_count) elements of data that specify errors that were
  2075. * generated by compiling this shader.
  2076. * This can be NULL if there were no errors or if (warning_count) is zero.
  2077. */
  2078. MOJOSHADER_error *warnings;
  2079. /*
  2080. * The name of the source profile used to compile the shader. Will be NULL
  2081. * on error.
  2082. */
  2083. const char *source_profile;
  2084. /*
  2085. * Bytes of output from compiling. This will be a null-terminated ASCII
  2086. * string of D3D assembly source code.
  2087. */
  2088. const char *output;
  2089. /*
  2090. * Byte count for output, not counting any null terminator.
  2091. * Will be 0 on error.
  2092. */
  2093. int output_len;
  2094. /*
  2095. * The number of elements pointed to by (symbols).
  2096. */
  2097. int symbol_count;
  2098. /*
  2099. * (symbol_count) elements of data that specify high-level symbol data
  2100. * for the shader. This can be used by MOJOSHADER_assemble() to
  2101. * generate a CTAB section in bytecode, which is needed by
  2102. * MOJOSHADER_parseData() to handle some shaders. This can be NULL on
  2103. * error or if (symbol_count) is zero.
  2104. */
  2105. MOJOSHADER_symbol *symbols;
  2106. /*
  2107. * This is the malloc implementation you passed to MOJOSHADER_parse().
  2108. */
  2109. MOJOSHADER_malloc malloc;
  2110. /*
  2111. * This is the free implementation you passed to MOJOSHADER_parse().
  2112. */
  2113. MOJOSHADER_free free;
  2114. /*
  2115. * This is the pointer you passed as opaque data for your allocator.
  2116. */
  2117. void *malloc_data;
  2118. } MOJOSHADER_compileData;
  2119. /*
  2120. * This function is optional. Use this to compile high-level shader programs.
  2121. *
  2122. * This is intended to turn HLSL source code into D3D assembly code, which
  2123. * can then be passed to MOJOSHADER_assemble() to convert it to D3D bytecode
  2124. * (which can then be used with MOJOSHADER_parseData() to support other
  2125. * shading targets).
  2126. *
  2127. * (srcprofile) specifies the source language of the shader. You can specify
  2128. * a shader model with this, too. See MOJOSHADER_SRC_PROFILE_* constants.
  2129. *
  2130. * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
  2131. * actually access this file, as we obtain our data from (source). This
  2132. * string is copied when we need to report errors while processing (source),
  2133. * as opposed to errors in a file referenced via the #include directive in
  2134. * (source). If this is NULL, then errors will report the filename as NULL,
  2135. * too.
  2136. *
  2137. * (source) is an UTF-8 string of valid high-level shader source code.
  2138. * It does not need to be NULL-terminated.
  2139. *
  2140. * (sourcelen) is the length of the string pointed to by (source), in bytes.
  2141. *
  2142. * (defines) points to (define_count) preprocessor definitions, and can be
  2143. * NULL. These are treated by the preprocessor as if the source code started
  2144. * with one #define for each entry you pass in here.
  2145. *
  2146. * (include_open) and (include_close) let the app control the preprocessor's
  2147. * behaviour for #include statements. Both are optional and can be NULL, but
  2148. * both must be specified if either is specified.
  2149. *
  2150. * This will return a MOJOSHADER_compileData. The data supplied here is
  2151. * sufficient to supply to MOJOSHADER_assemble() for further processing.
  2152. * When you are done with this data, pass it to MOJOSHADER_freeCompileData()
  2153. * to deallocate resources.
  2154. *
  2155. * This function will never return NULL, even if the system is completely
  2156. * out of memory upon entry (in which case, this function returns a static
  2157. * MOJOSHADER_compileData object, which is still safe to pass to
  2158. * MOJOSHADER_freeCompileData()).
  2159. *
  2160. * As compiling requires some memory to be allocated, you may provide a
  2161. * custom allocator to this function, which will be used to allocate/free
  2162. * memory. They function just like malloc() and free(). We do not use
  2163. * realloc(). If you don't care, pass NULL in for the allocator functions.
  2164. * If your allocator needs instance-specific data, you may supply it with the
  2165. * (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
  2166. *
  2167. * This function is thread safe, so long as the various callback functions
  2168. * are, too, and that the parameters remains intact for the duration of the
  2169. * call. This allows you to compile several shaders on separate CPU cores
  2170. * at the same time.
  2171. */
  2172. const MOJOSHADER_compileData *MOJOSHADER_compile(const char *srcprofile,
  2173. const char *filename, const char *source,
  2174. unsigned int sourcelen,
  2175. const MOJOSHADER_preprocessorDefine *defs,
  2176. unsigned int define_count,
  2177. MOJOSHADER_includeOpen include_open,
  2178. MOJOSHADER_includeClose include_close,
  2179. MOJOSHADER_malloc m, MOJOSHADER_free f,
  2180. void *d);
  2181. /*
  2182. * Call this to dispose of compile results when you are done with them.
  2183. * This will call the MOJOSHADER_free function you provided to
  2184. * MOJOSHADER_compile() multiple times, if you provided one.
  2185. * Passing a NULL here is a safe no-op.
  2186. *
  2187. * This function is thread safe, so long as any allocator you passed into
  2188. * MOJOSHADER_compile() is, too.
  2189. */
  2190. void MOJOSHADER_freeCompileData(const MOJOSHADER_compileData *data);
  2191. /* OpenGL interface... */
  2192. /*
  2193. * Signature for function lookup callbacks. MojoShader will call a function
  2194. * you provide to get OpenGL entry points (both standard functions and
  2195. * extensions). Through this, MojoShader never links directly to OpenGL,
  2196. * but relies on you to provide the implementation. This means you can
  2197. * swap in different drivers, or hook functions (log every GL call MojoShader
  2198. * makes, etc).
  2199. *
  2200. * (fnname) is the function name we want the address for ("glBegin" or
  2201. * whatever. (data) is a void pointer you provide, if this callback needs
  2202. * extra information. If you don't need it, you may specify NULL.
  2203. *
  2204. * Return the entry point on success, NULL if it couldn't be found.
  2205. * Note that this could ask for standard entry points like glEnable(), or
  2206. * extensions like glProgramLocalParameterI4ivNV(), so you might need
  2207. * to check two places to find the desired entry point, depending on your
  2208. * platform (Windows might need to look in OpenGL32.dll and use WGL, etc).
  2209. */
  2210. typedef void *(*MOJOSHADER_glGetProcAddress)(const char *fnname, void *data);
  2211. /*
  2212. * "Contexts" map to OpenGL contexts...you need one per window, or whatever,
  2213. * and need to inform MojoShader when you make a new one current.
  2214. *
  2215. * "Shaders" refer to individual vertex or pixel programs, and are created
  2216. * by "compiling" Direct3D shader bytecode. A vertex and pixel shader are
  2217. * "linked" into a "Program" before you can use them to render.
  2218. *
  2219. * To the calling application, these are all opaque handles.
  2220. */
  2221. typedef struct MOJOSHADER_glContext MOJOSHADER_glContext;
  2222. typedef struct MOJOSHADER_glShader MOJOSHADER_glShader;
  2223. typedef struct MOJOSHADER_glProgram MOJOSHADER_glProgram;
  2224. /*
  2225. * Get a list of available profiles. This will fill in the array (profs)
  2226. * with up to (size) pointers of profiles that the current system can handle;
  2227. * that is, the profiles are built into MojoShader and the OpenGL extensions
  2228. * required for them exist at runtime. This function returns the number of
  2229. * available profiles, which may be more, less, or equal to (size).
  2230. *
  2231. * If there are more than (size) profiles, the (profs) buffer will not
  2232. * overflow. You can check the return value for the total number of
  2233. * available profiles, allocate more space, and try again if necessary.
  2234. * Calling this function with (size) == 0 is legal.
  2235. *
  2236. * You can only call this AFTER you have successfully built your GL context
  2237. * and made it current. This function will lookup the GL functions it needs
  2238. * through the callback you supply, via (lookup) and (d). The lookup function
  2239. * is neither stored nor used by MojoShader after this function returns, nor
  2240. * are the functions it might look up.
  2241. *
  2242. * You should not free any strings returned from this function; they are
  2243. * pointers to internal, probably static, memory.
  2244. *
  2245. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2246. * safe, you should probably only call this from the same thread that created
  2247. * the GL context.
  2248. */
  2249. int MOJOSHADER_glAvailableProfiles(MOJOSHADER_glGetProcAddress lookup, void *d,
  2250. const char **profs, const int size);
  2251. /*
  2252. * Determine the best profile to use for the current system.
  2253. *
  2254. * You can only call this AFTER you have successfully built your GL context
  2255. * and made it current. This function will lookup the GL functions it needs
  2256. * through the callback you supply via (lookup) and (d). The lookup function
  2257. * is neither stored nor used by MojoShader after this function returns, nor
  2258. * are the functions it might look up.
  2259. *
  2260. * Returns the name of the "best" profile on success, NULL if none of the
  2261. * available profiles will work on this system. "Best" is a relative term,
  2262. * but it generally means the best trade off between feature set and
  2263. * performance. The selection algorithm may be arbitrary and complex.
  2264. *
  2265. * The returned value is an internal static string, and should not be free()'d
  2266. * by the caller. If you get a NULL, calling MOJOSHADER_glGetError() might
  2267. * shed some light on why.
  2268. *
  2269. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2270. * safe, you should probably only call this from the same thread that created
  2271. * the GL context.
  2272. */
  2273. const char *MOJOSHADER_glBestProfile(MOJOSHADER_glGetProcAddress lookup, void *d);
  2274. /*
  2275. * Prepare MojoShader to manage OpenGL shaders.
  2276. *
  2277. * You do not need to call this if all you want is MOJOSHADER_parse().
  2278. *
  2279. * You must call this once AFTER you have successfully built your GL context
  2280. * and made it current. This function will lookup the GL functions it needs
  2281. * through the callback you supply via (lookup) and (lookup_d), after which
  2282. * it may call them at any time up until you call
  2283. * MOJOSHADER_glDestroyContext(). The lookup function is neither stored nor
  2284. * used by MojoShader after this function returns.
  2285. *
  2286. * (profile) is an OpenGL-specific MojoShader profile, which decides how
  2287. * Direct3D bytecode shaders get turned into OpenGL programs, and how they
  2288. * are fed to the GL.
  2289. *
  2290. * (lookup) is a callback that is used to load GL entry points. This callback
  2291. * has to look up base GL functions and extension entry points. The pointer
  2292. * you supply in (lookup_d) is passed as-is to the callback.
  2293. *
  2294. * As MojoShader requires some memory to be allocated, you may provide a
  2295. * custom allocator to this function, which will be used to allocate/free
  2296. * memory. They function just like malloc() and free(). We do not use
  2297. * realloc(). If you don't care, pass NULL in for the allocator functions.
  2298. * If your allocator needs instance-specific data, you may supply it with the
  2299. * (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
  2300. * functions.
  2301. *
  2302. * Returns a new context on success, NULL on error. If you get a new context,
  2303. * you need to make it current before using it with
  2304. * MOJOSHADER_glMakeContextCurrent().
  2305. *
  2306. * This call is NOT thread safe! It must return success before you may call
  2307. * any other MOJOSHADER_gl* function. Also, as most OpenGL implementations
  2308. * are not thread safe, you should probably only call this from the same
  2309. * thread that created the GL context.
  2310. */
  2311. MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *profile,
  2312. MOJOSHADER_glGetProcAddress lookup,
  2313. void *lookup_d,
  2314. MOJOSHADER_malloc m, MOJOSHADER_free f,
  2315. void *malloc_d);
  2316. /*
  2317. * You must call this before using the context that you got from
  2318. * MOJOSHADER_glCreateContext(), and must use it when you switch to a new GL
  2319. * context.
  2320. *
  2321. * You can only have one MOJOSHADER_glContext per actual GL context, or
  2322. * undefined behaviour will result.
  2323. *
  2324. * It is legal to call this with a NULL pointer to make no context current,
  2325. * but you need a valid context to be current to use most of MojoShader.
  2326. */
  2327. void MOJOSHADER_glMakeContextCurrent(MOJOSHADER_glContext *ctx);
  2328. /*
  2329. * Get any error state we might have picked up. MojoShader will NOT call
  2330. * glGetError() internally, but there are other errors we can pick up,
  2331. * such as failed shader compilation, etc.
  2332. *
  2333. * Returns a human-readable string. This string is for debugging purposes, and
  2334. * not guaranteed to be localized, coherent, or user-friendly in any way.
  2335. * It's for programmers!
  2336. *
  2337. * The latest error may remain between calls. New errors replace any existing
  2338. * error. Don't check this string for a sign that an error happened, check
  2339. * return codes instead and use this for explanation when debugging.
  2340. *
  2341. * Do not free the returned string: it's a pointer to a static internal
  2342. * buffer. Do not keep the pointer around, either, as it's likely to become
  2343. * invalid as soon as you call into MojoShader again.
  2344. *
  2345. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2346. * safe, you should probably only call this from the same thread that created
  2347. * the GL context.
  2348. *
  2349. * This call does NOT require a valid MOJOSHADER_glContext to have been made
  2350. * current. The error buffer is shared between contexts, so you can get
  2351. * error results from a failed MOJOSHADER_glCreateContext().
  2352. */
  2353. const char *MOJOSHADER_glGetError(void);
  2354. /*
  2355. * Get the maximum uniforms a shader can support for the current GL context,
  2356. * MojoShader profile, and shader type. You can use this to make decisions
  2357. * about what shaders you want to use (for example, a less complicated
  2358. * shader may be swapped in for lower-end systems).
  2359. *
  2360. * Returns the number, or -1 on error.
  2361. *
  2362. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2363. * safe, you should probably only call this from the same thread that created
  2364. * the GL context.
  2365. *
  2366. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2367. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2368. */
  2369. int MOJOSHADER_glMaxUniforms(MOJOSHADER_shaderType shader_type);
  2370. /*
  2371. * Compile a buffer of Direct3D shader bytecode into an OpenGL shader.
  2372. * You still need to link the shader before you may render with it.
  2373. *
  2374. * (tokenbuf) is a buffer of Direct3D shader bytecode.
  2375. * (bufsize) is the size, in bytes, of the bytecode buffer.
  2376. * (swiz), (swizcount), (smap), and (smapcount) are passed to
  2377. * MOJOSHADER_parse() unmolested.
  2378. *
  2379. * Returns NULL on error, or a shader handle on success.
  2380. *
  2381. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2382. * safe, you should probably only call this from the same thread that created
  2383. * the GL context.
  2384. *
  2385. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2386. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2387. *
  2388. * Compiled shaders from this function may not be shared between contexts.
  2389. */
  2390. MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
  2391. const unsigned int bufsize,
  2392. const MOJOSHADER_swizzle *swiz,
  2393. const unsigned int swizcount,
  2394. const MOJOSHADER_samplerMap *smap,
  2395. const unsigned int smapcount);
  2396. /*
  2397. * Get the MOJOSHADER_parseData structure that was produced from the
  2398. * call to MOJOSHADER_glCompileShader().
  2399. *
  2400. * This data is read-only, and you should NOT attempt to free it. This
  2401. * pointer remains valid until the shader is deleted.
  2402. */
  2403. const MOJOSHADER_parseData *MOJOSHADER_glGetShaderParseData(
  2404. MOJOSHADER_glShader *shader);
  2405. /*
  2406. * Link a vertex and pixel shader into an OpenGL program.
  2407. * (vshader) or (pshader) can be NULL, to specify that the GL should use the
  2408. * fixed-function pipeline instead of the programmable pipeline for that
  2409. * portion of the work. You can reuse shaders in various combinations across
  2410. * multiple programs, by relinking different pairs.
  2411. *
  2412. * It is illegal to give a vertex shader for (pshader) or a pixel shader
  2413. * for (vshader).
  2414. *
  2415. * Once you have successfully linked a program, you may render with it.
  2416. *
  2417. * Returns NULL on error, or a program handle on success.
  2418. *
  2419. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2420. * safe, you should probably only call this from the same thread that created
  2421. * the GL context.
  2422. *
  2423. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2424. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2425. *
  2426. * Linked programs from this function may not be shared between contexts.
  2427. */
  2428. MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
  2429. MOJOSHADER_glShader *pshader);
  2430. /*
  2431. * This binds the program (using, for example, glUseProgramObjectARB()), and
  2432. * disables all the client-side arrays so we can reset them with new values
  2433. * if appropriate.
  2434. *
  2435. * Call with NULL to disable the programmable pipeline and all enabled
  2436. * client-side arrays.
  2437. *
  2438. * After binding a program, you should update any uniforms you care about
  2439. * with MOJOSHADER_glSetVertexShaderUniformF() (etc), set any vertex arrays
  2440. * you want to use with MOJOSHADER_glSetVertexAttribute(), and finally call
  2441. * MOJOSHADER_glProgramReady() to commit everything to the GL. Then you may
  2442. * begin drawing through standard GL entry points.
  2443. *
  2444. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2445. * safe, you should probably only call this from the same thread that created
  2446. * the GL context.
  2447. *
  2448. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2449. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2450. */
  2451. void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program);
  2452. /*
  2453. * This binds individual shaders as if you had linked them with
  2454. * MOJOSHADER_glLinkProgram(), and used MOJOSHADER_glBindProgram() on the
  2455. * linked result.
  2456. *
  2457. * MojoShader will handle linking behind the scenes, and keep a cache of
  2458. * programs linked here. Programs are removed from this cache when one of the
  2459. * invidual shaders in it is deleted, otherwise they remain cached so future
  2460. * calls to this function don't need to relink a previously-used shader
  2461. * grouping.
  2462. *
  2463. * This function is for convenience, as the API is closer to how Direct3D
  2464. * works, and retrofitting linking into your app can be difficult;
  2465. * frequently, you just end up building your own cache, anyhow.
  2466. *
  2467. * Calling with all shaders set to NULL is equivalent to calling
  2468. * MOJOSHADER_glBindProgram(NULL).
  2469. *
  2470. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2471. * safe, you should probably only call this from the same thread that created
  2472. * the GL context.
  2473. *
  2474. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2475. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2476. */
  2477. void MOJOSHADER_glBindShaders(MOJOSHADER_glShader *vshader,
  2478. MOJOSHADER_glShader *pshader);
  2479. /*
  2480. * Set a floating-point uniform value (what Direct3D calls a "constant").
  2481. *
  2482. * There is a single array of 4-float "registers" shared by all vertex shaders.
  2483. * This is the "c" register file in Direct3D (c0, c1, c2, etc...)
  2484. * MojoShader will take care of synchronizing this internal array with the
  2485. * appropriate variables in the GL shaders.
  2486. *
  2487. * (idx) is the index into the internal array: 0 is the first four floats,
  2488. * 1 is the next four, etc.
  2489. * (data) is a pointer to (vec4count*4) floats.
  2490. *
  2491. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2492. * safe, you should probably only call this from the same thread that created
  2493. * the GL context.
  2494. *
  2495. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2496. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2497. *
  2498. * Uniforms are not shared between contexts.
  2499. */
  2500. void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
  2501. unsigned int vec4count);
  2502. /*
  2503. * Retrieve a floating-point uniform value (what Direct3D calls a "constant").
  2504. *
  2505. * There is a single array of 4-float "registers" shared by all vertex shaders.
  2506. * This is the "c" register file in Direct3D (c0, c1, c2, etc...)
  2507. * MojoShader will take care of synchronizing this internal array with the
  2508. * appropriate variables in the GL shaders.
  2509. *
  2510. * (idx) is the index into the internal array: 0 is the first four floats,
  2511. * 1 is the next four, etc.
  2512. * (data) is a pointer to space for (vec4count*4) floats.
  2513. * (data) will be filled will current values in the register file. Results
  2514. * are undefined if you request data past the end of the register file or
  2515. * previously uninitialized registers.
  2516. *
  2517. * This is a "fast" call; we're just reading memory from internal memory. We
  2518. * do not query the GPU or the GL for this information.
  2519. *
  2520. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2521. * safe, you should probably only call this from the same thread that created
  2522. * the GL context.
  2523. *
  2524. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2525. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2526. *
  2527. * Uniforms are not shared between contexts.
  2528. */
  2529. void MOJOSHADER_glGetVertexShaderUniformF(unsigned int idx, float *data,
  2530. unsigned int vec4count);
  2531. /*
  2532. * Set an integer uniform value (what Direct3D calls a "constant").
  2533. *
  2534. * There is a single array of 4-int "registers" shared by all vertex shaders.
  2535. * This is the "i" register file in Direct3D (i0, i1, i2, etc...)
  2536. * MojoShader will take care of synchronizing this internal array with the
  2537. * appropriate variables in the GL shaders.
  2538. *
  2539. * (idx) is the index into the internal array: 0 is the first four ints,
  2540. * 1 is the next four, etc.
  2541. * (data) is a pointer to (ivec4count*4) ints.
  2542. *
  2543. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2544. * safe, you should probably only call this from the same thread that created
  2545. * the GL context.
  2546. *
  2547. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2548. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2549. *
  2550. * Uniforms are not shared between contexts.
  2551. */
  2552. void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
  2553. unsigned int ivec4count);
  2554. /*
  2555. * Retrieve an integer uniform value (what Direct3D calls a "constant").
  2556. *
  2557. * There is a single array of 4-int "registers" shared by all vertex shaders.
  2558. * This is the "i" register file in Direct3D (i0, i1, i2, etc...)
  2559. * MojoShader will take care of synchronizing this internal array with the
  2560. * appropriate variables in the GL shaders.
  2561. *
  2562. * (idx) is the index into the internal array: 0 is the first four ints,
  2563. * 1 is the next four, etc.
  2564. * (data) is a pointer to space for (ivec4count*4) ints.
  2565. * (data) will be filled will current values in the register file. Results
  2566. * are undefined if you request data past the end of the register file or
  2567. * previously uninitialized registers.
  2568. *
  2569. * This is a "fast" call; we're just reading memory from internal memory. We
  2570. * do not query the GPU or the GL for this information.
  2571. *
  2572. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2573. * safe, you should probably only call this from the same thread that created
  2574. * the GL context.
  2575. *
  2576. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2577. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2578. *
  2579. * Uniforms are not shared between contexts.
  2580. */
  2581. void MOJOSHADER_glGetVertexShaderUniformI(unsigned int idx, int *data,
  2582. unsigned int ivec4count);
  2583. /*
  2584. * Set a boolean uniform value (what Direct3D calls a "constant").
  2585. *
  2586. * There is a single array of "registers" shared by all vertex shaders.
  2587. * This is the "b" register file in Direct3D (b0, b1, b2, etc...)
  2588. * MojoShader will take care of synchronizing this internal array with the
  2589. * appropriate variables in the GL shaders.
  2590. *
  2591. * Unlike the float and int counterparts, booleans are single values, not
  2592. * four-element vectors...so idx==1 is the second boolean in the internal
  2593. * array, not the fifth.
  2594. *
  2595. * Non-zero values are considered "true" and zero is considered "false".
  2596. *
  2597. * (idx) is the index into the internal array.
  2598. * (data) is a pointer to (bcount) ints.
  2599. *
  2600. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2601. * safe, you should probably only call this from the same thread that created
  2602. * the GL context.
  2603. *
  2604. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2605. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2606. *
  2607. * Uniforms are not shared between contexts.
  2608. */
  2609. void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
  2610. unsigned int bcount);
  2611. /*
  2612. * Retrieve a boolean uniform value (what Direct3D calls a "constant").
  2613. *
  2614. * There is a single array of "registers" shared by all vertex shaders.
  2615. * This is the "b" register file in Direct3D (b0, b1, b2, etc...)
  2616. * MojoShader will take care of synchronizing this internal array with the
  2617. * appropriate variables in the GL shaders.
  2618. *
  2619. * Unlike the float and int counterparts, booleans are single values, not
  2620. * four-element vectors...so idx==1 is the second boolean in the internal
  2621. * array, not the fifth.
  2622. *
  2623. * Non-zero values are considered "true" and zero is considered "false".
  2624. * This function will always return true values as 1, regardless of what
  2625. * non-zero integer you originally used to set the registers.
  2626. *
  2627. * (idx) is the index into the internal array.
  2628. * (data) is a pointer to space for (bcount) ints.
  2629. * (data) will be filled will current values in the register file. Results
  2630. * are undefined if you request data past the end of the register file or
  2631. * previously uninitialized registers.
  2632. *
  2633. * This is a "fast" call; we're just reading memory from internal memory. We
  2634. * do not query the GPU or the GL for this information.
  2635. *
  2636. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2637. * safe, you should probably only call this from the same thread that created
  2638. * the GL context.
  2639. *
  2640. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2641. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2642. *
  2643. * Uniforms are not shared between contexts.
  2644. */
  2645. void MOJOSHADER_glGetVertexShaderUniformB(unsigned int idx, int *data,
  2646. unsigned int bcount);
  2647. /*
  2648. * The equivalent of MOJOSHADER_glSetVertexShaderUniformF() for pixel
  2649. * shaders. Other than using a different internal array that is specific
  2650. * to pixel shaders, this functions just like its vertex array equivalent.
  2651. *
  2652. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2653. * safe, you should probably only call this from the same thread that created
  2654. * the GL context.
  2655. *
  2656. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2657. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2658. *
  2659. * Uniforms are not shared between contexts.
  2660. */
  2661. void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
  2662. unsigned int vec4count);
  2663. /*
  2664. * The equivalent of MOJOSHADER_glGetVertexShaderUniformF() for pixel
  2665. * shaders. Other than using a different internal array that is specific
  2666. * to pixel shaders, this functions just like its vertex array equivalent.
  2667. *
  2668. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2669. * safe, you should probably only call this from the same thread that created
  2670. * the GL context.
  2671. *
  2672. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2673. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2674. *
  2675. * Uniforms are not shared between contexts.
  2676. */
  2677. void MOJOSHADER_glGetPixelShaderUniformF(unsigned int idx, float *data,
  2678. unsigned int vec4count);
  2679. /*
  2680. * The equivalent of MOJOSHADER_glSetVertexShaderUniformI() for pixel
  2681. * shaders. Other than using a different internal array that is specific
  2682. * to pixel shaders, this functions just like its vertex array equivalent.
  2683. *
  2684. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2685. * safe, you should probably only call this from the same thread that created
  2686. * the GL context.
  2687. *
  2688. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2689. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2690. *
  2691. * Uniforms are not shared between contexts.
  2692. */
  2693. void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
  2694. unsigned int ivec4count);
  2695. /*
  2696. * The equivalent of MOJOSHADER_glGetVertexShaderUniformI() for pixel
  2697. * shaders. Other than using a different internal array that is specific
  2698. * to pixel shaders, this functions just like its vertex array equivalent.
  2699. *
  2700. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2701. * safe, you should probably only call this from the same thread that created
  2702. * the GL context.
  2703. *
  2704. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2705. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2706. *
  2707. * Uniforms are not shared between contexts.
  2708. */
  2709. void MOJOSHADER_glGetPixelShaderUniformI(unsigned int idx, int *data,
  2710. unsigned int ivec4count);
  2711. /*
  2712. * The equivalent of MOJOSHADER_glSetVertexShaderUniformB() for pixel
  2713. * shaders. Other than using a different internal array that is specific
  2714. * to pixel shaders, this functions just like its vertex array equivalent.
  2715. *
  2716. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2717. * safe, you should probably only call this from the same thread that created
  2718. * the GL context.
  2719. *
  2720. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2721. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2722. *
  2723. * Uniforms are not shared between contexts.
  2724. */
  2725. void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
  2726. unsigned int bcount);
  2727. /*
  2728. * The equivalent of MOJOSHADER_glGetVertexShaderUniformB() for pixel
  2729. * shaders. Other than using a different internal array that is specific
  2730. * to pixel shaders, this functions just like its vertex array equivalent.
  2731. *
  2732. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2733. * safe, you should probably only call this from the same thread that created
  2734. * the GL context.
  2735. *
  2736. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2737. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2738. *
  2739. * Uniforms are not shared between contexts.
  2740. */
  2741. void MOJOSHADER_glGetPixelShaderUniformB(unsigned int idx, int *data,
  2742. unsigned int bcount);
  2743. /*
  2744. * Set up the vector for the TEXBEM opcode. Most apps can ignore this API.
  2745. *
  2746. * Shader Model 1.1 through 1.3 had an instruction for "fake bump mapping"
  2747. * called TEXBEM. To use it, you had to set some sampler states,
  2748. * D3DTSS_BUMPENVMATxx, which would be referenced by the opcode.
  2749. *
  2750. * This functionality was removed from Shader Model 1.4 and later, because
  2751. * it was special-purpose and limited. The functionality could be built on
  2752. * more general opcodes, and the sampler state could be supplied in a more
  2753. * general uniform.
  2754. *
  2755. * However, to support this opcode, we supply a way to specify that sampler
  2756. * state, and the OpenGL glue code does the right thing to pass that
  2757. * information to the shader.
  2758. *
  2759. * This call maps to IDirect3DDevice::SetTextureStageState() with the
  2760. * D3DTSS_BUMPENVMAT00, D3DTSS_BUMPENVMAT01, D3DTSS_BUMPENVMAT10,
  2761. * D3DTSS_BUMPENVMAT11, D3DTSS_BUMPENVLSCALE, and D3DTSS_BUMPENVLOFFSET
  2762. * targets. This is only useful for Shader Model < 1.4 pixel shaders, if
  2763. * they use the TEXBEM or TEXBEML opcode. If you aren't sure, you don't need
  2764. * this function.
  2765. *
  2766. * Like the rest of your uniforms, you must call MOJOSHADER_glProgramReady()
  2767. * between setting new values and drawing with them.
  2768. *
  2769. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2770. * safe, you should probably only call this from the same thread that created
  2771. * the GL context.
  2772. *
  2773. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2774. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2775. *
  2776. * These values are not shared between contexts.
  2777. */
  2778. void MOJOSHADER_glSetLegacyBumpMapEnv(unsigned int sampler, float mat00,
  2779. float mat01, float mat10, float mat11,
  2780. float lscale, float loffset);
  2781. /*
  2782. * Connect a client-side array to the currently-bound program.
  2783. *
  2784. * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
  2785. * be MOJOSHADER_USAGE_COLOR and 1.
  2786. *
  2787. * The caller should bind VBOs before this call and treat (ptr) as an offset,
  2788. * if appropriate.
  2789. *
  2790. * MojoShader will figure out where to plug this stream into the
  2791. * currently-bound program, and enable the appropriate client-side array.
  2792. *
  2793. * (size), (type), (normalized), (stride), and (ptr) correspond to
  2794. * glVertexAttribPointer()'s parameters (in most cases, these get passed
  2795. * unmolested to that very entry point during this function).
  2796. *
  2797. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2798. * safe, you should probably only call this from the same thread that created
  2799. * the GL context.
  2800. *
  2801. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2802. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2803. *
  2804. * Vertex attributes are not shared between contexts.
  2805. */
  2806. /* !!! FIXME: this should probably be "input" and not "attribute" */
  2807. /* !!! FIXME: or maybe "vertex array" or something. */
  2808. void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
  2809. int index, unsigned int size,
  2810. MOJOSHADER_attributeType type,
  2811. int normalized, unsigned int stride,
  2812. const void *ptr);
  2813. /* These below functions are temporary and will be removed from the API once
  2814. the real Effects API is written. Do not use! */
  2815. void MOJOSHADER_glSetVertexPreshaderUniformF(unsigned int idx, const float *data,
  2816. unsigned int vec4n);
  2817. void MOJOSHADER_glGetVertexPreshaderUniformF(unsigned int idx, float *data,
  2818. unsigned int vec4n);
  2819. void MOJOSHADER_glSetPixelPreshaderUniformF(unsigned int idx, const float *data,
  2820. unsigned int vec4n);
  2821. void MOJOSHADER_glGetPixelPreshaderUniformF(unsigned int idx, float *data,
  2822. unsigned int vec4n);
  2823. /* These above functions are temporary and will be removed from the API once
  2824. the real Effects API is written. Do not use! */
  2825. /*
  2826. * Inform MojoShader that it should commit any pending state to the GL. This
  2827. * must be called after you bind a program and update any inputs, right
  2828. * before you start drawing, so any outstanding changes made to the shared
  2829. * constants array (etc) can propagate to the shader during this call.
  2830. *
  2831. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2832. * safe, you should probably only call this from the same thread that created
  2833. * the GL context.
  2834. *
  2835. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2836. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2837. */
  2838. void MOJOSHADER_glProgramReady(void);
  2839. /*
  2840. * Free the resources of a linked program. This will delete the GL object
  2841. * and free memory.
  2842. *
  2843. * If the program is currently bound by MOJOSHADER_glBindProgram(), it will
  2844. * be deleted as soon as it becomes unbound.
  2845. *
  2846. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2847. * safe, you should probably only call this from the same thread that created
  2848. * the GL context.
  2849. *
  2850. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2851. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2852. */
  2853. void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program);
  2854. /*
  2855. * Free the resources of a compiled shader. This will delete the GL object
  2856. * and free memory.
  2857. *
  2858. * If the shader is currently referenced by a linked program (or is currently
  2859. * bound with MOJOSHADER_glBindShaders()), it will be deleted as soon as all
  2860. * referencing programs are deleted and it is no longer bound, too.
  2861. *
  2862. * This call is NOT thread safe! As most OpenGL implementations are not thread
  2863. * safe, you should probably only call this from the same thread that created
  2864. * the GL context.
  2865. *
  2866. * This call requires a valid MOJOSHADER_glContext to have been made current,
  2867. * or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
  2868. */
  2869. void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader);
  2870. /*
  2871. * Deinitialize MojoShader's OpenGL shader management.
  2872. *
  2873. * You must call this once, while your GL context (not MojoShader context) is
  2874. * still current, if you previously had a successful call to
  2875. * MOJOSHADER_glCreateContext(). This should be the last MOJOSHADER_gl*
  2876. * function you call until you've prepared a context again.
  2877. *
  2878. * This will clean up resources previously allocated, and may call into the GL.
  2879. *
  2880. * This will not clean up shaders and programs you created! Please call
  2881. * MOJOSHADER_glDeleteShader() and MOJOSHADER_glDeleteProgram() to clean
  2882. * those up before calling this function!
  2883. *
  2884. * This function destroys the MOJOSHADER_glContext you pass it. If it's the
  2885. * current context, then no context will be current upon return.
  2886. *
  2887. * This call is NOT thread safe! There must not be any other MOJOSHADER_gl*
  2888. * functions running when this is called. Also, as most OpenGL implementations
  2889. * are not thread safe, you should probably only call this from the same
  2890. * thread that created the GL context.
  2891. */
  2892. void MOJOSHADER_glDestroyContext(MOJOSHADER_glContext *ctx);
  2893. #ifdef __cplusplus
  2894. }
  2895. #endif
  2896. #endif /* include-once blocker. */
  2897. /* end of mojoshader.h ... */