Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <plugin\plugin.h>
- #include <game_sa\RenderWare.h>
- #include <D3D9Headers\d3dx9.h>
- #include <patch\CPatch.h>
- #include <game_sa\CSprite2d.h>
- #define pRwCamera (*(RwCamera**)0xC1703C)
- using namespace plugin;
- #define g_D3Dpp ((D3DPRESENT_PARAMETERS *)0xC9C040)
- class GBuffer
- {
- public:
- static IDirect3DTexture9 *m_pDepthTexture;
- static IDirect3DTexture9 *m_pNormalsTexture;
- static IDirect3DSurface9 *m_pDepthSurface;
- static IDirect3DSurface9 *m_pNormalsSurface;
- static IDirect3DSurface9 *m_pDepthForDepth;
- static IDirect3DSurface9 *m_pDepthForNormals;
- static ID3DXEffect *m_pGBufferShader;
- static void Patch()
- {
- plugin::Core::RegisterFunc(FUNC_INITIALISE_RW, Create);
- plugin::Core::RegisterFunc(FUNC_SHUTDOWN_RW, Destroy);
- plugin::Core::RegisterFunc(FUNC_BEFORE_RESET, Destroy);
- plugin::Core::RegisterFunc(FUNC_AFTER_RESET, Reset);
- // Clear()
- CPatch::RedirectCall(0x53EA8E, Clear);
- // Render for custom pipelines
- CPatch::RedirectJump(0x7FA360, RenderPart::CustomRenderer::OnRender);
- CPatch::RedirectJump(0x7FA320, RenderPart::CustomRenderer::OnRenderIndexed);
- // Matrices setup
- CPatch::RedirectCall(0x5D6495, RenderPart::CustomRenderer::SetupMatrices);
- CPatch::RedirectCall(0x5D77E5, RenderPart::CustomRenderer::SetupMatrices);
- CPatch::RedirectCall(0x5D993F, RenderPart::CustomRenderer::SetupMatrices);
- // Debug
- plugin::Core::RegisterFunc(FUNC_DRAWING, DebugDraw);
- }
- static bool LoadGBufferShader(char *filename)
- {
- TCHAR message[300];
- ID3DXBuffer *errors;
- HRESULT result = D3DXCreateEffectFromFile(RwD3DDevice, filename, 0, 0, 0, NULL, &m_pGBufferShader, &errors);
- if(errors)
- {
- MessageBox(0, (TCHAR *)errors->GetBufferPointer(), 0, 0);
- errors->Release();
- }
- if(FAILED(result))
- {
- sprintf(message, "Failed to compile shader from %s", filename);
- MessageBox(*(HWND *)RsGlobal->ps, message, "shaderAPI.cleo", MB_OK|MB_ICONERROR);
- return false;
- }
- return true;
- }
- static void UnloadGBufferShader()
- {
- if(m_pGBufferShader);
- {
- m_pGBufferShader->Release();
- m_pGBufferShader = NULL;
- }
- }
- static void Create()
- {
- RwD3DDevice->CreateTexture(g_D3Dpp->BackBufferWidth, g_D3Dpp->BackBufferHeight,
- 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pDepthTexture, NULL);
- m_pDepthTexture->GetSurfaceLevel(0, &m_pDepthSurface);
- RwD3DDevice->CreateTexture(g_D3Dpp->BackBufferWidth, g_D3Dpp->BackBufferHeight,
- 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pNormalsTexture, NULL);
- m_pNormalsTexture->GetSurfaceLevel(0, &m_pNormalsSurface);
- RwD3DDevice->CreateDepthStencilSurface(g_D3Dpp->BackBufferWidth, g_D3Dpp->BackBufferHeight, D3DFMT_D24X8, D3DMULTISAMPLE_NONE, 0, 0,
- &m_pDepthForDepth, NULL);
- RwD3DDevice->CreateDepthStencilSurface(g_D3Dpp->BackBufferWidth, g_D3Dpp->BackBufferHeight, D3DFMT_D24X8, D3DMULTISAMPLE_NONE, 0, 0,
- &m_pDepthForNormals, NULL);
- LoadGBufferShader("shaderApi.txt");
- m_pGBufferShader->SetTechnique("gbuffer");
- // temporary here
- if(D3DXCreateEffectPool(&ShaderOpcodes::effectPool) != D3D_OK)
- MessageBox(*(HWND *)RsGlobal->ps, "Failed to create effect pool", "shaderAPI.cleo", MB_OK|MB_ICONERROR);
- }
- static void Reset()
- {
- Create();
- }
- static void Destroy()
- {
- if(m_pDepthTexture)
- {
- m_pDepthTexture->Release();
- m_pDepthTexture->Release();
- m_pDepthTexture = NULL;
- }
- if(m_pNormalsTexture)
- {
- m_pNormalsSurface->Release();
- m_pNormalsTexture->Release();
- m_pNormalsTexture = NULL;
- }
- if(m_pDepthForDepth)
- {
- m_pDepthForDepth->Release();
- m_pDepthForDepth = NULL;
- }
- if(m_pDepthForNormals)
- {
- m_pDepthForNormals->Release();
- m_pDepthForNormals = NULL;
- }
- UnloadGBufferShader();
- }
- static void Clear()
- {
- CALLVOID(0x734650); // DefinedState()
- IDirect3DSurface9 *saved_rt, *saved_z;
- RwD3DDevice->GetRenderTarget(0, &saved_rt);
- if(saved_rt)
- saved_rt->Release();
- RwD3DDevice->GetDepthStencilSurface(&saved_z);
- if(saved_z)
- saved_z->Release();
- // depth
- RwD3DDevice->SetRenderTarget(0, m_pDepthSurface);
- RwD3DDevice->SetDepthStencilSurface(m_pDepthForDepth);
- RwD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0);
- // normals
- RwD3DDevice->SetRenderTarget(0, m_pNormalsSurface);
- RwD3DDevice->SetDepthStencilSurface(m_pDepthForNormals);
- RwD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0);
- RwD3DDevice->SetRenderTarget(0, saved_rt);
- RwD3DDevice->SetDepthStencilSurface(saved_z);
- m_pGBufferShader->SetFloat("g_farz", pRwCamera->farPlane);
- }
- static void DebugDraw()
- {
- IDirect3DBaseTexture9 *tx_saved;
- RwD3DDevice->GetTexture(0, &tx_saved);
- if(tx_saved)
- tx_saved->Release();
- // dummy texture
- rwD3D9SetTexture(((CSprite2d *)0xC71AD0)->m_pTexture, 0);
- RwD3DDevice->SetTexture(0, m_pDepthTexture);
- CSprite2d::DrawTxRect(CRect(100.0f, 100.0f, 400.0f, 400.0f), CRGBA(255, 255, 255, 255));
- RwD3DDevice->SetTexture(0, m_pNormalsTexture);
- CSprite2d::DrawTxRect(CRect(450.0f, 100.0f, 750.0f, 400.0f), CRGBA(255, 255, 255, 255));
- RwD3DDevice->SetTexture(0, tx_saved);
- }
- class RenderPart
- {
- public:
- class CustomRenderer
- {
- public:
- static void OnRender(D3DPRIMITIVETYPE primitiveType, unsigned int startVertex, unsigned int primitiveCount)
- {
- rwD3D9RenderStateFlushCache();
- // default render
- RwD3DDevice->DrawPrimitive(primitiveType, startVertex, primitiveCount);
- UINT passes;
- IDirect3DSurface9 *saved_rt, *saved_z;
- RwD3DDevice->GetRenderTarget(0, &saved_rt);
- if(saved_rt)
- saved_rt->Release();
- RwD3DDevice->GetDepthStencilSurface(&saved_z);
- if(saved_z)
- saved_z->Release();
- m_pGBufferShader->Begin(&passes, 0);
- // depth
- RwD3DDevice->SetRenderTarget(0, m_pDepthSurface);
- RwD3DDevice->SetDepthStencilSurface(m_pDepthForDepth);
- m_pGBufferShader->BeginPass(0);
- RwD3DDevice->DrawPrimitive(primitiveType, startVertex, primitiveCount);
- m_pGBufferShader->EndPass();
- // normals
- RwD3DDevice->SetRenderTarget(0, m_pNormalsSurface);
- RwD3DDevice->SetDepthStencilSurface(m_pDepthForNormals);
- m_pGBufferShader->BeginPass(1);
- RwD3DDevice->DrawPrimitive(primitiveType, startVertex, primitiveCount);
- m_pGBufferShader->EndPass();
- m_pGBufferShader->End();
- RwD3DDevice->SetRenderTarget(0, saved_rt);
- RwD3DDevice->SetDepthStencilSurface(saved_z);
- }
- static void OnRenderIndexed(D3DPRIMITIVETYPE primitiveType, unsigned int baseVertexIndex, unsigned int minIndex,
- unsigned int numVertices, unsigned int startIndex, unsigned int primitiveCount)
- {
- rwD3D9RenderStateFlushCache();
- // default render
- RwD3DDevice->DrawIndexedPrimitive(primitiveType, baseVertexIndex, minIndex, numVertices, startIndex, primitiveCount);
- UINT passes;
- IDirect3DSurface9 *saved_rt, *saved_z;
- RwD3DDevice->GetRenderTarget(0, &saved_rt);
- if(saved_rt)
- saved_rt->Release();
- RwD3DDevice->GetDepthStencilSurface(&saved_z);
- if(saved_z)
- saved_z->Release();
- m_pGBufferShader->Begin(&passes, 0);
- // depth
- RwD3DDevice->SetRenderTarget(0, m_pDepthSurface);
- RwD3DDevice->SetDepthStencilSurface(m_pDepthForDepth);
- m_pGBufferShader->BeginPass(0);
- RwD3DDevice->DrawIndexedPrimitive(primitiveType, baseVertexIndex, minIndex, numVertices, startIndex, primitiveCount);
- m_pGBufferShader->EndPass();
- // normals
- RwD3DDevice->SetRenderTarget(0, m_pNormalsSurface);
- RwD3DDevice->SetDepthStencilSurface(m_pDepthForNormals);
- m_pGBufferShader->BeginPass(1);
- RwD3DDevice->DrawIndexedPrimitive(primitiveType, baseVertexIndex, minIndex, numVertices, startIndex, primitiveCount);
- m_pGBufferShader->EndPass();
- m_pGBufferShader->End();
- RwD3DDevice->SetRenderTarget(0, saved_rt);
- RwD3DDevice->SetDepthStencilSurface(saved_z);
- }
- static void SetupMatrices(RpAtomic *atomic, unsigned int objectType)
- {
- D3DXMATRIX view, world, worldViewProj;
- RwMatrix *ltm = RwFrameGetLTM(atomic->object.object.parent);
- rwD3D9VSSetActiveWorldMatrix(ltm);
- rwD3D9VSGetWorldNormalizedTransposeMatrix(&world);
- m_pGBufferShader->SetMatrix("g_world", &world);
- rwD3D9VSGetComposedTransformMatrix(&worldViewProj);
- m_pGBufferShader->SetMatrix("g_worldviewproj", &worldViewProj);
- rwD3D9EnableClippingIfNeeded(atomic, objectType);
- }
- };
- };
- };
- IDirect3DTexture9 *GBuffer::m_pDepthTexture;
- IDirect3DTexture9 *GBuffer::m_pNormalsTexture;
- IDirect3DSurface9 *GBuffer::m_pDepthSurface;
- IDirect3DSurface9 *GBuffer::m_pNormalsSurface;
- IDirect3DSurface9 *GBuffer::m_pDepthForDepth;
- IDirect3DSurface9 *GBuffer::m_pDepthForNormals;
- ID3DXEffect *GBuffer::m_pGBufferShader;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement