Advertisement
Dimitri_UA

GBuffer

Jul 28th, 2014
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.40 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <plugin\plugin.h>
  4. #include <game_sa\RenderWare.h>
  5. #include <D3D9Headers\d3dx9.h>
  6. #include <patch\CPatch.h>
  7. #include <game_sa\CSprite2d.h>
  8.  
  9. #define pRwCamera    (*(RwCamera**)0xC1703C)
  10.  
  11. using namespace plugin;
  12.  
  13. #define g_D3Dpp ((D3DPRESENT_PARAMETERS *)0xC9C040)
  14.  
  15. class GBuffer
  16. {
  17. public:
  18.     static IDirect3DTexture9 *m_pDepthTexture;
  19.     static IDirect3DTexture9 *m_pNormalsTexture;
  20.     static IDirect3DSurface9 *m_pDepthSurface;
  21.     static IDirect3DSurface9 *m_pNormalsSurface;
  22.     static IDirect3DSurface9 *m_pDepthForDepth;
  23.     static IDirect3DSurface9 *m_pDepthForNormals;
  24.     static ID3DXEffect *m_pGBufferShader;
  25.  
  26.     static void Patch()
  27.     {
  28.         plugin::Core::RegisterFunc(FUNC_INITIALISE_RW, Create);
  29.         plugin::Core::RegisterFunc(FUNC_SHUTDOWN_RW, Destroy);
  30.         plugin::Core::RegisterFunc(FUNC_BEFORE_RESET, Destroy);
  31.         plugin::Core::RegisterFunc(FUNC_AFTER_RESET, Reset);
  32.         // Clear()
  33.         CPatch::RedirectCall(0x53EA8E, Clear);
  34.         // Render for custom pipelines
  35.         CPatch::RedirectJump(0x7FA360, RenderPart::CustomRenderer::OnRender);
  36.         CPatch::RedirectJump(0x7FA320, RenderPart::CustomRenderer::OnRenderIndexed);
  37.         // Matrices setup
  38.         CPatch::RedirectCall(0x5D6495, RenderPart::CustomRenderer::SetupMatrices);
  39.         CPatch::RedirectCall(0x5D77E5, RenderPart::CustomRenderer::SetupMatrices);
  40.         CPatch::RedirectCall(0x5D993F, RenderPart::CustomRenderer::SetupMatrices);
  41.  
  42.         // Debug
  43.         plugin::Core::RegisterFunc(FUNC_DRAWING, DebugDraw);
  44.     }
  45.  
  46.     static bool LoadGBufferShader(char *filename)
  47.     {
  48.         TCHAR message[300];
  49.         ID3DXBuffer *errors;
  50.         HRESULT result = D3DXCreateEffectFromFile(RwD3DDevice, filename, 0, 0, 0, NULL, &m_pGBufferShader, &errors);
  51.         if(errors)
  52.         {
  53.             MessageBox(0, (TCHAR *)errors->GetBufferPointer(), 0, 0);
  54.             errors->Release();
  55.         }
  56.         if(FAILED(result))
  57.         {
  58.             sprintf(message, "Failed to compile shader from %s", filename);
  59.             MessageBox(*(HWND *)RsGlobal->ps, message, "shaderAPI.cleo", MB_OK|MB_ICONERROR);
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64.  
  65.     static void UnloadGBufferShader()
  66.     {
  67.         if(m_pGBufferShader);
  68.         {
  69.             m_pGBufferShader->Release();
  70.             m_pGBufferShader = NULL;
  71.         }
  72.     }
  73.  
  74.     static void Create()
  75.     {
  76.         RwD3DDevice->CreateTexture(g_D3Dpp->BackBufferWidth, g_D3Dpp->BackBufferHeight,
  77.             1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pDepthTexture, NULL);
  78.         m_pDepthTexture->GetSurfaceLevel(0, &m_pDepthSurface);
  79.         RwD3DDevice->CreateTexture(g_D3Dpp->BackBufferWidth, g_D3Dpp->BackBufferHeight,
  80.             1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pNormalsTexture, NULL);
  81.         m_pNormalsTexture->GetSurfaceLevel(0, &m_pNormalsSurface);
  82.         RwD3DDevice->CreateDepthStencilSurface(g_D3Dpp->BackBufferWidth, g_D3Dpp->BackBufferHeight, D3DFMT_D24X8, D3DMULTISAMPLE_NONE, 0, 0,
  83.             &m_pDepthForDepth, NULL);
  84.         RwD3DDevice->CreateDepthStencilSurface(g_D3Dpp->BackBufferWidth, g_D3Dpp->BackBufferHeight, D3DFMT_D24X8, D3DMULTISAMPLE_NONE, 0, 0,
  85.             &m_pDepthForNormals, NULL);
  86.         LoadGBufferShader("shaderApi.txt");
  87.         m_pGBufferShader->SetTechnique("gbuffer");
  88.         // temporary here
  89.         if(D3DXCreateEffectPool(&ShaderOpcodes::effectPool) != D3D_OK)
  90.             MessageBox(*(HWND *)RsGlobal->ps, "Failed to create effect pool", "shaderAPI.cleo", MB_OK|MB_ICONERROR);
  91.     }
  92.  
  93.     static void Reset()
  94.     {
  95.         Create();
  96.     }
  97.  
  98.     static void Destroy()
  99.     {
  100.         if(m_pDepthTexture)
  101.         {
  102.             m_pDepthTexture->Release();
  103.             m_pDepthTexture->Release();
  104.             m_pDepthTexture = NULL;
  105.         }
  106.         if(m_pNormalsTexture)
  107.         {
  108.             m_pNormalsSurface->Release();
  109.             m_pNormalsTexture->Release();
  110.             m_pNormalsTexture = NULL;
  111.         }
  112.         if(m_pDepthForDepth)
  113.         {
  114.             m_pDepthForDepth->Release();
  115.             m_pDepthForDepth = NULL;
  116.         }
  117.         if(m_pDepthForNormals)
  118.         {
  119.             m_pDepthForNormals->Release();
  120.             m_pDepthForNormals = NULL;
  121.         }
  122.         UnloadGBufferShader();
  123.     }
  124.  
  125.     static void Clear()
  126.     {
  127.         CALLVOID(0x734650); // DefinedState()
  128.         IDirect3DSurface9 *saved_rt, *saved_z;
  129.         RwD3DDevice->GetRenderTarget(0, &saved_rt);
  130.         if(saved_rt)
  131.             saved_rt->Release();
  132.         RwD3DDevice->GetDepthStencilSurface(&saved_z);
  133.         if(saved_z)
  134.             saved_z->Release();
  135.         // depth
  136.         RwD3DDevice->SetRenderTarget(0, m_pDepthSurface);
  137.         RwD3DDevice->SetDepthStencilSurface(m_pDepthForDepth);
  138.         RwD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0);
  139.         // normals
  140.         RwD3DDevice->SetRenderTarget(0, m_pNormalsSurface);
  141.         RwD3DDevice->SetDepthStencilSurface(m_pDepthForNormals);
  142.         RwD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0);
  143.         RwD3DDevice->SetRenderTarget(0, saved_rt);
  144.         RwD3DDevice->SetDepthStencilSurface(saved_z);
  145.         m_pGBufferShader->SetFloat("g_farz", pRwCamera->farPlane);
  146.     }
  147.  
  148.     static void DebugDraw()
  149.     {
  150.         IDirect3DBaseTexture9 *tx_saved;
  151.         RwD3DDevice->GetTexture(0, &tx_saved);
  152.         if(tx_saved)
  153.             tx_saved->Release();
  154.         // dummy texture
  155.         rwD3D9SetTexture(((CSprite2d *)0xC71AD0)->m_pTexture, 0);
  156.         RwD3DDevice->SetTexture(0, m_pDepthTexture);
  157.         CSprite2d::DrawTxRect(CRect(100.0f, 100.0f, 400.0f, 400.0f), CRGBA(255, 255, 255, 255));
  158.         RwD3DDevice->SetTexture(0, m_pNormalsTexture);
  159.         CSprite2d::DrawTxRect(CRect(450.0f, 100.0f, 750.0f, 400.0f), CRGBA(255, 255, 255, 255));
  160.         RwD3DDevice->SetTexture(0, tx_saved);
  161.     }
  162.  
  163.     class RenderPart
  164.     {
  165.     public:
  166.         class CustomRenderer
  167.         {
  168.         public:
  169.             static void OnRender(D3DPRIMITIVETYPE primitiveType, unsigned int startVertex, unsigned int primitiveCount)
  170.             {
  171.                 rwD3D9RenderStateFlushCache();
  172.                 // default render
  173.                 RwD3DDevice->DrawPrimitive(primitiveType, startVertex, primitiveCount);
  174.                 UINT passes;
  175.                 IDirect3DSurface9 *saved_rt, *saved_z;
  176.                 RwD3DDevice->GetRenderTarget(0, &saved_rt);
  177.                 if(saved_rt)
  178.                     saved_rt->Release();
  179.                 RwD3DDevice->GetDepthStencilSurface(&saved_z);
  180.                 if(saved_z)
  181.                     saved_z->Release();
  182.                 m_pGBufferShader->Begin(&passes, 0);
  183.                 // depth
  184.                 RwD3DDevice->SetRenderTarget(0, m_pDepthSurface);
  185.                 RwD3DDevice->SetDepthStencilSurface(m_pDepthForDepth);
  186.                 m_pGBufferShader->BeginPass(0);
  187.                 RwD3DDevice->DrawPrimitive(primitiveType, startVertex, primitiveCount);
  188.                 m_pGBufferShader->EndPass();
  189.                 // normals
  190.                 RwD3DDevice->SetRenderTarget(0, m_pNormalsSurface);
  191.                 RwD3DDevice->SetDepthStencilSurface(m_pDepthForNormals);
  192.                 m_pGBufferShader->BeginPass(1);
  193.                 RwD3DDevice->DrawPrimitive(primitiveType, startVertex, primitiveCount);
  194.                 m_pGBufferShader->EndPass();
  195.                 m_pGBufferShader->End();
  196.                 RwD3DDevice->SetRenderTarget(0, saved_rt);
  197.                 RwD3DDevice->SetDepthStencilSurface(saved_z);
  198.             }
  199.  
  200.             static void OnRenderIndexed(D3DPRIMITIVETYPE primitiveType, unsigned int baseVertexIndex, unsigned int minIndex,
  201.                 unsigned int numVertices, unsigned int startIndex, unsigned int primitiveCount)
  202.             {
  203.                 rwD3D9RenderStateFlushCache();
  204.                 // default render
  205.                 RwD3DDevice->DrawIndexedPrimitive(primitiveType, baseVertexIndex, minIndex, numVertices, startIndex, primitiveCount);
  206.                 UINT passes;
  207.                 IDirect3DSurface9 *saved_rt, *saved_z;
  208.                 RwD3DDevice->GetRenderTarget(0, &saved_rt);
  209.                 if(saved_rt)
  210.                     saved_rt->Release();
  211.                 RwD3DDevice->GetDepthStencilSurface(&saved_z);
  212.                 if(saved_z)
  213.                     saved_z->Release();
  214.                 m_pGBufferShader->Begin(&passes, 0);
  215.                 // depth
  216.                 RwD3DDevice->SetRenderTarget(0, m_pDepthSurface);
  217.                 RwD3DDevice->SetDepthStencilSurface(m_pDepthForDepth);
  218.                 m_pGBufferShader->BeginPass(0);
  219.                 RwD3DDevice->DrawIndexedPrimitive(primitiveType, baseVertexIndex, minIndex, numVertices, startIndex, primitiveCount);
  220.                 m_pGBufferShader->EndPass();
  221.                 // normals
  222.                 RwD3DDevice->SetRenderTarget(0, m_pNormalsSurface);
  223.                 RwD3DDevice->SetDepthStencilSurface(m_pDepthForNormals);
  224.                 m_pGBufferShader->BeginPass(1);
  225.                 RwD3DDevice->DrawIndexedPrimitive(primitiveType, baseVertexIndex, minIndex, numVertices, startIndex, primitiveCount);
  226.                 m_pGBufferShader->EndPass();
  227.                 m_pGBufferShader->End();
  228.                 RwD3DDevice->SetRenderTarget(0, saved_rt);
  229.                 RwD3DDevice->SetDepthStencilSurface(saved_z);
  230.             }
  231.  
  232.             static void SetupMatrices(RpAtomic *atomic, unsigned int objectType)
  233.             {
  234.                 D3DXMATRIX view, world, worldViewProj;
  235.                 RwMatrix *ltm = RwFrameGetLTM(atomic->object.object.parent);
  236.                 rwD3D9VSSetActiveWorldMatrix(ltm);
  237.                 rwD3D9VSGetWorldNormalizedTransposeMatrix(&world);
  238.                 m_pGBufferShader->SetMatrix("g_world", &world);
  239.                 rwD3D9VSGetComposedTransformMatrix(&worldViewProj);
  240.                 m_pGBufferShader->SetMatrix("g_worldviewproj", &worldViewProj);
  241.                 rwD3D9EnableClippingIfNeeded(atomic, objectType);
  242.             }
  243.         };
  244.     };
  245. };
  246.  
  247. IDirect3DTexture9 *GBuffer::m_pDepthTexture;
  248. IDirect3DTexture9 *GBuffer::m_pNormalsTexture;
  249. IDirect3DSurface9 *GBuffer::m_pDepthSurface;
  250. IDirect3DSurface9 *GBuffer::m_pNormalsSurface;
  251. IDirect3DSurface9 *GBuffer::m_pDepthForDepth;
  252. IDirect3DSurface9 *GBuffer::m_pDepthForNormals;
  253. ID3DXEffect *GBuffer::m_pGBufferShader;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement