gfxD3D9Shader.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #if defined(TORQUE_OS_XENON)
  24. # include <xtl.h>
  25. #else
  26. # include <d3d9.h>
  27. #endif
  28. #include "gfx/D3D9/gfxD3D9Shader.h"
  29. #include "gfx/D3D9/gfxD3D9Device.h"
  30. #include "core/frameAllocator.h"
  31. #include "core/stream/fileStream.h"
  32. #include "core/util/safeDelete.h"
  33. #include "console/console.h"
  34. using namespace Torque;
  35. extern bool gDisassembleAllShaders;
  36. /// D3DXInclude plugin
  37. class _gfxD3DXInclude : public ID3DXInclude, public StrongRefBase
  38. {
  39. private:
  40. Vector<String> mLastPath;
  41. public:
  42. void setPath( const String &path )
  43. {
  44. mLastPath.clear();
  45. mLastPath.push_back( path );
  46. }
  47. _gfxD3DXInclude() {}
  48. virtual ~_gfxD3DXInclude() {}
  49. STDMETHOD(Close)(THIS_ LPCVOID pData);
  50. // 360
  51. STDMETHOD(Open)(THIS_ D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes, /* OUT */ LPSTR pFullPath, DWORD cbFullPath);
  52. // PC
  53. STDMETHOD(Open)(THIS_ D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes)
  54. {
  55. return Open( IncludeType, pFileName, pParentData, ppData, pBytes, NULL, 0 );
  56. }
  57. };
  58. _gfxD3DXIncludeRef GFXD3D9Shader::smD3DXInclude = NULL;
  59. HRESULT _gfxD3DXInclude::Open(THIS_ D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName,
  60. LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes,
  61. LPSTR pFullPath, DWORD cbFullPath)
  62. {
  63. // First try making the path relative to the parent.
  64. Torque::Path path = Torque::Path::Join( mLastPath.last(), '/', pFileName );
  65. path = Torque::Path::CompressPath( path );
  66. if ( !Torque::FS::ReadFile( path, (void *&)*ppData, *pBytes, true ) )
  67. {
  68. // Ok... now try using the path as is.
  69. path = String( pFileName );
  70. path = Torque::Path::CompressPath( path );
  71. if ( !Torque::FS::ReadFile( path, (void *&)*ppData, *pBytes, true ) )
  72. {
  73. AssertISV(false, avar( "Failed to open include '%s'.", pFileName));
  74. return E_FAIL;
  75. }
  76. }
  77. // If the data was of zero size then we cannot recurse
  78. // into this file and DX won't call Close() below.
  79. //
  80. // So in this case don't push on the path.
  81. if ( *pBytes > 0 )
  82. mLastPath.push_back( path.getRootAndPath() );
  83. return S_OK;
  84. }
  85. HRESULT _gfxD3DXInclude::Close( THIS_ LPCVOID pData )
  86. {
  87. // Free the data file and pop its path off the stack.
  88. delete [] (U8*)pData;
  89. mLastPath.pop_back();
  90. return S_OK;
  91. }
  92. GFXD3D9ShaderConstHandle::GFXD3D9ShaderConstHandle()
  93. {
  94. clear();
  95. }
  96. const String& GFXD3D9ShaderConstHandle::getName() const
  97. {
  98. if ( mVertexConstant )
  99. return mVertexHandle.name;
  100. else
  101. return mPixelHandle.name;
  102. }
  103. GFXShaderConstType GFXD3D9ShaderConstHandle::getType() const
  104. {
  105. if ( mVertexConstant )
  106. return mVertexHandle.constType;
  107. else
  108. return mPixelHandle.constType;
  109. }
  110. U32 GFXD3D9ShaderConstHandle::getArraySize() const
  111. {
  112. if ( mVertexConstant )
  113. return mVertexHandle.arraySize;
  114. else
  115. return mPixelHandle.arraySize;
  116. }
  117. S32 GFXD3D9ShaderConstHandle::getSamplerRegister() const
  118. {
  119. if ( !mValid || !isSampler() )
  120. return -1;
  121. // We always store sampler type and register index in the pixelHandle,
  122. // sampler registers are shared between vertex and pixel shaders anyway.
  123. return mPixelHandle.offset;
  124. }
  125. //------------------------------------------------------------------------------
  126. //------------------------------------------------------------------------------
  127. bool GFXD3D9ShaderBufferLayout::setMatrix(const ParamDesc& pd, const GFXShaderConstType constType, const U32 size, const void* data, U8* basePointer)
  128. {
  129. PROFILE_SCOPE(GFXD3D9ShaderBufferLayout_setMatrix);
  130. if (pd.constType == GFXSCT_Float4x4)
  131. {
  132. // Special case, we can just blast this guy.
  133. AssertFatal(pd.size >= size, "Not enough room in the buffer for this data!");
  134. if (dMemcmp(basePointer+pd.offset, data, size) != 0)
  135. {
  136. dMemcpy(basePointer+pd.offset, data, size);
  137. return true;
  138. }
  139. return false;
  140. }
  141. else
  142. {
  143. PROFILE_SCOPE(GFXD3D9ShaderBufferLayout_setMatrix_not4x4);
  144. // Figure out how big of a chunk we are copying. We're going to copy 4 columns by N rows of data
  145. U32 csize;
  146. switch (pd.constType)
  147. {
  148. case GFXSCT_Float2x2 :
  149. csize = 32;
  150. break;
  151. case GFXSCT_Float3x3 :
  152. csize = 48;
  153. break;
  154. default:
  155. AssertFatal(false, "Unhandled case!");
  156. return false;
  157. break;
  158. }
  159. // Loop through and copy
  160. bool ret = false;
  161. U8* currDestPointer = basePointer+pd.offset;
  162. const U8* currSourcePointer = static_cast<const U8*>(data);
  163. const U8* endData = currSourcePointer + size;
  164. while (currSourcePointer < endData)
  165. {
  166. if (dMemcmp(currDestPointer, currSourcePointer, csize) != 0)
  167. {
  168. dMemcpy(currDestPointer, currSourcePointer, csize);
  169. ret = true;
  170. }
  171. currDestPointer += csize;
  172. currSourcePointer += sizeof(MatrixF);
  173. }
  174. return ret;
  175. }
  176. }
  177. //------------------------------------------------------------------------------
  178. GFXD3D9ShaderConstBuffer::GFXD3D9ShaderConstBuffer( GFXD3D9Shader* shader,
  179. GFXD3D9ShaderBufferLayout* vertexLayoutF,
  180. GFXD3D9ShaderBufferLayout* vertexLayoutI,
  181. GFXD3D9ShaderBufferLayout* pixelLayoutF,
  182. GFXD3D9ShaderBufferLayout* pixelLayoutI )
  183. {
  184. AssertFatal( shader, "GFXD3D9ShaderConstBuffer() - Got a null shader!" );
  185. // We hold on to this so we don't have to call
  186. // this virtual method during activation.
  187. mDevice = static_cast<GFXD3D9Device*>( GFX )->getDevice();
  188. mShader = shader;
  189. // TODO: Remove buffers and layouts that don't exist for performance?
  190. mVertexConstBufferLayoutF = vertexLayoutF;
  191. mVertexConstBufferF = new GenericConstBuffer(vertexLayoutF);
  192. mVertexConstBufferLayoutI = vertexLayoutI;
  193. mVertexConstBufferI = new GenericConstBuffer(vertexLayoutI);
  194. mPixelConstBufferLayoutF = pixelLayoutF;
  195. mPixelConstBufferF = new GenericConstBuffer(pixelLayoutF);
  196. mPixelConstBufferLayoutI = pixelLayoutI;
  197. mPixelConstBufferI = new GenericConstBuffer(pixelLayoutI);
  198. }
  199. GFXD3D9ShaderConstBuffer::~GFXD3D9ShaderConstBuffer()
  200. {
  201. SAFE_DELETE(mVertexConstBufferF);
  202. SAFE_DELETE(mPixelConstBufferF);
  203. SAFE_DELETE(mVertexConstBufferI);
  204. SAFE_DELETE(mPixelConstBufferI);
  205. if ( mShader )
  206. mShader->_unlinkBuffer( this );
  207. }
  208. GFXShader* GFXD3D9ShaderConstBuffer::getShader()
  209. {
  210. return mShader;
  211. }
  212. // This is kind of cheesy, but I don't think templates would work well here because
  213. // these functions potentially need to be handled differently by other derived types
  214. template<class T>
  215. inline void GFXD3D9ShaderConstBuffer::SET_CONSTANT( GFXShaderConstHandle* handle, const T& fv, GenericConstBuffer *vBuffer, GenericConstBuffer *pBuffer )
  216. {
  217. AssertFatal(dynamic_cast<const GFXD3D9ShaderConstHandle*>(handle), "Incorrect const buffer type!");
  218. const GFXD3D9ShaderConstHandle* h = static_cast<const GFXD3D9ShaderConstHandle*>(handle);
  219. AssertFatal(h, "Handle is NULL!" );
  220. AssertFatal(h->isValid(), "Handle is not valid!" );
  221. AssertFatal(!h->isSampler(), "Handle is sampler constant!" );
  222. AssertFatal(!mShader.isNull(), "Buffer's shader is null!" );
  223. AssertFatal(!h->mShader.isNull(), "Handle's shader is null!" );
  224. AssertFatal(h->mShader.getPointer() == mShader.getPointer(), "Mismatched shaders!");
  225. if ( h->mInstancingConstant )
  226. {
  227. dMemcpy( mInstPtr+h->mPixelHandle.offset, &fv, sizeof( fv ) );
  228. return;
  229. }
  230. if (h->mVertexConstant)
  231. vBuffer->set(h->mVertexHandle, fv);
  232. if (h->mPixelConstant)
  233. pBuffer->set(h->mPixelHandle, fv);
  234. }
  235. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const F32 fv)
  236. {
  237. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  238. }
  239. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point2F& fv)
  240. {
  241. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  242. }
  243. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point3F& fv)
  244. {
  245. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  246. }
  247. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point4F& fv)
  248. {
  249. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  250. }
  251. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const PlaneF& fv)
  252. {
  253. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  254. }
  255. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const ColorF& fv)
  256. {
  257. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  258. }
  259. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const S32 f)
  260. {
  261. // This is the only type that is allowed to be used
  262. // with a sampler shader constant type, but it is only
  263. // allowed to be set from GLSL.
  264. //
  265. // So we ignore it here... all other cases will assert.
  266. //
  267. if ( ((GFXD3D9ShaderConstHandle*)handle)->isSampler() )
  268. return;
  269. SET_CONSTANT(handle, f, mVertexConstBufferI, mPixelConstBufferI);
  270. }
  271. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point2I& fv)
  272. {
  273. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  274. }
  275. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point3I& fv)
  276. {
  277. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  278. }
  279. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point4I& fv)
  280. {
  281. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  282. }
  283. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<F32>& fv)
  284. {
  285. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  286. }
  287. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point2F>& fv)
  288. {
  289. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  290. }
  291. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point3F>& fv)
  292. {
  293. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  294. }
  295. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point4F>& fv)
  296. {
  297. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  298. }
  299. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<S32>& fv)
  300. {
  301. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  302. }
  303. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point2I>& fv)
  304. {
  305. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  306. }
  307. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point3I>& fv)
  308. {
  309. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  310. }
  311. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point4I>& fv)
  312. {
  313. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  314. }
  315. #undef SET_CONSTANT
  316. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const MatrixF& mat, const GFXShaderConstType matrixType)
  317. {
  318. AssertFatal(handle, "Handle is NULL!" );
  319. AssertFatal(handle->isValid(), "Handle is not valid!" );
  320. AssertFatal(dynamic_cast<const GFXD3D9ShaderConstHandle*>(handle), "Incorrect const buffer type!");
  321. const GFXD3D9ShaderConstHandle* h = static_cast<const GFXD3D9ShaderConstHandle*>(handle);
  322. AssertFatal(!h->isSampler(), "Handle is sampler constant!" );
  323. AssertFatal(h->mShader == mShader, "Mismatched shaders!");
  324. MatrixF transposed;
  325. mat.transposeTo(transposed);
  326. if (h->mInstancingConstant)
  327. {
  328. if ( matrixType == GFXSCT_Float4x4 )
  329. dMemcpy( mInstPtr+h->mPixelHandle.offset, mat, sizeof( mat ) );
  330. // TODO: Support 3x3 and 2x2 matricies?
  331. return;
  332. }
  333. if (h->mVertexConstant)
  334. mVertexConstBufferF->set(h->mVertexHandle, transposed, matrixType);
  335. if (h->mPixelConstant)
  336. mPixelConstBufferF->set(h->mPixelHandle, transposed, matrixType);
  337. }
  338. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType)
  339. {
  340. AssertFatal(handle, "Handle is NULL!" );
  341. AssertFatal(handle->isValid(), "Handle is not valid!" );
  342. AssertFatal(dynamic_cast<const GFXD3D9ShaderConstHandle*>(handle), "Incorrect const buffer type!");
  343. const GFXD3D9ShaderConstHandle* h = static_cast<const GFXD3D9ShaderConstHandle*>(handle);
  344. AssertFatal(!h->isSampler(), "Handle is sampler constant!" );
  345. AssertFatal(h->mShader == mShader, "Mismatched shaders!");
  346. static Vector<MatrixF> transposed;
  347. if (arraySize > transposed.size())
  348. transposed.setSize(arraySize);
  349. for (U32 i = 0; i < arraySize; i++)
  350. mat[i].transposeTo(transposed[i]);
  351. // TODO: Maybe support this in the future?
  352. if (h->mInstancingConstant)
  353. return;
  354. if (h->mVertexConstant)
  355. mVertexConstBufferF->set(h->mVertexHandle, transposed.begin(), arraySize, matrixType);
  356. if (h->mPixelConstant)
  357. mPixelConstBufferF->set(h->mPixelHandle, transposed.begin(), arraySize, matrixType);
  358. }
  359. const String GFXD3D9ShaderConstBuffer::describeSelf() const
  360. {
  361. String ret;
  362. ret = String(" GFXD3D9ShaderConstBuffer\n");
  363. for (U32 i = 0; i < mVertexConstBufferLayoutF->getParameterCount(); i++)
  364. {
  365. GenericConstBufferLayout::ParamDesc pd;
  366. mVertexConstBufferLayoutF->getDesc(i, pd);
  367. ret += String::ToString(" Constant name: %s", pd.name.c_str());
  368. }
  369. return ret;
  370. }
  371. void GFXD3D9ShaderConstBuffer::zombify()
  372. {
  373. }
  374. void GFXD3D9ShaderConstBuffer::resurrect()
  375. {
  376. }
  377. bool GFXD3D9ShaderConstBuffer::isDirty()
  378. {
  379. bool ret = mVertexConstBufferF->isDirty();
  380. ret |= mVertexConstBufferI->isDirty();
  381. ret |= mPixelConstBufferF->isDirty();
  382. ret |= mPixelConstBufferI->isDirty();
  383. return ret;
  384. }
  385. void GFXD3D9ShaderConstBuffer::activate( GFXD3D9ShaderConstBuffer *prevShaderBuffer )
  386. {
  387. PROFILE_SCOPE(GFXD3D9ShaderConstBuffer_activate);
  388. // NOTE: This is a really critical function as it gets
  389. // called between every draw call to update the constants.
  390. //
  391. // Alot of the calls here are inlined... be careful
  392. // what you change.
  393. // If the buffer has changed we need to compare it
  394. // with the new buffer to see if we can skip copying
  395. // equal buffer content.
  396. //
  397. // If the buffer hasn't changed then we only will
  398. // be copying the changes that have occured since
  399. // the last activate call.
  400. //
  401. if ( prevShaderBuffer != this )
  402. {
  403. // If the previous buffer is dirty, than we can't compare
  404. // against it, because it hasn't sent its contents to the
  405. // card yet and must be copied.
  406. if ( prevShaderBuffer && !prevShaderBuffer->isDirty() )
  407. {
  408. PROFILE_SCOPE(GFXD3D9ShaderConstBuffer_activate_dirty_check_1);
  409. // If the buffer content is equal then we set the dirty
  410. // flag to false knowing the current state of the card matches
  411. // the new buffer.
  412. //
  413. // If the content is not equal we set the dirty flag to
  414. // true which causes the full content of the buffer to be
  415. // copied to the card.
  416. //
  417. mVertexConstBufferF->setDirty( !prevShaderBuffer->mVertexConstBufferF->isEqual( mVertexConstBufferF ) );
  418. mPixelConstBufferF->setDirty( !prevShaderBuffer->mPixelConstBufferF->isEqual( mPixelConstBufferF ) );
  419. mVertexConstBufferI->setDirty( !prevShaderBuffer->mVertexConstBufferF->isEqual( mVertexConstBufferI ) );
  420. mPixelConstBufferI->setDirty( !prevShaderBuffer->mPixelConstBufferF->isEqual( mPixelConstBufferI ) );
  421. }
  422. else
  423. {
  424. // This happens rarely... but it can happen.
  425. //
  426. // We copy the entire dirty state to the card.
  427. PROFILE_SCOPE(GFXD3D9ShaderConstBuffer_activate_dirty_check_2);
  428. mVertexConstBufferF->setDirty( true );
  429. mPixelConstBufferF->setDirty( true );
  430. mVertexConstBufferI->setDirty( true );
  431. mPixelConstBufferI->setDirty( true );
  432. }
  433. }
  434. const U32 bytesToFloat4 = 16;
  435. const U32 bytesToInt4 = 16;
  436. U32 start, bufferSize;
  437. const U8* buf;
  438. if ( mVertexConstBufferF->isDirty() )
  439. {
  440. buf = mVertexConstBufferF->getDirtyBuffer( &start, &bufferSize );
  441. mDevice->SetVertexShaderConstantF( start / bytesToFloat4, (float*)buf, bufferSize / bytesToFloat4 );
  442. }
  443. if ( mPixelConstBufferF->isDirty() )
  444. {
  445. buf = mPixelConstBufferF->getDirtyBuffer( &start, &bufferSize );
  446. mDevice->SetPixelShaderConstantF( start / bytesToFloat4, (float*)buf, bufferSize / bytesToFloat4 );
  447. }
  448. if ( mVertexConstBufferI->isDirty() )
  449. {
  450. buf = mVertexConstBufferI->getDirtyBuffer( &start, &bufferSize );
  451. mDevice->SetVertexShaderConstantI( start / bytesToInt4, (int*)buf, bufferSize / bytesToInt4 );
  452. }
  453. if ( mPixelConstBufferI->isDirty() )
  454. {
  455. buf = mPixelConstBufferI->getDirtyBuffer( &start, &bufferSize );
  456. mDevice->SetPixelShaderConstantI( start / bytesToInt4, (int*)buf, bufferSize / bytesToInt4 );
  457. }
  458. #ifdef TORQUE_DEBUG
  459. // Make sure all the constants for this buffer were assigned.
  460. if ( mWasLost )
  461. {
  462. mVertexConstBufferF->assertUnassignedConstants( mShader->getVertexShaderFile().c_str() );
  463. mVertexConstBufferI->assertUnassignedConstants( mShader->getVertexShaderFile().c_str() );
  464. mPixelConstBufferF->assertUnassignedConstants( mShader->getPixelShaderFile().c_str() );
  465. mPixelConstBufferI->assertUnassignedConstants( mShader->getPixelShaderFile().c_str() );
  466. }
  467. #endif
  468. // Clear the lost state.
  469. mWasLost = false;
  470. }
  471. void GFXD3D9ShaderConstBuffer::onShaderReload( GFXD3D9Shader *shader )
  472. {
  473. AssertFatal( shader == mShader, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
  474. SAFE_DELETE( mVertexConstBufferF );
  475. SAFE_DELETE( mPixelConstBufferF );
  476. SAFE_DELETE( mVertexConstBufferI );
  477. SAFE_DELETE( mPixelConstBufferI );
  478. AssertFatal( mVertexConstBufferLayoutF == shader->mVertexConstBufferLayoutF, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
  479. AssertFatal( mPixelConstBufferLayoutF == shader->mPixelConstBufferLayoutF, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
  480. AssertFatal( mVertexConstBufferLayoutI == shader->mVertexConstBufferLayoutI, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
  481. AssertFatal( mPixelConstBufferLayoutI == shader->mPixelConstBufferLayoutI, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
  482. mVertexConstBufferF = new GenericConstBuffer( mVertexConstBufferLayoutF );
  483. mVertexConstBufferI = new GenericConstBuffer( mVertexConstBufferLayoutI );
  484. mPixelConstBufferF = new GenericConstBuffer( mPixelConstBufferLayoutF );
  485. mPixelConstBufferI = new GenericConstBuffer( mPixelConstBufferLayoutI );
  486. // Set the lost state.
  487. mWasLost = true;
  488. }
  489. //------------------------------------------------------------------------------
  490. GFXD3D9Shader::GFXD3D9Shader()
  491. {
  492. VECTOR_SET_ASSOCIATION( mShaderConsts );
  493. mD3D9Device = dynamic_cast<GFXD3D9Device *>(GFX)->getDevice();
  494. AssertFatal(mD3D9Device, "Invalid device for shader.");
  495. mVertShader = NULL;
  496. mPixShader = NULL;
  497. mVertexConstBufferLayoutF = NULL;
  498. mPixelConstBufferLayoutF = NULL;
  499. mVertexConstBufferLayoutI = NULL;
  500. mPixelConstBufferLayoutI = NULL;
  501. if( smD3DXInclude == NULL )
  502. smD3DXInclude = new _gfxD3DXInclude;
  503. }
  504. //------------------------------------------------------------------------------
  505. GFXD3D9Shader::~GFXD3D9Shader()
  506. {
  507. for (HandleMap::Iterator i = mHandles.begin(); i != mHandles.end(); i++)
  508. delete i->value;
  509. SAFE_DELETE(mVertexConstBufferLayoutF);
  510. SAFE_DELETE(mPixelConstBufferLayoutF);
  511. SAFE_DELETE(mVertexConstBufferLayoutI);
  512. SAFE_DELETE(mPixelConstBufferLayoutI);
  513. SAFE_RELEASE(mVertShader);
  514. SAFE_RELEASE(mPixShader);
  515. }
  516. bool GFXD3D9Shader::_init()
  517. {
  518. PROFILE_SCOPE( GFXD3D9Shader_Init );
  519. if ( mPixVersion > GFX->getPixelShaderVersion() )
  520. {
  521. if ( smLogErrors )
  522. Con::errorf( "GFXD3D9Shader::init - Bad pixel shader version!" );
  523. return false;
  524. }
  525. if ( mPixVersion < 1.0f && mPixelFile.getFileName().isNotEmpty() )
  526. {
  527. if ( smLogErrors )
  528. Con::errorf( "GFXD3D9Shader::init - Pixel shaders not supported on SM %.1f!", mPixVersion );
  529. return false;
  530. }
  531. SAFE_RELEASE(mVertShader);
  532. SAFE_RELEASE(mPixShader);
  533. U32 mjVer = (U32)mFloor( mPixVersion );
  534. U32 mnVer = (U32)( ( mPixVersion - F32( mjVer ) ) * 10.01f ); // 10.01 instead of 10.0 because of floating point issues
  535. String vertTarget = String::ToString("vs_%d_%d", mjVer, mnVer);
  536. String pixTarget = String::ToString("ps_%d_%d", mjVer, mnVer);
  537. // Adjust version for vertex shaders
  538. if (mjVer == 2 && mnVer == 1)
  539. {
  540. pixTarget = "ps_2_a";
  541. vertTarget = "vs_2_0";
  542. }
  543. else if ( mjVer == 2 && mnVer == 2 )
  544. {
  545. pixTarget = "ps_2_b";
  546. vertTarget = "vs_2_0";
  547. }
  548. else if ( ( mPixVersion < 2.0f ) && ( mPixVersion > 1.101f ) )
  549. vertTarget = "vs_1_1";
  550. // Create the macro array including the system wide macros.
  551. const U32 macroCount = smGlobalMacros.size() + mMacros.size() + 2;
  552. FrameTemp<D3DXMACRO> d3dXMacros( macroCount );
  553. for ( U32 i=0; i < smGlobalMacros.size(); i++ )
  554. {
  555. d3dXMacros[i].Name = smGlobalMacros[i].name.c_str();
  556. d3dXMacros[i].Definition = smGlobalMacros[i].value.c_str();
  557. }
  558. for ( U32 i=0; i < mMacros.size(); i++ )
  559. {
  560. d3dXMacros[i+smGlobalMacros.size()].Name = mMacros[i].name.c_str();
  561. d3dXMacros[i+smGlobalMacros.size()].Definition = mMacros[i].value.c_str();
  562. }
  563. String smVersion = String::ToString( mjVer * 10 + mnVer );
  564. d3dXMacros[macroCount - 2].Name = "TORQUE_SM";
  565. d3dXMacros[macroCount - 2].Definition = smVersion.c_str();
  566. d3dXMacros[macroCount - 1].Name = NULL;
  567. d3dXMacros[macroCount - 1].Definition = NULL;
  568. if ( !mVertexConstBufferLayoutF )
  569. mVertexConstBufferLayoutF = new GFXD3D9ShaderBufferLayout();
  570. else
  571. mVertexConstBufferLayoutF->clear();
  572. if ( !mVertexConstBufferLayoutI )
  573. mVertexConstBufferLayoutI = new GFXD3D9ShaderBufferLayout();
  574. else
  575. mVertexConstBufferLayoutI->clear();
  576. if ( !mPixelConstBufferLayoutF )
  577. mPixelConstBufferLayoutF = new GFXD3D9ShaderBufferLayout();
  578. else
  579. mPixelConstBufferLayoutF->clear();
  580. if ( !mPixelConstBufferLayoutI )
  581. mPixelConstBufferLayoutI = new GFXD3D9ShaderBufferLayout();
  582. else
  583. mPixelConstBufferLayoutI->clear();
  584. mSamplerDescriptions.clear();
  585. mShaderConsts.clear();
  586. if ( GFXD3DX.isLoaded && !Con::getBoolVariable( "$shaders::forceLoadCSF", false ) )
  587. {
  588. if ( !mVertexFile.isEmpty() &&
  589. !_compileShader( mVertexFile, vertTarget, d3dXMacros, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mSamplerDescriptions ) )
  590. return false;
  591. if ( !mPixelFile.isEmpty() &&
  592. !_compileShader( mPixelFile, pixTarget, d3dXMacros, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI, mSamplerDescriptions ) )
  593. return false;
  594. }
  595. else
  596. {
  597. if ( !_loadCompiledOutput( mVertexFile, vertTarget, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mSamplerDescriptions ) )
  598. {
  599. if ( smLogErrors )
  600. Con::errorf( "GFXD3D9Shader::init - Unable to load precompiled vertex shader for '%s'.",
  601. mVertexFile.getFullPath().c_str() );
  602. return false;
  603. }
  604. if ( !_loadCompiledOutput( mPixelFile, pixTarget, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI, mSamplerDescriptions ) )
  605. {
  606. if ( smLogErrors )
  607. Con::errorf( "GFXD3D9Shader::init - Unable to load precompiled pixel shader for '%s'.",
  608. mPixelFile.getFullPath().c_str() );
  609. return false;
  610. }
  611. }
  612. // Existing handles are resored to an uninitialized state.
  613. // Those that are found when parsing the layout parameters
  614. // will then be re-initialized.
  615. HandleMap::Iterator iter = mHandles.begin();
  616. for ( ; iter != mHandles.end(); iter++ )
  617. (iter->value)->clear();
  618. _buildShaderConstantHandles(mVertexConstBufferLayoutF, true);
  619. _buildShaderConstantHandles(mVertexConstBufferLayoutI, true);
  620. _buildShaderConstantHandles(mPixelConstBufferLayoutF, false);
  621. _buildShaderConstantHandles(mPixelConstBufferLayoutI, false);
  622. _buildSamplerShaderConstantHandles( mSamplerDescriptions );
  623. _buildInstancingShaderConstantHandles();
  624. // Notify any existing buffers that the buffer
  625. // layouts have changed and they need to update.
  626. Vector<GFXShaderConstBuffer*>::iterator biter = mActiveBuffers.begin();
  627. for ( ; biter != mActiveBuffers.end(); biter++ )
  628. ((GFXD3D9ShaderConstBuffer*)(*biter))->onShaderReload( this );
  629. return true;
  630. }
  631. bool GFXD3D9Shader::_compileShader( const Torque::Path &filePath,
  632. const String& target,
  633. const D3DXMACRO *defines,
  634. GenericConstBufferLayout* bufferLayoutF,
  635. GenericConstBufferLayout* bufferLayoutI,
  636. Vector<GFXShaderConstDesc> &samplerDescriptions )
  637. {
  638. PROFILE_SCOPE( GFXD3D9Shader_CompileShader );
  639. HRESULT res = D3DERR_INVALIDCALL;
  640. LPD3DXBUFFER code = NULL;
  641. LPD3DXBUFFER errorBuff = NULL;
  642. #ifdef TORQUE_DEBUG
  643. U32 flags = D3DXSHADER_DEBUG;
  644. #else
  645. U32 flags = 0;
  646. #endif
  647. #ifdef TORQUE_OS_XENON
  648. flags |= D3DXSHADER_PREFER_FLOW_CONTROL;
  649. #endif
  650. #ifdef D3DXSHADER_USE_LEGACY_D3DX9_31_DLL
  651. if( D3DX_SDK_VERSION >= 32 )
  652. {
  653. // will need to use old compiler for 1_1 shaders - check for pixel
  654. // or vertex shader with appropriate version.
  655. if ((target.compare("vs1", 3) == 0) || (target.compare("vs_1", 4) == 0))
  656. flags |= D3DXSHADER_USE_LEGACY_D3DX9_31_DLL;
  657. if ((target.compare("ps1", 3) == 0) || (target.compare("ps_1", 4) == 0))
  658. flags |= D3DXSHADER_USE_LEGACY_D3DX9_31_DLL;
  659. }
  660. #endif
  661. #if !defined(TORQUE_OS_XENON) && (D3DX_SDK_VERSION <= 40)
  662. #error This version of the DirectX SDK is too old. Please install a newer version of the DirectX SDK: http://msdn.microsoft.com/en-us/directx/default.aspx
  663. #endif
  664. ID3DXConstantTable* table = NULL;
  665. static String sHLSLStr( "hlsl" );
  666. static String sOBJStr( "obj" );
  667. // Is it an HLSL shader?
  668. if ( filePath.getExtension().equal(sHLSLStr, String::NoCase) )
  669. {
  670. FrameAllocatorMarker fam;
  671. char *buffer = NULL;
  672. // Set this so that the D3DXInclude::Open will have this
  673. // information for relative paths.
  674. smD3DXInclude->setPath( filePath.getRootAndPath() );
  675. FileStream s;
  676. if ( !s.open( filePath, Torque::FS::File::Read ) )
  677. {
  678. AssertISV(false, avar("GFXD3D9Shader::initShader - failed to open shader '%s'.", filePath.getFullPath().c_str()));
  679. if ( smLogErrors )
  680. Con::errorf( "GFXD3D9Shader::_compileShader - Failed to open shader file '%s'.",
  681. filePath.getFullPath().c_str() );
  682. return false;
  683. }
  684. // Convert the path which might have virtualized
  685. // mount paths to a real file system path.
  686. Torque::Path realPath;
  687. if ( !FS::GetFSPath( filePath, realPath ) )
  688. realPath = filePath;
  689. // Add a #line pragma so that error and warning messages
  690. // returned by the HLSL compiler report the right file.
  691. String linePragma = String::ToString( "#line 1 \"%s\"\r\n", realPath.getFullPath().c_str() );
  692. U32 linePragmaLen = linePragma.length();
  693. U32 bufSize = s.getStreamSize();
  694. buffer = (char *)fam.alloc( bufSize + linePragmaLen + 1 );
  695. dStrncpy( buffer, linePragma.c_str(), linePragmaLen );
  696. s.read( bufSize, buffer + linePragmaLen );
  697. buffer[bufSize+linePragmaLen] = 0;
  698. res = GFXD3DX.D3DXCompileShader( buffer, bufSize + linePragmaLen, defines, smD3DXInclude, "main",
  699. target, flags, &code, &errorBuff, &table );
  700. }
  701. // Is it a precompiled obj shader?
  702. else if ( filePath.getExtension().equal( sOBJStr, String::NoCase ) )
  703. {
  704. FileStream s;
  705. if(!s.open(filePath, Torque::FS::File::Read))
  706. {
  707. AssertISV(false, avar("GFXD3D9Shader::initShader - failed to open shader '%s'.", filePath.getFullPath().c_str()));
  708. if ( smLogErrors )
  709. Con::errorf( "GFXD3D9Shader::_compileShader - Failed to open shader file '%s'.",
  710. filePath.getFullPath().c_str() );
  711. return false;
  712. }
  713. res = GFXD3DX.D3DXCreateBuffer(s.getStreamSize(), &code);
  714. AssertISV(res == D3D_OK, "Unable to create buffer!");
  715. s.read(s.getStreamSize(), code->GetBufferPointer());
  716. if (res == D3D_OK)
  717. {
  718. DWORD* data = (DWORD*) code->GetBufferPointer();
  719. res = GFXD3DX.D3DXGetShaderConstantTable(data, &table);
  720. }
  721. }
  722. else
  723. {
  724. if ( smLogErrors )
  725. Con::errorf( "GFXD3D9Shader::_compileShader - Unsupported shader file type '%s'.",
  726. filePath.getFullPath().c_str() );
  727. return false;
  728. }
  729. if ( res != D3D_OK && smLogErrors )
  730. Con::errorf( "GFXD3D9Shader::_compileShader - Error compiling shader: %s: %s (%x)",
  731. DXGetErrorStringA(res), DXGetErrorDescriptionA(res), res );
  732. if ( errorBuff )
  733. {
  734. // remove \n at end of buffer
  735. U8 *buffPtr = (U8*) errorBuff->GetBufferPointer();
  736. U32 len = dStrlen( (const char*) buffPtr );
  737. buffPtr[len-1] = '\0';
  738. if( res != D3D_OK )
  739. {
  740. if ( smLogErrors )
  741. Con::errorf( " %s", (const char*) errorBuff->GetBufferPointer() );
  742. }
  743. else
  744. {
  745. if ( smLogWarnings )
  746. Con::warnf( "%s", (const char*) errorBuff->GetBufferPointer() );
  747. }
  748. }
  749. else if ( code == NULL && smLogErrors )
  750. Con::errorf( "GFXD3D9Shader::_compileShader - no compiled code produced; possibly missing file '%s'.",
  751. filePath.getFullPath().c_str() );
  752. // Create the proper shader if we have code
  753. if( code != NULL )
  754. {
  755. #ifndef TORQUE_SHIPPING
  756. LPD3DXBUFFER disassem = NULL;
  757. D3DXDisassembleShader( (DWORD*)code->GetBufferPointer(), false, NULL, &disassem );
  758. mDissasembly = (const char*)disassem->GetBufferPointer();
  759. SAFE_RELEASE( disassem );
  760. if ( gDisassembleAllShaders )
  761. {
  762. String filename = filePath.getFullPath();
  763. filename.replace( ".hlsl", "_dis.txt" );
  764. FileStream *fstream = FileStream::createAndOpen( filename, Torque::FS::File::Write );
  765. if ( fstream )
  766. {
  767. fstream->write( mDissasembly );
  768. fstream->close();
  769. delete fstream;
  770. }
  771. }
  772. #endif
  773. if (target.compare("ps_", 3) == 0)
  774. res = mD3D9Device->CreatePixelShader( (DWORD*)code->GetBufferPointer(), &mPixShader );
  775. else
  776. res = mD3D9Device->CreateVertexShader( (DWORD*)code->GetBufferPointer(), &mVertShader );
  777. if (res == S_OK)
  778. _getShaderConstants(table, bufferLayoutF, bufferLayoutI, samplerDescriptions);
  779. #ifdef TORQUE_ENABLE_CSF_GENERATION
  780. // Ok, we've got a valid shader and constants, let's write them all out.
  781. if ( !_saveCompiledOutput(filePath, code, bufferLayoutF, bufferLayoutI) && smLogErrors )
  782. Con::errorf( "GFXD3D9Shader::_compileShader - Unable to save shader compile output for: %s",
  783. filePath.getFullPath().c_str() );
  784. #endif
  785. SAFE_RELEASE(table);
  786. if ( res != S_OK && smLogErrors )
  787. Con::errorf( "GFXD3D9Shader::_compileShader - Unable to create shader for '%s'.",
  788. filePath.getFullPath().c_str() );
  789. }
  790. bool result = code != NULL && res == S_OK;
  791. SAFE_RELEASE( code );
  792. SAFE_RELEASE( errorBuff );
  793. return result;
  794. }
  795. void GFXD3D9Shader::_getShaderConstants( ID3DXConstantTable *table,
  796. GenericConstBufferLayout *bufferLayoutF,
  797. GenericConstBufferLayout* bufferLayoutI,
  798. Vector<GFXShaderConstDesc> &samplerDescriptions )
  799. {
  800. PROFILE_SCOPE( GFXD3D9Shader_GetShaderConstants );
  801. AssertFatal(table, "NULL constant table not allowed, is this an assembly shader?");
  802. D3DXCONSTANTTABLE_DESC tableDesc;
  803. D3D9Assert(table->GetDesc(&tableDesc), "Unable to get constant table info.");
  804. for (U32 i = 0; i < tableDesc.Constants; i++)
  805. {
  806. D3DXHANDLE handle = table->GetConstant(0, i);
  807. const U32 descSize=16;
  808. D3DXCONSTANT_DESC constantDescArray[descSize];
  809. U32 size = descSize;
  810. if (table->GetConstantDesc(handle, constantDescArray, &size) == S_OK)
  811. {
  812. D3DXCONSTANT_DESC& constantDesc = constantDescArray[0];
  813. GFXShaderConstDesc desc;
  814. desc.name = String(constantDesc.Name);
  815. // Prepend a "$" if it doesn't exist. Just to make things consistent.
  816. if (desc.name.find("$") != 0)
  817. desc.name = String::ToString("$%s", desc.name.c_str());
  818. //Con::printf("name %s: , offset: %d, size: %d, constantDesc.Elements: %d", desc.name.c_str(), constantDesc.RegisterIndex, constantDesc.Bytes, constantDesc.Elements);
  819. desc.arraySize = constantDesc.Elements;
  820. GenericConstBufferLayout* bufferLayout = NULL;
  821. switch (constantDesc.RegisterSet)
  822. {
  823. case D3DXRS_INT4 :
  824. {
  825. bufferLayout = bufferLayoutI;
  826. switch (constantDesc.Class)
  827. {
  828. case D3DXPC_SCALAR :
  829. desc.constType = GFXSCT_Int;
  830. break;
  831. case D3DXPC_VECTOR :
  832. {
  833. switch (constantDesc.Columns)
  834. {
  835. case 1 :
  836. desc.constType = GFXSCT_Int;
  837. break;
  838. case 2 :
  839. desc.constType = GFXSCT_Int2;
  840. break;
  841. case 3 :
  842. desc.constType = GFXSCT_Int3;
  843. break;
  844. case 4 :
  845. desc.constType = GFXSCT_Int4;
  846. break;
  847. default:
  848. AssertFatal(false, "Unknown int vector type!");
  849. break;
  850. }
  851. }
  852. break;
  853. }
  854. desc.constType = GFXSCT_Int4;
  855. break;
  856. }
  857. case D3DXRS_FLOAT4 :
  858. {
  859. bufferLayout = bufferLayoutF;
  860. switch (constantDesc.Class)
  861. {
  862. case D3DXPC_SCALAR:
  863. desc.constType = GFXSCT_Float;
  864. break;
  865. case D3DXPC_VECTOR :
  866. {
  867. switch (constantDesc.Columns)
  868. {
  869. case 1 :
  870. desc.constType = GFXSCT_Float;
  871. break;
  872. case 2 :
  873. desc.constType = GFXSCT_Float2;
  874. break;
  875. case 3 :
  876. desc.constType = GFXSCT_Float3;
  877. break;
  878. case 4 :
  879. desc.constType = GFXSCT_Float4;
  880. break;
  881. default:
  882. AssertFatal(false, "Unknown float vector type!");
  883. break;
  884. }
  885. }
  886. break;
  887. case D3DXPC_MATRIX_ROWS :
  888. case D3DXPC_MATRIX_COLUMNS :
  889. {
  890. switch (constantDesc.RegisterCount)
  891. {
  892. case 3 :
  893. desc.constType = GFXSCT_Float3x3;
  894. break;
  895. case 4 :
  896. desc.constType = GFXSCT_Float4x4;
  897. break;
  898. }
  899. }
  900. break;
  901. case D3DXPC_OBJECT :
  902. case D3DXPC_STRUCT :
  903. bufferLayout = NULL;
  904. break;
  905. }
  906. }
  907. break;
  908. case D3DXRS_SAMPLER :
  909. {
  910. AssertFatal( constantDesc.Elements == 1, "Sampler Arrays not yet supported!" );
  911. switch (constantDesc.Type)
  912. {
  913. case D3DXPT_SAMPLER :
  914. case D3DXPT_SAMPLER1D :
  915. case D3DXPT_SAMPLER2D :
  916. case D3DXPT_SAMPLER3D :
  917. // Hi-jack the desc's arraySize to store the registerIndex.
  918. desc.constType = GFXSCT_Sampler;
  919. desc.arraySize = constantDesc.RegisterIndex;
  920. samplerDescriptions.push_back( desc );
  921. mShaderConsts.push_back(desc);
  922. break;
  923. case D3DXPT_SAMPLERCUBE :
  924. desc.constType = GFXSCT_SamplerCube;
  925. desc.arraySize = constantDesc.RegisterIndex;
  926. samplerDescriptions.push_back( desc );
  927. mShaderConsts.push_back(desc);
  928. break;
  929. }
  930. }
  931. break;
  932. default:
  933. AssertFatal(false, "Unknown shader constant class enum");
  934. break;
  935. }
  936. if (bufferLayout)
  937. {
  938. mShaderConsts.push_back(desc);
  939. U32 alignBytes = getAlignmentValue(desc.constType);
  940. U32 paramSize = alignBytes * desc.arraySize;
  941. bufferLayout->addParameter( desc.name,
  942. desc.constType,
  943. constantDesc.RegisterIndex * sizeof(Point4F),
  944. paramSize,
  945. desc.arraySize,
  946. alignBytes );
  947. }
  948. }
  949. else
  950. AssertFatal(false, "Unable to get shader constant description! (may need more elements of constantDesc");
  951. }
  952. }
  953. const U32 GFXD3D9Shader::smCompiledShaderTag = MakeFourCC('t','c','s','f');
  954. bool GFXD3D9Shader::_saveCompiledOutput( const Torque::Path &filePath,
  955. LPD3DXBUFFER buffer,
  956. GenericConstBufferLayout *bufferLayoutF,
  957. GenericConstBufferLayout *bufferLayoutI,
  958. Vector<GFXShaderConstDesc> &samplerDescriptions )
  959. {
  960. Torque::Path outputPath(filePath);
  961. outputPath.setExtension("csf"); // "C"ompiled "S"hader "F"ile (fancy!)
  962. FileStream f;
  963. if (!f.open(outputPath, Torque::FS::File::Write))
  964. return false;
  965. if (!f.write(smCompiledShaderTag))
  966. return false;
  967. // We could reverse engineer the structure in the compiled output, but this
  968. // is a bit easier because we can just read it into the struct that we want.
  969. if (!bufferLayoutF->write(&f))
  970. return false;
  971. if (!bufferLayoutI->write(&f))
  972. return false;
  973. U32 bufferSize = buffer->GetBufferSize();
  974. if (!f.write(bufferSize))
  975. return false;
  976. if (!f.write(bufferSize, buffer->GetBufferPointer()))
  977. return false;
  978. // Write out sampler descriptions.
  979. f.write( samplerDescriptions.size() );
  980. for ( U32 i = 0; i < samplerDescriptions.size(); i++ )
  981. {
  982. f.write( samplerDescriptions[i].name );
  983. f.write( (U32)(samplerDescriptions[i].constType) );
  984. f.write( samplerDescriptions[i].arraySize );
  985. }
  986. f.close();
  987. return true;
  988. }
  989. bool GFXD3D9Shader::_loadCompiledOutput( const Torque::Path &filePath,
  990. const String &target,
  991. GenericConstBufferLayout *bufferLayoutF,
  992. GenericConstBufferLayout *bufferLayoutI,
  993. Vector<GFXShaderConstDesc> &samplerDescriptions )
  994. {
  995. Torque::Path outputPath(filePath);
  996. outputPath.setExtension("csf"); // "C"ompiled "S"hader "F"ile (fancy!)
  997. FileStream f;
  998. if (!f.open(outputPath, Torque::FS::File::Read))
  999. return false;
  1000. U32 fileTag;
  1001. if (!f.read(&fileTag))
  1002. return false;
  1003. if (fileTag != smCompiledShaderTag)
  1004. return false;
  1005. if (!bufferLayoutF->read(&f))
  1006. return false;
  1007. if (!bufferLayoutI->read(&f))
  1008. return false;
  1009. U32 bufferSize;
  1010. if (!f.read(&bufferSize))
  1011. return false;
  1012. U32 waterMark = FrameAllocator::getWaterMark();
  1013. DWORD* buffer = static_cast<DWORD*>(FrameAllocator::alloc(bufferSize));
  1014. if (!f.read(bufferSize, buffer))
  1015. return false;
  1016. // Read sampler descriptions.
  1017. U32 samplerCount;
  1018. f.read( &samplerCount );
  1019. for ( U32 i = 0; i < samplerCount; i++ )
  1020. {
  1021. GFXShaderConstDesc samplerDesc;
  1022. f.read( &(samplerDesc.name) );
  1023. f.read( (U32*)&(samplerDesc.constType) );
  1024. f.read( &(samplerDesc.arraySize) );
  1025. samplerDescriptions.push_back( samplerDesc );
  1026. }
  1027. f.close();
  1028. HRESULT res;
  1029. if (target.compare("ps_", 3) == 0)
  1030. res = mD3D9Device->CreatePixelShader(buffer, &mPixShader );
  1031. else
  1032. res = mD3D9Device->CreateVertexShader(buffer, &mVertShader );
  1033. AssertFatal(SUCCEEDED(res), "Unable to load shader!");
  1034. FrameAllocator::setWaterMark(waterMark);
  1035. return SUCCEEDED(res);
  1036. }
  1037. void GFXD3D9Shader::_buildShaderConstantHandles(GenericConstBufferLayout* layout, bool vertexConst)
  1038. {
  1039. for (U32 i = 0; i < layout->getParameterCount(); i++)
  1040. {
  1041. GenericConstBufferLayout::ParamDesc pd;
  1042. layout->getDesc(i, pd);
  1043. GFXD3D9ShaderConstHandle* handle;
  1044. HandleMap::Iterator j = mHandles.find(pd.name);
  1045. if (j != mHandles.end())
  1046. {
  1047. handle = j->value;
  1048. handle->mShader = this;
  1049. handle->setValid( true );
  1050. }
  1051. else
  1052. {
  1053. handle = new GFXD3D9ShaderConstHandle();
  1054. handle->mShader = this;
  1055. mHandles[pd.name] = handle;
  1056. handle->setValid( true );
  1057. }
  1058. if ( vertexConst )
  1059. {
  1060. handle->mVertexConstant = true;
  1061. handle->mVertexHandle = pd;
  1062. }
  1063. else
  1064. {
  1065. handle->mPixelConstant = true;
  1066. handle->mPixelHandle = pd;
  1067. }
  1068. }
  1069. }
  1070. void GFXD3D9Shader::_buildSamplerShaderConstantHandles( Vector<GFXShaderConstDesc> &samplerDescriptions )
  1071. {
  1072. Vector<GFXShaderConstDesc>::iterator iter = samplerDescriptions.begin();
  1073. for ( ; iter != samplerDescriptions.end(); iter++ )
  1074. {
  1075. const GFXShaderConstDesc &desc = *iter;
  1076. AssertFatal( desc.constType == GFXSCT_Sampler ||
  1077. desc.constType == GFXSCT_SamplerCube,
  1078. "GFXD3D9Shader::_buildSamplerShaderConstantHandles - Invalid samplerDescription type!" );
  1079. GFXD3D9ShaderConstHandle *handle;
  1080. HandleMap::Iterator j = mHandles.find(desc.name);
  1081. if ( j != mHandles.end() )
  1082. handle = j->value;
  1083. else
  1084. {
  1085. handle = new GFXD3D9ShaderConstHandle();
  1086. mHandles[desc.name] = handle;
  1087. }
  1088. handle->mShader = this;
  1089. handle->setValid( true );
  1090. handle->mPixelConstant = true;
  1091. handle->mPixelHandle.name = desc.name;
  1092. handle->mPixelHandle.constType = desc.constType;
  1093. handle->mPixelHandle.offset = desc.arraySize;
  1094. }
  1095. }
  1096. void GFXD3D9Shader::_buildInstancingShaderConstantHandles()
  1097. {
  1098. U32 offset = 0;
  1099. for ( U32 i=0; i < mInstancingFormat.getElementCount(); i++ )
  1100. {
  1101. const GFXVertexElement &element = mInstancingFormat.getElement( i );
  1102. String constName = String::ToString( "$%s", element.getSemantic().c_str() );
  1103. GFXD3D9ShaderConstHandle *handle;
  1104. HandleMap::Iterator j = mHandles.find( constName );
  1105. if ( j != mHandles.end() )
  1106. handle = j->value;
  1107. else
  1108. {
  1109. handle = new GFXD3D9ShaderConstHandle();
  1110. mHandles[ constName ] = handle;
  1111. }
  1112. handle->mShader = this;
  1113. handle->setValid( true );
  1114. handle->mInstancingConstant = true;
  1115. // We shouldn't have an instancing constant that is also
  1116. // a vertex or pixel constant! This means the shader features
  1117. // are confused as to what is instanced.
  1118. //
  1119. AssertFatal( !handle->mVertexConstant &&
  1120. !handle->mPixelConstant,
  1121. "GFXD3D9Shader::_buildInstancingShaderConstantHandles - Bad instanced constant!" );
  1122. // HACK: The GFXD3D9ShaderConstHandle will check mVertexConstant then
  1123. // fall back to reading the mPixelHandle values. We depend on this here
  1124. // and store the data we need in the mPixelHandle constant although its
  1125. // not a pixel shader constant.
  1126. //
  1127. handle->mPixelHandle.name = constName;
  1128. handle->mPixelHandle.offset = offset;
  1129. // If this is a matrix we will have 2 or 3 more of these
  1130. // semantics with the same name after it.
  1131. for ( ; i < mInstancingFormat.getElementCount(); i++ )
  1132. {
  1133. const GFXVertexElement &nextElement = mInstancingFormat.getElement( i );
  1134. if ( nextElement.getSemantic() != element.getSemantic() )
  1135. {
  1136. i--;
  1137. break;
  1138. }
  1139. offset += nextElement.getSizeInBytes();
  1140. }
  1141. }
  1142. }
  1143. GFXShaderConstBufferRef GFXD3D9Shader::allocConstBuffer()
  1144. {
  1145. if (mVertexConstBufferLayoutF && mPixelConstBufferLayoutF)
  1146. {
  1147. GFXD3D9ShaderConstBuffer* buffer = new GFXD3D9ShaderConstBuffer(this, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI);
  1148. mActiveBuffers.push_back( buffer );
  1149. buffer->registerResourceWithDevice(getOwningDevice());
  1150. return buffer;
  1151. } else {
  1152. return NULL;
  1153. }
  1154. }
  1155. /// Returns a shader constant handle for name
  1156. GFXShaderConstHandle* GFXD3D9Shader::getShaderConstHandle(const String& name)
  1157. {
  1158. HandleMap::Iterator i = mHandles.find(name);
  1159. if ( i != mHandles.end() )
  1160. {
  1161. return i->value;
  1162. }
  1163. else
  1164. {
  1165. GFXD3D9ShaderConstHandle *handle = new GFXD3D9ShaderConstHandle();
  1166. handle->setValid( false );
  1167. handle->mShader = this;
  1168. mHandles[name] = handle;
  1169. return handle;
  1170. }
  1171. }
  1172. /// Returns a shader constant handle for name, if the variable doesn't exist NULL is returned.
  1173. GFXShaderConstHandle* GFXD3D9Shader::findShaderConstHandle(const String& name)
  1174. {
  1175. HandleMap::Iterator i = mHandles.find(name);
  1176. if ( i != mHandles.end() )
  1177. {
  1178. return i->value;
  1179. }
  1180. else
  1181. {
  1182. return NULL;
  1183. }
  1184. }
  1185. const Vector<GFXShaderConstDesc>& GFXD3D9Shader::getShaderConstDesc() const
  1186. {
  1187. return mShaderConsts;
  1188. }
  1189. U32 GFXD3D9Shader::getAlignmentValue(const GFXShaderConstType constType) const
  1190. {
  1191. const U32 mRowSizeF = 16;
  1192. const U32 mRowSizeI = 16;
  1193. switch (constType)
  1194. {
  1195. case GFXSCT_Float :
  1196. case GFXSCT_Float2 :
  1197. case GFXSCT_Float3 :
  1198. case GFXSCT_Float4 :
  1199. return mRowSizeF;
  1200. break;
  1201. // Matrices
  1202. case GFXSCT_Float2x2 :
  1203. return mRowSizeF * 2;
  1204. break;
  1205. case GFXSCT_Float3x3 :
  1206. return mRowSizeF * 3;
  1207. break;
  1208. case GFXSCT_Float4x4 :
  1209. return mRowSizeF * 4;
  1210. break;
  1211. //// Scalar
  1212. case GFXSCT_Int :
  1213. case GFXSCT_Int2 :
  1214. case GFXSCT_Int3 :
  1215. case GFXSCT_Int4 :
  1216. return mRowSizeI;
  1217. break;
  1218. default:
  1219. AssertFatal(false, "Unsupported type!");
  1220. return 0;
  1221. break;
  1222. }
  1223. }
  1224. void GFXD3D9Shader::zombify()
  1225. {
  1226. // Shaders don't need zombification
  1227. }
  1228. void GFXD3D9Shader::resurrect()
  1229. {
  1230. // Shaders are never zombies, and therefore don't have to be brought back.
  1231. }