-
-
Notifications
You must be signed in to change notification settings - Fork 486
Queued dxDrawPrimitive and dxDrawMaterialPrimitive #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f4d22cf
df3eea7
9209c67
97a7bea
7f37b28
6dd15ab
11de36f
0855ae7
71a7ca7
1b54f7b
e54d345
21e6305
bb469d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
/***************************************************************************** | ||
* | ||
* PROJECT: Multi Theft Auto | ||
* (Shared logic for modifications) | ||
* LICENSE: See LICENSE in the top level directory | ||
* FILE: CPrimitiveBatcher.cpp | ||
* PURPOSE: | ||
* | ||
* | ||
*****************************************************************************/ | ||
#include <StdInc.h> | ||
#include "CPrimitiveBatcher.h" | ||
//////////////////////////////////////////////////////////////// | ||
// | ||
// CPrimitiveBatcher::CPrimitiveBatcher | ||
// | ||
// | ||
// | ||
//////////////////////////////////////////////////////////////// | ||
CPrimitiveBatcher::CPrimitiveBatcher(bool m_bZTest) | ||
{ | ||
m_bZTest = m_bZTest; | ||
} | ||
//////////////////////////////////////////////////////////////// | ||
// | ||
// CPrimitiveBatcher::~CPrimitiveBatcher | ||
// | ||
// | ||
// | ||
//////////////////////////////////////////////////////////////// | ||
CPrimitiveBatcher::~CPrimitiveBatcher(void) | ||
{ | ||
} | ||
//////////////////////////////////////////////////////////////// | ||
// | ||
// CPrimitiveBatcher::OnDeviceCreate | ||
// | ||
// | ||
// | ||
//////////////////////////////////////////////////////////////// | ||
void CPrimitiveBatcher::OnDeviceCreate(IDirect3DDevice9* pDevice, float fViewportSizeX, float fViewportSizeY) | ||
{ | ||
m_pDevice = pDevice; | ||
// Cache matrices | ||
UpdateMatrices(fViewportSizeX, fViewportSizeY); | ||
} | ||
//////////////////////////////////////////////////////////////// | ||
// | ||
// CPrimitiveBatcher::OnRenderTargetChange | ||
// | ||
// | ||
// | ||
//////////////////////////////////////////////////////////////// | ||
void CPrimitiveBatcher::OnChangingRenderTarget(uint uiNewViewportSizeX, uint uiNewViewportSizeY) | ||
{ | ||
// Flush dx draws | ||
Flush(); | ||
// Make new projection transform | ||
UpdateMatrices(uiNewViewportSizeX, uiNewViewportSizeY); | ||
} | ||
//////////////////////////////////////////////////////////////// | ||
// | ||
// CPrimitiveBatcher::UpdateMatrices | ||
// | ||
// | ||
// | ||
//////////////////////////////////////////////////////////////// | ||
void CPrimitiveBatcher::UpdateMatrices(float fViewportSizeX, float fViewportSizeY) | ||
{ | ||
m_fViewportSizeX = fViewportSizeX; | ||
m_fViewportSizeY = fViewportSizeY; | ||
D3DXMatrixIdentity(&m_MatView); | ||
D3DXMatrixIdentity(&m_MatProjection); | ||
// Make projection 3D friendly, so shaders can alter the z coord for fancy effects | ||
float fFarPlane = 10000; | ||
float fNearPlane = 100; | ||
float Q = fFarPlane / (fFarPlane - fNearPlane); | ||
float fAdjustZFactor = 1000.f; | ||
float rcpSizeX = 2.0f / m_fViewportSizeX; | ||
float rcpSizeY = -2.0f / m_fViewportSizeY; | ||
rcpSizeX *= fAdjustZFactor; | ||
rcpSizeY *= fAdjustZFactor; | ||
m_MatProjection.m[0][0] = rcpSizeX; | ||
m_MatProjection.m[1][1] = rcpSizeY; | ||
m_MatProjection.m[2][2] = Q; | ||
m_MatProjection.m[2][3] = 1; | ||
m_MatProjection.m[3][0] = (-m_fViewportSizeX / 2.0f - 0.5f) * rcpSizeX; | ||
m_MatProjection.m[3][1] = (-m_fViewportSizeY / 2.0f - 0.5f) * rcpSizeY; | ||
m_MatProjection.m[3][2] = -Q * fNearPlane; | ||
m_MatProjection.m[3][3] = 0; | ||
m_MatView.m[3][2] = fAdjustZFactor; | ||
} | ||
//////////////////////////////////////////////////////////////// | ||
// | ||
// CPrimitiveBatcher::Flush | ||
// | ||
// Send all buffered vertices to D3D | ||
// | ||
//////////////////////////////////////////////////////////////// | ||
void CPrimitiveBatcher::Flush(void) | ||
{ | ||
if (m_primitiveList.empty()) | ||
return; | ||
|
||
// Save render states | ||
IDirect3DStateBlock9* pSavedStateBlock = nullptr; | ||
m_pDevice->CreateStateBlock(D3DSBT_ALL, &pSavedStateBlock); | ||
// Set transformations | ||
D3DXMATRIX matWorld; | ||
D3DXMatrixIdentity(&matWorld); | ||
m_pDevice->SetTransform(D3DTS_WORLD, &matWorld); | ||
m_pDevice->SetTransform(D3DTS_VIEW, &m_MatView); | ||
m_pDevice->SetTransform(D3DTS_PROJECTION, &m_MatProjection); | ||
|
||
// Set vertex FVF | ||
m_pDevice->SetFVF(PrimitiveVertice::FNV); | ||
|
||
// Set states | ||
m_pDevice->SetRenderState(D3DRS_ZENABLE, m_bZTest ? D3DZB_TRUE : D3DZB_FALSE); | ||
m_pDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL); | ||
m_pDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); | ||
m_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); | ||
m_pDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); | ||
m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); | ||
m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); | ||
m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); | ||
m_pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); | ||
m_pDevice->SetRenderState(D3DRS_ALPHAREF, 0x01); | ||
m_pDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); | ||
m_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE); | ||
m_pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2); | ||
m_pDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); | ||
m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2); | ||
m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE); | ||
m_pDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); | ||
m_pDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a new function |
||
|
||
// Draw | ||
m_pDevice->SetTexture(0, nullptr); | ||
for (int i = 0; i < m_primitiveList.size(); i++) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use |
||
{ | ||
sDrawQueuePrimitive primitive = m_primitiveList[i]; | ||
|
||
const void* pVertexStreamZeroData = &primitive.vertices[0]; | ||
uint uiVertexStreamZeroStride = sizeof(PrimitiveVertice); | ||
|
||
DrawPrimitive(primitive.type, primitive.vertices.size(), pVertexStreamZeroData, uiVertexStreamZeroStride); | ||
} | ||
|
||
// Clean up | ||
ClearQueue(); | ||
// Restore render states | ||
if (pSavedStateBlock) | ||
{ | ||
pSavedStateBlock->Apply(); | ||
SAFE_RELEASE(pSavedStateBlock); | ||
} | ||
} | ||
//////////////////////////////////////////////////////////////// | ||
// | ||
// CPrimitiveBatcher::DrawPrimitive | ||
// | ||
// Draws the primitives on render target | ||
// | ||
//////////////////////////////////////////////////////////////// | ||
void CPrimitiveBatcher::DrawPrimitive(D3DPRIMITIVETYPE eType, size_t iCollectionSize, const void* pDataAddr, size_t uiVertexStride) | ||
{ | ||
int iSize = 1; | ||
switch (eType) | ||
{ | ||
case D3DPT_POINTLIST: | ||
iSize = iCollectionSize; | ||
break; | ||
case D3DPT_LINELIST: | ||
iSize = iCollectionSize / 2; | ||
break; | ||
case D3DPT_LINESTRIP: | ||
iSize = iCollectionSize - 1; | ||
break; | ||
case D3DPT_TRIANGLEFAN: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. D3DPT_TRIANGLEFANsize needs to be same as D3DPT_TRIANGLESTRIP, so break is unnecessary, it uses shared case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that makes sense. |
||
case D3DPT_TRIANGLESTRIP: | ||
iSize = iCollectionSize - 2; | ||
break; | ||
case D3DPT_TRIANGLELIST: | ||
iSize = iCollectionSize / 3; | ||
break; | ||
} | ||
m_pDevice->DrawPrimitiveUP(eType, iSize, pDataAddr, uiVertexStride); | ||
} | ||
//////////////////////////////////////////////////////////////// | ||
// | ||
// CPrimitiveBatcher::ClearQueue | ||
// | ||
// Clears all primitives in current queue | ||
// | ||
//////////////////////////////////////////////////////////////// | ||
void CPrimitiveBatcher::ClearQueue() | ||
{ | ||
// Clean up | ||
size_t prevSize = m_primitiveList.size(); | ||
m_primitiveList.clear(); | ||
m_primitiveList.reserve(prevSize); | ||
} | ||
//////////////////////////////////////////////////////////////// | ||
// | ||
// CPrimitiveBatcher::AddTriangle | ||
// | ||
// Add a new primitive to the list | ||
// | ||
//////////////////////////////////////////////////////////////// | ||
void CPrimitiveBatcher::AddPrimitive(sDrawQueuePrimitive primitive) | ||
{ | ||
m_primitiveList.push_back(primitive); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a new function
void CPrimitiveMaterialBatcher::SetRenderStates ()
, callm_pDevice->SetRenderState
within that function.