Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
6.1 KiB

  1. //====== Copyright � , Valve Corporation, All rights reserved. =======//
  2. #ifndef SCECGC_PROXY_HDR
  3. #define SCECGC_PROXY_HDR
  4. #include "cg/cgc.h"
  5. class SceCgcProxyModule
  6. {
  7. public:
  8. SceCgcProxyModule();
  9. ~SceCgcProxyModule();
  10. /// Loads the module and acquires function pointers, returns if the module was
  11. /// loaded successfully.
  12. /// If the module was already loaded the call has no effect and returns TRUE.
  13. BOOL Load( void );
  14. /// Frees the loaded module.
  15. void Free( void );
  16. private:
  17. // see C:\usr\local\cell\host-win32\Cg\include\cgc.h for the function signatures
  18. enum Func {
  19. sceCgcCapCalcHash_Fletcher32,
  20. sceCgcCapCalcHash_Fletcher32_Enhanced,
  21. sceCgcCapCalcHash_MD5,
  22. sceCgcCapGetShaderHash,
  23. sceCgcCapGetShaderHash_header,
  24. compile_program_from_string,
  25. free_compiled_program,
  26. sceCgcCompileFile,
  27. sceCgcCompileString,
  28. sceCgcDebugTraceBin,
  29. sceCgcDeleteBin,
  30. sceCgcDeleteContext,
  31. sceCgcGetBinData,
  32. sceCgcGetBinSize,
  33. sceCgcNewBin,
  34. sceCgcNewContext,
  35. sceCgcStoreBinData,
  36. fnTotal
  37. };
  38. HMODULE m_hModule; //!< The handle of the loaded dx_proxy.dll
  39. FARPROC m_arrFuncs[fnTotal]; //!< The array of loaded function pointers
  40. typedef CGCcontext* (*sceCgcNewContext_t)( CGCmem* pool );
  41. typedef void (*sceCgcDeleteContext_t)( CGCcontext* context );
  42. typedef CGCbin* (*sceCgcNewBin_t)( CGCmem* pool );
  43. typedef void* (*sceCgcGetBinData_t)( CGCbin* bin );
  44. typedef size_t (*sceCgcGetBinSize_t)( CGCbin* bin );
  45. typedef CGCstatus (*sceCgcStoreBinData_t)( CGCbin* bin, void* data, size_t size );
  46. typedef void (*sceCgcDeleteBin_t)( CGCbin* bin );
  47. typedef CGCstatus (*sceCgcCompileString_t)( CGCcontext* context,
  48. const char* sourceString,
  49. const char* profile,
  50. const char* entry,
  51. const char** options,
  52. CGCbin* shaderBinary,
  53. CGCbin* messages ,
  54. CGCbin* asciiOutput ,
  55. CGCinclude* includeHandler );
  56. sceCgcNewContext_t m_sceCgcNewContext;
  57. sceCgcDeleteContext_t m_sceCgcDeleteContext;
  58. sceCgcNewBin_t m_sceCgcNewBin;
  59. sceCgcGetBinData_t m_sceCgcGetBinData;
  60. sceCgcGetBinSize_t m_sceCgcGetBinSize;
  61. sceCgcStoreBinData_t m_sceCgcStoreBinData;
  62. sceCgcDeleteBin_t m_sceCgcDeleteBin;
  63. sceCgcCompileString_t m_sceCgcCompileString;
  64. ///
  65. /// Interface functions calling into DirectX proxy
  66. ///
  67. public:
  68. HRESULT CompileShaderFromFile(
  69. LPCSTR pSrcFile,
  70. CONST D3DXMACRO* pDefines,
  71. LPD3DXINCLUDE pInclude,
  72. LPCSTR pFunctionName,
  73. LPCSTR pProfile,
  74. DWORD Flags,
  75. LPD3DXBUFFER* ppShader,
  76. LPD3DXBUFFER* ppErrorMsgs,
  77. LPD3DXCONSTANTTABLE* ppConstantTable
  78. );
  79. };
  80. //////////////////////////////////////////////////////////////////////////
  81. //////////////////////////////////////////////////////////////////////////
  82. //
  83. // IMPLEMENTATION
  84. //
  85. //////////////////////////////////////////////////////////////////////////
  86. //////////////////////////////////////////////////////////////////////////
  87. inline SceCgcProxyModule::SceCgcProxyModule( void )
  88. {
  89. m_hModule = NULL;
  90. ZeroMemory( m_arrFuncs, sizeof( m_arrFuncs ) );
  91. }
  92. inline SceCgcProxyModule::~SceCgcProxyModule( void )
  93. {
  94. Free();
  95. }
  96. inline BOOL SceCgcProxyModule::Load( void )
  97. {
  98. if ( (m_hModule == NULL) &&
  99. ( ( m_hModule = ::LoadLibrary( "libcgc.dll" ) ) != NULL
  100. //||( m_hModule = ::LoadLibrary( "\\usr\\local\\cell\\host-win32\\Cg\\bin\\libcgc.dll" ) ) != NULL
  101. ||( m_hModule = ::LoadLibrary( "C:\\usr\\local\\cell\\host-win32\\Cg\\bin\\libcgc.dll" ) ) != NULL
  102. )
  103. )
  104. {
  105. // Requested function names array
  106. LPCSTR const arrFuncNames[fnTotal] = {
  107. "sceCgcCapCalcHash_Fletcher32",
  108. "sceCgcCapCalcHash_Fletcher32_Enhanced",
  109. "sceCgcCapCalcHash_MD5",
  110. "sceCgcCapGetShaderHash",
  111. "sceCgcCapGetShaderHash_header",
  112. "compile_program_from_string",
  113. "free_compiled_program",
  114. "sceCgcCompileFile",
  115. "sceCgcCompileString",
  116. "sceCgcDebugTraceBin",
  117. "sceCgcDeleteBin",
  118. "sceCgcDeleteContext",
  119. "sceCgcGetBinData",
  120. "sceCgcGetBinSize",
  121. "sceCgcNewBin",
  122. "sceCgcNewContext",
  123. "sceCgcStoreBinData"
  124. };
  125. // Acquire the functions
  126. for ( int k = 0; k < fnTotal; ++ k )
  127. {
  128. m_arrFuncs[k] = ::GetProcAddress( m_hModule, arrFuncNames[k] );
  129. }
  130. // regexp: replace {[(a-z)|(A-Z)]*}_t m_[(a-z)|(A-Z)]*; with m_\1 = (\1_t)m_arrFuncs[\1];
  131. m_sceCgcNewContext = (sceCgcNewContext_t)m_arrFuncs[sceCgcNewContext];
  132. m_sceCgcDeleteContext = (sceCgcDeleteContext_t) m_arrFuncs[sceCgcNewContext];
  133. m_sceCgcNewBin = (sceCgcNewBin_t)m_arrFuncs[sceCgcNewBin];
  134. m_sceCgcGetBinData = (sceCgcGetBinData_t)m_arrFuncs[sceCgcGetBinData];
  135. m_sceCgcGetBinSize = (sceCgcGetBinSize_t)m_arrFuncs[sceCgcGetBinSize];
  136. m_sceCgcStoreBinData = (sceCgcStoreBinData_t)m_arrFuncs[sceCgcStoreBinData];
  137. m_sceCgcDeleteBin = (sceCgcDeleteBin_t)m_arrFuncs[sceCgcDeleteBin];
  138. m_sceCgcCompileString = (sceCgcCompileString_t)m_arrFuncs[sceCgcCompileString];
  139. }
  140. return !!m_hModule;
  141. }
  142. inline void SceCgcProxyModule::Free( void )
  143. {
  144. if ( m_hModule )
  145. {
  146. ::FreeLibrary( m_hModule );
  147. m_hModule = NULL;
  148. ZeroMemory( m_arrFuncs, sizeof( m_arrFuncs ) );
  149. }
  150. }
  151. //////////////////////////////////////////////////////////////////////////
  152. //////////////////////////////////////////////////////////////////////////
  153. //
  154. // INTERFACE FUNCTIONS IMPLEMENTATION
  155. //
  156. //////////////////////////////////////////////////////////////////////////
  157. //////////////////////////////////////////////////////////////////////////
  158. inline HRESULT SceCgcProxyModule::CompileShaderFromFile(
  159. LPCSTR pSrcFile,
  160. CONST D3DXMACRO* pDefines,
  161. LPD3DXINCLUDE pInclude,
  162. LPCSTR pFunctionName,
  163. LPCSTR pProfile,
  164. DWORD Flags,
  165. LPD3DXBUFFER* ppShader,
  166. LPD3DXBUFFER* ppErrorMsgs,
  167. LPD3DXCONSTANTTABLE* ppConstantTable )
  168. {
  169. if ( !Load() )
  170. return MAKE_HRESULT( SEVERITY_ERROR, FACILITY_ITF, 1 );
  171. if ( !m_arrFuncs[sceCgcCompileFile] )
  172. return MAKE_HRESULT( SEVERITY_ERROR, FACILITY_ITF, 2 );
  173. // TODO: call m_sceCgcCompileFile
  174. return S_OK;
  175. }
  176. #endif