Skip to content

Commit

Permalink
Merge pull request #18477 from hrydgard/half-pixel-offset
Browse files Browse the repository at this point in the history
D3D9: Apply a half-pixel offset in 2D shader draws.
  • Loading branch information
hrydgard authored Dec 6, 2023
2 parents 81d741a + e2480b9 commit 4b6dbd1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions Common/GPU/D3D9/thin3d_d3d9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ D3D9Context::D3D9Context(IDirect3D9 *d3d, IDirect3D9Ex *d3dEx, int adapterId, ID
caps_.framebufferSeparateDepthCopySupported = false;
caps_.texture3DSupported = true;
caps_.fragmentShaderDepthWriteSupported = true;
caps_.requiresHalfPixelOffset = true;
caps_.fragmentShaderStencilWriteSupported = false;
caps_.blendMinMaxSupported = true;
caps_.isTilingGPU = false;
Expand Down
1 change: 1 addition & 0 deletions Common/GPU/ShaderWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ShaderWriter {

void ConstFloat(const char *name, float value);
void SetFlags(ShaderWriterFlags flags) { flags_ |= flags; }
ShaderWriterFlags Flags() const { return flags_; }
void SetTexBindingBase(int base) { texBindingBase_ = base; }

ShaderWriter &SampleTexture2D(const char *texName, const char *uv);
Expand Down
1 change: 1 addition & 0 deletions Common/GPU/thin3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ struct DeviceCaps {
bool sampleRateShadingSupported;
bool setMaxFrameLatencySupported;
bool textureSwizzleSupported;
bool requiresHalfPixelOffset;

bool verySlowShaderCompiler;

Expand Down
14 changes: 10 additions & 4 deletions GPU/Common/Draw2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,17 @@ void Draw2D::Blit(Draw2DPipeline *pipeline, float srcX1, float srcY1, float srcX
float dY = 1.0f / (float)dstHeight;
float sX = 1.0f / (float)srcWidth;
float sY = 1.0f / (float)srcHeight;
float xOffset = 0.0f;
float yOffset = 0.0f;
if (draw_->GetDeviceCaps().requiresHalfPixelOffset) {
xOffset = -dX * 0.5f;
yOffset = -dY * 0.5f;
}
Draw2DVertex vtx[4] = {
{ -1.0f + 2.0f * dX * dstX1, -(1.0f - 2.0f * dY * dstY1), sX * srcX1, sY * srcY1 },
{ -1.0f + 2.0f * dX * dstX2, -(1.0f - 2.0f * dY * dstY1), sX * srcX2, sY * srcY1 },
{ -1.0f + 2.0f * dX * dstX1, -(1.0f - 2.0f * dY * dstY2), sX * srcX1, sY * srcY2 },
{ -1.0f + 2.0f * dX * dstX2, -(1.0f - 2.0f * dY * dstY2), sX * srcX2, sY * srcY2 },
{ -1.0f + 2.0f * dX * dstX1 + xOffset, -(1.0f - 2.0f * dY * dstY1) + yOffset, sX * srcX1, sY * srcY1 },
{ -1.0f + 2.0f * dX * dstX2 + xOffset, -(1.0f - 2.0f * dY * dstY1) + yOffset, sX * srcX2, sY * srcY1 },
{ -1.0f + 2.0f * dX * dstX1 + xOffset, -(1.0f - 2.0f * dY * dstY2) + yOffset, sX * srcX1, sY * srcY2 },
{ -1.0f + 2.0f * dX * dstX2 + xOffset, -(1.0f - 2.0f * dY * dstY2) + yOffset, sX * srcX2, sY * srcY2 },
};

DrawStrip2D(nullptr, vtx, 4, linear, pipeline, srcWidth, srcHeight, scaleFactor);
Expand Down

0 comments on commit 4b6dbd1

Please sign in to comment.