mirror of
https://github.com/libretro/ppsspp.git
synced 2025-02-08 18:16:12 +00:00
d3d9: Replace alpha and premultiply blend.
Unfortunately they are fairly tied together.
This commit is contained in:
parent
6e34fe1c4b
commit
856154a5f9
@ -144,9 +144,14 @@ ReplaceAlphaType ReplaceAlphaWithStencil(ReplaceBlendType replaceBlend) {
|
||||
if (nonAlphaSrcFactors[gstate.getBlendFuncA()] && nonAlphaDestFactors[gstate.getBlendFuncB()]) {
|
||||
return REPLACE_ALPHA_YES;
|
||||
} else {
|
||||
// TODO
|
||||
#if 0
|
||||
if (pD3DdeviceEx) {
|
||||
return REPLACE_ALPHA_DUALSOURCE;
|
||||
} else {
|
||||
#else
|
||||
{
|
||||
#endif
|
||||
return REPLACE_ALPHA_NO;
|
||||
}
|
||||
}
|
||||
@ -390,8 +395,6 @@ void ComputeFragmentShaderIDDX9(FragmentShaderIDDX9 *id) {
|
||||
bool doTextureAlpha = gstate.isTextureAlphaUsed();
|
||||
ReplaceBlendType replaceBlend = ReplaceBlendWithShader();
|
||||
ReplaceAlphaType stencilToAlpha = ReplaceAlphaWithStencil(replaceBlend);
|
||||
// This isn't really correct, but it's a hack to get doubled blend modes to work more correctly.
|
||||
bool enableAlphaDoubling = CanDoubleSrcBlendMode();
|
||||
|
||||
// All texfuncs except replace are the same for RGB as for RGBA with full alpha.
|
||||
if (gstate_c.textureFullAlpha && gstate.getTextureFunction() != GE_TEXFUNC_REPLACE)
|
||||
@ -445,9 +448,7 @@ void ComputeFragmentShaderIDDX9(FragmentShaderIDDX9 *id) {
|
||||
gpuStats.numNonAlphaTestedDraws++;
|
||||
|
||||
id0 |= (gstate_c.bgraTexture & 1) << 29;
|
||||
// TODO: Kill.
|
||||
id0 |= (enableAlphaDoubling & 1) << 30;
|
||||
// 31 is free.
|
||||
// 30 and 31 are free.
|
||||
|
||||
// 3 bits.
|
||||
id1 |= replaceBlend << 0;
|
||||
@ -480,15 +481,24 @@ void GenerateFragmentShaderDX9(char *buffer) {
|
||||
bool textureAtOffset = gstate_c.curTextureXOffset != 0 || gstate_c.curTextureYOffset != 0;
|
||||
ReplaceBlendType replaceBlend = ReplaceBlendWithShader();
|
||||
ReplaceAlphaType stencilToAlpha = ReplaceAlphaWithStencil(replaceBlend);
|
||||
// This isn't really correct, but it's a hack to get doubled blend modes to work more correctly.
|
||||
bool enableAlphaDoubling = CanDoubleSrcBlendMode();
|
||||
|
||||
if (gstate_c.textureFullAlpha && gstate.getTextureFunction() != GE_TEXFUNC_REPLACE)
|
||||
doTextureAlpha = false;
|
||||
|
||||
if (doTexture)
|
||||
WRITE(p, "sampler tex: register(s0);\n");
|
||||
|
||||
WRITE(p, "sampler tex : register(s0);\n");
|
||||
if (!gstate.isModeClear() && replaceBlend > REPLACE_BLEND_STANDARD) {
|
||||
if (replaceBlend == REPLACE_BLEND_COPY_FBO) {
|
||||
WRITE(p, "float2 u_fbotexSize : register(c%i);\n", CONST_PS_FBOTEXSIZE);
|
||||
WRITE(p, "sampler fbotex : register(s1);\n");
|
||||
}
|
||||
if (gstate.getBlendFuncA() == GE_SRCBLEND_FIXA) {
|
||||
WRITE(p, "float3 u_blendFixA : register(c%i);\n", CONST_PS_BLENDFIXA);
|
||||
}
|
||||
if (gstate.getBlendFuncB() == GE_DSTBLEND_FIXB) {
|
||||
WRITE(p, "float3 u_blendFixB : register(c%i);\n", CONST_PS_BLENDFIXB);
|
||||
}
|
||||
}
|
||||
if (gstate_c.needShaderTexClamp && doTexture) {
|
||||
WRITE(p, "float4 u_texclamp : register(c%i);\n", CONST_PS_TEXCLAMP);
|
||||
if (textureAtOffset) {
|
||||
@ -500,7 +510,9 @@ void GenerateFragmentShaderDX9(char *buffer) {
|
||||
WRITE(p, "float4 u_alphacolorref : register(c%i);\n", CONST_PS_ALPHACOLORREF);
|
||||
WRITE(p, "float4 u_alphacolormask : register(c%i);\n", CONST_PS_ALPHACOLORMASK);
|
||||
}
|
||||
|
||||
if (stencilToAlpha && ReplaceAlphaWithStencilType() == STENCIL_VALUE_UNIFORM) {
|
||||
WRITE(p, "float u_stencilReplaceValue : register(c%i);\n", CONST_PS_STENCILREPLACE);
|
||||
}
|
||||
if (gstate.isTextureMapEnabled() && gstate.getTextureFunction() == GE_TEXFUNC_BLEND) {
|
||||
WRITE(p, "float3 u_texenv : register(c%i);\n", CONST_PS_TEXENV);
|
||||
}
|
||||
@ -535,7 +547,7 @@ void GenerateFragmentShaderDX9(char *buffer) {
|
||||
|
||||
if (gstate.isModeClear()) {
|
||||
// Clear mode does not allow any fancy shading.
|
||||
WRITE(p, " return In.v_color0;\n");
|
||||
WRITE(p, " float4 v = In.v_color0;\n");
|
||||
} else {
|
||||
const char *secondary = "";
|
||||
// Secondary color for specular on top of texture
|
||||
@ -672,33 +684,113 @@ void GenerateFragmentShaderDX9(char *buffer) {
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// TODO: Before or after the color test?
|
||||
if (enableColorDoubling && enableAlphaDoubling) {
|
||||
WRITE(p, " v = v * 2.0;\n");
|
||||
} else if (enableColorDoubling) {
|
||||
WRITE(p, " v.rgb = v.rgb * 2.0;\n");
|
||||
} else if (enableAlphaDoubling) {
|
||||
WRITE(p, " v.a = v.a * 2.0;\n");
|
||||
}
|
||||
|
||||
if (enableColorTest) {
|
||||
GEComparison colorTestFunc = gstate.getColorTestFunction();
|
||||
const char *colorTestFuncs[] = { "#", "#", " != ", " == " }; // never/always don't make sense
|
||||
u32 colorTestMask = gstate.getColorTestMask();
|
||||
if (colorTestFuncs[colorTestFunc][0] != '#') {
|
||||
const char * test = colorTestFuncs[colorTestFunc];
|
||||
WRITE(p, "float3 colortest = roundAndScaleTo255v(v.rgb);\n");
|
||||
WRITE(p, "if ((colortest.r %s u_alphacolorref.r) && (colortest.g %s u_alphacolorref.g) && (colortest.b %s u_alphacolorref.b )) clip(-1);\n", test, test, test);
|
||||
WRITE(p, " float3 colortest = roundAndScaleTo255v(v.rgb);\n");
|
||||
WRITE(p, " if ((colortest.r %s u_alphacolorref.r) && (colortest.g %s u_alphacolorref.g) && (colortest.b %s u_alphacolorref.b )) clip(-1);\n", test, test, test);
|
||||
} else {
|
||||
WRITE(p, " clip(-1);\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Color doubling happens after the color test.
|
||||
if (enableColorDoubling && replaceBlend == REPLACE_BLEND_2X_SRC) {
|
||||
WRITE(p, " v.rgb = v.rgb * 4.0;\n");
|
||||
} else if (enableColorDoubling || replaceBlend == REPLACE_BLEND_2X_SRC) {
|
||||
WRITE(p, " v.rgb = v.rgb * 2.0;\n");
|
||||
}
|
||||
|
||||
if (enableFog) {
|
||||
WRITE(p, " float fogCoef = clamp(In.v_fogdepth.x, 0.0, 1.0);\n");
|
||||
WRITE(p, " return lerp(float4(u_fogcolor, v.a), v, fogCoef);\n");
|
||||
} else {
|
||||
WRITE(p, " return v;\n");
|
||||
WRITE(p, " v = lerp(float4(u_fogcolor, v.a), v, fogCoef);\n");
|
||||
}
|
||||
|
||||
if (replaceBlend == REPLACE_BLEND_PRE_SRC || replaceBlend == REPLACE_BLEND_PRE_SRC_2X_ALPHA) {
|
||||
GEBlendSrcFactor funcA = gstate.getBlendFuncA();
|
||||
const char *srcFactor = "ERROR";
|
||||
switch (funcA) {
|
||||
case GE_SRCBLEND_DSTCOLOR: srcFactor = "ERROR"; break;
|
||||
case GE_SRCBLEND_INVDSTCOLOR: srcFactor = "ERROR"; break;
|
||||
case GE_SRCBLEND_SRCALPHA: srcFactor = "float3(v.a, v.a, v.a)"; break;
|
||||
case GE_SRCBLEND_INVSRCALPHA: srcFactor = "float3(1.0 - v.a, 1.0 - v.a, 1.0 - v.a)"; break;
|
||||
case GE_SRCBLEND_DSTALPHA: srcFactor = "ERROR"; break;
|
||||
case GE_SRCBLEND_INVDSTALPHA: srcFactor = "ERROR"; break;
|
||||
case GE_SRCBLEND_DOUBLESRCALPHA: srcFactor = "float3(v.a * 2.0, v.a * 2.0, v.a * 2.0)"; break;
|
||||
// TODO: Double inverse, or inverse double? Following softgpu for now...
|
||||
case GE_SRCBLEND_DOUBLEINVSRCALPHA: srcFactor = "float3(1.0 - v.a * 2.0, 1.0 - v.a * 2.0, 1.0 - v.a * 2.0)"; break;
|
||||
case GE_SRCBLEND_DOUBLEDSTALPHA: srcFactor = "ERROR"; break;
|
||||
case GE_SRCBLEND_DOUBLEINVDSTALPHA: srcFactor = "ERROR"; break;
|
||||
case GE_SRCBLEND_FIXA: srcFactor = "u_blendFixA"; break;
|
||||
}
|
||||
|
||||
WRITE(p, " v.rgb = v.rgb * %s;\n", srcFactor);
|
||||
}
|
||||
|
||||
// TODO: Copy FBO
|
||||
|
||||
if (replaceBlend == REPLACE_BLEND_2X_ALPHA || replaceBlend == REPLACE_BLEND_PRE_SRC_2X_ALPHA) {
|
||||
WRITE(p, " v.a = v.a * 2.0;\n");
|
||||
}
|
||||
}
|
||||
|
||||
std::string replacedAlpha = "0.0";
|
||||
char replacedAlphaTemp[64] = "";
|
||||
if (stencilToAlpha != REPLACE_ALPHA_NO) {
|
||||
switch (ReplaceAlphaWithStencilType()) {
|
||||
case STENCIL_VALUE_UNIFORM:
|
||||
replacedAlpha = "u_stencilReplaceValue";
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_ZERO:
|
||||
replacedAlpha = "0.0";
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_ONE:
|
||||
case STENCIL_VALUE_INVERT:
|
||||
// In invert, we subtract by one, but we want to output one here.
|
||||
replacedAlpha = "1.0";
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_INCR_4:
|
||||
case STENCIL_VALUE_DECR_4:
|
||||
// We're adding/subtracting, just by the smallest value in 4-bit.
|
||||
snprintf(replacedAlphaTemp, sizeof(replacedAlphaTemp), "%f", 1.0 / 15.0);
|
||||
replacedAlpha = replacedAlphaTemp;
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_INCR_8:
|
||||
case STENCIL_VALUE_DECR_8:
|
||||
// We're adding/subtracting, just by the smallest value in 8-bit.
|
||||
snprintf(replacedAlphaTemp, sizeof(replacedAlphaTemp), "%f", 1.0 / 255.0);
|
||||
replacedAlpha = replacedAlphaTemp;
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_KEEP:
|
||||
// Do nothing. We'll mask out the alpha using color mask.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (stencilToAlpha) {
|
||||
case REPLACE_ALPHA_DUALSOURCE:
|
||||
WRITE(p, " v.a = %s;\n", replacedAlpha.c_str());
|
||||
// TODO: Output the second color as well using original v.a.
|
||||
break;
|
||||
|
||||
case REPLACE_ALPHA_YES:
|
||||
WRITE(p, " v.a = %s;\n", replacedAlpha.c_str());
|
||||
break;
|
||||
|
||||
case REPLACE_ALPHA_NO:
|
||||
// Do nothing, v is already fine.
|
||||
break;
|
||||
}
|
||||
|
||||
WRITE(p, " return v;\n");
|
||||
WRITE(p, "}\n");
|
||||
}
|
||||
|
||||
|
@ -78,11 +78,6 @@ static const D3DCMPFUNC ztests[] = {
|
||||
D3DCMP_LESS, D3DCMP_LESSEQUAL, D3DCMP_GREATER, D3DCMP_GREATEREQUAL,
|
||||
};
|
||||
|
||||
static const D3DCMPFUNC ztests_backwards[] = {
|
||||
D3DCMP_NEVER, D3DCMP_ALWAYS, D3DCMP_EQUAL, D3DCMP_NOTEQUAL,
|
||||
D3DCMP_GREATER, D3DCMP_GREATEREQUAL, D3DCMP_LESS, D3DCMP_LESSEQUAL,
|
||||
};
|
||||
|
||||
static const D3DSTENCILOP stencilOps[] = {
|
||||
D3DSTENCILOP_KEEP,
|
||||
D3DSTENCILOP_ZERO,
|
||||
@ -94,7 +89,21 @@ static const D3DSTENCILOP stencilOps[] = {
|
||||
D3DSTENCILOP_KEEP, // reserved
|
||||
};
|
||||
|
||||
static u32 blendColor2Func(u32 fix) {
|
||||
static D3DBLEND toDualSource(D3DBLEND blendfunc) {
|
||||
switch (blendfunc) {
|
||||
// TODO
|
||||
#if 0
|
||||
case D3DBLEND_SRCALPHA:
|
||||
return D3DBLEND_SRCCOLOR2;
|
||||
case D3DBLEND_INVSRCALPHA:
|
||||
return D3DBLEND_INVSRCCOLOR2;
|
||||
#endif
|
||||
default:
|
||||
return blendfunc;
|
||||
}
|
||||
}
|
||||
|
||||
static D3DBLEND blendColor2Func(u32 fix) {
|
||||
if (fix == 0xFFFFFF)
|
||||
return D3DBLEND_ONE;
|
||||
if (fix == 0)
|
||||
@ -108,13 +117,339 @@ static u32 blendColor2Func(u32 fix) {
|
||||
return D3DBLEND_UNK;
|
||||
}
|
||||
|
||||
static bool blendColorSimilar(const Vec3f &a, const Vec3f &b, float margin = 0.1f) {
|
||||
static inline bool blendColorSimilar(const Vec3f &a, const Vec3f &b, float margin = 0.1f) {
|
||||
const Vec3f diff = a - b;
|
||||
if (fabsf(diff.x) <= margin && fabsf(diff.y) <= margin && fabsf(diff.z) <= margin)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TransformDrawEngineDX9::ApplyShaderBlending() {
|
||||
bool skipBlit = false;
|
||||
|
||||
static const int MAX_REASONABLE_BLITS_PER_FRAME = 24;
|
||||
|
||||
static int lastFrameBlit = -1;
|
||||
static int blitsThisFrame = 0;
|
||||
if (lastFrameBlit != gpuStats.numFlips) {
|
||||
if (blitsThisFrame > MAX_REASONABLE_BLITS_PER_FRAME) {
|
||||
WARN_LOG_REPORT_ONCE(blendingBlit, G3D, "Lots of blits needed for obscure blending: %d per frame, blend %d/%d/%d", blitsThisFrame, gstate.getBlendFuncA(), gstate.getBlendFuncB(), gstate.getBlendEq());
|
||||
}
|
||||
blitsThisFrame = 0;
|
||||
lastFrameBlit = gpuStats.numFlips;
|
||||
}
|
||||
++blitsThisFrame;
|
||||
if (blitsThisFrame > MAX_REASONABLE_BLITS_PER_FRAME * 2) {
|
||||
WARN_LOG_ONCE(blendingBlit2, G3D, "Skipping additional blits needed for obscure blending: %d per frame, blend %d/%d/%d", blitsThisFrame, gstate.getBlendFuncA(), gstate.getBlendFuncB(), gstate.getBlendEq());
|
||||
ResetShaderBlending();
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Need a param to set stage 1.
|
||||
// framebufferManager_->BindFramebufferColor(NULL);
|
||||
// If we are rendering at a higher resolution, linear is probably best for the dest color.
|
||||
pD3Ddevice->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
||||
pD3Ddevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
||||
fboTexBound_ = true;
|
||||
|
||||
shaderManager_->DirtyUniform(DIRTY_SHADERBLEND);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void TransformDrawEngineDX9::ResetShaderBlending() {
|
||||
if (fboTexBound_) {
|
||||
pD3Ddevice->SetTexture(1, nullptr);
|
||||
fboTexBound_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void TransformDrawEngineDX9::ApplyStencilReplaceOnly() {
|
||||
// We're not blending, but we may still want to blend for stencil.
|
||||
// This is only useful for INCR/DECR/INVERT. Others can write directly.
|
||||
switch (ReplaceAlphaWithStencilType()) {
|
||||
case STENCIL_VALUE_INCR_4:
|
||||
case STENCIL_VALUE_INCR_8:
|
||||
// We'll add the incremented value output by the shader.
|
||||
dxstate.blendFunc.set(D3DBLEND_ONE, D3DBLEND_ZERO, D3DBLEND_ONE, D3DBLEND_ONE);
|
||||
dxstate.blendEquation.set(D3DBLENDOP_ADD, D3DBLENDOP_ADD);
|
||||
dxstate.blend.enable();
|
||||
dxstate.blendSeparate.enable();
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_DECR_4:
|
||||
case STENCIL_VALUE_DECR_8:
|
||||
// We'll subtract the incremented value output by the shader.
|
||||
dxstate.blendFunc.set(D3DBLEND_ONE, D3DBLEND_ZERO, D3DBLEND_ONE, D3DBLEND_ONE);
|
||||
dxstate.blendEquation.set(D3DBLENDOP_ADD, D3DBLENDOP_SUBTRACT);
|
||||
dxstate.blend.enable();
|
||||
dxstate.blendSeparate.enable();
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_INVERT:
|
||||
// The shader will output one, and reverse subtracting will essentially invert.
|
||||
dxstate.blendFunc.set(D3DBLEND_ONE, D3DBLEND_ZERO, D3DBLEND_ONE, D3DBLEND_ONE);
|
||||
dxstate.blendEquation.set(D3DBLENDOP_ADD, D3DBLENDOP_REVSUBTRACT);
|
||||
dxstate.blend.enable();
|
||||
dxstate.blendSeparate.enable();
|
||||
break;
|
||||
|
||||
default:
|
||||
dxstate.blend.disable();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void TransformDrawEngineDX9::ApplyBlendState() {
|
||||
// Blending is a bit complex to emulate. This is due to several reasons:
|
||||
//
|
||||
// * Doubled blend modes (src, dst, inversed) aren't supported in OpenGL.
|
||||
// If possible, we double the src color or src alpha in the shader to account for these.
|
||||
// These may clip incorrectly, so we avoid unfortunately.
|
||||
// * OpenGL only has one arbitrary fixed color. We premultiply the other in the shader.
|
||||
// * The written output alpha should actually be the stencil value. Alpha is not written.
|
||||
//
|
||||
// If we can't apply blending, we make a copy of the framebuffer and do it manually.
|
||||
// TODO
|
||||
gstate_c.allowShaderBlend = false; //!g_Config.bDisableSlowFramebufEffects;
|
||||
|
||||
ReplaceBlendType replaceBlend = ReplaceBlendWithShader();
|
||||
ReplaceAlphaType replaceAlphaWithStencil = ReplaceAlphaWithStencil(replaceBlend);
|
||||
bool usePreSrc = false;
|
||||
|
||||
switch (replaceBlend) {
|
||||
case REPLACE_BLEND_NO:
|
||||
ResetShaderBlending();
|
||||
// We may still want to do something about stencil -> alpha.
|
||||
if (replaceAlphaWithStencil == REPLACE_ALPHA_YES) {
|
||||
ApplyStencilReplaceOnly();
|
||||
} else {
|
||||
dxstate.blend.disable();
|
||||
}
|
||||
return;
|
||||
|
||||
case REPLACE_BLEND_COPY_FBO:
|
||||
if (ApplyShaderBlending()) {
|
||||
// We may still want to do something about stencil -> alpha.
|
||||
if (replaceAlphaWithStencil == REPLACE_ALPHA_YES) {
|
||||
ApplyStencilReplaceOnly();
|
||||
} else {
|
||||
// None of the below logic is interesting, we're gonna do it entirely in the shader.
|
||||
dxstate.blend.disable();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Until next time, force it off.
|
||||
gstate_c.allowShaderBlend = false;
|
||||
break;
|
||||
|
||||
case REPLACE_BLEND_PRE_SRC:
|
||||
case REPLACE_BLEND_PRE_SRC_2X_ALPHA:
|
||||
usePreSrc = true;
|
||||
break;
|
||||
|
||||
case REPLACE_BLEND_STANDARD:
|
||||
case REPLACE_BLEND_2X_ALPHA:
|
||||
case REPLACE_BLEND_2X_SRC:
|
||||
break;
|
||||
}
|
||||
|
||||
dxstate.blend.enable();
|
||||
dxstate.blendSeparate.enable();
|
||||
ResetShaderBlending();
|
||||
|
||||
GEBlendMode blendFuncEq = gstate.getBlendEq();
|
||||
int blendFuncA = gstate.getBlendFuncA();
|
||||
int blendFuncB = gstate.getBlendFuncB();
|
||||
if (blendFuncA > GE_SRCBLEND_FIXA)
|
||||
blendFuncA = GE_SRCBLEND_FIXA;
|
||||
if (blendFuncB > GE_DSTBLEND_FIXB)
|
||||
blendFuncB = GE_DSTBLEND_FIXB;
|
||||
|
||||
float constantAlpha = 1.0f;
|
||||
if (gstate.isStencilTestEnabled() && replaceAlphaWithStencil == REPLACE_ALPHA_NO) {
|
||||
switch (ReplaceAlphaWithStencilType()) {
|
||||
case STENCIL_VALUE_UNIFORM:
|
||||
constantAlpha = (float) gstate.getStencilTestRef() * (1.0f / 255.0f);
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_INCR_4:
|
||||
case STENCIL_VALUE_DECR_4:
|
||||
constantAlpha = 1.0f / 15.0f;
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_INCR_8:
|
||||
case STENCIL_VALUE_DECR_8:
|
||||
constantAlpha = 1.0f / 255.0f;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Shortcut by using D3DBLEND_ONE where possible, no need to set blendcolor
|
||||
D3DBLEND glBlendFuncA = blendFuncA == GE_SRCBLEND_FIXA ? blendColor2Func(gstate.getFixA()) : aLookup[blendFuncA];
|
||||
D3DBLEND glBlendFuncB = blendFuncB == GE_DSTBLEND_FIXB ? blendColor2Func(gstate.getFixB()) : bLookup[blendFuncB];
|
||||
|
||||
if (usePreSrc) {
|
||||
glBlendFuncA = D3DBLEND_ONE;
|
||||
// Need to pull in the fixed color.
|
||||
if (blendFuncA == GE_SRCBLEND_FIXA) {
|
||||
shaderManager_->DirtyUniform(DIRTY_SHADERBLEND);
|
||||
}
|
||||
}
|
||||
|
||||
if (replaceAlphaWithStencil == REPLACE_ALPHA_DUALSOURCE) {
|
||||
glBlendFuncA = toDualSource(glBlendFuncA);
|
||||
glBlendFuncB = toDualSource(glBlendFuncB);
|
||||
}
|
||||
|
||||
if (blendFuncA == GE_SRCBLEND_FIXA || blendFuncB == GE_DSTBLEND_FIXB) {
|
||||
Vec3f fixA = Vec3f::FromRGB(gstate.getFixA());
|
||||
Vec3f fixB = Vec3f::FromRGB(gstate.getFixB());
|
||||
if (glBlendFuncA == D3DBLEND_UNK && glBlendFuncB != D3DBLEND_UNK) {
|
||||
// Can use blendcolor trivially.
|
||||
const float blendColor[4] = {fixA.x, fixA.y, fixA.z, constantAlpha};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
glBlendFuncA = D3DBLEND_BLENDFACTOR;
|
||||
} else if (glBlendFuncA != D3DBLEND_UNK && glBlendFuncB == D3DBLEND_UNK) {
|
||||
// Can use blendcolor trivially.
|
||||
const float blendColor[4] = {fixB.x, fixB.y, fixB.z, constantAlpha};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
glBlendFuncB = D3DBLEND_BLENDFACTOR;
|
||||
} else if (glBlendFuncA == D3DBLEND_UNK && glBlendFuncB == D3DBLEND_UNK) {
|
||||
if (blendColorSimilar(fixA, Vec3f::AssignToAll(constantAlpha) - fixB)) {
|
||||
glBlendFuncA = D3DBLEND_BLENDFACTOR;
|
||||
glBlendFuncB = D3DBLEND_INVBLENDFACTOR;
|
||||
const float blendColor[4] = {fixA.x, fixA.y, fixA.z, constantAlpha};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
} else if (blendColorSimilar(fixA, fixB)) {
|
||||
glBlendFuncA = D3DBLEND_BLENDFACTOR;
|
||||
glBlendFuncB = D3DBLEND_BLENDFACTOR;
|
||||
const float blendColor[4] = {fixA.x, fixA.y, fixA.z, constantAlpha};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
} else {
|
||||
static bool didReportBlend = false;
|
||||
if (!didReportBlend)
|
||||
Reporting::ReportMessage("ERROR INVALID blendcolorstate: FixA=%06x FixB=%06x FuncA=%i FuncB=%i", gstate.getFixA(), gstate.getFixB(), gstate.getBlendFuncA(), gstate.getBlendFuncB());
|
||||
didReportBlend = true;
|
||||
|
||||
DEBUG_LOG(G3D, "ERROR INVALID blendcolorstate: FixA=%06x FixB=%06x FuncA=%i FuncB=%i", gstate.getFixA(), gstate.getFixB(), gstate.getBlendFuncA(), gstate.getBlendFuncB());
|
||||
// Let's approximate, at least. Close is better than totally off.
|
||||
const bool nearZeroA = blendColorSimilar(fixA, Vec3f::AssignToAll(0.0f), 0.25f);
|
||||
const bool nearZeroB = blendColorSimilar(fixB, Vec3f::AssignToAll(0.0f), 0.25f);
|
||||
if (nearZeroA || blendColorSimilar(fixA, Vec3f::AssignToAll(1.0f), 0.25f)) {
|
||||
glBlendFuncA = nearZeroA ? D3DBLEND_ZERO : D3DBLEND_ONE;
|
||||
glBlendFuncB = D3DBLEND_BLENDFACTOR;
|
||||
const float blendColor[4] = {fixB.x, fixB.y, fixB.z, constantAlpha};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
// We need to pick something. Let's go with A as the fixed color.
|
||||
} else {
|
||||
glBlendFuncA = D3DBLEND_BLENDFACTOR;
|
||||
glBlendFuncB = nearZeroB ? D3DBLEND_ZERO : D3DBLEND_ONE;
|
||||
const float blendColor[4] = {fixA.x, fixA.y, fixA.z, constantAlpha};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// We optimized both, but that's probably not necessary, so let's pick one to be constant.
|
||||
// For now let's just pick whichever was fixed instead of checking error.
|
||||
if (blendFuncA == GE_SRCBLEND_FIXA && !usePreSrc) {
|
||||
glBlendFuncA = D3DBLEND_BLENDFACTOR;
|
||||
const float blendColor[4] = {fixA.x, fixA.y, fixA.z, constantAlpha};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
} else {
|
||||
glBlendFuncB = D3DBLEND_BLENDFACTOR;
|
||||
const float blendColor[4] = {fixB.x, fixB.y, fixB.z, constantAlpha};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
}
|
||||
}
|
||||
} else if (constantAlpha < 1.0f) {
|
||||
const float blendColor[4] = {1.0f, 1.0f, 1.0f, constantAlpha};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
}
|
||||
|
||||
// At this point, through all paths above, glBlendFuncA and glBlendFuncB will be set right somehow.
|
||||
|
||||
// The stencil-to-alpha in fragment shader doesn't apply here (blending is enabled), and we shouldn't
|
||||
// do any blending in the alpha channel as that doesn't seem to happen on PSP. So lacking a better option,
|
||||
// the only value we can set alpha to here without multipass and dual source alpha is zero (by setting
|
||||
// the factors to zero). So let's do that.
|
||||
D3DBLENDOP alphaEq = D3DBLENDOP_ADD;
|
||||
if (replaceAlphaWithStencil != REPLACE_ALPHA_NO) {
|
||||
// Let the fragment shader take care of it.
|
||||
switch (ReplaceAlphaWithStencilType()) {
|
||||
case STENCIL_VALUE_INCR_4:
|
||||
case STENCIL_VALUE_INCR_8:
|
||||
// We'll add the increment value.
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ONE, D3DBLEND_ONE);
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_DECR_4:
|
||||
case STENCIL_VALUE_DECR_8:
|
||||
// Like add with a small value, but subtracting.
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ONE, D3DBLEND_ONE);
|
||||
alphaEq = D3DBLENDOP_SUBTRACT;
|
||||
break;
|
||||
|
||||
case STENCIL_VALUE_INVERT:
|
||||
// This will subtract by one, effectively inverting the bits.
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ONE, D3DBLEND_ONE);
|
||||
alphaEq = D3DBLENDOP_REVSUBTRACT;
|
||||
break;
|
||||
|
||||
default:
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ONE, D3DBLEND_ZERO);
|
||||
break;
|
||||
}
|
||||
} else if (gstate.isStencilTestEnabled()) {
|
||||
switch (ReplaceAlphaWithStencilType()) {
|
||||
case STENCIL_VALUE_KEEP:
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ZERO, D3DBLEND_ONE);
|
||||
break;
|
||||
case STENCIL_VALUE_ONE:
|
||||
// This won't give one but it's our best shot...
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ONE, D3DBLEND_ONE);
|
||||
break;
|
||||
case STENCIL_VALUE_ZERO:
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ZERO, D3DBLEND_ZERO);
|
||||
break;
|
||||
case STENCIL_VALUE_UNIFORM:
|
||||
// This won't give a correct value (it multiplies) but it may be better than random values.
|
||||
if (constantAlpha < 1.0f) {
|
||||
// TODO: Does this work as the alpha component of the fixed blend factor?
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_BLENDFACTOR, D3DBLEND_ZERO);
|
||||
} else {
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ONE, D3DBLEND_ZERO);
|
||||
}
|
||||
break;
|
||||
case STENCIL_VALUE_INCR_4:
|
||||
case STENCIL_VALUE_INCR_8:
|
||||
// This won't give a correct value always, but it will try to increase at least.
|
||||
// TODO: Does this work as the alpha component of the fixed blend factor?
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_BLENDFACTOR, D3DBLEND_ONE);
|
||||
break;
|
||||
case STENCIL_VALUE_DECR_4:
|
||||
case STENCIL_VALUE_DECR_8:
|
||||
// This won't give a correct value always, but it will try to decrease at least.
|
||||
// TODO: Does this work as the alpha component of the fixed blend factor?
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_BLENDFACTOR, D3DBLEND_ONE);
|
||||
alphaEq = D3DBLENDOP_SUBTRACT;
|
||||
break;
|
||||
case STENCIL_VALUE_INVERT:
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ONE, D3DBLEND_ONE);
|
||||
// If the output alpha is near 1, this will basically invert. It's our best shot.
|
||||
alphaEq = D3DBLENDOP_REVSUBTRACT;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// Retain the existing value when stencil testing is off.
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ZERO, D3DBLEND_ONE);
|
||||
}
|
||||
|
||||
dxstate.blendEquation.set(eqLookup[blendFuncEq], alphaEq);
|
||||
}
|
||||
|
||||
void TransformDrawEngineDX9::ApplyDrawState(int prim) {
|
||||
// TODO: All this setup is soon so expensive that we'll need dirty flags, or simply do it in the command writes where we detect dirty by xoring. Silly to do all this work on every drawcall.
|
||||
|
||||
@ -128,82 +463,8 @@ void TransformDrawEngineDX9::ApplyDrawState(int prim) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: The top bit of the alpha channel should be written to the stencil bit somehow. This appears to require very expensive multipass rendering :( Alternatively, one could do a
|
||||
// single fullscreen pass that converts alpha to stencil (or 2 passes, to set both the 0 and 1 values) very easily.
|
||||
|
||||
// Set blend
|
||||
bool wantBlend = !gstate.isModeClear() && gstate.isAlphaBlendEnabled();
|
||||
dxstate.blend.set(wantBlend);
|
||||
if (wantBlend) {
|
||||
// This can't be done exactly as there are several PSP blend modes that are impossible to do on OpenGL ES 2.0, and some even on regular OpenGL for desktop.
|
||||
// HOWEVER - we should be able to approximate the 2x modes in the shader, although they will clip wrongly.
|
||||
|
||||
// Examples of seen unimplementable blend states:
|
||||
// Mortal Kombat Unchained: FixA=0000ff FixB=000080 FuncA=10 FuncB=10
|
||||
|
||||
int blendFuncA = gstate.getBlendFuncA();
|
||||
int blendFuncB = gstate.getBlendFuncB();
|
||||
int blendFuncEq = gstate.getBlendEq();
|
||||
if (blendFuncA > GE_SRCBLEND_FIXA) blendFuncA = GE_SRCBLEND_FIXA;
|
||||
if (blendFuncB > GE_DSTBLEND_FIXB) blendFuncB = GE_DSTBLEND_FIXB;
|
||||
|
||||
// Shortcut by using D3DBLEND_ONE where possible, no need to set blendcolor
|
||||
u32 glBlendFuncA = blendFuncA == GE_SRCBLEND_FIXA ? blendColor2Func(gstate.getFixA()) : aLookup[blendFuncA];
|
||||
u32 glBlendFuncB = blendFuncB == GE_DSTBLEND_FIXB ? blendColor2Func(gstate.getFixB()) : bLookup[blendFuncB];
|
||||
if (blendFuncA == GE_SRCBLEND_FIXA || blendFuncB == GE_DSTBLEND_FIXB) {
|
||||
Vec3f fixA = Vec3f::FromRGB(gstate.getFixA());
|
||||
Vec3f fixB = Vec3f::FromRGB(gstate.getFixB());
|
||||
if (glBlendFuncA == D3DBLEND_UNK && glBlendFuncB != D3DBLEND_UNK) {
|
||||
// Can use blendcolor trivially.
|
||||
const float blendColor[4] = {fixA.x, fixA.y, fixA.z, 1.0f};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
glBlendFuncA = D3DBLEND_BLENDFACTOR;
|
||||
} else if (glBlendFuncA != D3DBLEND_UNK && glBlendFuncB == D3DBLEND_UNK) {
|
||||
// Can use blendcolor trivially.
|
||||
const float blendColor[4] = {fixB.x, fixB.y, fixB.z, 1.0f};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
glBlendFuncB = D3DBLEND_BLENDFACTOR;
|
||||
} else if (glBlendFuncA == D3DBLEND_UNK && glBlendFuncB == D3DBLEND_UNK) {
|
||||
if (blendColorSimilar(fixA, Vec3f::AssignToAll(1.0f) - fixB)) {
|
||||
glBlendFuncA = D3DBLEND_BLENDFACTOR;
|
||||
glBlendFuncB = D3DBLEND_INVBLENDFACTOR;
|
||||
const float blendColor[4] = {fixA.x, fixA.y, fixA.z, 1.0f};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
} else if (blendColorSimilar(fixA, fixB)) {
|
||||
glBlendFuncA = D3DBLEND_BLENDFACTOR;
|
||||
glBlendFuncB = D3DBLEND_BLENDFACTOR;
|
||||
const float blendColor[4] = {fixA.x, fixA.y, fixA.z, 1.0f};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
} else {
|
||||
static bool didReportBlend = false;
|
||||
if (!didReportBlend)
|
||||
Reporting::ReportMessage("ERROR INVALID blendcolorstate: FixA=%06x FixB=%06x FuncA=%i FuncB=%i", gstate.getFixA(), gstate.getFixB(), gstate.getBlendFuncA(), gstate.getBlendFuncB());
|
||||
didReportBlend = true;
|
||||
|
||||
DEBUG_LOG(HLE, "ERROR INVALID blendcolorstate: FixA=%06x FixB=%06x FuncA=%i FuncB=%i", gstate.getFixA(), gstate.getFixB(), gstate.getBlendFuncA(), gstate.getBlendFuncB());
|
||||
// Let's approximate, at least. Close is better than totally off.
|
||||
const bool nearZeroA = blendColorSimilar(fixA, Vec3f::AssignToAll(0.0f), 0.25f);
|
||||
const bool nearZeroB = blendColorSimilar(fixB, Vec3f::AssignToAll(0.0f), 0.25f);
|
||||
if (nearZeroA || blendColorSimilar(fixA, Vec3f::AssignToAll(1.0f), 0.25f)) {
|
||||
glBlendFuncA = nearZeroA ? D3DBLEND_ZERO : D3DBLEND_ONE;
|
||||
glBlendFuncB = D3DBLEND_BLENDFACTOR;
|
||||
const float blendColor[4] = {fixB.x, fixB.y, fixB.z, 1.0f};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
// We need to pick something. Let's go with A as the fixed color.
|
||||
} else {
|
||||
glBlendFuncA = D3DBLEND_BLENDFACTOR;
|
||||
glBlendFuncB = nearZeroB ? D3DBLEND_ZERO : D3DBLEND_ONE;
|
||||
const float blendColor[4] = {fixA.x, fixA.y, fixA.z, 1.0f};
|
||||
dxstate.blendColor.set(blendColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// At this point, through all paths above, glBlendFuncA and glBlendFuncB will be set right somehow.
|
||||
dxstate.blendFunc.set(glBlendFuncA, glBlendFuncB, D3DBLEND_ONE, D3DBLEND_ZERO);
|
||||
dxstate.blendEquation.set(eqLookup[blendFuncEq], D3DBLENDOP_ADD);
|
||||
}
|
||||
// Set blend - unless we need to do it in the shader.
|
||||
ApplyBlendState();
|
||||
|
||||
// Set Dither
|
||||
if (gstate.isDitherEnabled()) {
|
||||
|
@ -95,7 +95,8 @@ TransformDrawEngineDX9::TransformDrawEngineDX9()
|
||||
vertexCountInDrawCalls(0),
|
||||
decodeCounter_(0),
|
||||
dcid_(0),
|
||||
uvScale(0) {
|
||||
uvScale(0),
|
||||
fboTexBound_(false) {
|
||||
|
||||
memset(&decOptions_, 0, sizeof(decOptions_));
|
||||
decOptions_.expandAllUVtoFloat = true;
|
||||
|
@ -182,6 +182,10 @@ private:
|
||||
|
||||
void ApplyDrawState(int prim);
|
||||
void ApplyDrawStateLate();
|
||||
void ApplyBlendState();
|
||||
void ApplyStencilReplaceOnly();
|
||||
bool ApplyShaderBlending();
|
||||
inline void ResetShaderBlending();
|
||||
|
||||
IDirect3DVertexDeclaration9 *SetupDecFmtForDraw(VSShader *vshader, const DecVtxFormat &decFmt, u32 pspFmt);
|
||||
|
||||
@ -241,6 +245,7 @@ private:
|
||||
|
||||
UVScale *uvScale;
|
||||
|
||||
bool fboTexBound_;
|
||||
VertexDecoderOptions decOptions_;
|
||||
};
|
||||
|
||||
|
@ -21,6 +21,7 @@ void DirectxState::Restore() {
|
||||
int count = 0;
|
||||
|
||||
blend.restore(); count++;
|
||||
blendSeparate.restore(); count++;
|
||||
blendEquation.restore(); count++;
|
||||
blendFunc.restore(); count++;
|
||||
blendColor.restore(); count++;
|
||||
|
@ -355,6 +355,7 @@ public:
|
||||
|
||||
// When adding a state here, don't forget to add it to DirectxState::Restore() too
|
||||
BoolState<D3DRS_ALPHABLENDENABLE, false> blend;
|
||||
BoolState<D3DRS_SEPARATEALPHABLENDENABLE, false> blendSeparate;
|
||||
DxState4<D3DRS_SRCBLEND, D3DBLEND_SRCALPHA, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA, D3DRS_SRCBLENDALPHA, D3DBLEND_ONE, D3DRS_DESTBLENDALPHA, D3DBLEND_ZERO> blendFunc;
|
||||
DxState2<D3DRS_BLENDOP, D3DBLENDOP_ADD, D3DRS_BLENDOPALPHA, D3DBLENDOP_ADD> blendEquation;
|
||||
SavedBlendFactor blendColor;
|
||||
|
@ -31,6 +31,6 @@ bool CompileVertexShader(const char *code, LPDIRECT3DVERTEXSHADER9 *pShader, ID3
|
||||
void DestroyShaders();
|
||||
void DirectxInit(HWND window);
|
||||
|
||||
#define D3DBLEND_UNK D3DSTENCILOP_FORCE_DWORD
|
||||
#define D3DBLEND_UNK D3DBLEND_FORCE_DWORD
|
||||
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user