CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/GPU/Directx9/StateMappingDX9.cpp
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "Common/Profiler/Profiler.h"18#include "Common/GPU/D3D9/D3D9ShaderCompiler.h"19#include "Common/GPU/D3D9/D3D9StateCache.h"2021#include "Core/System.h"22#include "Core/Config.h"2324#include "GPU/Math3D.h"25#include "GPU/GPUState.h"26#include "GPU/ge_constants.h"2728#include "GPU/Directx9/GPU_DX9.h"29#include "GPU/Directx9/ShaderManagerDX9.h"30#include "GPU/Directx9/TextureCacheDX9.h"31#include "GPU/Directx9/FramebufferManagerDX9.h"3233static const D3DBLEND dxBlendFactorLookup[(size_t)BlendFactor::COUNT] = {34D3DBLEND_ZERO,35D3DBLEND_ONE,36D3DBLEND_SRCCOLOR,37D3DBLEND_INVSRCCOLOR,38D3DBLEND_DESTCOLOR,39D3DBLEND_INVDESTCOLOR,40D3DBLEND_SRCALPHA,41D3DBLEND_INVSRCALPHA,42D3DBLEND_DESTALPHA,43D3DBLEND_INVDESTALPHA,44D3DBLEND_BLENDFACTOR,45D3DBLEND_INVBLENDFACTOR,46D3DBLEND_BLENDFACTOR,47D3DBLEND_INVBLENDFACTOR,48#if 0 // TODO: Requires D3D9Ex49D3DBLEND_SRCCOLOR2,50D3DBLEND_INVSRCCOLOR2,51D3DBLEND_SRCCOLOR2,52D3DBLEND_INVSRCCOLOR2,53#else54D3DBLEND_FORCE_DWORD,55D3DBLEND_FORCE_DWORD,56#endif57D3DBLEND_FORCE_DWORD,58};5960static const D3DBLENDOP dxBlendEqLookup[(size_t)BlendEq::COUNT] = {61D3DBLENDOP_ADD,62D3DBLENDOP_SUBTRACT,63D3DBLENDOP_REVSUBTRACT,64D3DBLENDOP_MIN,65D3DBLENDOP_MAX,66};6768static const D3DCULL cullingMode[] = {69D3DCULL_CW,70D3DCULL_CCW,71};7273static const D3DCMPFUNC ztests[] = {74D3DCMP_NEVER, D3DCMP_ALWAYS, D3DCMP_EQUAL, D3DCMP_NOTEQUAL,75D3DCMP_LESS, D3DCMP_LESSEQUAL, D3DCMP_GREATER, D3DCMP_GREATEREQUAL,76};7778static const D3DSTENCILOP stencilOps[] = {79D3DSTENCILOP_KEEP,80D3DSTENCILOP_ZERO,81D3DSTENCILOP_REPLACE,82D3DSTENCILOP_INVERT,83D3DSTENCILOP_INCRSAT,84D3DSTENCILOP_DECRSAT,85D3DSTENCILOP_KEEP, // reserved86D3DSTENCILOP_KEEP, // reserved87};8889void DrawEngineDX9::ApplyDrawState(int prim) {90if (!gstate_c.IsDirty(DIRTY_BLEND_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE)) {91// nothing to do92return;93}9495// At this point, we know if the vertices are full alpha or not.96// TODO: Set the nearest/linear here (since we correctly know if alpha/color tests are needed)?97if (!gstate.isModeClear()) {98textureCache_->ApplyTexture();99100if (fboTexBindState_ == FBO_TEX_COPY_BIND_TEX) {101// Note that this is positions, not UVs, that we need the copy from.102framebufferManager_->BindFramebufferAsColorTexture(1, framebufferManager_->GetCurrentRenderVFB(), BINDFBCOLOR_MAY_COPY, 0);103// If we are rendering at a higher resolution, linear is probably best for the dest color.104device_->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);105device_->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);106fboTexBound_ = true;107fboTexBindState_ = FBO_TEX_NONE;108}109110// TODO: Test texture?111}112113bool useBufferedRendering = framebufferManager_->UseBufferedRendering();114115if (gstate_c.IsDirty(DIRTY_BLEND_STATE)) {116if (gstate.isModeClear()) {117dxstate.blend.disable();118// Color Mask119u32 mask = 0;120if (gstate.isClearModeColorMask()) {121mask |= 7;122}123if (gstate.isClearModeAlphaMask()) {124mask |= 8;125}126dxstate.colorMask.set(mask);127} else {128pipelineState_.Convert(draw_->GetShaderLanguageDesc().bitwiseOps);129GenericMaskState &maskState = pipelineState_.maskState;130GenericBlendState &blendState = pipelineState_.blendState;131// We ignore the logicState on D3D since there's no support, the emulation of it is blend-and-shader only.132133if (pipelineState_.FramebufferRead()) {134ApplyFramebufferRead(&fboTexBindState_);135// The shader takes over the responsibility for blending, so recompute.136ApplyStencilReplaceAndLogicOpIgnoreBlend(blendState.replaceAlphaWithStencil, blendState);137138if (fboTexBindState_ == FBO_TEX_COPY_BIND_TEX) {139// Note that this is positions, not UVs, that we need the copy from.140framebufferManager_->BindFramebufferAsColorTexture(1, framebufferManager_->GetCurrentRenderVFB(), BINDFBCOLOR_MAY_COPY | BINDFBCOLOR_UNCACHED, Draw::ALL_LAYERS);141// If we are rendering at a higher resolution, linear is probably best for the dest color.142device_->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);143device_->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);144fboTexBound_ = true;145fboTexBindState_ = FBO_TEX_NONE;146dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE;147gstate_c.Dirty(DIRTY_BLEND_STATE);148} else if (fboTexBindState_ == FBO_TEX_READ_FRAMEBUFFER) {149// Not supported.150fboTexBindState_ = FBO_TEX_NONE;151}152153dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE;154gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE);155} else {156if (fboTexBound_) {157dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE;158gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE);159fboTexBound_ = false;160}161}162163if (blendState.blendEnabled) {164dxstate.blend.enable();165dxstate.blendSeparate.enable();166dxstate.blendEquation.set(dxBlendEqLookup[(size_t)blendState.eqColor], dxBlendEqLookup[(size_t)blendState.eqAlpha]);167dxstate.blendFunc.set(168dxBlendFactorLookup[(size_t)blendState.srcColor], dxBlendFactorLookup[(size_t)blendState.dstColor],169dxBlendFactorLookup[(size_t)blendState.srcAlpha], dxBlendFactorLookup[(size_t)blendState.dstAlpha]);170if (blendState.dirtyShaderBlendFixValues) {171dirtyRequiresRecheck_ |= DIRTY_SHADERBLEND;172gstate_c.Dirty(DIRTY_SHADERBLEND);173}174if (blendState.useBlendColor) {175dxstate.blendColor.setDWORD(blendState.blendColor);176}177} else {178dxstate.blend.disable();179}180181dxstate.colorMask.set(maskState.channelMask);182}183}184185if (gstate_c.IsDirty(DIRTY_RASTER_STATE)) {186bool wantCull = !gstate.isModeClear() && prim != GE_PRIM_RECTANGLES && prim > GE_PRIM_LINE_STRIP && gstate.isCullEnabled();187if (wantCull) {188if (gstate.getCullMode() == 1) {189dxstate.cullMode.set(D3DCULL_CCW);190} else {191dxstate.cullMode.set(D3DCULL_CW);192}193} else {194dxstate.cullMode.set(D3DCULL_NONE);195}196if (gstate.isModeClear()) {197// Well, probably doesn't matter...198dxstate.shadeMode.set(D3DSHADE_GOURAUD);199} else {200dxstate.shadeMode.set(gstate.getShadeMode() == GE_SHADE_GOURAUD ? D3DSHADE_GOURAUD : D3DSHADE_FLAT);201}202203// We use fixed-function user clipping on D3D9, where available, for negative Z clipping.204if (draw_->GetDeviceCaps().clipPlanesSupported >= 1) {205bool wantClip = !gstate.isModeThrough() && gstate_c.submitType == SubmitType::DRAW;206dxstate.clipPlaneEnable.set(wantClip ? 1 : 0);207}208}209210if (gstate_c.IsDirty(DIRTY_DEPTHSTENCIL_STATE)) {211GenericStencilFuncState stencilState;212ConvertStencilFuncState(stencilState);213214// Set Stencil/Depth215if (gstate.isModeClear()) {216// Depth Test217dxstate.depthTest.enable();218dxstate.depthFunc.set(D3DCMP_ALWAYS);219dxstate.depthWrite.set(gstate.isClearModeDepthMask());220221// Stencil Test222bool alphaMask = gstate.isClearModeAlphaMask();223if (alphaMask) {224dxstate.stencilTest.enable();225dxstate.stencilOp.set(D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE);226dxstate.stencilFunc.set(D3DCMP_ALWAYS);227dxstate.stencilRef.set(0xFF);228dxstate.stencilCompareMask.set(0xFF);229dxstate.stencilWriteMask.set(stencilState.writeMask);230} else {231dxstate.stencilTest.disable();232}233} else {234// Depth Test235if (!IsDepthTestEffectivelyDisabled()) {236dxstate.depthTest.enable();237dxstate.depthFunc.set(ztests[gstate.getDepthTestFunction()]);238dxstate.depthWrite.set(gstate.isDepthWriteEnabled());239UpdateEverUsedEqualDepth(gstate.getDepthTestFunction());240} else {241dxstate.depthTest.disable();242}243244// Stencil Test245if (stencilState.enabled) {246dxstate.stencilTest.enable();247dxstate.stencilFunc.set(ztests[stencilState.testFunc]);248dxstate.stencilRef.set(stencilState.testRef);249dxstate.stencilCompareMask.set(stencilState.testMask);250dxstate.stencilOp.set(stencilOps[stencilState.sFail], stencilOps[stencilState.zFail], stencilOps[stencilState.zPass]);251dxstate.stencilWriteMask.set(stencilState.writeMask);252253// Nasty special case for Spongebob and similar where it tries to write zeros to alpha/stencil during254// depth-fail. We can't write to alpha then because the pixel is killed. However, we can invert the depth255// test and modify the alpha function...256if (SpongebobDepthInverseConditions(stencilState)) {257dxstate.blend.set(true);258dxstate.blendEquation.set(D3DBLENDOP_ADD, D3DBLENDOP_ADD);259dxstate.blendFunc.set(D3DBLEND_ZERO, D3DBLEND_ZERO, D3DBLEND_ZERO, D3DBLEND_ZERO);260dxstate.colorMask.set(8);261262dxstate.depthFunc.set(D3DCMP_LESS);263dxstate.stencilFunc.set(D3DCMP_ALWAYS);264// Invert265dxstate.stencilOp.set(D3DSTENCILOP_ZERO, D3DSTENCILOP_KEEP, D3DSTENCILOP_ZERO);266267dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE;268gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE);269}270} else {271dxstate.stencilTest.disable();272}273}274}275276if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {277ViewportAndScissor vpAndScissor;278ConvertViewportAndScissor(useBufferedRendering,279framebufferManager_->GetRenderWidth(), framebufferManager_->GetRenderHeight(),280framebufferManager_->GetTargetBufferWidth(), framebufferManager_->GetTargetBufferHeight(),281vpAndScissor);282UpdateCachedViewportState(vpAndScissor);283284dxstate.scissorTest.enable();285dxstate.scissorRect.set(vpAndScissor.scissorX, vpAndScissor.scissorY, vpAndScissor.scissorX + vpAndScissor.scissorW, vpAndScissor.scissorY + vpAndScissor.scissorH);286287float depthMin = vpAndScissor.depthRangeMin;288float depthMax = vpAndScissor.depthRangeMax;289290dxstate.viewport.set(vpAndScissor.viewportX, vpAndScissor.viewportY, vpAndScissor.viewportW, vpAndScissor.viewportH, depthMin, depthMax);291}292293gstate_c.Clean(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_BLEND_STATE);294gstate_c.Dirty(dirtyRequiresRecheck_);295dirtyRequiresRecheck_ = 0;296}297298void DrawEngineDX9::ApplyDrawStateLate() {299// At this point, we know if the vertices are full alpha or not.300// TODO: Set the nearest/linear here (since we correctly know if alpha/color tests are needed)?301}302303304