Shader Main.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. #include "../Shaders/!Header CPU.h"
  4. namespace EE{
  5. /******************************************************************************/
  6. MainShaderClass Sh;
  7. AmbientOcclusion AO;
  8. LayeredCloudsFx LC;
  9. VolumetricCloudsFx VolCloud;
  10. VolumetricLights VL;
  11. HDR Hdr;
  12. MotionBlur Mtn;
  13. DepthOfField Dof;
  14. WaterShader WS;
  15. ShaderImage::Sampler SamplerPoint, SamplerLinearWrap, SamplerLinearWCC, SamplerLinearCWC, SamplerLinearCWW, SamplerLinearClamp, SamplerFont, SamplerAnisotropic, SamplerShadowMap;
  16. /******************************************************************************/
  17. // MAIN SHADER
  18. /******************************************************************************/
  19. #if DX11
  20. void CreateAnisotropicSampler()
  21. {
  22. D3D11_SAMPLER_DESC sd; Zero(sd);
  23. if(D.texFilter()> 1)sd.Filter=D3D11_FILTER_ANISOTROPIC;else
  24. if(D.texFilter()==1)sd.Filter=(D.texMipFilter() ? D3D11_FILTER_MIN_MAG_MIP_LINEAR : D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT );else
  25. sd.Filter=(D.texMipFilter() ? D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT : D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR);
  26. sd.AddressU=D3D11_TEXTURE_ADDRESS_WRAP;
  27. sd.AddressV=D3D11_TEXTURE_ADDRESS_WRAP;
  28. sd.AddressW=D3D11_TEXTURE_ADDRESS_WRAP;
  29. sd.MipLODBias =0;
  30. sd.MaxAnisotropy =Mid(D.texFilter(), 1, 16);
  31. sd.MinLOD =Max(D.texLod (), 0 );
  32. sd.MaxLOD =FLT_MAX;
  33. sd.ComparisonFunc=D3D11_COMPARISON_NEVER;
  34. SamplerAnisotropic.create(sd);
  35. }
  36. void CreateFontSampler()
  37. {
  38. D3D11_SAMPLER_DESC sd; Zero(sd);
  39. sd.Filter =D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  40. sd.AddressU=D3D11_TEXTURE_ADDRESS_CLAMP;
  41. sd.AddressV=D3D11_TEXTURE_ADDRESS_CLAMP;
  42. sd.AddressW=D3D11_TEXTURE_ADDRESS_CLAMP;
  43. sd.MipLODBias =-D.fontSharpness();
  44. sd.MaxAnisotropy =1;
  45. sd.MinLOD =0;
  46. sd.MaxLOD =FLT_MAX;
  47. sd.ComparisonFunc=D3D11_COMPARISON_NEVER;
  48. SamplerFont.create(sd);
  49. }
  50. #endif
  51. /******************************************************************************/
  52. Shader* MainShaderClass::find(C Str8 &name) {return shader->find(name);}
  53. Shader* MainShaderClass:: get(C Str8 &name) {return shader-> get(name);}
  54. /******************************************************************************/
  55. void MainShaderClass::del()
  56. {
  57. // delete all to detect GPU memory leaks when using D3D_DEBUG
  58. shader =null;
  59. ShaderFiles .del();
  60. ShaderParams .del(); // params before buffers, because they point to them
  61. ShaderBuffers.del();
  62. ShaderImages .del();
  63. SamplerPoint .del();
  64. SamplerLinearWrap .del();
  65. SamplerLinearWCC .del();
  66. SamplerLinearCWC .del();
  67. SamplerLinearCWW .del();
  68. SamplerLinearClamp.del();
  69. SamplerFont .del();
  70. SamplerAnisotropic.del();
  71. SamplerShadowMap .del();
  72. }
  73. void MainShaderClass::createSamplers()
  74. {
  75. #if DX9
  76. REPAO(SamplerPoint.filter )=D3DTEXF_POINT;
  77. REPAO(SamplerPoint.address)=D3DTADDRESS_CLAMP;
  78. REPAO(SamplerLinearWrap.filter )=D3DTEXF_LINEAR;
  79. REPAO(SamplerLinearWrap.address)=D3DTADDRESS_WRAP;
  80. REPAO(SamplerLinearWCC.filter )=D3DTEXF_LINEAR;
  81. REPAO(SamplerLinearWCC.address)=D3DTADDRESS_CLAMP; SamplerLinearWCC.address[0]=D3DTADDRESS_WRAP;
  82. REPAO(SamplerLinearCWC.filter )=D3DTEXF_LINEAR;
  83. REPAO(SamplerLinearCWC.address)=D3DTADDRESS_CLAMP; SamplerLinearCWC.address[1]=D3DTADDRESS_WRAP;
  84. REPAO(SamplerLinearCWW.filter )=D3DTEXF_LINEAR;
  85. REPAO(SamplerLinearCWW.address)=D3DTADDRESS_WRAP; SamplerLinearCWW.address[0]=D3DTADDRESS_CLAMP;
  86. REPAO(SamplerLinearClamp.filter )=D3DTEXF_LINEAR;
  87. REPAO(SamplerLinearClamp.address)=D3DTADDRESS_CLAMP;
  88. REPAO(SamplerFont.filter )=D3DTEXF_LINEAR;
  89. REPAO(SamplerFont.address)=D3DTADDRESS_CLAMP;
  90. #elif DX11
  91. D3D11_SAMPLER_DESC sd; Zero(sd);
  92. sd.MipLODBias =0;
  93. sd.MaxAnisotropy =1;
  94. sd.MinLOD =0;
  95. sd.MaxLOD =FLT_MAX;
  96. sd.ComparisonFunc=D3D11_COMPARISON_NEVER;
  97. sd.Filter =D3D11_FILTER_MIN_MAG_MIP_POINT;
  98. sd.AddressU=D3D11_TEXTURE_ADDRESS_CLAMP;
  99. sd.AddressV=D3D11_TEXTURE_ADDRESS_CLAMP;
  100. sd.AddressW=D3D11_TEXTURE_ADDRESS_CLAMP;
  101. SamplerPoint.create(sd);
  102. sd.Filter =D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  103. sd.AddressU=D3D11_TEXTURE_ADDRESS_WRAP;
  104. sd.AddressV=D3D11_TEXTURE_ADDRESS_WRAP;
  105. sd.AddressW=D3D11_TEXTURE_ADDRESS_WRAP;
  106. SamplerLinearWrap.create(sd);
  107. sd.Filter =D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  108. sd.AddressU=D3D11_TEXTURE_ADDRESS_WRAP;
  109. sd.AddressV=D3D11_TEXTURE_ADDRESS_CLAMP;
  110. sd.AddressW=D3D11_TEXTURE_ADDRESS_CLAMP;
  111. SamplerLinearWCC.create(sd);
  112. sd.Filter =D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  113. sd.AddressU=D3D11_TEXTURE_ADDRESS_CLAMP;
  114. sd.AddressV=D3D11_TEXTURE_ADDRESS_WRAP;
  115. sd.AddressW=D3D11_TEXTURE_ADDRESS_CLAMP;
  116. SamplerLinearCWC.create(sd);
  117. sd.Filter =D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  118. sd.AddressU=D3D11_TEXTURE_ADDRESS_CLAMP;
  119. sd.AddressV=D3D11_TEXTURE_ADDRESS_WRAP;
  120. sd.AddressW=D3D11_TEXTURE_ADDRESS_WRAP;
  121. SamplerLinearCWW.create(sd);
  122. sd.Filter =D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  123. sd.AddressU=D3D11_TEXTURE_ADDRESS_CLAMP;
  124. sd.AddressV=D3D11_TEXTURE_ADDRESS_CLAMP;
  125. sd.AddressW=D3D11_TEXTURE_ADDRESS_CLAMP;
  126. SamplerLinearClamp.create(sd);
  127. sd.Filter =D3D11_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT;
  128. sd.AddressU=D3D11_TEXTURE_ADDRESS_CLAMP;
  129. sd.AddressV=D3D11_TEXTURE_ADDRESS_CLAMP;
  130. sd.AddressW=D3D11_TEXTURE_ADDRESS_CLAMP;
  131. sd.ComparisonFunc=(REVERSE_DEPTH ? D3D11_COMPARISON_GREATER_EQUAL : D3D11_COMPARISON_LESS_EQUAL);
  132. SamplerShadowMap.create(sd);
  133. CreateAnisotropicSampler();
  134. CreateFontSampler ();
  135. #elif GL
  136. REPAO(SamplerPoint.filter )=GL_NEAREST;
  137. REPAO(SamplerPoint.address)=GL_CLAMP_TO_EDGE;
  138. REPAO(SamplerLinearWrap.filter )=GL_LINEAR;
  139. REPAO(SamplerLinearWrap.address)=GL_REPEAT;
  140. REPAO(SamplerLinearWCC.filter )=GL_LINEAR;
  141. REPAO(SamplerLinearWCC.address)=GL_CLAMP_TO_EDGE; SamplerLinearWCC.address[0]=GL_REPEAT;
  142. REPAO(SamplerLinearCWC.filter )=GL_LINEAR;
  143. REPAO(SamplerLinearCWC.address)=GL_CLAMP_TO_EDGE; SamplerLinearCWC.address[1]=GL_REPEAT;
  144. REPAO(SamplerLinearCWW.filter )=GL_LINEAR;
  145. REPAO(SamplerLinearCWW.address)=GL_REPEAT; SamplerLinearCWW.address[0]=GL_CLAMP_TO_EDGE;
  146. REPAO(SamplerLinearClamp.filter )=GL_LINEAR;
  147. REPAO(SamplerLinearClamp.address)=GL_CLAMP_TO_EDGE;
  148. REPAO(SamplerFont.filter )=GL_LINEAR;
  149. REPAO(SamplerFont.address)=GL_CLAMP_TO_EDGE;
  150. #endif
  151. }
  152. void MainShaderClass::create()
  153. {
  154. if(LogInit)LogN("MainShaderClass.create");
  155. compile();
  156. if(D.shaderModel()>=SM_4)path="Shader\\4\\" ;else
  157. if(D.shaderModel()>=SM_3)path="Shader\\3\\" ;else
  158. path="Shader\\GL\\";
  159. shader=ShaderFiles("Main"); if(!shader)Exit("Can't load the Main Shader");
  160. #define MEASURE_SHADER_LOAD_TIME 0
  161. #if MEASURE_SHADER_LOAD_TIME
  162. //#define SLOW_SHADER_LOAD 0
  163. #pragma message("!! Warning: Use this only for debugging !!")
  164. Flt t=Time.curTime();
  165. #endif
  166. getTechniques();
  167. #if MEASURE_SHADER_LOAD_TIME
  168. Exit(S+"Shaders Loaded in: "+(Time.curTime()-t)+'s');
  169. #endif
  170. connectRT ();
  171. }
  172. /******************************************************************************/
  173. void MainShaderClass::clear( C Vec4 &color, C Rect *rect) {Sh.h_Color[0]->set(color); Sh.h_SetCol->draw(null , rect);}
  174. void MainShaderClass::draw (C Image &image , C Rect *rect) { Sh.h_Draw ->draw(image, rect);}
  175. void MainShaderClass::draw (C Image &image, C Vec4 &color, C Vec4 &color_add, C Rect *rect) {Sh.h_Color[0]->set(color); Sh.h_Color[1]->set(color_add); Sh.h_DrawC ->draw(image, rect);}
  176. void MainShaderClass::draw (C Image &image, C Color &color, C Color &color_add, C Rect *rect) {Sh.h_Color[0]->set(color); Sh.h_Color[1]->set(color_add); Sh.h_DrawC ->draw(image, rect);}
  177. /******************************************************************************/
  178. Shader* MainShaderClass::getBloomDS(Bool glow, Bool viewport_clamp, Bool half, Bool saturate) {return get(S8+"Bloom"+(glow?'G':'\0')+"DS"+(viewport_clamp?'C':'\0')+(half?'H':'\0')+(saturate?'S':'\0'));}
  179. Shader* MainShaderClass::getBloom (Bool dither ) {return get(S8+"Bloom"+(dither?'D':'\0'));}
  180. Shader* MainShaderClass::getShdDir (Int map_num, Bool clouds, Bool multi_sample) {return get(S8+"ShdDir"+map_num+(clouds?'C':'\0')+(multi_sample?'M':'\0'));}
  181. Shader* MainShaderClass::getShdPnt ( Bool multi_sample) {return get(S8+"ShdPnt" +(multi_sample?'M':'\0'));}
  182. Shader* MainShaderClass::getShdCone( Bool multi_sample) {return get(S8+"ShdCone" +(multi_sample?'M':'\0'));}
  183. Shader* MainShaderClass::getLightDir (Bool shadow, Bool multi_sample, Bool quality) {return get(S8+"LightDir" +(shadow?'S':'\0')+((quality && !multi_sample)?'Q':'\0') +(multi_sample?'M':'\0'));} // MSAA doesn't have quality version (to make it faster)
  184. Shader* MainShaderClass::getLightPnt (Bool shadow, Bool multi_sample, Bool quality) {return get(S8+"LightPnt" +(shadow?'S':'\0')+((quality && !multi_sample)?'Q':'\0') +(multi_sample?'M':'\0'));} // MSAA doesn't have quality version (to make it faster)
  185. Shader* MainShaderClass::getLightSqr (Bool shadow, Bool multi_sample, Bool quality) {return get(S8+"LightSqr" +(shadow?'S':'\0')+((quality && !multi_sample)?'Q':'\0') +(multi_sample?'M':'\0'));} // MSAA doesn't have quality version (to make it faster)
  186. Shader* MainShaderClass::getLightCone(Bool shadow, Bool image, Bool multi_sample, Bool quality) {return get(S8+"LightCone"+(shadow?'S':'\0')+((quality && !multi_sample)?'Q':'\0')+(image?'I':'\0')+(multi_sample?'M':'\0'));} // MSAA doesn't have quality version (to make it faster)
  187. Shader* MainShaderClass::getColLight(Int multi_sample, Bool ao, Bool cel_shade, Bool night_shade) {return get(S8+"ColLight"+multi_sample+(ao?'A':'\0')+(cel_shade?'C':'\0')+(night_shade?'N':'\0'));}
  188. Shader* MainShaderClass::getSunRaysMask(Bool mask ) {return get(S8+"SunRaysMask"+(mask?'1':'\0'));}
  189. Shader* MainShaderClass::getSunRays (Bool high, Bool jitter) {return get(S8+"SunRays" +(high?'H':'\0')+(jitter?'J':'\0'));}
  190. Shader* MainShaderClass::getSkyTF(Int textures, Bool cloud , Bool dither) {return get(S8+"SkyTF"+textures+(cloud?'C':'\0')+(dither?'D':'\0'));}
  191. Shader* MainShaderClass::getSkyT (Int textures, Int multi_sample, Bool dither) {return get(S8+"SkyT" +textures+multi_sample+(dither?'D':'\0'));}
  192. Shader* MainShaderClass::getSkyAF(Bool per_vertex, Bool stars, Bool cloud , Bool dither) {return get(S8+"SkyAF"+(per_vertex?'V':'\0')+(stars?'S':'\0')+(cloud?'C':'\0')+(dither?'D':'\0'));}
  193. Shader* MainShaderClass::getSkyA (Bool per_vertex, Bool stars, Bool density, Int multi_sample, Bool dither) {return get(S8+"SkyA" +(per_vertex?'V':'\0')+(stars?'S':'\0')+(density?'P':'\0')+multi_sample+(dither?'D':'\0'));}
  194. void MainShaderClass::initCubicShaders()
  195. {
  196. if(!h_DrawTexCubic) // check if not yet initialized because this is called multiple times for SLOW_SHADER_LOAD
  197. {
  198. h_DrawTexCubic =get("DrawTexCubic");
  199. h_DrawTexCubicC =get("DrawTexCubicC");
  200. h_DrawTexCubic1 =get("DrawTexCubic1");
  201. h_DrawTexCubicD =get("DrawTexCubicD");
  202. h_DrawTexCubicRGB =get("DrawTexCubicRGB");
  203. h_DrawTexCubicRGBD=get("DrawTexCubicRGBD");
  204. }
  205. }
  206. void MainShaderClass::initFogBoxShaders()
  207. {
  208. if(!h_FogBox) // check if not yet initialized because this is called multiple times for SLOW_SHADER_LOAD
  209. {
  210. h_FogBox =get("FogBox" );
  211. h_FogBox0=get("FogBox0" );
  212. h_FogBox1=get("FogBox1" );
  213. }
  214. }
  215. void MainShaderClass::initFogHgtShaders()
  216. {
  217. if(!h_FogHgt) // check if not yet initialized because this is called multiple times for SLOW_SHADER_LOAD
  218. {
  219. h_FogHgt =get("FogHgt" );
  220. h_FogHgt0=get("FogHgt0" );
  221. h_FogHgt1=get("FogHgt1" );
  222. }
  223. }
  224. void MainShaderClass::initFogBallShaders()
  225. {
  226. if(!h_FogBall) // check if not yet initialized because this is called multiple times for SLOW_SHADER_LOAD
  227. {
  228. h_FogBall =get("FogBall" );
  229. h_FogBall0=get("FogBall0");
  230. h_FogBall1=get("FogBall1");
  231. }
  232. }
  233. void MainShaderClass::getTechniques()
  234. {
  235. // images
  236. h_ImageCol[0] =ShaderImages("Col" );
  237. h_ImageCol[1] =ShaderImages("Col1" );
  238. h_ImageCol[2] =ShaderImages("Col2" );
  239. h_ImageCol[3] =ShaderImages("Col3" );
  240. h_ImageNrm[0] =ShaderImages("Nrm" );
  241. h_ImageNrm[1] =ShaderImages("Nrm1" );
  242. h_ImageNrm[2] =ShaderImages("Nrm2" );
  243. h_ImageNrm[3] =ShaderImages("Nrm3" );
  244. h_ImageDet[0] =ShaderImages("Det" );
  245. h_ImageDet[1] =ShaderImages("Det1" );
  246. h_ImageDet[2] =ShaderImages("Det2" );
  247. h_ImageDet[3] =ShaderImages("Det3" );
  248. h_ImageMac[0] =ShaderImages("Mac" );
  249. h_ImageMac[1] =ShaderImages("Mac1" );
  250. h_ImageMac[2] =ShaderImages("Mac2" );
  251. h_ImageMac[3] =ShaderImages("Mac3" );
  252. h_ImageRfl[0] =ShaderImages("Rfl" );
  253. h_ImageRfl[1] =ShaderImages("Rfl1" );
  254. h_ImageRfl[2] =ShaderImages("Rfl2" );
  255. h_ImageRfl[3] =ShaderImages("Rfl3" );
  256. h_ImageLum =ShaderImages("Lum" );
  257. h_ImageShdMap[0]=ShaderImages("ShdMap" ); h_ImageShdMap[0]->_sampler=&SamplerLinearClamp;
  258. h_ImageShdMap[1]=ShaderImages("ShdMap1"); h_ImageShdMap[1]->_sampler=&SamplerLinearClamp;
  259. h_ImageColMS =ShaderImages("ColMS" );
  260. h_ImageNrmMS =ShaderImages("NrmMS" );
  261. h_ImageLumMS =ShaderImages("LumMS" );
  262. h_ImageDepth =ShaderImages("Depth" );
  263. h_ImageDepthMS =ShaderImages("DepthMS");
  264. h_ImageCub =ShaderImages("Cub" );
  265. h_ImageVol[0] =ShaderImages("Vol" );
  266. h_ImageVol[1] =ShaderImages("Vol1" );
  267. h_ColSize =GetShaderParam("ColSize" );
  268. h_ColClamp =GetShaderParam("ColClamp");
  269. h_RTSizeI =GetShaderParam("RTSizeI" );
  270. h_Coords =GetShaderParam("Coords" );
  271. h_Viewport =GetShaderParam("Viewport");
  272. h_DepthWeightScale=GetShaderParam("DepthWeightScale");
  273. #if DX9
  274. h_PixelOffset=GetShaderParam("PixelOffset");
  275. #endif
  276. h_CamAngVel =GetShaderParam("CamAngVel" );
  277. h_ObjAngVel =GetShaderParam("ObjAngVel" );
  278. h_ObjVel =GetShaderParam("ObjVel" );
  279. h_ViewMatrix=GetShaderParam("ViewMatrix");
  280. h_CamMatrix =GetShaderParam("CamMatrix" );
  281. h_ProjMatrix=GetShaderParam("ProjMatrix");
  282. h_FurVel =GetShaderParam("FurVel" );
  283. h_ClipPlane =GetShaderParam("ClipPlane" );
  284. ConstCast(Renderer.highlight )=GetShaderParam("Highlight");
  285. h_Material=ConstCast(Renderer.material_color)=GetShaderParam("Material");
  286. h_MultiMaterial[0]=GetShaderParam("MultiMaterial0");
  287. h_MultiMaterial[1]=GetShaderParam("MultiMaterial1");
  288. h_MultiMaterial[2]=GetShaderParam("MultiMaterial2");
  289. h_MultiMaterial[3]=GetShaderParam("MultiMaterial3");
  290. h_Light_dir =GetShaderParam("Light_dir" );
  291. h_Light_pnt =GetShaderParam("Light_point");
  292. h_Light_sqr =GetShaderParam("Light_sqr" );
  293. h_Light_cone=GetShaderParam("Light_cone" );
  294. h_Step =GetShaderParam("Step" );
  295. h_Color[0] =GetShaderParam("Color[0]" );
  296. h_Color[1] =GetShaderParam("Color[1]" );
  297. h_BehindBias =GetShaderParam("BehindBias" );
  298. h_AllowBackFlip=GetShaderParam("AllowBackFlip");
  299. h_VtxSkinning =GetShaderParam("VtxSkinning" );
  300. h_VtxHeightmap=GetShaderParam("VtxHeightmap");
  301. #if DX9 // required
  302. h_VtxNrmMulAdd=GetShaderParam("VtxNrmMulAdd"); SetVtxNrmMulAdd(true);
  303. #endif
  304. h_LightMapScale =GetShaderParam("LightMapScale");
  305. h_LightMapColAdd =GetShaderParam("LightMapColAdd");
  306. h_LightMapSpecular=GetShaderParam("LightMapSpecular");
  307. h_GrassRangeMulAdd=GetShaderParam("GrassRangeMulAdd");
  308. h_BendFactor =GetShaderParam("BendFactor");
  309. ConstCast(Renderer.ambient_color)=GetShaderParam("AmbColor" ); Renderer.ambient_color->set(D.ambientColor ());
  310. h_AmbientMaterial=GetShaderParam("AmbMaterial" );
  311. h_AmbientContrast=GetShaderParam("AmbContrast" ); h_AmbientContrast->set(D.ambientContrast());
  312. h_AmbientRange =GetShaderParam("AmbRange" ); h_AmbientRange ->set(D.ambientRange ());
  313. h_AmbientScale =GetShaderParam("AmbScale" ); h_AmbientScale ->set(D.ambientScale ());
  314. h_AmbientBias =GetShaderParam("AmbBias" ); h_AmbientBias ->set(D.ambientBias ());
  315. h_NightShadeColor=GetShaderParam("NightShadeColor"); h_NightShadeColor->set(D.nightShadeColor());
  316. h_HdrBrightness=GetShaderParam("HdrBrightness"); h_HdrBrightness->set(D.eyeAdaptationBrightness());
  317. h_HdrMaxDark =GetShaderParam("HdrMaxDark" ); h_HdrMaxDark ->set(D.eyeAdaptationMaxDark ());
  318. h_HdrMaxBright =GetShaderParam("HdrMaxBright" ); h_HdrMaxBright ->set(D.eyeAdaptationMaxBright ());
  319. h_HdrWeight =GetShaderParam("HdrWeight" ); h_HdrWeight ->set(D.eyeAdaptationWeight()/4 );
  320. h_TesselationDensity=GetShaderParam("TesselationDensity"); h_TesselationDensity->set(D.tesselationDensity());
  321. // TECHNIQUES
  322. h_Draw2DFlat =get("Draw2DFlat");
  323. h_Draw3DFlat =get("Draw3DFlat");
  324. h_Draw2DCol =get("Draw2DCol");
  325. h_Draw3DCol =get("Draw3DCol");
  326. h_Draw2DTex =get("Draw2DTex");
  327. h_Draw2DTexC =get("Draw2DTexC");
  328. h_Draw2DTexCol=get("Draw2DTexCol");
  329. REPD(at, 2)
  330. REPD(f , 2)
  331. {
  332. h_Draw3DTex [at][f]=get(S8+"Draw3DTex" +(at?"AT":"")+(f?'F':'\0'));
  333. h_Draw3DTexCol[at][f]=get(S8+"Draw3DTexCol"+(at?"AT":"")+(f?'F':'\0'));
  334. }
  335. h_PaletteDraw=get("PaletteDraw");
  336. h_Simple =get("Simple");
  337. h_DrawTexX =get("DrawTexX");
  338. //h_DrawTexZ =get("DrawTexZ"); used by Editor
  339. h_DrawTexW =get("DrawTexW");
  340. h_DrawTexXC =get("DrawTexXC");
  341. h_DrawTexWC =get("DrawTexWC");
  342. h_DrawTexXCD =get("DrawTexXCD");
  343. h_DrawTexWCD =get("DrawTexWCD");
  344. //h_DrawTexNrm =get("DrawTexNrm"); used by Editor
  345. h_DrawMask =get("DrawMask");
  346. // POINT (these can be null if failed to load)
  347. h_DrawTexPoint =find("DrawTexPoint");
  348. h_DrawTexPointC=find("DrawTexPointC");
  349. // CUBIC (these can be null if failed to load)
  350. h_DrawTexCubicFast =find("DrawTexCubicFast");
  351. h_DrawTexCubicFastC =find("DrawTexCubicFastC");
  352. h_DrawTexCubicFast1 =find("DrawTexCubicFast1");
  353. h_DrawTexCubicFastD =find("DrawTexCubicFastD");
  354. h_DrawTexCubicFastRGB =find("DrawTexCubicFastRGB");
  355. h_DrawTexCubicFastRGBD=find("DrawTexCubicFastRGBD");
  356. #if !SLOW_SHADER_LOAD
  357. initCubicShaders();
  358. #endif
  359. // FONT
  360. h_FontShadow =GetShaderParam("FontShadow" );
  361. h_FontContrast=GetShaderParam("FontContrast");
  362. h_FontShade =GetShaderParam("FontShade" );
  363. h_FontDepth =GetShaderParam("FontDepth" );
  364. h_Font =get("Font");
  365. h_FontD=get("FontD"); // font with custom depth
  366. // BASIC 2D
  367. h_Dither=get("Dither");
  368. h_SetCol=get("SetCol");
  369. h_Draw =get("Draw" );
  370. h_DrawC =get("DrawC" );
  371. h_DrawA =get("DrawA" );
  372. if(D.shaderModel()>=SM_4)
  373. {
  374. h_DrawMs1=get("DrawMs1");
  375. h_DrawMsN=get("DrawMsN");
  376. if(D.shaderModel()>=SM_4_1)h_DrawMsM=get("DrawMsM");
  377. }
  378. // BLOOM
  379. h_BloomParams=GetShaderParam("BloomParams");
  380. #if !SLOW_SHADER_LOAD
  381. REPD(g, 2)
  382. REPD(c, 2)
  383. REPD(h, 2)
  384. REPD(s, 2)h_BloomDS[g][c][h][s]=getBloomDS(g, c, h, s);
  385. REPD(d, 2)h_Bloom [d] =getBloom (d);
  386. #endif
  387. // BLUR
  388. REPD(h, 2)
  389. {
  390. h_BlurX[h]=get(S8+"BlurX"+(h?'H':'\0'));
  391. h_BlurY[h]=get(S8+"BlurY"+(h?'H':'\0'));
  392. }
  393. // DEPTH
  394. #if DX9
  395. REPD(p, 2)h_LinearizeDepthRAWZ[p]=get(S8+"LinearizeDepthRAWZ"+(p?'P':'\0'));
  396. #endif
  397. REPD(m, (D.shaderModel()>=SM_4_1) ? 3 : (D.shaderModel()>=SM_4) ? 2 : 1)
  398. REPD(p, 2)h_LinearizeDepth[p][m]=get(S8+"LinearizeDepth"+(p?'P':'\0')+m);
  399. #if GL_ES
  400. h_SetDepth=find("SetDepth"); // GL ES 2.0 may fail because of depth writing
  401. #else
  402. h_SetDepth=get ("SetDepth");
  403. #endif
  404. if(D.shaderModel()>=SM_4)
  405. {
  406. h_ResolveDepth=get("ResolveDepth");
  407. h_DetectMSCol =get("DetectMSCol");
  408. //h_DetectMSNrm =get("DetectMSNrm");
  409. }
  410. #if !SLOW_SHADER_LOAD
  411. h_Combine =get("Combine");
  412. if(D.shaderModel()>=SM_4)h_CombineMS =get("CombineMS");
  413. h_CombineSS =get("CombineSS");
  414. h_CombineSSAlpha=get("CombineSSAlpha");
  415. #endif
  416. // SKY
  417. h_Sun =GetShaderParam("Sun" );
  418. h_SkyDnsExp =GetShaderParam("SkyDnsExp" );
  419. h_SkyHorExp =GetShaderParam("SkyHorExp" );
  420. h_SkyBoxBlend =GetShaderParam("SkyBoxBlend" );
  421. h_SkyHorCol =GetShaderParam("SkyHorCol" );
  422. h_SkySkyCol =GetShaderParam("SkySkyCol" );
  423. h_SkyStarOrn =GetShaderParam("SkyStarOrn" );
  424. h_SkyFracMulAdd =GetShaderParam("SkyFracMulAdd" );
  425. h_SkyDnsMulAdd =GetShaderParam("SkyDnsMulAdd" );
  426. h_SkySunHighlight=GetShaderParam("SkySunHighlight");
  427. h_SkySunPos =GetShaderParam("SkySunPos" );
  428. #if !SLOW_SHADER_LOAD
  429. REPD(dither, 2)
  430. {
  431. REPD(t, 2)
  432. REPD(c, 2)h_SkyTF[t][c][dither]=getSkyTF(t+1, c, dither);
  433. REPD(v, 2)
  434. REPD(s, 2)
  435. REPD(c, 2)h_SkyAF[v][s][c][dither]=getSkyAF(v, s, c, dither);
  436. REPD(m, (D.shaderModel()>=SM_4_1) ? 3 : (D.shaderModel()>=SM_4) ? 2 : 1)
  437. {
  438. REPD(t, 2)h_SkyT[t][m][dither]=getSkyT(t+1, m, dither);
  439. REPD(v, 2)
  440. REPD(s, 2)
  441. REPD(d, 2)h_SkyA[v][s][d][m][dither]=getSkyA(v, s, d, m, dither);
  442. }
  443. }
  444. REPD(m, 2)h_SunRaysMask[m]=getSunRaysMask(m);
  445. REPD(h, 2)
  446. REPD(j, 2)h_SunRays[h][j]=getSunRays(h, j);
  447. //h_SunRaysSoft=get("SunRaysSoft");
  448. #endif
  449. // SHADOWS
  450. REPAO(h_ShdStep )=GetShaderParam(S8+"ShdStep["+i+']');
  451. h_ShdJitter =GetShaderParam("ShdJitter");
  452. h_ShdRange =GetShaderParam("ShdRange");
  453. h_ShdRangeMulAdd=GetShaderParam("ShdRangeMulAdd");
  454. h_ShdOpacity =GetShaderParam("ShdOpacity");
  455. h_ShdMatrix =GetShaderParam("ShdMatrix");
  456. REPAO(h_ShdMatrix4 )=GetShaderParam(S8+"ShdMatrix4["+i+']');
  457. // can be used for shadows in deferred and AO
  458. h_ShdBlur[0]=get("ShdBlur4");
  459. h_ShdBlur[1]=get("ShdBlur6");
  460. h_ShdBlur[2]=get("ShdBlur8");
  461. h_ShdBlur[3]=get("ShdBlur12");
  462. h_ShdBlurX =get("ShdBlurX2");
  463. h_ShdBlurY =get("ShdBlurY2");
  464. #if !SLOW_SHADER_LOAD
  465. if(!D.deferredUnavailable())
  466. {
  467. // SHADOW
  468. REPD(m, (D.shaderModel()>=SM_4_1) ? 2 : 1)
  469. {
  470. REPD(n, 6)
  471. REPD(c, 2)h_ShdDir[n][c][m]=getShdDir (n+1, c, m);
  472. h_ShdPnt [m]=getShdPnt ( m);
  473. h_ShdCone [m]=getShdCone( m);
  474. }
  475. // LIGHT
  476. REPD(m, (D.shaderModel()>=SM_4_1) ? 2 : 1) // multisample
  477. REPD(s, 2) // shadow
  478. REPD(q, 2) // quality unpack
  479. {
  480. h_LightDir [s] [m][q]=getLightDir (s, m, q);
  481. h_LightPnt [s] [m][q]=getLightPnt (s, m, q);
  482. h_LightSqr [s] [m][q]=getLightSqr (s, m, q);
  483. REPD(i, 2)h_LightCone[s][i][m][q]=getLightCone(s, i, m, q);
  484. }
  485. // COL LIGHT
  486. REPD(m, (D.shaderModel()>=SM_4_1) ? 3 : 1)
  487. REPD(a, 2)
  488. REPD(c, 2)
  489. REPD(n, 2)h_ColLight[m][a][c][n]=getColLight(m, a, c, n);
  490. }
  491. #endif
  492. // PARTICLE
  493. h_ParticleFrames=GetShaderParam("ParticleFrames");
  494. h_Bilb =get("Bilb");
  495. REPD(p, 2)
  496. REPD(s, 2)
  497. REPD(a, 3)
  498. REPD(m, 2)
  499. h_Particle[p][s][a][m]=get(S8+"ParticleTex"+(p?'P':'\0')+(s?'S':'\0')+((a==0) ? "" : (a==1) ? "A" : "AA")+(m?'M':'\0'));
  500. // FOG
  501. h_VertexFogMulAdd=GetShaderParam("VertexFogMulAdd");
  502. h_VertexFogColor =GetShaderParam("VertexFogColor");
  503. h_FogColor_Density =GetShaderParam( "FogColor_Density");
  504. h_LocalFogColor_Density=GetShaderParam("LocalFogColor_Density");
  505. h_LocalFogInside =GetShaderParam("LocalFogInside" );
  506. h_Fog[0]=get("Fog");
  507. if(D.shaderModel()>=SM_4)
  508. {
  509. h_Fog[1]=get("FogN");
  510. if(D.shaderModel()>=SM_4_1)h_Fog[2]=get("FogM");
  511. }
  512. #if !SLOW_SHADER_LOAD
  513. initFogBoxShaders ();
  514. initFogHgtShaders ();
  515. initFogBallShaders();
  516. #endif
  517. // DECAL
  518. h_DecalParams =GetShaderParam( "DecalParams");
  519. h_OverlayParams=GetShaderParam("OverlayParams");
  520. // OTHER
  521. h_Laser[0]=get("Laser");
  522. h_Laser[1]=get("LaserN");
  523. }
  524. void MainShaderClass::connectRT()
  525. {
  526. if(h_ImageShdMap[0])h_ImageShdMap[0]->set(Renderer._shd_map);
  527. if(h_ImageShdMap[1])h_ImageShdMap[1]->set(Renderer._cld_map);
  528. }
  529. /******************************************************************************/
  530. // EFFECTS
  531. /******************************************************************************/
  532. void VolumetricCloudsFx::load()
  533. {
  534. if(!shader)if(shader=ShaderFiles("Volumetric Clouds"))
  535. {
  536. h_Cloud =GetShaderParam("Cloud");
  537. h_CloudMap =GetShaderParam("CloudMap");
  538. h_Clouds =shader->get("Clouds");
  539. h_CloudsDraw=shader->get("CloudsDraw");
  540. h_CloudsMap =shader->get("CloudsMap");
  541. }
  542. }
  543. /******************************************************************************/
  544. void VolumetricLights::load()
  545. {
  546. if(!shader)if(shader=ShaderFiles("Volumetric Lights"))
  547. {
  548. h_Light_point_range=GetShaderParam("Light_point_range");
  549. REPD(n, 6)
  550. REPD(c, 2)h_VolDir[n][c]=shader->get(S8+"VolDir"+(n+1)+(c?'C':'\0'));
  551. h_VolPnt =shader->get( "VolPnt" );
  552. h_VolSqr =shader->get( "VolSqr" );
  553. h_VolCone =shader->get( "VolCone");
  554. h_Volumetric =shader->get("Volumetric" );
  555. h_VolumetricA=shader->get("VolumetricA");
  556. }
  557. }
  558. /******************************************************************************/
  559. void HDR::load()
  560. {
  561. if(!shader)if(shader=ShaderFiles("Hdr"))
  562. {
  563. REPD(s, 2)h_HdrDS[s] =shader->get(S8+"HdrDS"+s );
  564. h_HdrUpdate=shader->get( "HdrUpdate");
  565. h_Hdr =shader->get( "Hdr" );
  566. }
  567. }
  568. /******************************************************************************/
  569. void MotionBlur::load()
  570. {
  571. if(!shader)if(shader=ShaderFiles("Motion Blur"))
  572. {
  573. h_MotionUVMulAdd =GetShaderParam("MotionUVMulAdd");
  574. h_MotionVelScaleLimit=GetShaderParam("MotionVelScaleLimit");
  575. h_MotionPixelSize =GetShaderParam("MotionPixelSize");
  576. h_Explosion=shader->get("Explosion");
  577. h_ClearSkyVel=shader->get("ClearSkyVel");
  578. REPD(h, 2)
  579. REPD(c, 2)h_Convert[h][c]=shader->get(S8+"Convert"+(h?'H':'\0')+(c?'C':'\0'));
  580. h_Dilate=shader->get("Dilate");
  581. REPD(c, 2)h_SetDirs[c]=shader->get(S8+"SetDirs"+(c?'C':'\0'));
  582. REPD(d, 2)h_Blur[d]=shader->get(S8+"Blur"+(d?'D':'\0'));
  583. pixels[0].pixels=1;
  584. pixels[1].pixels=2;
  585. pixels[2].pixels=4;
  586. pixels[3].pixels=6;
  587. pixels[4].pixels=8;
  588. pixels[5].pixels=12;
  589. pixels[6].pixels=16;
  590. pixels[7].pixels=24;
  591. pixels[8].pixels=32;
  592. ASSERT(ELMS(pixels)==9);
  593. }
  594. }
  595. C MotionBlur::Pixel* MotionBlur::pixel(Int pixel, Bool diagonal)
  596. {
  597. if(pixel<=0)return null;
  598. Pixel *p;
  599. FREPA(pixels) // start from the smallest to find exact match or bigger, order is important
  600. {
  601. p=&pixels[i]; if(p->pixels>=pixel)break; // if this covers desired range of pixels to blur
  602. }
  603. if(!p->h_DilateX[diagonal])
  604. {
  605. p->h_DilateX[diagonal]=shader->get(S8+"DilateX"+(diagonal?'D':'\0')+p->pixels);
  606. p->h_DilateY[diagonal]=shader->get(S8+"DilateY"+(diagonal?'D':'\0')+p->pixels);
  607. }
  608. return p;
  609. }
  610. /******************************************************************************/
  611. Shader* DepthOfField::getDS(Bool clamp , Bool realistic, Bool half) {return shader->get(S8+"DofDS"+(clamp?'C':'\0')+(realistic?'R':'\0')+(half?'H':'\0')+(D.shaderModel()>=SM_4_1 ? 'G' : '\0'));}
  612. Shader* DepthOfField::get (Bool dither, Bool realistic ) {return shader->get(S8+"Dof"+(dither?'D':'\0')+(realistic?'R':'\0'));}
  613. void DepthOfField::load()
  614. {
  615. if(!shader)if(shader=ShaderFiles("Depth of Field"))
  616. {
  617. h_DofParams=GetShaderParam("DofParams");
  618. #if !SLOW_SHADER_LOAD
  619. REPD(c, 2)
  620. REPD(r, 2)
  621. REPD(h, 2)h_DofDS[c][r][h]=getDS(c, r, h);
  622. REPD(d, 2)
  623. REPD(r, 2)h_Dof[d][r]=get(d, r);
  624. #endif
  625. pixels[ 0].pixels=2;
  626. pixels[ 1].pixels=3;
  627. pixels[ 2].pixels=4;
  628. pixels[ 3].pixels=5;
  629. pixels[ 4].pixels=6;
  630. pixels[ 5].pixels=7;
  631. pixels[ 6].pixels=8;
  632. pixels[ 7].pixels=9;
  633. pixels[ 8].pixels=10;
  634. pixels[ 9].pixels=11;
  635. pixels[10].pixels=12;
  636. ASSERT(ELMS(pixels)==11);
  637. }
  638. }
  639. C DepthOfField::Pixel& DepthOfField::pixel(Int pixel)
  640. {
  641. Pixel *p;
  642. FREPA(pixels) // start from the smallest to find exact match or bigger, order is important
  643. {
  644. p=&pixels[i]; if(p->pixels>=pixel)break; // if this covers desired range of pixels to blur
  645. }
  646. if(!p->h_BlurX)
  647. {
  648. p->h_BlurX=shader->get(S8+"DofBlurX"+p->pixels);
  649. p->h_BlurY=shader->get(S8+"DofBlurY"+p->pixels);
  650. }
  651. return *p;
  652. }
  653. /******************************************************************************/
  654. void WaterShader::load()
  655. {
  656. if(!shader)if(shader=ShaderFiles("Water"))
  657. {
  658. REPD(fake_reflect, 2) // fake reflection
  659. {
  660. h_Lake [fake_reflect]=shader->get(S8+"Lake" +(fake_reflect?'F':'\0'));
  661. h_River[fake_reflect]=shader->get(S8+"River"+(fake_reflect?'F':'\0'));
  662. h_Ocean[fake_reflect]=shader->get(S8+"Ocean"+(fake_reflect?'F':'\0'));
  663. REPD(shadow, 7)
  664. REPD(soft , 2)
  665. {
  666. h_LakeL [fake_reflect][shadow][soft]=shader->get(S8+"LakeL" +shadow+(soft?'S':'\0')+(fake_reflect?'F':'\0'));
  667. h_RiverL[fake_reflect][shadow][soft]=shader->get(S8+"RiverL"+shadow+(soft?'S':'\0')+(fake_reflect?'F':'\0'));
  668. h_OceanL[fake_reflect][shadow][soft]=shader->get(S8+"OceanL"+shadow+(soft?'S':'\0')+(fake_reflect?'F':'\0'));
  669. }
  670. }
  671. REPD(r, 2)
  672. REPD(d, DX11 ? 2 : 1)h_Apply[r][d]=shader->get(S8+"Apply"+(r?'R':'\0')+(d?'D':'\0'));
  673. REPD(r, 2)h_Under[r]=shader->get(S8+"Under"+(r?'R':'\0'));
  674. }
  675. }
  676. /******************************************************************************/
  677. }
  678. /******************************************************************************/