Shader.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /******************************************************************************/
  2. struct ShaderImage // Shader Image
  3. {
  4. C Image* get( )C {return _image ;}
  5. void set(C Image *image) {T._image= image ;}
  6. void set(C Image &image) {T._image=&image ;}
  7. void set(C ImageRTPtr &image) {T._image= image();}
  8. //void set(C ImagePtr &image) {T._image= image();} this is not safe, as 'ShaderImage' does not store 'ImagePtr' for performance reasons
  9. ShaderImage() {_image=null; _sampler=null;}
  10. #if !EE_PRIVATE
  11. private:
  12. #endif
  13. C Image *_image;
  14. #if EE_PRIVATE
  15. struct Sampler
  16. {
  17. #if DX9
  18. D3DTEXTUREFILTERTYPE filter [2];
  19. D3DTEXTUREADDRESS address[3];
  20. void set(Int index);
  21. void del() {}
  22. #elif DX11
  23. ID3D11SamplerState *state;
  24. Bool is()C {return state!=null;}
  25. void del ();
  26. Bool createTry(D3D11_SAMPLER_DESC &desc);
  27. void create (D3D11_SAMPLER_DESC &desc);
  28. void setVS (Int index);
  29. void setHS (Int index);
  30. void setDS (Int index);
  31. void setPS (Int index);
  32. void set (Int index);
  33. Sampler() {state=null;}
  34. ~Sampler() {del();}
  35. #elif GL
  36. UInt filter [2];
  37. UInt address[3];
  38. void del() {}
  39. #endif
  40. };
  41. Sampler *_sampler;
  42. #if DX9
  43. INLINE IDirect3DBaseTexture9 * getBase()C {return _image ? _image->_base : null;}
  44. #elif DX11
  45. INLINE ID3D11ShaderResourceView* getSRV ()C {return _image ? _image->_srv : null;}
  46. #endif
  47. #else
  48. Ptr _sampler;
  49. #endif
  50. };
  51. /******************************************************************************/
  52. struct ShaderParam // Shader Parameter
  53. {
  54. void set( Bool b ); // set boolean value
  55. void set( Int i ); // set integer value
  56. void set( Flt f ); // set float value
  57. void set( Dbl d ); // set double value
  58. void set(C Vec2 &v ); // set vector2D value
  59. void set(C VecD2 &v ); // set vector2D value
  60. void set(C VecI2 &v ); // set vector2D value
  61. void set(C Vec &v ); // set vector3D value
  62. void set(C VecD &v ); // set vector3D value
  63. void set(C VecI &v ); // set vector3D value
  64. void set(C Vec4 &v ); // set vector4D value
  65. void set(C VecD4 &v ); // set vector4D value
  66. void set(C VecI4 &v ); // set vector4D value
  67. void set(C Color &color ); // set vector4D value
  68. void set(C Rect &rect ); // set vector4D value
  69. void set(C Matrix3 &matrix ); // set matrix3 value
  70. void set(C Matrix &matrix ); // set matrix value
  71. void set(C MatrixM &matrix ); // set matrix value
  72. void set(C Matrix4 &matrix ); // set matrix4 value
  73. void set(C Vec *v , Int elms); // set vector3D array
  74. void set(C Vec4 *v , Int elms); // set vector4D array
  75. void set(C Matrix *matrix, Int elms); // set matrix array
  76. void set( CPtr data , Int size); // set memory
  77. T1(TYPE) void set(C TYPE &data ) {set((CPtr)&data, SIZE(data));}
  78. #if EE_PRIVATE
  79. void set (C Vec &v , Int elm ); // set vector3D array element value
  80. void set (C Vec4 &v , Int elm ); // set vector4D array element value
  81. void set (C Matrix &matrix , Int elm ); // set matrix array element value
  82. void fromMul(C Matrix &a, C Matrix &b ); // set matrix value from "a*b"
  83. void fromMul(C Matrix &a, C MatrixM &b ); // set matrix value from "a*b"
  84. void fromMul(C MatrixM &a, C MatrixM &b ); // set matrix value from "a*b"
  85. void fromMul(C Matrix &a, C Matrix &b, Int elm ); // set matrix array element value from "a*b"
  86. void fromMul(C Matrix &a, C MatrixM &b, Int elm ); // set matrix array element value from "a*b"
  87. void fromMul(C MatrixM &a, C MatrixM &b, Int elm ); // set matrix array element value from "a*b"
  88. void set (C GpuMatrix &matrix ); // set matrix value
  89. void set (C GpuMatrix &matrix , Int elm ); // set matrix array element value
  90. void set (C GpuMatrix *matrix , Int elms); // set matrix array
  91. void setConditional(C Flt &f ); // set float value only if it's different
  92. void setConditional(C Vec2 &v ); // set vector2D value only if it's different
  93. void setConditional(C Vec &v ); // set vector3D value only if it's different
  94. void setConditional(C Vec4 &v ); // set vector4D value only if it's different
  95. void setConditional(C Vec &v, Int elm); // set vector3D array element value only if it's different
  96. void setConditional(C Rect &r ); // set vector4D value only if it's different
  97. void setSafe(C Vec4 &v); // set from vector4D value, but limit the actual size copied based on 'ShaderParam' size
  98. #endif
  99. #if EE_PRIVATE
  100. struct Translation
  101. {
  102. Int cpu_offset, gpu_offset, elm_size;
  103. Bool operator!=(C Translation &trans)C {return T.cpu_offset!=trans.cpu_offset || T.gpu_offset!=trans.gpu_offset || T.elm_size!=trans.elm_size;}
  104. void set(Int cpu_offset, Int gpu_offset, Int elm_size) {T.cpu_offset=cpu_offset; T.gpu_offset=gpu_offset; T.elm_size=elm_size;}
  105. };
  106. Byte *_data;
  107. Int _cpu_data_size, _gpu_data_size, _elements, _constant_count;
  108. Bool *_changed, _owns_data;
  109. Mems<Translation> _full_translation, _optimized_translation;
  110. Bool is()C {return _cpu_data_size>0;}
  111. Int fullConstantCount()C {return Ceil16(_gpu_data_size)/16;} // number of Vec4's, SIZE(Vec4)==16, DX9 constants operate on Vec4, DX10+ does not use this, for OpenGL this will work only for Vec4's
  112. INLINE void setChanged() {*_changed=true;}
  113. void optimize();
  114. void initAsElement(ShaderParam &parent, Int index);
  115. Bool save(File &f, C Str8 &name)C;
  116. INLINE GpuMatrix* asGpuMatrix() {return (GpuMatrix*)_data;}
  117. ~ShaderParam();
  118. ShaderParam();
  119. NO_COPY_CONSTRUCTOR(ShaderParam);
  120. #endif
  121. };
  122. /******************************************************************************/
  123. struct ShaderParamChange // Shader Parameter Change
  124. {
  125. ShaderParam *param; // parameter to change
  126. Vec4 value; // value to change to
  127. ShaderParamChange& set( Bool b) {value.x =b; return T;}
  128. ShaderParamChange& set( Int i) {value.x =i; return T;}
  129. ShaderParamChange& set( Flt f) {value.x =f; return T;}
  130. ShaderParamChange& set(C Vec2 &v) {value.xy =v; return T;}
  131. ShaderParamChange& set(C Vec &v) {value.xyz=v; return T;}
  132. ShaderParamChange& set(C Vec4 &v) {value =v; return T;}
  133. ShaderParamChange& set(ShaderParam *param) {T.param=param; return T;}
  134. ShaderParamChange() {param=null; value.zero();}
  135. };
  136. /******************************************************************************/
  137. #if EE_PRIVATE
  138. struct ShaderBuffer // Constant Buffer
  139. {
  140. struct Buffer
  141. {
  142. GPU_API(Ptr, ID3D11Buffer*, Ptr) buffer; // keep this is first member because it's used most often
  143. Int size ;
  144. void del ();
  145. void create(Int size);
  146. Bool is()C {return size>0;}
  147. void zero() {buffer=null; size=0;}
  148. ~Buffer() {del ();}
  149. Buffer() {zero();}
  150. // intentionally keep copy constructor as raw member copy, because we expect this behavior
  151. };
  152. Buffer buffer; // keep this is first member because it's used most often
  153. Byte *data;
  154. Bool changed;
  155. Mems<Buffer> parts;
  156. Bool is ()C {return buffer.is();}
  157. Int size ()C {return parts.elms() ? parts[0].size : buffer.size;} // first part always has the full size, so use it if available, because 'buffer' can be set to a smaller part
  158. void create (Int size );
  159. void update ( );
  160. void bind (Int index);
  161. void bindCheck (Int index);
  162. void setPart (Int part );
  163. void createParts(C Int *elms, Int elms_num);
  164. ShaderBuffer();
  165. ~ShaderBuffer();
  166. NO_COPY_CONSTRUCTOR(ShaderBuffer);
  167. };
  168. STRUCT(ShaderParamName , ShaderParam)
  169. //{
  170. Str8 name;
  171. Bool save(File &f)C {return super::save(f, name);}
  172. };
  173. struct ShaderBufferParams
  174. {
  175. ShaderBuffer *buffer;
  176. Int index;
  177. Mems<ShaderParamName> params;
  178. };
  179. #endif
  180. /******************************************************************************/
  181. struct ShaderShader
  182. {
  183. #if EE_PRIVATE
  184. Mems<Byte> data;
  185. void clean() {data.del();}
  186. Bool save(File &f)C {return data.saveRaw(f);}
  187. Bool load(File &f) {return data.loadRaw(f);}
  188. #endif
  189. };
  190. #if EE_PRIVATE
  191. /******************************************************************************/
  192. #if WINDOWS_OLD
  193. STRUCT(ShaderVS9 , ShaderShader)
  194. //{
  195. IDirect3DVertexShader9 *vs;
  196. IDirect3DVertexShader9* create();
  197. ~ShaderVS9();
  198. ShaderVS9() {vs=null;}
  199. NO_COPY_CONSTRUCTOR(ShaderVS9);
  200. };
  201. STRUCT(ShaderPS9 , ShaderShader)
  202. //{
  203. IDirect3DPixelShader9 *ps;
  204. IDirect3DPixelShader9* create();
  205. ~ShaderPS9();
  206. ShaderPS9() {ps=null;}
  207. NO_COPY_CONSTRUCTOR(ShaderPS9);
  208. };
  209. #endif
  210. /******************************************************************************/
  211. #if WINDOWS
  212. STRUCT(ShaderVS11 , ShaderShader)
  213. //{
  214. ID3D11VertexShader *vs;
  215. ID3D11VertexShader* create();
  216. ~ShaderVS11();
  217. ShaderVS11() {vs=null;}
  218. NO_COPY_CONSTRUCTOR(ShaderVS11);
  219. };
  220. STRUCT(ShaderHS11 , ShaderShader)
  221. //{
  222. ID3D11HullShader *hs;
  223. ID3D11HullShader* create();
  224. ~ShaderHS11();
  225. ShaderHS11() {hs=null;}
  226. NO_COPY_CONSTRUCTOR(ShaderHS11);
  227. };
  228. STRUCT(ShaderDS11 , ShaderShader)
  229. //{
  230. ID3D11DomainShader *ds;
  231. ID3D11DomainShader* create();
  232. ~ShaderDS11();
  233. ShaderDS11() {ds=null;}
  234. NO_COPY_CONSTRUCTOR(ShaderDS11);
  235. };
  236. STRUCT(ShaderPS11 , ShaderShader)
  237. //{
  238. ID3D11PixelShader *ps;
  239. ID3D11PixelShader* create();
  240. ~ShaderPS11();
  241. ShaderPS11() {ps=null;}
  242. NO_COPY_CONSTRUCTOR(ShaderPS11);
  243. };
  244. #endif
  245. /******************************************************************************/
  246. STRUCT(ShaderVSGL , ShaderShader)
  247. //{
  248. UInt vs;
  249. UInt create(Bool clean, Str *messages);
  250. Str source();
  251. ~ShaderVSGL();
  252. ShaderVSGL() {vs=0;}
  253. NO_COPY_CONSTRUCTOR(ShaderVSGL);
  254. };
  255. STRUCT(ShaderPSGL , ShaderShader)
  256. //{
  257. UInt ps;
  258. UInt create(Bool clean, Str *messages);
  259. Str source();
  260. ~ShaderPSGL();
  261. ShaderPSGL() {ps=0;}
  262. NO_COPY_CONSTRUCTOR(ShaderPSGL);
  263. };
  264. /******************************************************************************/
  265. #if WINDOWS_OLD
  266. struct Shader9
  267. {
  268. struct Texture
  269. {
  270. Int index;
  271. ShaderImage *image;
  272. void set(Int index, ShaderImage &image) {T.index=index; T.image=&image;}
  273. };
  274. const_mem_addr struct Constant
  275. {
  276. Int start, count, *final_count; // 'final_count'=points to either 'Constant.count' or 'ShaderParam._constant_count' (for example 'ShaderParam ViewMatrix _constant_count' can be limited depending on 'SetMatrixCount')
  277. Ptr data;
  278. Bool *changed;
  279. ShaderParam *sp;
  280. void set(Int start, Int count, Ptr data, ShaderParam &sp) {T.start=start; T.count=count; T.final_count=&T.count; T.data=data; T.changed=sp._changed; T.sp=&sp;}
  281. };
  282. IDirect3DVertexShader9 *vs;
  283. IDirect3DPixelShader9 *ps;
  284. Str8 name;
  285. Int vs_index, ps_index;
  286. Mems<Texture > textures;
  287. Mems<Constant> vs_constants, ps_constants;
  288. Bool validate (ShaderFile &shader, Str *messages=null);
  289. void commit ();
  290. void commitTex();
  291. void start ();
  292. void begin ();
  293. Bool save (File &f, C Map<Str8, ShaderParam> &params, C Memc <ShaderImage* > &images)C;
  294. Bool load (File &f, C MemtN<ShaderParam*, 256> &params, C MemtN<ShaderImage*, 256> &images);
  295. Shader9();
  296. };
  297. #endif
  298. /******************************************************************************/
  299. #if WINDOWS
  300. struct Shader11
  301. {
  302. struct Texture
  303. {
  304. Int index;
  305. ShaderImage *image;
  306. void set(Int index, ShaderImage &image) {T.index=index; T.image=&image;}
  307. };
  308. struct Buffer
  309. {
  310. Int index ;
  311. ShaderBuffer *buffer;
  312. void set(Int index, ShaderBuffer &buffer) {T.index=index; T.buffer=&buffer;}
  313. };
  314. ID3D11VertexShader *vs;
  315. ID3D11HullShader *hs;
  316. ID3D11DomainShader *ds;
  317. ID3D11PixelShader *ps;
  318. Str8 name;
  319. Int vs_index , hs_index , ds_index , ps_index ;
  320. Mems<Texture > vs_textures, hs_textures, ds_textures, ps_textures;
  321. Mems< Buffer > vs_buffers , hs_buffers , ds_buffers , ps_buffers ;
  322. Mems<ShaderBuffer*> buffers;
  323. Bool validate (ShaderFile &shader, Str *messages=null);
  324. void commit ();
  325. void commitTex();
  326. void start ();
  327. void begin ();
  328. Bool save (File &f, C Memc <ShaderBufferParams> &buffers, C Memc <ShaderImage* > &images)C;
  329. Bool load (File &f, C MemtN<ShaderBuffer*, 256> &buffers, C MemtN<ShaderImage*, 256> &images);
  330. Shader11();
  331. };
  332. #endif
  333. /******************************************************************************/
  334. struct ShaderGL
  335. {
  336. struct Texture
  337. {
  338. Int index;
  339. ShaderImage *image;
  340. void set(Int index, ShaderImage &image) {T.index=index; T.image=&image;}
  341. };
  342. const_mem_addr struct Constant
  343. {
  344. #if GL
  345. typedef void (PLATFORM(WINAPI,) *glUniformPtr) (GLint location, GLsizei count, const GLfloat *value);
  346. #endif
  347. Int index, count, *final_count; // 'final_count'=points to either 'Constant.count' or 'ShaderParam._constant_count' (for example 'ShaderParam ViewMatrix _constant_count' can be limited depending on 'SetMatrixCount')
  348. Ptr data;
  349. Bool *changed;
  350. ShaderParam *sp;
  351. GPU_API(Ptr, Ptr, glUniformPtr) uniform;
  352. void set(Int index, Int count, Ptr data, ShaderParam &sp) {T.index=index; T.count=count; T.final_count=&T.count; T.data=data; T.changed=sp._changed; T.sp=&sp;}
  353. };
  354. struct GLSLParam
  355. {
  356. U16 gpu_offset;
  357. ShaderParam *param;
  358. Str8 glsl_name;
  359. void set(Int gpu_offset, ShaderParam &param, C Str8 &glsl_name);
  360. };
  361. Str8 name;
  362. Int vs_index, ps_index;
  363. UInt vs, ps, prog;
  364. Mems<Texture> textures;
  365. Mems<Constant> constants;
  366. Mems<GLSLParam> glsl_params;
  367. Str source ();
  368. UInt compileEx(MemPtr<ShaderVSGL> vs_array, MemPtr<ShaderPSGL> ps_array, Bool clean, ShaderFile *shader, Str *messages);
  369. void compile (MemPtr<ShaderVSGL> vs_array, MemPtr<ShaderPSGL> ps_array, Str *messages);
  370. Bool validate (ShaderFile &shader, Str *messages=null);
  371. void commit ();
  372. void commitTex();
  373. void start ();
  374. void begin ();
  375. Bool save (File &f, C Map <Str8, ShaderParam> &params, C Memc <ShaderImage* > &images)C;
  376. Bool load (File &f, C MemtN<ShaderParam*, 256> &params, C MemtN<ShaderImage*, 256> &images);
  377. ShaderGL();
  378. ~ShaderGL();
  379. };
  380. /******************************************************************************/
  381. struct ShaderBase
  382. {
  383. Int shader_draw; // index of this shader in 'ShaderDraws' container
  384. INLINE Shader& asShader () {return (Shader& )T;}
  385. INLINE Shader& asForwardShader() {return **(Shader**)(((Byte*)this)+Renderer._frst_light_offset);}
  386. INLINE Shader& asBlendShader () {return **(Shader**)(((Byte*)this)+Renderer._blst_light_offset);}
  387. INLINE Shader& getShader(Bool forward)
  388. {
  389. if(forward)return asForwardShader();
  390. else return asShader ();
  391. }
  392. INLINE Shader& getBlendShader(Bool blst)
  393. {
  394. if(blst)return asBlendShader();
  395. else return asShader ();
  396. }
  397. void unlink() {shader_draw=-1;}
  398. ShaderBase() {unlink();}
  399. };
  400. struct Shader : ShaderBase, GPU_API(Shader9, Shader11, ShaderGL)
  401. {
  402. #else
  403. struct Shader
  404. {
  405. #endif
  406. void draw(C Image *image=null, C Rect *rect=null ); // apply custom 2D effect on the screen, 'image'=image to automatically set as 'Col' shader image, 'rect'=screen rectangle for the effect (set null for full viewport)
  407. void draw(C Image &image , C Rect *rect=null ) {draw(&image , rect );} // apply custom 2D effect on the screen, 'image'=image to automatically set as 'Col' shader image, 'rect'=screen rectangle for the effect (set null for full viewport)
  408. void draw(C ImageRTPtr &image , C Rect *rect=null ) {draw( image(), rect );} // apply custom 2D effect on the screen, 'image'=image to automatically set as 'Col' shader image, 'rect'=screen rectangle for the effect (set null for full viewport)
  409. void draw(C Image *image , C Rect *rect, C Rect &tex); // apply custom 2D effect on the screen, 'image'=image to automatically set as 'Col' shader image, 'rect'=screen rectangle for the effect (set null for full viewport), 'tex'=source image texture coordinates
  410. void draw(C Image &image , C Rect *rect, C Rect &tex) {draw(&image , rect, tex);} // apply custom 2D effect on the screen, 'image'=image to automatically set as 'Col' shader image, 'rect'=screen rectangle for the effect (set null for full viewport), 'tex'=source image texture coordinates
  411. void draw(C ImageRTPtr &image , C Rect *rect, C Rect &tex) {draw( image(), rect, tex);} // apply custom 2D effect on the screen, 'image'=image to automatically set as 'Col' shader image, 'rect'=screen rectangle for the effect (set null for full viewport), 'tex'=source image texture coordinates
  412. };
  413. /******************************************************************************/
  414. struct ShaderFile // Shader File
  415. {
  416. // get
  417. Shader* first( ); // first shader, null on fail
  418. Shader* find(C Str8 &name); // find shader, null on fail
  419. Shader* get(C Str8 &name); // get shader, Exit on fail
  420. #if EE_PRIVATE
  421. Shader* find(C Str8 &name, Str *messages); // find shader, put error messages into 'messages', null on fail
  422. #endif
  423. // manage
  424. void del();
  425. // io
  426. Bool load(C Str &name); // load, false on fail
  427. ShaderFile();
  428. ~ShaderFile() {del();}
  429. #if !EE_PRIVATE
  430. private:
  431. #endif
  432. #if EE_PRIVATE
  433. #if DX9
  434. Mems<ShaderVS9 > _vs;
  435. Mems<ShaderShader> _hs;
  436. Mems<ShaderShader> _ds;
  437. Mems<ShaderPS9 > _ps;
  438. #elif DX11
  439. Mems<ShaderVS11> _vs;
  440. Mems<ShaderHS11> _hs;
  441. Mems<ShaderDS11> _ds;
  442. Mems<ShaderPS11> _ps;
  443. #elif GL
  444. Mems<ShaderVSGL > _vs;
  445. Mems<ShaderShader> _hs;
  446. Mems<ShaderShader> _ds;
  447. Mems<ShaderPSGL > _ps;
  448. #endif
  449. #else
  450. Mems<ShaderShader> _vs, _hs, _ds, _ps;
  451. #endif
  452. Mems<Shader > _shaders;
  453. NO_COPY_CONSTRUCTOR(ShaderFile);
  454. };
  455. /******************************************************************************/
  456. #if EE_PRIVATE
  457. struct FRSTKey // Forward Rendering Shader Techniques Key
  458. {
  459. Byte skin, materials, textures, bump_mode, alpha_test, light_map, detail, rflct, color, mtrl_blend, heightmap, fx, tess;
  460. FRSTKey() {Zero(T);}
  461. };
  462. struct FRST : ShaderBase // Forward Rendering Shader Techniques
  463. {
  464. Bool all_passes;
  465. Shader
  466. *none, // no light
  467. *dir , * dir_shd[6], // directional light, [MapNum]
  468. *pnt , * pnt_shd , // point light
  469. *sqr , * sqr_shd , // square light
  470. *cone, *cone_shd ; // cone light
  471. };
  472. /******************************************************************************/
  473. struct BLSTKey // Blend Light Shader Techniques
  474. {
  475. Byte skin, color, textures, bump_mode, alpha_test, alpha, light_map, rflct, fx, per_pixel;
  476. BLSTKey() {Zero(T);}
  477. };
  478. struct BLST // Blend Light Shader Techniques
  479. {
  480. Shader *dir[7];
  481. };
  482. /******************************************************************************/
  483. extern GPU_API(Shader9, Shader11, ShaderGL) *ShaderCur;
  484. extern ThreadSafeMap<FRSTKey, FRST > Frsts ; // Forward Rendering Shader Techniques
  485. extern ThreadSafeMap<BLSTKey, BLST > Blsts ; // Blend Light Shader Techniques
  486. extern ThreadSafeMap<Str8 , ShaderImage > ShaderImages ; // Shader Images
  487. extern ThreadSafeMap<Str8 , ShaderParam > ShaderParams ; // Shader Parameters
  488. extern ThreadSafeMap<Str8 , ShaderBuffer> ShaderBuffers; // Shader Constant Buffers
  489. #endif
  490. extern Cache<ShaderFile> ShaderFiles; // Shader File Cache
  491. /******************************************************************************/
  492. struct ShaderMacro // macro used for shader compilation
  493. {
  494. Str8 name ,
  495. definition;
  496. void set(C Str8 &name, C Str8 &definition) {T.name=name; T.definition=definition;}
  497. };
  498. #if EE_PRIVATE
  499. struct ShaderGLSL
  500. {
  501. Str8 group_name, tech_name;
  502. Memc<ShaderMacro> params;
  503. ShaderGLSL& set(C Str8 &group_name, C Str8 &tech_name ) {T.group_name=group_name; T.tech_name=tech_name; return T;}
  504. ShaderGLSL& par(C Str8 &name , C Str8 &definition) {params.New().set(name, definition); return T;}
  505. };
  506. #endif
  507. /******************************************************************************/
  508. // shader image
  509. ShaderImage* FindShaderImage(CChar8 *name); // find shader image, null on fail (shader image can be returned only after loading a shader which contains the image)
  510. ShaderImage* GetShaderImage(CChar8 *name); // find shader image, Exit on fail (shader image can be returned only after loading a shader which contains the image)
  511. // shader parameter
  512. ShaderParam* FindShaderParam(CChar8 *name); // find shader parameter, null on fail (shader parameter can be returned only after loading a shader which contains the parameter)
  513. ShaderParam* GetShaderParam(CChar8 *name); // find shader parameter, Exit on fail (shader parameter can be returned only after loading a shader which contains the parameter)
  514. inline void SPSet(CChar8 *name, Bool b ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(b );} // set boolean value
  515. inline void SPSet(CChar8 *name, Int i ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(i );} // set integer value
  516. inline void SPSet(CChar8 *name, Flt f ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(f );} // set float value
  517. inline void SPSet(CChar8 *name, Dbl d ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(d );} // set double value
  518. inline void SPSet(CChar8 *name, C Vec2 &v ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v );} // set vector2D value
  519. inline void SPSet(CChar8 *name, C VecD2 &v ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v );} // set vector2D value
  520. inline void SPSet(CChar8 *name, C VecI2 &v ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v );} // set vector2D value
  521. inline void SPSet(CChar8 *name, C Vec &v ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v );} // set vector3D value
  522. inline void SPSet(CChar8 *name, C VecD &v ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v );} // set vector3D value
  523. inline void SPSet(CChar8 *name, C VecI &v ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v );} // set vector3D value
  524. inline void SPSet(CChar8 *name, C Vec4 &v ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v );} // set vector4D value
  525. inline void SPSet(CChar8 *name, C VecD4 &v ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v );} // set vector4D value
  526. inline void SPSet(CChar8 *name, C VecI4 &v ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v );} // set vector4D value
  527. inline void SPSet(CChar8 *name, C Color &color ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(color );} // set vector4D value
  528. inline void SPSet(CChar8 *name, C Rect &rect ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(rect );} // set vector4D value
  529. inline void SPSet(CChar8 *name, C Matrix3 &matrix ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(matrix );} // set matrix value
  530. inline void SPSet(CChar8 *name, C Matrix &matrix ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(matrix );} // set matrix value
  531. inline void SPSet(CChar8 *name, C Matrix4 &matrix ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(matrix );} // set matrix4 value
  532. inline void SPSet(CChar8 *name, C Vec *v , Int elms) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v , elms);} // set vector3D array
  533. inline void SPSet(CChar8 *name, C Vec4 *v , Int elms) {if(ShaderParam *sp=FindShaderParam(name))sp->set(v , elms);} // set vector4D array
  534. inline void SPSet(CChar8 *name, C Matrix *matrix, Int elms) {if(ShaderParam *sp=FindShaderParam(name))sp->set(matrix, elms);} // set matrix array
  535. inline void SPSet(CChar8 *name, CPtr data , Int size) {if(ShaderParam *sp=FindShaderParam(name))sp->set(data , size);} // set memory
  536. T1(TYPE) inline void SPSet(CChar8 *name, C TYPE &data ) {if(ShaderParam *sp=FindShaderParam(name))sp->set(data );} // set memory
  537. #if EE_PRIVATE
  538. ShaderBuffer* FindShaderBuffer(CChar8 *name);
  539. ShaderBuffer* GetShaderBuffer(CChar8 *name);
  540. #if DX9
  541. void ShaderEnd();
  542. #else
  543. INLINE void ShaderEnd() {}
  544. #endif
  545. #endif
  546. // compile
  547. #if EE_PRIVATE
  548. Bool ShaderCompileTry(C Str &src, C Str &dest, SHADER_MODEL model, C MemPtr<ShaderMacro> &macros, C MemPtr<ShaderGLSL> &stg=null, Str *messages=null); // compile shader from 'src' file to 'dest' using additional 'macros', false on fail, 'messages'=optional parameter which will receive any messages that occurred during compilation
  549. void ShaderCompile (C Str &src, C Str &dest, SHADER_MODEL model, C MemPtr<ShaderMacro> &macros, C MemPtr<ShaderGLSL> &stg ); // compile shader from 'src' file to 'dest' using additional 'macros', Exit on fail
  550. #endif
  551. Bool ShaderCompileTry(Str src, Str dest, SHADER_MODEL model, C MemPtr<ShaderMacro> &macros=null, Str *messages=null); // compile shader from 'src' file to 'dest' using additional 'macros', false on fail, 'messages'=optional parameter which will receive any messages that occurred during compilation
  552. void ShaderCompile (Str src, Str dest, SHADER_MODEL model, C MemPtr<ShaderMacro> &macros=null ); // compile shader from 'src' file to 'dest' using additional 'macros', Exit on fail
  553. /******************************************************************************/