Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cDXEffect.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cDXEffect_H
7 #define _INC_cDXEffect_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cDXBase.h"
13 
14 #ifdef USE_DXX
17 
18 namespace Gray3D
19 {
20  class GRAY3D_LINK cDXEffectPool
21  {
25  };
26 
27  class GRAY3D_LINK cDXEffect : public cDXBaseT<ID3DXEffect>
28  {
33 
34  typedef cDXBaseT<ID3DXEffect> SUPER_t;
35 
36  private:
37  cStringF m_sFilePath;
38 
39  public:
40  cDXEffect( cStringF sFilePath = "", DWORD dwShaderFlags=0 );
41  virtual ~cDXEffect();
42 
43  cStringF get_Name() const
44  {
46  return m_sFilePath;
47  }
48 
49  bool isEffectLoaded() const
50  { return m_pDXObj != nullptr; }
51 
52  ID3DXEffect* get_DXEffect() const
53  { return m_pDXObj; }
54 
55  operator ID3DXEffect* () const
56  {
57  return( m_pDXObj );
58  }
59 
60 #ifdef _DEBUG
61  void DumpDetails();
62 #endif
63  HRESULT CheckReturnHR( HRESULT hRes ) const
64  {
66  if ( FAILED(hRes))
67  {
68  DEBUG_CHECK( !FAILED(hRes));
69  }
70  return hRes;
71  }
72  bool CheckReturnDX( D3DXHANDLE hRet ) const
73  {
75  if ( hRet == HANDLE_NULL )
76  {
77  DEBUG_CHECK( hRet != HANDLE_NULL );
78  return false;
79  }
80  return true;
81  }
82 
83  HRESULT CreateEffectFromFile( cStringF sFilePath, DWORD dwShaderFlags=0 );
84 
85  D3DXEFFECT_DESC get_Desc() const
86  {
90  ASSERT(m_pDXObj!=nullptr);
91  D3DXEFFECT_DESC desc;
92  HRESULT hRes = m_pDXObj->GetDesc(&desc);
93  CheckReturnHR(hRes);
94  return desc;
95  }
96 
97  // Techniques
98  D3DXHANDLE GetTechniqueByName( const char* pszTechName, bool bCritical=true ) const
99  {
102 
103  ASSERT(m_pDXObj!=nullptr);
104  D3DXHANDLE hRet = m_pDXObj->GetTechniqueByName( pszTechName );
105  if ( bCritical )
106  {
107  if ( ! CheckReturnDX(hRet))
108  return HANDLE_NULL;
109  }
110  return hRet;
111  }
112  D3DXHANDLE GetTechniqueEnum(const int nr) const
113  {
114  ASSERT(m_pDXObj!=nullptr);
115  D3DXHANDLE hRet = m_pDXObj->GetTechnique(nr);
116  CheckReturnDX(hRet);
117  return hRet;
118  }
119  D3DXHANDLE get_CurrentTechnique() const
120  {
122  ASSERT(m_pDXObj!=nullptr);
123  D3DXHANDLE hRet = m_pDXObj->GetCurrentTechnique();
124  CheckReturnDX(hRet);
125  return hRet;
126  }
127  HRESULT put_CurrentTechnique( D3DXHANDLE hTech ) const
128  {
130  ASSERT(m_pDXObj!=nullptr);
131  HRESULT hRes = m_pDXObj->SetTechnique(hTech);
132  return CheckReturnHR(hRes);
133  }
134  HRESULT ValidateTechnique( D3DXHANDLE hTech ) const
135  {
137  if ( hTech == HANDLE_NULL )
138  return E_FAIL;
139  ASSERT(m_pDXObj!=nullptr);
140  HRESULT hRes = m_pDXObj->ValidateTechnique( hTech );
141  // CheckReturnHR(hRes);
142  return hRes;
143  }
144  D3DXHANDLE BindTechnique( const char* pszTechName ) const
145  {
146  D3DXHANDLE hTech = GetTechniqueByName( pszTechName );
147  if ( ! CheckReturnDX(hTech))
148  return HANDLE_NULL;
149  HRESULT hRes = ValidateTechnique( hTech ); // not usable here?
150  if ( hRes != S_OK )
151  return HANDLE_NULL;
152  return hTech;
153  }
154  HRESULT SetNextValidTechnique( D3DXHANDLE hTechniqueLike = HANDLE_NULL );
155 
156  D3DXTECHNIQUE_DESC GetTechniqueDesc(D3DXHANDLE hTech) const
157  {
161  ASSERT(m_pDXObj!=nullptr);
162  D3DXTECHNIQUE_DESC desc;
163  HRESULT hRes = m_pDXObj->GetTechniqueDesc(hTech, &desc);
164  CheckReturnHR(hRes);
165  return desc;
166  }
167  D3DXHANDLE GetTechniquePass(D3DXHANDLE hTech, const int passNr) const
168  {
169  ASSERT(m_pDXObj!=nullptr);
170  D3DXHANDLE hRet = m_pDXObj->GetPass(hTech, passNr);
171  CheckReturnDX(hTech);
172  return hRet;
173  }
174 
175  // Technique Pass
176 
177  D3DXPASS_DESC GetPassDesc(D3DXHANDLE hPass) const
178  {
181  ASSERT(m_pDXObj!=nullptr);
182  D3DXPASS_DESC desc;
183  HRESULT hRes = m_pDXObj->GetPassDesc(hPass, &desc);
184  CheckReturnHR(hRes);
185  return desc;
186  }
187 
188  // Annotation
189  D3DXHANDLE GetAnnotationEnum(D3DXHANDLE hTechniquePassOrGlobalParam, const int nr)
190  {
191  ASSERT(m_pDXObj!=nullptr);
192  return m_pDXObj->GetAnnotation(hTechniquePassOrGlobalParam, nr);
193  }
194  D3DXHANDLE GetAnnotationByName(D3DXHANDLE hTechniquePassOrGlobalParam, const char* pszName ) const
195  {
197  ASSERT(m_pDXObj!=nullptr);
198  D3DXHANDLE hRet = m_pDXObj->GetAnnotationByName(hTechniquePassOrGlobalParam, pszName );
199  CheckReturnDX(hRet);
200  return hRet;
201  }
202 
203  // Params
204  bool isParameterUsedCur( D3DXHANDLE hParam ) const
205  {
207  ASSERT(m_pDXObj!=nullptr);
208  return m_pDXObj->IsParameterUsed( hParam, get_CurrentTechnique()) ? true : false;
209  }
210  D3DXHANDLE GetParameterByName( const char* pszName ) const
211  {
213  ASSERT(m_pDXObj!=nullptr);
214  D3DXHANDLE hRet = m_pDXObj->GetParameterByName( HANDLE_NULL, pszName );
215  CheckReturnDX(hRet);
216  return hRet;
217  }
218 
219  D3DXHANDLE GetParameterEnum(D3DXHANDLE hParent, UINT iNum) const
220  {
222  ASSERT(m_pDXObj!=nullptr);
223  D3DXHANDLE hRet = m_pDXObj->GetParameter( hParent, iNum);
224  CheckReturnDX(hRet);
225  return hRet;
226  }
227  D3DXHANDLE GetParameterElement(D3DXHANDLE hParameter, UINT iNum) const
228  {
229  ASSERT(m_pDXObj!=nullptr);
230  D3DXHANDLE hRet = m_pDXObj->GetParameterElement( hParameter, iNum );
231  CheckReturnDX(hRet);
232  return hRet;
233  }
234 
235  D3DXPARAMETER_DESC GetParamDesc( D3DXHANDLE hParamOrAnnotation ) const
236  {
241  ASSERT(m_pDXObj!=nullptr);
242  D3DXPARAMETER_DESC desc;
243  HRESULT hRes = m_pDXObj->GetParameterDesc(hParamOrAnnotation, &desc);
244  CheckReturnHR(hRes);
245  return desc;
246  }
247 
248  // Get or Set Params values
249  HRESULT GetParamFloat( D3DXHANDLE hParam, FLOAT* pValue ) const
250  {
251  ASSERT(m_pDXObj!=nullptr);
252  HRESULT hRes = m_pDXObj->GetFloat(hParam, pValue );
253  CheckReturnHR(hRes);
254  return hRes;
255  }
256  HRESULT GetParamMatrixTranspose( D3DXHANDLE hParam, XMFLOAT4X4* pMat )
257  {
258  ASSERT(m_pDXObj!=nullptr);
259  HRESULT hRes = m_pDXObj->GetMatrixTranspose(hParam, pMat );
260  CheckReturnHR(hRes);
261  return hRes;
262  }
263  HRESULT GetParamTexture( D3DXHANDLE hParam, IDirect3DBaseTexture9** ppTexture )
264  {
265  ASSERT(m_pDXObj!=nullptr);
266  HRESULT hRes = m_pDXObj->GetTexture( hParam, ppTexture );
267  CheckReturnHR(hRes);
268  return hRes;
269  }
270 
271  // Set Values for params.
272  HRESULT SetParamValue(D3DXHANDLE hParam, const void* pBuffer, UINT numBytes)
273  {
274  ASSERT(m_pDXObj!=nullptr);
275  HRESULT hRes = m_pDXObj->SetValue(hParam, pBuffer, numBytes);
276  CheckReturnHR(hRes);
277  return hRes;
278  }
279  HRESULT SetParamInt(D3DXHANDLE hParam, int iValue )
280  {
281  ASSERT(m_pDXObj!=nullptr);
282  HRESULT hRes = m_pDXObj->SetInt(hParam, iValue );
283  CheckReturnHR(hRes);
284  return hRes;
285  }
286  HRESULT SetParamFloat( D3DXHANDLE hParam, FLOAT fVal )
287  {
288  ASSERT(m_pDXObj!=nullptr);
289  HRESULT hRes = m_pDXObj->SetFloat(hParam, fVal );
290  CheckReturnHR(hRes);
291  return hRes;
292  }
293  HRESULT SetParamBool( D3DXHANDLE hParam, BOOL bVal )
294  {
295  ASSERT(m_pDXObj!=nullptr);
296  HRESULT hRes = m_pDXObj->SetBool(hParam, bVal );
297  CheckReturnHR(hRes);
298  return hRes;
299  }
300  HRESULT SetParamVector(D3DXHANDLE hParam, const XMFLOAT4* pVector)
301  {
302  // Shader only does v4 internally!?
303  ASSERT(m_pDXObj!=nullptr);
304  HRESULT hRes = m_pDXObj->SetVector(hParam, pVector );
305  CheckReturnHR(hRes);
306  return hRes;
307  }
308  HRESULT SetParamMatrix(D3DXHANDLE hParam, const XMFLOAT4X4* pMat)
309  {
310  ASSERT(m_pDXObj!=nullptr);
311  HRESULT hRes = m_pDXObj->SetMatrix(hParam, pMat );
312  CheckReturnHR(hRes);
313  return hRes;
314  }
315  HRESULT SetParamMatrixTranspose( D3DXHANDLE hParam, const XMFLOAT4X4* pMat )
316  {
317  ASSERT(m_pDXObj!=nullptr);
318  HRESULT hRes = m_pDXObj->SetMatrixTranspose(hParam, pMat );
319  CheckReturnHR(hRes);
320  return hRes;
321  }
322  HRESULT SetParamTexture(D3DXHANDLE hParam, IDirect3DBaseTexture9* pTexture )
323  {
324  ASSERT(m_pDXObj!=nullptr);
325  HRESULT hRes = m_pDXObj->SetTexture( hParam, pTexture );
326  CheckReturnHR(hRes);
327  return hRes;
328  }
329 
330  HRESULT SetParamFloatArray(D3DXHANDLE hParam, const FLOAT* pVals, int size)
331  {
332  ASSERT(m_pDXObj!=nullptr);
333  HRESULT hRes = m_pDXObj->SetFloatArray(hParam, pVals, size);
334  CheckReturnHR(hRes);
335  return hRes;
336  }
337  HRESULT SetParamVectorArray(D3DXHANDLE hParam, const XMFLOAT4* pVals, int size)
338  {
339  ASSERT(m_pDXObj!=nullptr);
340  HRESULT hRes = m_pDXObj->SetVectorArray(hParam, pVals, size);
341  CheckReturnHR(hRes);
342  return hRes;
343  }
344  HRESULT SetParamMatrixArray(D3DXHANDLE hParam, const XMFLOAT4X4* pVals, int size)
345  {
346  ASSERT(m_pDXObj!=nullptr);
347  HRESULT hRes = m_pDXObj->SetMatrixArray(hParam, pVals, size);
348  CheckReturnHR(hRes);
349  return hRes;
350  }
351  HRESULT SetParamArrayRange( D3DXHANDLE hParam, UINT uStart, UINT uEnd )
352  {
353  ASSERT(m_pDXObj!=nullptr);
354  HRESULT hRes = m_pDXObj->SetArrayRange(hParam, uStart, uEnd);
355  CheckReturnHR(hRes);
356  return hRes;
357  }
358  };
359 
360  // for global prop handles we just use a name.
361 #define D3DPROP(p) (D3DXHANDLE)(#p) // convert this string to a D3DXHANDLE ?
362 
363  // For sub class of cDXEffect
364  // Create a handle for some shader/effect element, (technique, param)
365 #define EFF_HAND(parm) m_h##parm // the variable that holds the handle.
366 #define EFF_HAND_DECL(parm) D3DXHANDLE EFF_HAND(parm) // declare the handle for the param (in manager)
367 
368  // Create a handle for the technique.
369 #define EFF_BIND_TECH( tech ) EFF_HAND(tech) = BindTechnique( #tech );
370 
371  // Create a handle for the param
372 #define EFF_BIND_PARM( parm ) EFF_HAND(parm) = GetParameterByName( #parm )
373 
374  // set by handle
375 #define EFF_PARM_SET( manager, eff, setter, parm, arg ) (manager).eff.setter((manager).eff.EFF_HAND(parm), arg )
376 #define EFF_PARM_SET2( manager, eff, setter, parm, arg, arg2 ) (manager).eff.setter((manager).eff.EFF_HAND(parm), arg, arg2 )
377 
378  class GRAY3D_LINK cDXEffectDraw
379  {
383  public:
384  cDXEffectDraw( ID3DXEffect* pEffect, DWORD dwFlags=0 )
385  : m_pEffect( pEffect )
386  , m_uPasses(0)
387  {
388  ASSERT(m_pEffect!=nullptr);
389  if ( dwFlags != 0xFFFFFFFF ) // don't start just yet.
390  {
391  HRESULT hRes = m_pEffect->Begin(&m_uPasses,dwFlags);
392  if ( FAILED(hRes))
393  {
394  m_uPasses = 0;
395  }
396  else
397  {
398  ASSERT(m_uPasses);
399  }
400  }
401  }
402  ~cDXEffectDraw()
403  {
404  // end the effect
405  if ( m_uPasses )
406  {
407  m_pEffect->End();
408  }
409  }
410  bool isValidEffect() const
411  {
412  if ( m_uPasses <= 0 )
413  return false;
414  ASSERT(m_pEffect!=nullptr);
415  return true;
416  }
417  public:
418  ID3DXEffect* m_pEffect;
419  UINT m_uPasses;
420  };
421 
422  class GRAY3D_LINK cDXEffectDrawPass
423  {
427 
428  public:
429  cDXEffectDraw& m_draw;
430  UINT m_uPassCur;
431 
432  public:
433  cDXEffectDrawPass( cDXEffectDraw& draw, UINT uPassCur=0 )
434  : m_draw(draw)
435  , m_uPassCur(uPassCur)
436  {
437  ASSERT(m_draw.m_pEffect!=nullptr);
438  if ( m_draw.m_uPasses )
439  {
440  ASSERT( uPassCur < m_draw.m_uPasses );
441  HRESULT hRes = m_draw.m_pEffect->BeginPass( uPassCur );
442  if ( FAILED(hRes))
443  {
444  m_draw.m_uPasses = 0;
445  }
446  }
447  else
448  {
449  ASSERT(m_uPassCur==0);
450  }
451  }
452  ~cDXEffectDrawPass()
453  {
455  if ( m_draw.m_uPasses )
456  {
457  m_draw.m_pEffect->EndPass();
458  }
459  }
460 
461  HRESULT CommitChanges()
462  {
465  HRESULT hRes = m_draw.m_pEffect->CommitChanges();
466  return hRes;
467  }
468 
469  HRESULT SetPass( UINT uPassCur )
470  {
472  ASSERT( m_draw.m_uPasses > 0 );
473  if ( m_uPassCur == uPassCur )
474  return S_FALSE;
475  m_uPassCur = uPassCur;
476  m_draw.m_pEffect->EndPass();
477  return m_draw.m_pEffect->BeginPass( uPassCur );
478  }
479 
480  HRESULT SetTechnique( D3DXHANDLE hTechnique, UINT uPassCur=0 )
481  {
483  ASSERT(m_draw.m_pEffect);
484  ASSERT( hTechnique != m_draw.m_pEffect->GetCurrentTechnique() ); // get_CurrentTechnique
485  if ( m_draw.m_uPasses )
486  {
487  m_draw.m_pEffect->EndPass();
488  m_draw.m_pEffect->End();
489  m_draw.m_uPasses = 0;
490  }
491  HRESULT hRes = m_draw.m_pEffect->SetTechnique( hTechnique );
492  if ( FAILED(hRes))
493  {
494  return hRes;
495  }
496  // Start the pass back up again.
497  hRes = m_draw.m_pEffect->Begin( &m_draw.m_uPasses, 0 );
498  if ( FAILED(hRes))
499  {
500  m_draw.m_uPasses = 0;
501  return hRes;
502  }
503  hRes = m_draw.m_pEffect->BeginPass( uPassCur );
504  if ( FAILED(hRes))
505  {
506  m_draw.m_uPasses = 0;
507  return hRes;
508  }
509  m_uPassCur = uPassCur;
510  return hRes;
511  }
512  };
513 };
514 #endif // USE_DXX
515 #endif // _INC_cDXEffect_H
#define GRAY3D_LINK
Definition: Gray3D.h:15
#define FAILED(x)
Definition: HResult.h:30
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define DEBUG_CHECK(exp)
Definition: cDebugAssert.h:90
#define HANDLE_NULL
Invalid OS handle for _WIN32. Not invalid OS handle for linux.
Definition: cOSHandle.h:21
Definition: Gray3D.cpp:12
cStringT< FILECHAR_t > cStringF
A file name. checks USE_UNICODE_FN. related to cFilePath.
Definition: cFilePath.h:17
Definition: MathDX.h:138
Definition: MathDX.h:106