Skip to content
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

D3D9: Support old-style user clip planes #16615

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Common/GPU/D3D9/D3D9StateCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ class DirectXState {

DxState1<D3DRS_CULLMODE, D3DCULL_NONE> cullMode;
DxState1<D3DRS_SHADEMODE, D3DSHADE_GOURAUD> shadeMode;
DxState1<D3DRS_CLIPPLANEENABLE, 0> clipPlaneEnable;

BoolState<D3DRS_ZENABLE, false> depthTest;

Expand Down
4 changes: 4 additions & 0 deletions Common/GPU/D3D9/thin3d_d3d9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ class D3D9RasterState : public RasterState {
void Apply(LPDIRECT3DDEVICE9 device) {
dxstate.cullMode.set(cullMode);
dxstate.scissorTest.enable();
// Force user clipping off.
dxstate.clipPlaneEnable.set(0);
}
};

Expand Down Expand Up @@ -766,6 +768,8 @@ D3D9Context::D3D9Context(IDirect3D9 *d3d, IDirect3D9Ex *d3dEx, int adapterId, ID
caps_.isTilingGPU = false;
caps_.multiSampleLevelsMask = 1; // More could be supported with some work.

caps_.clipPlanesSupported = caps.MaxUserClipPlanes;

if ((caps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) != 0 && caps.MaxAnisotropy > 1) {
caps_.anisoSupported = true;
}
Expand Down
3 changes: 3 additions & 0 deletions Common/GPU/thin3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,9 @@ struct DeviceCaps {
// From the other backends, we can detect if D3D9 support is known bad (like on Xe) and disable it.
bool supportsD3D9;

// Old style, for older GL or Direct3D 9.
u32 clipPlanesSupported;

u32 multiSampleLevelsMask; // Bit n is set if (1 << n) is a valid multisample level. Bit 0 is always set.
std::string deviceName; // The device name to use when creating the thin3d context, to get the same one.
};
Expand Down
6 changes: 6 additions & 0 deletions GPU/Directx9/ShaderManagerDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ void ShaderManagerDX9::VSUpdateUniforms(u64 dirtyUniforms) {

float data[4] = { viewZScale, viewZCenter, reverseTranslate, reverseScale };
VSSetFloatUniform4(CONST_VS_DEPTHRANGE, data);

if (draw_->GetDeviceCaps().clipPlanesSupported >= 1) {
float clip[4] = { 0.0f, 0.0f, reverseScale, 1.0f - reverseTranslate * reverseScale };
// Well, not a uniform, but we treat it as one like other backends.
device_->SetClipPlane(0, clip);
}
}
if (dirtyUniforms & DIRTY_CULLRANGE) {
float minValues[4], maxValues[4];
Expand Down
6 changes: 6 additions & 0 deletions GPU/Directx9/StateMappingDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ void DrawEngineDX9::ApplyDrawState(int prim) {
} else {
dxstate.shadeMode.set(gstate.getShadeMode() == GE_SHADE_GOURAUD ? D3DSHADE_GOURAUD : D3DSHADE_FLAT);
}

// We use fixed-function user clipping on D3D9, where available, for negative Z clipping.
if (draw_->GetDeviceCaps().clipPlanesSupported >= 1) {
bool wantClip = !gstate.isModeThrough() && gstate_c.submitType == SubmitType::DRAW;
dxstate.clipPlaneEnable.set(wantClip ? 1 : 0);
}
}

if (gstate_c.IsDirty(DIRTY_DEPTHSTENCIL_STATE)) {
Expand Down
8 changes: 4 additions & 4 deletions GPU/GPUCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,7 @@ void GPUCommon::Execute_Bezier(u32 op, u32 diff) {

SetDrawType(DRAW_BEZIER, PatchPrimToPrim(surface.primType));

gstate_c.Dirty(DIRTY_VERTEXSHADER_STATE | DIRTY_GEOMETRYSHADER_STATE);
gstate_c.Dirty(DIRTY_RASTER_STATE | DIRTY_VERTEXSHADER_STATE | DIRTY_GEOMETRYSHADER_STATE);
if (drawEngineCommon_->CanUseHardwareTessellation(surface.primType)) {
gstate_c.submitType = SubmitType::HW_BEZIER;
if (gstate_c.spline_num_points_u != surface.num_points_u) {
Expand All @@ -2130,7 +2130,7 @@ void GPUCommon::Execute_Bezier(u32 op, u32 diff) {
UpdateUVScaleOffset();
drawEngineCommon_->SubmitCurve(control_points, indices, surface, gstate.vertType, &bytesRead, "bezier");

gstate_c.Dirty(DIRTY_VERTEXSHADER_STATE | DIRTY_GEOMETRYSHADER_STATE);
gstate_c.Dirty(DIRTY_RASTER_STATE | DIRTY_VERTEXSHADER_STATE | DIRTY_GEOMETRYSHADER_STATE);
gstate_c.submitType = SubmitType::DRAW;

// After drawing, we advance pointers - see SubmitPrim which does the same.
Expand Down Expand Up @@ -2190,7 +2190,7 @@ void GPUCommon::Execute_Spline(u32 op, u32 diff) {

SetDrawType(DRAW_SPLINE, PatchPrimToPrim(surface.primType));

gstate_c.Dirty(DIRTY_VERTEXSHADER_STATE);
gstate_c.Dirty(DIRTY_RASTER_STATE | DIRTY_VERTEXSHADER_STATE | DIRTY_GEOMETRYSHADER_STATE);
if (drawEngineCommon_->CanUseHardwareTessellation(surface.primType)) {
gstate_c.submitType = SubmitType::HW_SPLINE;
if (gstate_c.spline_num_points_u != surface.num_points_u) {
Expand All @@ -2205,7 +2205,7 @@ void GPUCommon::Execute_Spline(u32 op, u32 diff) {
UpdateUVScaleOffset();
drawEngineCommon_->SubmitCurve(control_points, indices, surface, gstate.vertType, &bytesRead, "spline");

gstate_c.Dirty(DIRTY_VERTEXSHADER_STATE | DIRTY_GEOMETRYSHADER_STATE);
gstate_c.Dirty(DIRTY_RASTER_STATE | DIRTY_VERTEXSHADER_STATE | DIRTY_GEOMETRYSHADER_STATE);
gstate_c.submitType = SubmitType::DRAW;

// After drawing, we advance pointers - see SubmitPrim which does the same.
Expand Down