Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #6791 from unknownbrackets/d3d
d3d: Enable state caching, fix another color issue, fix scale
  • Loading branch information
hrydgard committed Aug 26, 2014
2 parents 1f760e6 + 2191382 commit 141d3b6
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 63 deletions.
4 changes: 2 additions & 2 deletions GPU/Directx9/FramebufferDX9.cpp
Expand Up @@ -647,12 +647,12 @@ namespace DX9 {
if (g_Config.iBufFilter == SCALE_LINEAR) {
dxstate.texMagFilter.set(D3DTEXF_LINEAR);
dxstate.texMinFilter.set(D3DTEXF_LINEAR);
dxstate.texMipFilter.set(D3DTEXF_NONE);
} else {
dxstate.texMagFilter.set(D3DTEXF_POINT);
dxstate.texMinFilter.set(D3DTEXF_POINT);
dxstate.texMipFilter.set(D3DTEXF_NONE);
}
dxstate.texMipFilter.set(D3DTEXF_NONE);
dxstate.texMipLodBias.set(0);
DrawActiveTexture(colorTexture, x, y, w, h, (float)PSP_CoreParameter().pixelWidth, (float)PSP_CoreParameter().pixelHeight, false, 480.0f / (float)vfb->width, 272.0f / (float)vfb->height);
}
/*
Expand Down
10 changes: 4 additions & 6 deletions GPU/Directx9/TextureCacheDX9.cpp
Expand Up @@ -491,15 +491,13 @@ void TextureCacheDX9::UpdateSamplingParams(TexCacheEntry &entry, bool force) {

bool noMip = (gstate.texlevel & 0xFFFFFF) == 0x000001 || (gstate.texlevel & 0xFFFFFF) == 0x100001 ; // Fix texlevel at 0

float lodBias = 0.0;
if (entry.maxLevel == 0) {
// Enforce no mip filtering, for safety.
minFilt &= 1; // no mipmaps yet
} else {
// TODO: Is this a signed value? Which direction?
float lodBias = 0.0; // -(float)((gstate.texlevel >> 16) & 0xFF) / 16.0f;
if (force || entry.lodBias != lodBias) {
entry.lodBias = lodBias;
}
// Texture lod bias should be signed.
lodBias = (float)(int)(s8)((gstate.texlevel >> 16) & 0xFF) / 16.0f;
}

if ((g_Config.iTexFiltering == LINEAR || (g_Config.iTexFiltering == LINEARFMV && g_iNumVideos)) && !gstate.isColorTestEnabled()) {
Expand All @@ -519,6 +517,7 @@ void TextureCacheDX9::UpdateSamplingParams(TexCacheEntry &entry, bool force) {

dxstate.texMinFilter.set(MinFilt[minFilt]);
dxstate.texMipFilter.set(MipFilt[minFilt]);
dxstate.texMipLodBias.set(lodBias);
dxstate.texMagFilter.set(MagFilt[magFilt]);
dxstate.texAddressU.set(sClamp ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP);
dxstate.texAddressV.set(tClamp ? D3DTADDRESS_CLAMP : D3DTADDRESS_WRAP);
Expand Down Expand Up @@ -939,7 +938,6 @@ void TextureCacheDX9::SetTexture(bool force) {
entry->lastFrame = gpuStats.numFlips;
entry->framebuffer = 0;
entry->maxLevel = maxLevel;
entry->lodBias = 0.0f;

entry->dim = gstate.getTextureDimension(0);
entry->bufw = bufw;
Expand Down
1 change: 0 additions & 1 deletion GPU/Directx9/TextureCacheDX9.h
Expand Up @@ -104,7 +104,6 @@ class TextureCacheDX9
u32 fullhash;
u32 cluthash;
int maxLevel;
float lodBias;

bool Matches(u16 dim2, u8 format2, int maxLevel2);
void ReleaseTexture() {
Expand Down
6 changes: 1 addition & 5 deletions GPU/Directx9/TransformPipelineDX9.cpp
Expand Up @@ -224,11 +224,7 @@ static const DeclTypeInfo VComp[] = {
static void VertexAttribSetup(D3DVERTEXELEMENT9 * VertexElement, u8 fmt, u8 offset, u8 usage, u8 usage_index = 0) {
memset(VertexElement, 0, sizeof(D3DVERTEXELEMENT9));
VertexElement->Offset = offset;
if (usage == D3DDECLUSAGE_COLOR && fmt == DEC_U8_4) {
VertexElement->Type = D3DDECLTYPE_D3DCOLOR;
} else {
VertexElement->Type = VComp[fmt].type;
}
VertexElement->Type = VComp[fmt].type;
VertexElement->Usage = usage;
VertexElement->UsageIndex = usage_index;
}
Expand Down
6 changes: 3 additions & 3 deletions GPU/Directx9/VertexDecoderDX9.cpp
Expand Up @@ -494,7 +494,7 @@ void VertexDecoderDX9::Step_PosS8() const
float *v = (float *)(decoded_ + decFmt.posoff);
const s8 *sv = (const s8*)(ptr_ + posoff);
for (int j = 0; j < 3; j++)
v[j] = (float)sv[j] * 1.0f / 127.0f;
v[j] = (float)sv[j] * (1.0f / 128.0f);
v[3] = 0;
#endif
}
Expand Down Expand Up @@ -569,7 +569,7 @@ void VertexDecoderDX9::Step_PosS8Morph() const
float *v = (float *)(decoded_ + decFmt.posoff);
memset(v, 0, sizeof(float) * 3);
for (int n = 0; n < morphcount; n++) {
float multiplier = 1.0f / 127.0f;
float multiplier = 1.0f / 128.0f;
const s8 *sv = (const s8*)(ptr_ + onesize_*n + posoff);
for (int j = 0; j < 3; j++)
v[j] += (float)sv[j] * (multiplier * gstate_c.morphWeights[n]);
Expand All @@ -581,7 +581,7 @@ void VertexDecoderDX9::Step_PosS16Morph() const
float *v = (float *)(decoded_ + decFmt.posoff);
memset(v, 0, sizeof(float) * 3);
for (int n = 0; n < morphcount; n++) {
float multiplier = 1.0f / 32767.0f;
float multiplier = 1.0f / 32768.0f;
const s16_le *sv = (const s16_le*)(ptr_ + onesize_*n + posoff);
for (int j = 0; j < 3; j++)
v[j] += (float)sv[j] * (multiplier * gstate_c.morphWeights[n]);
Expand Down
1 change: 1 addition & 0 deletions GPU/Directx9/helper/dx_state.cpp
Expand Up @@ -54,6 +54,7 @@ void DirectxState::Restore() {
texMinFilter.restore(); count++;
texMagFilter.restore(); count++;
texMipFilter.restore(); count++;
texMipLodBias.restore(); count++;
texAddressU.restore(); count++;
texAddressV.restore(); count++;

Expand Down
159 changes: 113 additions & 46 deletions GPU/Directx9/helper/dx_state.h
Expand Up @@ -17,8 +17,10 @@ class DirectxState {
}

inline void set(bool value) {
_value = value;
pD3Ddevice->SetRenderState(cap, value);
if (_value != value) {
_value = value;
pD3Ddevice->SetRenderState(cap, value);
}
}
inline void enable() {
set(true);
Expand Down Expand Up @@ -47,8 +49,10 @@ class DirectxState {
}

inline void set(DWORD newp1) {
p1 = newp1;
pD3Ddevice->SetRenderState(_state1, p1);
if (p1 != newp1) {
p1 = newp1;
pD3Ddevice->SetRenderState(_state1, p1);
}
}
void restore() {
pD3Ddevice->SetRenderState(_state1, p1);
Expand All @@ -65,14 +69,40 @@ class DirectxState {
}

inline void set(DWORD newp1) {
p1 = newp1;
pD3Ddevice->SetSamplerState(0, _state1, p1);
if (p1 != newp1) {
p1 = newp1;
pD3Ddevice->SetSamplerState(0, _state1, p1);
}
}
void restore() {
pD3Ddevice->SetSamplerState(0, _state1, p1);
}
};

// Can't have FLOAT template parameters...
template<D3DSAMPLERSTATETYPE state1, DWORD p1def>
class DxSampler0State1Float {
D3DSAMPLERSTATETYPE _state1;
union {
FLOAT p1;
DWORD p1d;
};
public:
DxSampler0State1Float() : _state1(state1), p1d(p1def) {
DirectxState::state_count++;
}

inline void set(FLOAT newp1) {
if (p1 != newp1) {
p1 = newp1;
pD3Ddevice->SetSamplerState(0, _state1, p1d);
}
}
void restore() {
pD3Ddevice->SetSamplerState(0, _state1, p1d);
}
};

template<D3DRENDERSTATETYPE state1, DWORD p1def, D3DRENDERSTATETYPE state2, DWORD p2def>
class DxState2 {
D3DRENDERSTATETYPE _state1;
Expand All @@ -85,10 +115,14 @@ class DirectxState {
}

inline void set(DWORD newp1, DWORD newp2) {
p1 = newp1;
p2 = newp2;
pD3Ddevice->SetRenderState(_state1, p1);
pD3Ddevice->SetRenderState(_state2, p2);
if (p1 != newp1) {
p1 = newp1;
pD3Ddevice->SetRenderState(_state1, p1);
}
if (p2 != newp2) {
p2 = newp2;
pD3Ddevice->SetRenderState(_state2, p2);
}
}
void restore() {
pD3Ddevice->SetRenderState(_state1, p1);
Expand All @@ -111,12 +145,18 @@ class DirectxState {
}

inline void set(DWORD newp1, DWORD newp2, DWORD newp3) {
p1 = newp1;
p2 = newp2;
p3 = newp3;
pD3Ddevice->SetRenderState(_state1, p1);
pD3Ddevice->SetRenderState(_state2, p2);
pD3Ddevice->SetRenderState(_state3, p3);
if (p1 != newp1) {
p1 = newp1;
pD3Ddevice->SetRenderState(_state1, p1);
}
if (p2 != newp2) {
p2 = newp2;
pD3Ddevice->SetRenderState(_state2, p2);
}
if (p3 != newp3) {
p3 = newp3;
pD3Ddevice->SetRenderState(_state3, p3);
}
}
void restore() {
pD3Ddevice->SetRenderState(_state1, p1);
Expand All @@ -142,14 +182,22 @@ class DirectxState {
}

inline void set(DWORD newp1, DWORD newp2, DWORD newp3, DWORD newp4) {
p1 = newp1;
p2 = newp2;
p3 = newp3;
p4 = newp4;
pD3Ddevice->SetRenderState(_state1, p1);
pD3Ddevice->SetRenderState(_state2, p2);
pD3Ddevice->SetRenderState(_state3, p3);
pD3Ddevice->SetRenderState(_state4, p4);
if (p1 != newp1) {
p1 = newp1;
pD3Ddevice->SetRenderState(_state1, p1);
}
if (p2 != newp2) {
p2 = newp2;
pD3Ddevice->SetRenderState(_state2, p2);
}
if (p3 != newp3) {
p3 = newp3;
pD3Ddevice->SetRenderState(_state3, p3);
}
if (p4 != newp4) {
p4 = newp4;
pD3Ddevice->SetRenderState(_state4, p4);
}
}
void restore() {
pD3Ddevice->SetRenderState(_state1, p1);
Expand All @@ -168,8 +216,11 @@ class DirectxState {
DirectxState::state_count++;
}
inline void set(const float v[4]) {
c = D3DCOLOR_COLORVALUE(v[0], v[1], v[2], v[3]);
pD3Ddevice->SetRenderState(D3DRS_BLENDFACTOR, c);
DWORD newc = D3DCOLOR_COLORVALUE(v[0], v[1], v[2], v[3]);
if (c != newc) {
c = newc;
pD3Ddevice->SetRenderState(D3DRS_BLENDFACTOR, c);
}
}
inline void restore() {
pD3Ddevice->SetRenderState(D3DRS_BLENDFACTOR, c);
Expand All @@ -185,20 +236,23 @@ class DirectxState {
}

inline void set(bool r, bool g, bool b, bool a) {
mask = 0;
DWORD newmask = 0;
if (r) {
mask |= D3DCOLORWRITEENABLE_RED;
newmask |= D3DCOLORWRITEENABLE_RED;
}
if (g) {
mask |= D3DCOLORWRITEENABLE_GREEN;
newmask |= D3DCOLORWRITEENABLE_GREEN;
}
if (b) {
mask |= D3DCOLORWRITEENABLE_BLUE;
newmask |= D3DCOLORWRITEENABLE_BLUE;
}
if (a) {
mask |= D3DCOLORWRITEENABLE_ALPHA;
newmask |= D3DCOLORWRITEENABLE_ALPHA;
}
if (mask != newmask) {
mask = newmask;
pD3Ddevice->SetRenderState(D3DRS_COLORWRITEENABLE, mask);
}
pD3Ddevice->SetRenderState(D3DRS_COLORWRITEENABLE, mask);

}
inline void restore() {
Expand Down Expand Up @@ -231,14 +285,18 @@ class DirectxState {
D3DVIEWPORT9 viewport;
public:
inline void set(int x, int y, int w, int h, float n = 0.f, float f = 1.f) {
viewport.X=x;
viewport.Y=y;
viewport.Width=w;
viewport.Height=h;
viewport.MinZ=n;
viewport.MaxZ=f;

pD3Ddevice->SetViewport(&viewport);
D3DVIEWPORT9 newviewport;
newviewport.X = x;
newviewport.Y = y;
newviewport.Width = w;
newviewport.Height = h;
newviewport.MinZ = n;
newviewport.MaxZ = f;

if (memcmp(&viewport, &newviewport, sizeof(viewport))) {
viewport = newviewport;
pD3Ddevice->SetViewport(&viewport);
}
}

inline void restore() {
Expand All @@ -247,29 +305,37 @@ class DirectxState {
};

class StateScissor {

RECT rect;
public:
inline void set(int x1, int y1, int x2, int y2) {
RECT rect = {x1, y1, x2, y2};
pD3Ddevice->SetScissorRect(&rect);
RECT newrect = {x1, y1, x2, y2};
if (memcmp(&rect, &newrect, sizeof(rect))) {
rect = newrect;
pD3Ddevice->SetScissorRect(&rect);
}
}

inline void restore() {
pD3Ddevice->SetScissorRect(&rect);
}
};

class CullMode {
DWORD cull;
public:
inline void set(int wantcull, int cullmode) {
DWORD newcull;
if (!wantcull) {
// disable
cull = D3DCULL_NONE;
newcull = D3DCULL_NONE;
} else {
// add front face ...
cull = cullmode==0 ? D3DCULL_CW:D3DCULL_CCW;
newcull = cullmode==0 ? D3DCULL_CW:D3DCULL_CCW;
}
if (cull != newcull) {
cull = newcull;
pD3Ddevice->SetRenderState(D3DRS_CULLMODE, cull);
}
pD3Ddevice->SetRenderState(D3DRS_CULLMODE, cull);
}
inline void restore() {
pD3Ddevice->SetRenderState(D3DRS_CULLMODE, cull);
Expand Down Expand Up @@ -318,6 +384,7 @@ class DirectxState {
DxSampler0State1<D3DSAMP_MINFILTER, D3DTEXF_POINT> texMinFilter;
DxSampler0State1<D3DSAMP_MAGFILTER, D3DTEXF_POINT> texMagFilter;
DxSampler0State1<D3DSAMP_MIPFILTER, D3DTEXF_NONE> texMipFilter;
DxSampler0State1Float<D3DSAMP_MIPMAPLODBIAS, 0> texMipLodBias;
DxSampler0State1<D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP> texAddressU;
DxSampler0State1<D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP> texAddressV;

Expand Down

0 comments on commit 141d3b6

Please sign in to comment.