Remove some dependencies on the pD3DDevice globals

This commit is contained in:
Henrik Rydgard 2017-02-05 20:36:00 +01:00
parent 4dd96d6993
commit f00f7d2fb8
14 changed files with 66 additions and 70 deletions

View File

@ -20,8 +20,6 @@
#include "Common/CommonTypes.h"
#include "GPU/ge_constants.h"
#include "GPU/Directx9/helper/global.h"
namespace DX9 {
class DepalShaderDX9 {

View File

@ -37,7 +37,6 @@
#include "GPU/Common/TransformCommon.h"
#include "GPU/Common/VertexDecoderCommon.h"
#include "GPU/Common/SoftwareTransformCommon.h"
#include "GPU/Directx9/StateMappingDX9.h"
#include "GPU/Directx9/TextureCacheDX9.h"
#include "GPU/Directx9/DrawEngineDX9.h"
#include "GPU/Directx9/ShaderManagerDX9.h"
@ -66,7 +65,7 @@ static const int D3DPRIMITIVEVERTEXCOUNT[8][2] = {
{1, 2}, // 6 = D3DPT_TRIANGLEFAN,
};
int D3DPrimCount(D3DPRIMITIVETYPE prim, int size) {
inline int D3DPrimCount(D3DPRIMITIVETYPE prim, int size) {
return (size / D3DPRIMITIVEVERTEXCOUNT[prim][0]) - D3DPRIMITIVEVERTEXCOUNT[prim][1];
}
@ -86,8 +85,9 @@ static const D3DVERTEXELEMENT9 TransformedVertexElements[] = {
D3DDECL_END()
};
DrawEngineDX9::DrawEngineDX9()
: decodedVerts_(0),
DrawEngineDX9::DrawEngineDX9(LPDIRECT3DDEVICE9 device)
: device_(device),
decodedVerts_(0),
prevPrim_(GE_PRIM_INVALID),
lastVType_(-1),
shaderManager_(0),
@ -118,7 +118,7 @@ DrawEngineDX9::DrawEngineDX9()
tessDataTransfer = new TessellationDataTransferDX9();
pD3Ddevice->CreateVertexDeclaration(TransformedVertexElements, &transformedVertexDecl_);
device_->CreateVertexDeclaration(TransformedVertexElements, &transformedVertexDecl_);
}
DrawEngineDX9::~DrawEngineDX9() {
@ -238,7 +238,7 @@ IDirect3DVertexDeclaration9 *DrawEngineDX9::SetupDecFmtForDraw(VSShader *vshader
// Create declaration
IDirect3DVertexDeclaration9 *pHardwareVertexDecl = nullptr;
HRESULT hr = pD3Ddevice->CreateVertexDeclaration( VertexElements, &pHardwareVertexDecl );
HRESULT hr = device_->CreateVertexDeclaration( VertexElements, &pHardwareVertexDecl );
if (FAILED(hr)) {
ERROR_LOG(G3D, "Failed to create vertex declaration!");
pHardwareVertexDecl = nullptr;
@ -689,14 +689,14 @@ void DrawEngineDX9::DoFlush() {
void * pVb;
u32 size = dec_->GetDecVtxFmt().stride * indexGen.MaxIndex();
pD3Ddevice->CreateVertexBuffer(size, D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &vai->vbo, NULL);
device_->CreateVertexBuffer(size, D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &vai->vbo, NULL);
vai->vbo->Lock(0, size, &pVb, 0);
memcpy(pVb, decoded, size);
vai->vbo->Unlock();
if (useElements) {
void * pIb;
u32 size = sizeof(short) * indexGen.VertexCount();
pD3Ddevice->CreateIndexBuffer(size, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &vai->ebo, NULL);
device_->CreateIndexBuffer(size, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &vai->ebo, NULL);
vai->ebo->Lock(0, size, &pIb, 0);
memcpy(pIb, decIndex, size);
vai->ebo->Unlock();
@ -776,22 +776,22 @@ rotateVBO:
IDirect3DVertexDeclaration9 *pHardwareVertexDecl = SetupDecFmtForDraw(vshader, dec_->GetDecVtxFmt(), dec_->VertexType());
if (pHardwareVertexDecl) {
pD3Ddevice->SetVertexDeclaration(pHardwareVertexDecl);
device_->SetVertexDeclaration(pHardwareVertexDecl);
if (vb_ == NULL) {
if (useElements) {
pD3Ddevice->DrawIndexedPrimitiveUP(glprim[prim], 0, maxIndex + 1, D3DPrimCount(glprim[prim], vertexCount), decIndex, D3DFMT_INDEX16, decoded, dec_->GetDecVtxFmt().stride);
device_->DrawIndexedPrimitiveUP(glprim[prim], 0, maxIndex + 1, D3DPrimCount(glprim[prim], vertexCount), decIndex, D3DFMT_INDEX16, decoded, dec_->GetDecVtxFmt().stride);
} else {
pD3Ddevice->DrawPrimitiveUP(glprim[prim], D3DPrimCount(glprim[prim], vertexCount), decoded, dec_->GetDecVtxFmt().stride);
device_->DrawPrimitiveUP(glprim[prim], D3DPrimCount(glprim[prim], vertexCount), decoded, dec_->GetDecVtxFmt().stride);
}
} else {
pD3Ddevice->SetStreamSource(0, vb_, 0, dec_->GetDecVtxFmt().stride);
device_->SetStreamSource(0, vb_, 0, dec_->GetDecVtxFmt().stride);
if (useElements) {
pD3Ddevice->SetIndices(ib_);
device_->SetIndices(ib_);
pD3Ddevice->DrawIndexedPrimitive(glprim[prim], 0, 0, maxIndex + 1, 0, D3DPrimCount(glprim[prim], vertexCount));
device_->DrawIndexedPrimitive(glprim[prim], 0, 0, maxIndex + 1, 0, D3DPrimCount(glprim[prim], vertexCount));
} else {
pD3Ddevice->DrawPrimitive(glprim[prim], 0, D3DPrimCount(glprim[prim], vertexCount));
device_->DrawPrimitive(glprim[prim], 0, D3DPrimCount(glprim[prim], vertexCount));
}
}
}
@ -847,11 +847,11 @@ rotateVBO:
// these spam the gDebugger log.
const int vertexSize = sizeof(transformed[0]);
pD3Ddevice->SetVertexDeclaration(transformedVertexDecl_);
device_->SetVertexDeclaration(transformedVertexDecl_);
if (drawIndexed) {
pD3Ddevice->DrawIndexedPrimitiveUP(glprim[prim], 0, maxIndex, D3DPrimCount(glprim[prim], numTrans), inds, D3DFMT_INDEX16, drawBuffer, sizeof(TransformedVertex));
device_->DrawIndexedPrimitiveUP(glprim[prim], 0, maxIndex, D3DPrimCount(glprim[prim], numTrans), inds, D3DFMT_INDEX16, drawBuffer, sizeof(TransformedVertex));
} else {
pD3Ddevice->DrawPrimitiveUP(glprim[prim], D3DPrimCount(glprim[prim], numTrans), drawBuffer, sizeof(TransformedVertex));
device_->DrawPrimitiveUP(glprim[prim], D3DPrimCount(glprim[prim], numTrans), drawBuffer, sizeof(TransformedVertex));
}
} else if (result.action == SW_CLEAR) {
u32 clearColor = result.color;
@ -870,7 +870,7 @@ rotateVBO:
dxstate.colorMask.set((mask & D3DCLEAR_TARGET) != 0, (mask & D3DCLEAR_TARGET) != 0, (mask & D3DCLEAR_TARGET) != 0, (mask & D3DCLEAR_STENCIL) != 0);
pD3Ddevice->Clear(0, NULL, mask, SwapRB(clearColor), clearDepth, clearColor >> 24);
device_->Clear(0, NULL, mask, SwapRB(clearColor), clearDepth, clearColor >> 24);
int scissorX1 = gstate.getScissorX1();
int scissorY1 = gstate.getScissorY1();

View File

@ -109,7 +109,7 @@ public:
// Handles transform, lighting and drawing.
class DrawEngineDX9 : public DrawEngineCommon {
public:
DrawEngineDX9();
DrawEngineDX9(LPDIRECT3DDEVICE9 device);
virtual ~DrawEngineDX9();
void SubmitPrim(void *verts, void *inds, GEPrimitiveType prim, int vertexCount, u32 vertType, int *bytesRead);
@ -171,6 +171,8 @@ private:
ReliableHashType ComputeHash(); // Reads deferred vertex data.
void MarkUnreliable(VertexArrayInfoDX9 *vai);
LPDIRECT3DDEVICE9 device_;
// Defer all vertex decoding to a Flush, so that we can hash and cache the
// generated buffers without having to redecode them every time.
struct DeferredDrawCall {

View File

@ -37,7 +37,6 @@
#include "GPU/GeDisasm.h"
#include "GPU/Common/FramebufferCommon.h"
#include "GPU/Directx9/helper/global.h"
#include "GPU/Directx9/ShaderManagerDX9.h"
#include "GPU/Directx9/GPU_DX9.h"
#include "GPU/Directx9/FramebufferDX9.h"
@ -394,11 +393,13 @@ static const CommandTableEntry commandTable[] = {
GPU_DX9::CommandInfo GPU_DX9::cmdInfo_[256];
GPU_DX9::GPU_DX9(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
: GPUCommon(gfxCtx, draw) {
: GPUCommon(gfxCtx, draw), drawEngine_((LPDIRECT3DDEVICE9)draw->GetNativeObject(Draw::NativeObject::DEVICE)) {
device_ = (LPDIRECT3DDEVICE9)draw->GetNativeObject(Draw::NativeObject::DEVICE);
deviceEx_ = (LPDIRECT3DDEVICE9EX)draw->GetNativeObject(Draw::NativeObject::DEVICE_EX);
lastVsync_ = g_Config.bVSync ? 1 : 0;
dxstate.SetVSyncInterval(g_Config.bVSync);
shaderManagerDX9_ = new ShaderManagerDX9();
shaderManagerDX9_ = new ShaderManagerDX9(device_);
framebufferManagerDX9_ = new FramebufferManagerDX9(draw);
framebufferManager_ = framebufferManagerDX9_;
textureCacheDX9_ = new TextureCacheDX9(draw);
@ -480,10 +481,10 @@ void GPU_DX9::CheckGPUFeatures() {
D3DCAPS9 caps;
ZeroMemory(&caps, sizeof(caps));
HRESULT result = 0;
if (pD3DdeviceEx) {
result = pD3DdeviceEx->GetDeviceCaps(&caps);
if (deviceEx_) {
result = deviceEx_->GetDeviceCaps(&caps);
} else {
result = pD3Ddevice->GetDeviceCaps(&caps);
result = device_->GetDeviceCaps(&caps);
}
if (FAILED(result)) {
WARN_LOG_REPORT(G3D, "Direct3D9: Failed to get the device caps!");
@ -544,7 +545,7 @@ void GPU_DX9::InitClearInternal() {
if (useNonBufferedRendering) {
dxstate.depthWrite.set(true);
dxstate.colorMask.set(true, true, true, true);
pD3Ddevice->Clear(0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.f, 0);
device_->Clear(0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.f, 0);
}
}
@ -1003,7 +1004,7 @@ bool GPU_DX9::GetCurrentTexture(GPUDebugBuffer &buffer, int level) {
HRESULT hr;
bool success = false;
hr = pD3Ddevice->GetTexture(0, &baseTex);
hr = device_->GetTexture(0, &baseTex);
if (SUCCEEDED(hr) && baseTex != NULL) {
hr = baseTex->QueryInterface(IID_IDirect3DTexture9, (void **)&tex);
if (SUCCEEDED(hr)) {
@ -1018,9 +1019,9 @@ bool GPU_DX9::GetCurrentTexture(GPUDebugBuffer &buffer, int level) {
LPDIRECT3DSURFACE9 renderTarget = nullptr;
hr = tex->GetSurfaceLevel(level, &renderTarget);
if (renderTarget && SUCCEEDED(hr)) {
hr = pD3Ddevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &offscreen, NULL);
hr = device_->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &offscreen, NULL);
if (SUCCEEDED(hr)) {
hr = pD3Ddevice->GetRenderTargetData(renderTarget, offscreen);
hr = device_->GetRenderTargetData(renderTarget, offscreen);
if (SUCCEEDED(hr)) {
hr = offscreen->LockRect(&locked, &rect, D3DLOCK_READONLY);
}

View File

@ -110,6 +110,9 @@ private:
void BeginFrameInternal() override;
void CopyDisplayToOutputInternal() override;
LPDIRECT3DDEVICE9 device_;
LPDIRECT3DDEVICE9EX deviceEx_;
FramebufferManagerDX9 *framebufferManagerDX9_;
TextureCacheDX9 *textureCacheDX9_;
DepalShaderCacheDX9 depalShaderCache_;

View File

@ -75,7 +75,6 @@ PSShader::PSShader(ShaderID id, const char *code) : id_(id), shader(nullptr), fa
}
PSShader::~PSShader() {
pD3Ddevice->SetPixelShader(NULL);
if (shader)
shader->Release();
}
@ -125,7 +124,6 @@ VSShader::VSShader(ShaderID id, const char *code, bool useHWTransform) : id_(id)
}
VSShader::~VSShader() {
pD3Ddevice->SetVertexShader(NULL);
if (shader)
shader->Release();
}
@ -148,7 +146,7 @@ void ShaderManagerDX9::PSSetColorUniform3(int creg, u32 color) {
((color & 0xFF0000) >> 16) * (1.0f / 255.0f),
0.0f
};
pD3Ddevice->SetPixelShaderConstantF(creg, col, 1);
device_->SetPixelShaderConstantF(creg, col, 1);
}
void ShaderManagerDX9::PSSetColorUniform3Alpha255(int creg, u32 color, u8 alpha) {
@ -158,12 +156,12 @@ void ShaderManagerDX9::PSSetColorUniform3Alpha255(int creg, u32 color, u8 alpha)
(float)((color & 0xFF0000) >> 16),
(float)alpha,
};
pD3Ddevice->SetPixelShaderConstantF(creg, col, 1);
device_->SetPixelShaderConstantF(creg, col, 1);
}
void ShaderManagerDX9::PSSetFloat(int creg, float value) {
const float f[4] = { value, 0.0f, 0.0f, 0.0f };
pD3Ddevice->SetPixelShaderConstantF(creg, f, 1);
device_->SetPixelShaderConstantF(creg, f, 1);
}
void ShaderManagerDX9::PSSetFloatArray(int creg, const float *value, int count) {
@ -171,12 +169,12 @@ void ShaderManagerDX9::PSSetFloatArray(int creg, const float *value, int count)
for (int i = 0; i < count; i++) {
f[i] = value[i];
}
pD3Ddevice->SetPixelShaderConstantF(creg, f, 1);
device_->SetPixelShaderConstantF(creg, f, 1);
}
void ShaderManagerDX9::VSSetFloat(int creg, float value) {
const float f[4] = { value, 0.0f, 0.0f, 0.0f };
pD3Ddevice->SetVertexShaderConstantF(creg, f, 1);
device_->SetVertexShaderConstantF(creg, f, 1);
}
void ShaderManagerDX9::VSSetFloatArray(int creg, const float *value, int count) {
@ -184,7 +182,7 @@ void ShaderManagerDX9::VSSetFloatArray(int creg, const float *value, int count)
for (int i = 0; i < count; i++) {
f[i] = value[i];
}
pD3Ddevice->SetVertexShaderConstantF(creg, f, 1);
device_->SetVertexShaderConstantF(creg, f, 1);
}
// Utility
@ -195,18 +193,18 @@ void ShaderManagerDX9::VSSetColorUniform3(int creg, u32 color) {
((color & 0xFF0000) >> 16) / 255.0f,
0.0f
};
pD3Ddevice->SetVertexShaderConstantF(creg, col, 1);
device_->SetVertexShaderConstantF(creg, col, 1);
}
void ShaderManagerDX9::VSSetFloatUniform4(int creg, float data[4]) {
pD3Ddevice->SetVertexShaderConstantF(creg, data, 1);
device_->SetVertexShaderConstantF(creg, data, 1);
}
void ShaderManagerDX9::VSSetFloat24Uniform3(int creg, const u32 data[3]) {
const u32 col[4] = {
data[0] << 8, data[1] << 8, data[2] << 8, 0
};
pD3Ddevice->SetVertexShaderConstantF(creg, (const float *)&col[0], 1);
device_->SetVertexShaderConstantF(creg, (const float *)&col[0], 1);
}
void ShaderManagerDX9::VSSetColorUniform3Alpha(int creg, u32 color, u8 alpha) {
@ -216,7 +214,7 @@ void ShaderManagerDX9::VSSetColorUniform3Alpha(int creg, u32 color, u8 alpha) {
((color & 0xFF0000) >> 16) / 255.0f,
alpha/255.0f
};
pD3Ddevice->SetVertexShaderConstantF(creg, col, 1);
device_->SetVertexShaderConstantF(creg, col, 1);
}
void ShaderManagerDX9::VSSetColorUniform3ExtraFloat(int creg, u32 color, float extra) {
@ -226,26 +224,26 @@ void ShaderManagerDX9::VSSetColorUniform3ExtraFloat(int creg, u32 color, float e
((color & 0xFF0000) >> 16) / 255.0f,
extra
};
pD3Ddevice->SetVertexShaderConstantF(creg, col, 1);
device_->SetVertexShaderConstantF(creg, col, 1);
}
// Utility
void ShaderManagerDX9::VSSetMatrix4x3(int creg, const float *m4x3) {
float m4x4[16];
ConvertMatrix4x3To4x4Transposed(m4x4, m4x3);
pD3Ddevice->SetVertexShaderConstantF(creg, m4x4, 4);
device_->SetVertexShaderConstantF(creg, m4x4, 4);
}
void ShaderManagerDX9::VSSetMatrix4x3_3(int creg, const float *m4x3) {
float m3x4[16];
ConvertMatrix4x3To3x4Transposed(m3x4, m4x3);
pD3Ddevice->SetVertexShaderConstantF(creg, m3x4, 3);
device_->SetVertexShaderConstantF(creg, m3x4, 3);
}
void ShaderManagerDX9::VSSetMatrix(int creg, const float* pMatrix) {
float transp[16];
Transpose4x4(transp, pMatrix);
pD3Ddevice->SetVertexShaderConstantF(creg, transp, 4);
device_->SetVertexShaderConstantF(creg, transp, 4);
}
// Depth in ogl is between -1;1 we need between 0;1 and optionally reverse it
@ -511,7 +509,7 @@ void ShaderManagerDX9::VSUpdateUniforms(u64 dirtyUniforms) {
}
}
ShaderManagerDX9::ShaderManagerDX9() : lastVShader_(nullptr), lastPShader_(nullptr) {
ShaderManagerDX9::ShaderManagerDX9(LPDIRECT3DDEVICE9 device) : device_(device), lastVShader_(nullptr), lastPShader_(nullptr) {
codeBuffer_ = new char[16384];
}
@ -626,8 +624,8 @@ VSShader *ShaderManagerDX9::ApplyShader(int prim, u32 vertType) {
gstate_c.CleanUniforms();
}
pD3Ddevice->SetPixelShader(fs->shader);
pD3Ddevice->SetVertexShader(vs->shader);
device_->SetPixelShader(fs->shader);
device_->SetVertexShader(vs->shader);
lastPShader_ = fs;
lastVShader_ = vs;

View File

@ -78,7 +78,7 @@ protected:
class ShaderManagerDX9 : public ShaderManagerCommon {
public:
ShaderManagerDX9();
ShaderManagerDX9(LPDIRECT3DDEVICE9 device);
~ShaderManagerDX9();
void ClearCache(bool deleteThem); // TODO: deleteThem currently not respected
@ -113,6 +113,8 @@ private:
void Clear();
LPDIRECT3DDEVICE9 device_;
ShaderID lastFSID_;
ShaderID lastVSID_;

View File

@ -22,7 +22,10 @@
#include "Core/System.h"
#include "Core/Config.h"
#include "Core/Reporting.h"
#include "GPU/Directx9/StateMappingDX9.h"
#include "helper/global.h"
#include "helper/dx_state.h"
#include "GPU/Directx9/GPU_DX9.h"
#include "GPU/Directx9/ShaderManagerDX9.h"
#include "GPU/Directx9/TextureCacheDX9.h"

View File

@ -1,5 +0,0 @@
#pragma once
#include "helper/global.h"
#include "helper/dx_state.h"

View File

@ -20,7 +20,6 @@
#include <map>
#include "../Globals.h"
#include "helper/global.h"
#include "helper/dx_fbo.h"
#include "GPU/GPU.h"
#include "GPU/GPUInterface.h"

View File

@ -2,6 +2,7 @@
#include <functional>
#include <string.h>
#include "global.h"
namespace DX9 {
@ -14,7 +15,7 @@ private:
public:
BoolState() : _value(init) {
DirectXState::state_count++;
}
}
inline void set(bool value) {
if (_value != value) {
@ -51,7 +52,7 @@ private:
public:
DxState1() : _state1(state1), p1(p1def) {
DirectXState::state_count++;
}
}
inline void set(DWORD newp1) {
if (p1 != newp1) {
@ -132,7 +133,7 @@ private:
public:
DxState2() : _state1(state1),_state2(state2), p1(p1def), p2(p2def) {
DirectXState::state_count++;
}
}
inline void set(DWORD newp1, DWORD newp2) {
if (p1 != newp1) {
@ -168,8 +169,7 @@ private:
public:
DxState3() : _state1(state1),_state2(state2), _state3(state3),
p1(p1def), p2(p2def), p3(p3def) {
// DirectxState::state_count++;
}
}
inline void set(DWORD newp1, DWORD newp2, DWORD newp3) {
if (p1 != newp1) {
@ -214,7 +214,6 @@ private:
public:
DxState4() : _state1(state1), _state2(state2), _state3(state3), _state4(state4),
p1(p1def), p2(p2def), p3(p3def), p4(p4def) {
// DirectxState::state_count++;
}
inline void set(DWORD newp1, DWORD newp2, DWORD newp3, DWORD newp4) {

View File

@ -212,7 +212,6 @@
<ClInclude Include="Directx9\PixelShaderGeneratorDX9.h" />
<ClInclude Include="Directx9\FramebufferDX9.h" />
<ClInclude Include="Directx9\ShaderManagerDX9.h" />
<ClInclude Include="Directx9\StateMappingDX9.h" />
<ClInclude Include="Directx9\TextureCacheDX9.h" />
<ClInclude Include="Directx9\TextureScalerDX9.h" />
<ClInclude Include="Directx9\DrawEngineDX9.h" />

View File

@ -81,9 +81,6 @@
<ClInclude Include="Directx9\TextureCacheDX9.h">
<Filter>DirectX9</Filter>
</ClInclude>
<ClInclude Include="Directx9\StateMappingDX9.h">
<Filter>DirectX9</Filter>
</ClInclude>
<ClInclude Include="Directx9\ShaderManagerDX9.h">
<Filter>DirectX9</Filter>
</ClInclude>

View File

@ -775,8 +775,8 @@ void NativeRender(GraphicsContext *graphicsContext) {
if (resized) {
resized = false;
graphicsContext->Resize();
// TODO: Move this to new GraphicsContext objects for each backend.
#ifndef _WIN32
if (GetGPUBackend() == GPUBackend::OPENGL) {