From 59b3df0643ac18d5ddcb5602379e214ec9a86d01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 3 Aug 2022 11:42:37 +0200 Subject: [PATCH 1/8] Draw2D --- CMakeLists.txt | 2 + GPU/Common/Draw2D.cpp | 123 ++++++++++++++++++++++++ GPU/Common/Draw2D.h | 9 ++ GPU/Common/FramebufferManagerCommon.cpp | 4 + GPU/Common/FramebufferManagerCommon.h | 12 ++- GPU/GPU.vcxproj | 4 +- GPU/GPU.vcxproj.filters | 8 +- android/jni/Android.mk | 1 + libretro/Makefile.common | 1 + 9 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 GPU/Common/Draw2D.cpp create mode 100644 GPU/Common/Draw2D.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 670e5f437c..d4b2fdc242 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1529,6 +1529,8 @@ endif() set(GPU_SOURCES ${GPU_IMPLS} ${GPU_NEON} + GPU/Common/Draw2D.cpp + GPU/Common/Draw2D.h GPU/Common/DepalettizeShaderCommon.cpp GPU/Common/DepalettizeShaderCommon.h GPU/Common/FragmentShaderGenerator.cpp diff --git a/GPU/Common/Draw2D.cpp b/GPU/Common/Draw2D.cpp new file mode 100644 index 0000000000..b827a3cf75 --- /dev/null +++ b/GPU/Common/Draw2D.cpp @@ -0,0 +1,123 @@ +// Copyright (c) 2014- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include "Common/GPU/Shader.h" +#include "Common/GPU/ShaderWriter.h" +#include "Common/GPU/thin3d.h" +#include "Core/Config.h" +#include "Core/ConfigValues.h" +#include "Core/Reporting.h" +#include "GPU/Common/Draw2D.h" +#include "GPU/Common/DrawEngineCommon.h" +#include "GPU/Common/FramebufferManagerCommon.h" +#include "GPU/Common/TextureCacheCommon.h" + +static const InputDef inputs[2] = { + { "vec2", "a_position", "POSITION" }, + { "vec2", "a_texcoord", "TEXCOORD0" }, +}; + +static const VaryingDef varyings[1] = { + { "vec2", "v_texcoord", "TEXCOORD0", 0, "highp" }, +}; + +void GenerateDraw2DFs(char *buffer, const ShaderLanguageDesc &lang, const Draw::Bugs &bugs) { + ShaderWriter writer(buffer, lang, ShaderStage::Fragment, nullptr, 0); + writer.DeclareSampler2D("samp", 0); + writer.DeclareTexture2D("tex", 0); + writer.BeginFSMain(Slice::empty(), varyings); + writer.C(" vec4 outColor = ").SampleTexture2D("tex", "samp", "v_texcoord.xy").C(";\n"); + writer.EndFSMain("outColor"); +} + +void GenerateDraw2DVS(char *buffer, const ShaderLanguageDesc &lang) { + ShaderWriter writer(buffer, lang, ShaderStage::Vertex, nullptr, 0); + + writer.BeginVSMain(inputs, Slice::empty(), varyings); + + writer.C(" v_texcoord = a_texcoord;\n"); // yes, this should be right. Should be 2.0 in the far corners. + writer.C(" gl_Position = a_position;\n"); + writer.F(" gl_Position.y *= %s1.0;\n", lang.viewportYSign); + + writer.EndVSMain(varyings); +} + +// verts have positions in clip coordinates. +void FramebufferManagerCommon::Draw2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount, float viewportWidth, float viewportHeight) { + using namespace Draw; + + if (!draw2DPipelineLinear_) { + const ShaderLanguageDesc &shaderLanguageDesc = draw_->GetShaderLanguageDesc(); + + char *fsCode = new char[4000]; + char *vsCode = new char[4000]; + GenerateDraw2DFs(fsCode, shaderLanguageDesc, draw_->GetBugs()); + GenerateDraw2DVS(vsCode, shaderLanguageDesc); + + draw2DFs_ = draw_->CreateShaderModule(ShaderStage::Fragment, shaderLanguageDesc.shaderLanguage, (const uint8_t *)fsCode, strlen(fsCode), "draw2d_fs"); + draw2DVs_ = draw_->CreateShaderModule(ShaderStage::Vertex, shaderLanguageDesc.shaderLanguage, (const uint8_t *)vsCode, strlen(vsCode), "draw2d_vs"); + + _assert_(stencilUploadFs_ && stencilUploadVs_); + + InputLayoutDesc desc = { + { + { 16, false }, + }, + { + { 0, SEM_POSITION, DataFormat::R32G32_FLOAT, 0 }, + { 16, SEM_TEXCOORD0, DataFormat::R32G32_FLOAT, 8 }, + }, + }; + InputLayout *inputLayout = draw_->CreateInputLayout(desc); + + BlendState *blendOff = draw_->CreateBlendState({ false, 0xF }); + DepthStencilStateDesc dsDesc{}; + DepthStencilState *noDepthStencil = draw_->CreateDepthStencilState(dsDesc); + RasterState *rasterNoCull = draw_->CreateRasterState({}); + + PipelineDesc stencilWriteDesc{ + Primitive::TRIANGLE_STRIP, + { draw2DVs_, draw2DFs_ }, + inputLayout, noDepthStencil, blendOff, rasterNoCull, nullptr, + }; + draw2DPipelineLinear_ = draw_->CreateGraphicsPipeline(stencilWriteDesc); + _assert_(draw2DPipelineLinear_); + + delete[] fsCode; + delete[] vsCode; + + rasterNoCull->Release(); + blendOff->Release(); + noDepthStencil->Release(); + inputLayout->Release(); + + SamplerStateDesc descLinear{}; + descLinear.magFilter = TextureFilter::LINEAR; + descLinear.minFilter = TextureFilter::LINEAR; + descLinear.mipFilter = TextureFilter::LINEAR; + descLinear.wrapU = TextureAddressMode::CLAMP_TO_EDGE; + descLinear.wrapV = TextureAddressMode::CLAMP_TO_EDGE; + draw2DSamplerLinear_= draw_->CreateSamplerState(descLinear); + } + + draw_->BindPipeline(draw2DPipelineLinear_); + draw_->BindTextures(TEX_SLOT_PSP_TEXTURE, 1, &tex); + draw_->BindSamplerStates(TEX_SLOT_PSP_TEXTURE, 1, &draw2DSamplerLinear_); + draw_->DrawUP(verts, vertexCount); + + gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE); +} diff --git a/GPU/Common/Draw2D.h b/GPU/Common/Draw2D.h new file mode 100644 index 0000000000..34c171e7a0 --- /dev/null +++ b/GPU/Common/Draw2D.h @@ -0,0 +1,9 @@ +#pragma once + +// For framebuffer copies and similar things that just require passthrough. +struct Draw2DVertex { + float x; + float y; + float u; + float v; +}; diff --git a/GPU/Common/FramebufferManagerCommon.cpp b/GPU/Common/FramebufferManagerCommon.cpp index 9478bf34fd..fec63dcb81 100644 --- a/GPU/Common/FramebufferManagerCommon.cpp +++ b/GPU/Common/FramebufferManagerCommon.cpp @@ -2492,6 +2492,10 @@ void FramebufferManagerCommon::DeviceLost() { DoRelease(stencilUploadVs_); DoRelease(stencilUploadSampler_); DoRelease(stencilUploadPipeline_); + DoRelease(draw2DPipelineLinear_); + DoRelease(draw2DSamplerLinear_); + DoRelease(draw2DVs_); + DoRelease(draw2DFs_); presentation_->DeviceLost(); draw_ = nullptr; } diff --git a/GPU/Common/FramebufferManagerCommon.h b/GPU/Common/FramebufferManagerCommon.h index 894d7266e8..5ec21e88b0 100644 --- a/GPU/Common/FramebufferManagerCommon.h +++ b/GPU/Common/FramebufferManagerCommon.h @@ -29,11 +29,11 @@ #include "Common/CommonTypes.h" #include "Common/Log.h" -#include "Core/MemMap.h" +#include "Common/GPU/thin3d.h" #include "GPU/GPU.h" #include "GPU/ge_constants.h" #include "GPU/GPUInterface.h" -#include "Common/GPU/thin3d.h" +#include "GPU/Common/Draw2D.h" enum { FB_USAGE_DISPLAYED_FRAMEBUFFER = 1, @@ -357,6 +357,8 @@ protected: virtual void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) = 0; virtual void Bind2DShader() = 0; + void Draw2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount, float viewportWidth, float viewportHeight); + bool UpdateSize(); void FlushBeforeCopy(); @@ -473,4 +475,10 @@ protected: Draw::ShaderModule *stencilUploadVs_ = nullptr; Draw::ShaderModule *stencilUploadFs_ = nullptr; Draw::SamplerState *stencilUploadSampler_ = nullptr; + + // Draw2D pipelines + Draw::Pipeline *draw2DPipelineLinear_ = nullptr; + Draw::SamplerState *draw2DSamplerLinear_ = nullptr; + Draw::ShaderModule *draw2DVs_ = nullptr; + Draw::ShaderModule *draw2DFs_ = nullptr; }; diff --git a/GPU/GPU.vcxproj b/GPU/GPU.vcxproj index ae899f0cd2..ca6cd6a03d 100644 --- a/GPU/GPU.vcxproj +++ b/GPU/GPU.vcxproj @@ -338,6 +338,7 @@ + @@ -459,6 +460,7 @@ + @@ -666,4 +668,4 @@ - + \ No newline at end of file diff --git a/GPU/GPU.vcxproj.filters b/GPU/GPU.vcxproj.filters index 9c8d6b0f93..db9157dde0 100644 --- a/GPU/GPU.vcxproj.filters +++ b/GPU/GPU.vcxproj.filters @@ -264,6 +264,9 @@ Software + + Common + @@ -518,6 +521,9 @@ Software + + Common + @@ -535,4 +541,4 @@ Shaders - + \ No newline at end of file diff --git a/android/jni/Android.mk b/android/jni/Android.mk index b2e4e6fe37..03dd60d89e 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -319,6 +319,7 @@ EXEC_AND_LIB_FILES := \ $(SRC)/GPU/GPUState.cpp \ $(SRC)/GPU/GeConstants.cpp \ $(SRC)/GPU/GeDisasm.cpp \ + $(SRC)/GPU/Common/Draw2D.cpp \ $(SRC)/GPU/Common/DepalettizeShaderCommon.cpp \ $(SRC)/GPU/Common/FragmentShaderGenerator.cpp \ $(SRC)/GPU/Common/FramebufferManagerCommon.cpp \ diff --git a/libretro/Makefile.common b/libretro/Makefile.common index dc7c0bc987..37e42af0ee 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -315,6 +315,7 @@ SOURCES_C +=\ $(COMMONDIR)/GPU/OpenGL/gl3stub.c SOURCES_CXX += \ + $(GPUCOMMONDIR)/Draw2D.cpp \ $(GPUCOMMONDIR)/VertexDecoderCommon.cpp \ $(GPUCOMMONDIR)/GPUStateUtils.cpp \ $(GPUCOMMONDIR)/DrawEngineCommon.cpp \ From d55c4a3456dd477b713bdf2789ab28b504425857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 3 Aug 2022 12:09:24 +0200 Subject: [PATCH 2/8] Convert the D3D11 DrawActiveTexture to thin3d --- GPU/Common/Draw2D.cpp | 12 ++++--- GPU/Common/FramebufferManagerCommon.h | 2 +- GPU/D3D11/FramebufferManagerD3D11.cpp | 49 ++++++++++----------------- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/GPU/Common/Draw2D.cpp b/GPU/Common/Draw2D.cpp index b827a3cf75..bec9e166e4 100644 --- a/GPU/Common/Draw2D.cpp +++ b/GPU/Common/Draw2D.cpp @@ -50,14 +50,14 @@ void GenerateDraw2DVS(char *buffer, const ShaderLanguageDesc &lang) { writer.BeginVSMain(inputs, Slice::empty(), varyings); writer.C(" v_texcoord = a_texcoord;\n"); // yes, this should be right. Should be 2.0 in the far corners. - writer.C(" gl_Position = a_position;\n"); + writer.C(" gl_Position = vec4(a_position, 0.0, 1.0);\n"); writer.F(" gl_Position.y *= %s1.0;\n", lang.viewportYSign); writer.EndVSMain(varyings); } // verts have positions in clip coordinates. -void FramebufferManagerCommon::Draw2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount, float viewportWidth, float viewportHeight) { +void FramebufferManagerCommon::DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount) { using namespace Draw; if (!draw2DPipelineLinear_) { @@ -71,7 +71,7 @@ void FramebufferManagerCommon::Draw2D(Draw::Texture *tex, Draw2DVertex *verts, i draw2DFs_ = draw_->CreateShaderModule(ShaderStage::Fragment, shaderLanguageDesc.shaderLanguage, (const uint8_t *)fsCode, strlen(fsCode), "draw2d_fs"); draw2DVs_ = draw_->CreateShaderModule(ShaderStage::Vertex, shaderLanguageDesc.shaderLanguage, (const uint8_t *)vsCode, strlen(vsCode), "draw2d_vs"); - _assert_(stencilUploadFs_ && stencilUploadVs_); + _assert_(draw2DFs_ && draw2DVs_); InputLayoutDesc desc = { { @@ -79,7 +79,7 @@ void FramebufferManagerCommon::Draw2D(Draw::Texture *tex, Draw2DVertex *verts, i }, { { 0, SEM_POSITION, DataFormat::R32G32_FLOAT, 0 }, - { 16, SEM_TEXCOORD0, DataFormat::R32G32_FLOAT, 8 }, + { 0, SEM_TEXCOORD0, DataFormat::R32G32_FLOAT, 8 }, }, }; InputLayout *inputLayout = draw_->CreateInputLayout(desc); @@ -115,7 +115,9 @@ void FramebufferManagerCommon::Draw2D(Draw::Texture *tex, Draw2DVertex *verts, i } draw_->BindPipeline(draw2DPipelineLinear_); - draw_->BindTextures(TEX_SLOT_PSP_TEXTURE, 1, &tex); + if (tex) { + draw_->BindTextures(TEX_SLOT_PSP_TEXTURE, 1, &tex); + } draw_->BindSamplerStates(TEX_SLOT_PSP_TEXTURE, 1, &draw2DSamplerLinear_); draw_->DrawUP(verts, vertexCount); diff --git a/GPU/Common/FramebufferManagerCommon.h b/GPU/Common/FramebufferManagerCommon.h index 5ec21e88b0..14a1fa142b 100644 --- a/GPU/Common/FramebufferManagerCommon.h +++ b/GPU/Common/FramebufferManagerCommon.h @@ -357,7 +357,7 @@ protected: virtual void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) = 0; virtual void Bind2DShader() = 0; - void Draw2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount, float viewportWidth, float viewportHeight); + void DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount); bool UpdateSize(); diff --git a/GPU/D3D11/FramebufferManagerD3D11.cpp b/GPU/D3D11/FramebufferManagerD3D11.cpp index 6d58e5caac..2162c1ae1e 100644 --- a/GPU/D3D11/FramebufferManagerD3D11.cpp +++ b/GPU/D3D11/FramebufferManagerD3D11.cpp @@ -171,14 +171,12 @@ void FramebufferManagerD3D11::SetDrawEngine(DrawEngineD3D11 *td) { } void FramebufferManagerD3D11::DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) { - struct Coord { - Lin::Vec3 pos; float u, v; - }; - Coord coord[4] = { - {{x, y, 0}, u0, v0}, - {{x + w, y, 0}, u1, v0}, - {{x + w, y + h,0}, u1, v1}, - {{x, y + h, 0}, u0, v1}, + // Will be drawn as a strip. + Draw2DVertex coord[4] = { + {x, y, u0, v0}, + {x + w, y, u1, v0}, + {x + w, y + h, u1, v1}, + {x, y + h, u0, v1}, }; if (uvRotation != ROTATION_LOCKED_HORIZONTAL) { @@ -200,39 +198,28 @@ void FramebufferManagerD3D11::DrawActiveTexture(float x, float y, float w, float } } - float invDestW = 1.0f / (destW * 0.5f); - float invDestH = 1.0f / (destH * 0.5f); + const float invDestW = 2.0f / destW; + const float invDestH = 2.0f / destH; for (int i = 0; i < 4; i++) { - coord[i].pos.x = coord[i].pos.x * invDestW - 1.0f; - coord[i].pos.y = -(coord[i].pos.y * invDestH - 1.0f); + coord[i].x = coord[i].x * invDestW - 1.0f; + coord[i].y = coord[i].y * invDestH - 1.0f; } if ((flags & DRAWTEX_TO_BACKBUFFER) && g_display_rotation != DisplayRotation::ROTATE_0) { for (int i = 0; i < 4; i++) { // backwards notation, should fix that... - coord[i].pos = coord[i].pos * g_display_rot_matrix; + Lin::Vec3 pos = Lin::Vec3(coord[i].x, coord[i].y, 0.0); + pos = pos * g_display_rot_matrix; + coord[i].x = pos.x; + coord[i].y = pos.y; } } - // The above code is for FAN geometry but we can only do STRIP. So rearrange it a little. - D3D11_MAPPED_SUBRESOURCE map; - context_->Map(quadBuffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &map); - float *dest = (float *)map.pData; - memcpy(dest, coord, sizeof(Coord)); - memcpy(dest + 5, coord + 1, sizeof(Coord)); - memcpy(dest + 10, coord + 3, sizeof(Coord)); - memcpy(dest + 15, coord + 2, sizeof(Coord)); - context_->Unmap(quadBuffer_, 0); + // Rearrange to strip form. + std::swap(coord[2], coord[3]); + + DrawStrip2D(nullptr, coord, 4); - context_->RSSetState(stockD3D11.rasterStateNoCull); - context_->OMSetBlendState(stockD3D11.blendStateDisabledWithColorMask[0xF], nullptr, 0xFFFFFFFF); - context_->OMSetDepthStencilState(stockD3D11.depthStencilDisabled, 0); - context_->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); - context_->PSSetSamplers(0, 1, (flags & DRAWTEX_LINEAR) ? &stockD3D11.samplerLinear2DClamp : &stockD3D11.samplerPoint2DClamp); - UINT stride = 20; - UINT offset = 0; - context_->IASetVertexBuffers(0, 1, &quadBuffer_, &stride, &offset); - context_->Draw(4, 0); gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE); } From 1d3075566f01593f12e7ca9f5fb2dd5006a6d8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 3 Aug 2022 12:14:28 +0200 Subject: [PATCH 3/8] Move to common (todo: merge with above) --- GPU/Common/Draw2D.cpp | 2 - GPU/Common/FramebufferManagerCommon.cpp | 59 ++++++++++++++++++++++++- GPU/Common/FramebufferManagerCommon.h | 2 +- GPU/D3D11/FramebufferManagerD3D11.cpp | 56 ----------------------- GPU/D3D11/FramebufferManagerD3D11.h | 1 - 5 files changed, 58 insertions(+), 62 deletions(-) diff --git a/GPU/Common/Draw2D.cpp b/GPU/Common/Draw2D.cpp index bec9e166e4..7d22dd817f 100644 --- a/GPU/Common/Draw2D.cpp +++ b/GPU/Common/Draw2D.cpp @@ -120,6 +120,4 @@ void FramebufferManagerCommon::DrawStrip2D(Draw::Texture *tex, Draw2DVertex *ver } draw_->BindSamplerStates(TEX_SLOT_PSP_TEXTURE, 1, &draw2DSamplerLinear_); draw_->DrawUP(verts, vertexCount); - - gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE); } diff --git a/GPU/Common/FramebufferManagerCommon.cpp b/GPU/Common/FramebufferManagerCommon.cpp index fec63dcb81..1bdf8c0265 100644 --- a/GPU/Common/FramebufferManagerCommon.cpp +++ b/GPU/Common/FramebufferManagerCommon.cpp @@ -23,6 +23,9 @@ #include "Common/GPU/OpenGL/GLFeatures.h" #include "Common/Data/Convert/ColorConv.h" #include "Common/Data/Text/I18n.h" +#include "Common/Math/lin/matrix4x4.h" +#include "Common/Math/math_util.h" +#include "Common/System/Display.h" #include "Common/CommonTypes.h" #include "Core/Config.h" #include "Core/ConfigValues.h" @@ -44,8 +47,7 @@ #include "GPU/GPUState.h" FramebufferManagerCommon::FramebufferManagerCommon(Draw::DrawContext *draw) - : draw_(draw), - displayFormat_(GE_FORMAT_565) { + : draw_(draw), displayFormat_(GE_FORMAT_565) { presentation_ = new PresentationCommon(draw); } @@ -2504,3 +2506,56 @@ void FramebufferManagerCommon::DeviceRestore(Draw::DrawContext *draw) { draw_ = draw; presentation_->DeviceRestore(draw); } + +void FramebufferManagerCommon::DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) { + // Will be drawn as a strip. + Draw2DVertex coord[4] = { + {x, y, u0, v0}, + {x + w, y, u1, v0}, + {x + w, y + h, u1, v1}, + {x, y + h, u0, v1}, + }; + + if (uvRotation != ROTATION_LOCKED_HORIZONTAL) { + float temp[8]; + int rotation = 0; + switch (uvRotation) { + case ROTATION_LOCKED_HORIZONTAL180: rotation = 2; break; + case ROTATION_LOCKED_VERTICAL: rotation = 1; break; + case ROTATION_LOCKED_VERTICAL180: rotation = 3; break; + } + for (int i = 0; i < 4; i++) { + temp[i * 2] = coord[((i + rotation) & 3)].u; + temp[i * 2 + 1] = coord[((i + rotation) & 3)].v; + } + + for (int i = 0; i < 4; i++) { + coord[i].u = temp[i * 2]; + coord[i].v = temp[i * 2 + 1]; + } + } + + const float invDestW = 2.0f / destW; + const float invDestH = 2.0f / destH; + for (int i = 0; i < 4; i++) { + coord[i].x = coord[i].x * invDestW - 1.0f; + coord[i].y = coord[i].y * invDestH - 1.0f; + } + + if ((flags & DRAWTEX_TO_BACKBUFFER) && g_display_rotation != DisplayRotation::ROTATE_0) { + for (int i = 0; i < 4; i++) { + // backwards notation, should fix that... + Lin::Vec3 pos = Lin::Vec3(coord[i].x, coord[i].y, 0.0); + pos = pos * g_display_rot_matrix; + coord[i].x = pos.x; + coord[i].y = pos.y; + } + } + + // Rearrange to strip form. + std::swap(coord[2], coord[3]); + + DrawStrip2D(nullptr, coord, 4); + + gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE); +} diff --git a/GPU/Common/FramebufferManagerCommon.h b/GPU/Common/FramebufferManagerCommon.h index 14a1fa142b..9140ed18e9 100644 --- a/GPU/Common/FramebufferManagerCommon.h +++ b/GPU/Common/FramebufferManagerCommon.h @@ -354,7 +354,7 @@ protected: virtual void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h); void SetViewport2D(int x, int y, int w, int h); Draw::Texture *MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height); - virtual void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) = 0; + virtual void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags); virtual void Bind2DShader() = 0; void DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount); diff --git a/GPU/D3D11/FramebufferManagerD3D11.cpp b/GPU/D3D11/FramebufferManagerD3D11.cpp index 2162c1ae1e..b4b406d774 100644 --- a/GPU/D3D11/FramebufferManagerD3D11.cpp +++ b/GPU/D3D11/FramebufferManagerD3D11.cpp @@ -21,9 +21,6 @@ #include "Common/Common.h" #include "Common/Data/Convert/ColorConv.h" -#include "Common/System/Display.h" -#include "Common/Math/lin/matrix4x4.h" -#include "Common/Math/math_util.h" #include "Common/GPU/thin3d.h" #include "Core/MemMap.h" @@ -170,59 +167,6 @@ void FramebufferManagerD3D11::SetDrawEngine(DrawEngineD3D11 *td) { drawEngine_ = td; } -void FramebufferManagerD3D11::DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) { - // Will be drawn as a strip. - Draw2DVertex coord[4] = { - {x, y, u0, v0}, - {x + w, y, u1, v0}, - {x + w, y + h, u1, v1}, - {x, y + h, u0, v1}, - }; - - if (uvRotation != ROTATION_LOCKED_HORIZONTAL) { - float temp[8]; - int rotation = 0; - switch (uvRotation) { - case ROTATION_LOCKED_HORIZONTAL180: rotation = 2; break; - case ROTATION_LOCKED_VERTICAL: rotation = 1; break; - case ROTATION_LOCKED_VERTICAL180: rotation = 3; break; - } - for (int i = 0; i < 4; i++) { - temp[i * 2] = coord[((i + rotation) & 3)].u; - temp[i * 2 + 1] = coord[((i + rotation) & 3)].v; - } - - for (int i = 0; i < 4; i++) { - coord[i].u = temp[i * 2]; - coord[i].v = temp[i * 2 + 1]; - } - } - - const float invDestW = 2.0f / destW; - const float invDestH = 2.0f / destH; - for (int i = 0; i < 4; i++) { - coord[i].x = coord[i].x * invDestW - 1.0f; - coord[i].y = coord[i].y * invDestH - 1.0f; - } - - if ((flags & DRAWTEX_TO_BACKBUFFER) && g_display_rotation != DisplayRotation::ROTATE_0) { - for (int i = 0; i < 4; i++) { - // backwards notation, should fix that... - Lin::Vec3 pos = Lin::Vec3(coord[i].x, coord[i].y, 0.0); - pos = pos * g_display_rot_matrix; - coord[i].x = pos.x; - coord[i].y = pos.y; - } - } - - // Rearrange to strip form. - std::swap(coord[2], coord[3]); - - DrawStrip2D(nullptr, coord, 4); - - gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE); -} - void FramebufferManagerD3D11::Bind2DShader() { context_->IASetInputLayout(quadInputLayout_); context_->PSSetShader(quadPixelShader_, 0, 0); diff --git a/GPU/D3D11/FramebufferManagerD3D11.h b/GPU/D3D11/FramebufferManagerD3D11.h index db843c1a02..38d0d6623b 100644 --- a/GPU/D3D11/FramebufferManagerD3D11.h +++ b/GPU/D3D11/FramebufferManagerD3D11.h @@ -39,7 +39,6 @@ public: void SetTextureCache(TextureCacheD3D11 *tc); void SetShaderManager(ShaderManagerD3D11 *sm); void SetDrawEngine(DrawEngineD3D11 *td); - void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) override; void EndFrame(); From 1fe2bced54e07d4b5c871984aad2a42f145a0aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 3 Aug 2022 12:16:27 +0200 Subject: [PATCH 4/8] Delete the D3D9 impl of DrawActiveTexture --- GPU/Directx9/FramebufferManagerDX9.cpp | 60 -------------------------- GPU/Directx9/FramebufferManagerDX9.h | 1 - 2 files changed, 61 deletions(-) diff --git a/GPU/Directx9/FramebufferManagerDX9.cpp b/GPU/Directx9/FramebufferManagerDX9.cpp index 47e86fc0a0..681cd3accb 100644 --- a/GPU/Directx9/FramebufferManagerDX9.cpp +++ b/GPU/Directx9/FramebufferManagerDX9.cpp @@ -152,66 +152,6 @@ static const D3DVERTEXELEMENT9 g_FramebufferVertexElements[] = { drawEngine_ = td; } - void FramebufferManagerDX9::DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) { - // TODO: StretchRect instead when possible? - float coord[20] = { - x,y,0, u0,v0, - x+w,y,0, u1,v0, - x+w,y+h,0, u1,v1, - x,y+h,0, u0,v1, - }; - - if (uvRotation != ROTATION_LOCKED_HORIZONTAL) { - float temp[8]; - int rotation = 0; - switch (uvRotation) { - case ROTATION_LOCKED_HORIZONTAL180: rotation = 2; break; - case ROTATION_LOCKED_VERTICAL: rotation = 1; break; - case ROTATION_LOCKED_VERTICAL180: rotation = 3; break; - } - - for (int i = 0; i < 4; i++) { - temp[i * 2] = coord[((i + rotation) & 3) * 5 + 3]; - temp[i * 2 + 1] = coord[((i + rotation) & 3) * 5 + 4]; - } - - for (int i = 0; i < 4; i++) { - coord[i * 5 + 3] = temp[i * 2]; - coord[i * 5 + 4] = temp[i * 2 + 1]; - } - } - - float invDestW = 1.0f / (destW * 0.5f); - float invDestH = 1.0f / (destH * 0.5f); - float halfPixelX = invDestW * 0.5f; - float halfPixelY = invDestH * 0.5f; - for (int i = 0; i < 4; i++) { - coord[i * 5] = coord[i * 5] * invDestW - 1.0f - halfPixelX; - coord[i * 5 + 1] = -(coord[i * 5 + 1] * invDestH - 1.0f - halfPixelY); - } - - if (flags & DRAWTEX_LINEAR) { - dxstate.texMagFilter.set(D3DTEXF_LINEAR); - dxstate.texMinFilter.set(D3DTEXF_LINEAR); - } else { - dxstate.texMagFilter.set(D3DTEXF_POINT); - dxstate.texMinFilter.set(D3DTEXF_POINT); - } - dxstate.texMipLodBias.set(0.0f); - dxstate.texMaxMipLevel.set(0); - dxstate.blend.disable(); - dxstate.cullMode.set(D3DCULL_NONE); - dxstate.depthTest.disable(); - dxstate.scissorTest.disable(); - dxstate.stencilTest.disable(); - dxstate.colorMask.set(0xF); - dxstate.stencilWriteMask.set(0xFF); - HRESULT hr = device_->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, coord, 5 * sizeof(float)); - if (FAILED(hr)) { - ERROR_LOG_REPORT(G3D, "DrawActiveTexture() failed: %08x", (uint32_t)hr); - } - } - void FramebufferManagerDX9::Bind2DShader() { device_->SetVertexDeclaration(pFramebufferVertexDecl); device_->SetPixelShader(pFramebufferPixelShader); diff --git a/GPU/Directx9/FramebufferManagerDX9.h b/GPU/Directx9/FramebufferManagerDX9.h index c8dbbe249a..2cc4371c92 100644 --- a/GPU/Directx9/FramebufferManagerDX9.h +++ b/GPU/Directx9/FramebufferManagerDX9.h @@ -42,7 +42,6 @@ public: void SetTextureCache(TextureCacheDX9 *tc); void SetShaderManager(ShaderManagerDX9 *sm); void SetDrawEngine(DrawEngineDX9 *td); - void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) override; void DestroyAllFBOs() override; From 6ca47076e70575fd41ae1b80a75da08148cfb53e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 3 Aug 2022 12:32:42 +0200 Subject: [PATCH 5/8] Delete the Vulkan implementation of DrawActiveTexture, fixups to shader gen --- Common/GPU/ShaderWriter.cpp | 44 ++++++++++-------- Common/GPU/ShaderWriter.h | 4 +- GPU/Common/Draw2D.cpp | 6 +-- GPU/Common/ReinterpretFramebuffer.cpp | 3 +- GPU/Common/StencilCommon.cpp | 4 +- GPU/Vulkan/FramebufferManagerVulkan.cpp | 60 ------------------------- GPU/Vulkan/FramebufferManagerVulkan.h | 3 -- 7 files changed, 35 insertions(+), 89 deletions(-) diff --git a/Common/GPU/ShaderWriter.cpp b/Common/GPU/ShaderWriter.cpp index c8c64d6a72..3fee6a6fd3 100644 --- a/Common/GPU/ShaderWriter.cpp +++ b/Common/GPU/ShaderWriter.cpp @@ -6,7 +6,7 @@ #include "Common/GPU/ShaderWriter.h" #include "Common/Log.h" -const char *vulkan_glsl_preamble_fs = +const char * const vulkan_glsl_preamble_fs = "#version 450\n" "#extension GL_ARB_separate_shader_objects : enable\n" "#extension GL_ARB_shading_language_420pack : enable\n" @@ -18,7 +18,7 @@ const char *vulkan_glsl_preamble_fs = "precision highp int;\n" "\n"; -const char *hlsl_preamble_fs = +const char * const hlsl_preamble_fs = "#define vec2 float2\n" "#define vec3 float3\n" "#define vec4 float4\n" @@ -35,14 +35,14 @@ const char *hlsl_preamble_fs = "#define highp\n" "#define mod(x, y) fmod(x, y)\n"; -const char *hlsl_d3d11_preamble_fs = +static const char * const hlsl_d3d11_preamble_fs = "#define DISCARD discard\n" "#define DISCARD_BELOW(x) clip(x);\n"; -const char *hlsl_d3d9_preamble_fs = +static const char * const hlsl_d3d9_preamble_fs = "#define DISCARD clip(-1)\n" "#define DISCARD_BELOW(x) clip(x)\n"; -const char *vulkan_glsl_preamble_vs = +static const char * const vulkan_glsl_preamble_vs = "#version 450\n" "#extension GL_ARB_separate_shader_objects : enable\n" "#extension GL_ARB_shading_language_420pack : enable\n" @@ -51,7 +51,7 @@ const char *vulkan_glsl_preamble_vs = "precision highp float;\n" "\n"; -const char *hlsl_preamble_vs = +static const char * const hlsl_preamble_vs = "#define vec2 float2\n" "#define vec3 float3\n" "#define vec4 float4\n" @@ -66,6 +66,16 @@ const char *hlsl_preamble_vs = "#define highp\n" "\n"; +static const char * const semanticNames[8] = { + "POSITION", + "COLOR0", + "TEXCOORD0", + "TEXCOORD1", + "NORMAL", + "TANGENT", + "BINORMAL", +}; + // Unsafe. But doesn't matter, we'll use big buffers for shader gen. ShaderWriter & ShaderWriter::F(const char *format, ...) { va_list args; @@ -153,7 +163,7 @@ void ShaderWriter::BeginVSMain(Slice inputs, Slice uniform { C("struct VS_OUTPUT {\n"); for (auto &varying : varyings) { - F(" %s %s : %s;\n", varying.type, varying.name, varying.semantic); + F(" %s %s : %s;\n", varying.type, varying.name, semanticNames[varying.semantic]); } F(" vec4 pos : %s;\n", lang_.shaderLanguage == HLSL_D3D11 ? "SV_Position" : "POSITION"); C("};\n"); @@ -164,27 +174,25 @@ void ShaderWriter::BeginVSMain(Slice inputs, Slice uniform } // List the inputs. for (auto &input : inputs) { - F("in %s %s : %s, ", input.type, input.name, input.semantic); + F("in %s %s : %s, ", input.type, input.name, semanticNames[input.semantic]); } Rewind(2); // Get rid of the last comma. C(") {\n"); C(" vec4 gl_Position;\n"); for (auto &varying : varyings) { - F(" %s %s;\n", varying.type, varying.name); + F(" %s %s; // %s\n", varying.type, varying.name, semanticNames[varying.semantic]); } break; } case GLSL_VULKAN: { - int i = 0; for (auto &input : inputs) { - F("layout(location = %d) in %s %s;\n", i, input.type, input.name); - i++; + F("layout(location = %d) in %s %s;\n", input.semantic /*index*/, input.type, input.name); } for (auto &varying : varyings) { F("layout(location = %d) %s out %s %s; // %s\n", - varying.index, varying.precision ? varying.precision : "", varying.type, varying.name, varying.semantic); + varying.index, varying.precision ? varying.precision : "", varying.type, varying.name, semanticNames[varying.semantic]); } C("void main() {\n"); break; @@ -194,7 +202,7 @@ void ShaderWriter::BeginVSMain(Slice inputs, Slice uniform F("in %s %s;\n", input.type, input.name); } for (auto &varying : varyings) { - F("%s %s %s %s; // %s (%d)\n", lang_.varying_vs, varying.precision ? varying.precision : "", varying.type, varying.name, varying.semantic, varying.index); + F("%s %s %s %s; // %s (%d)\n", lang_.varying_vs, varying.precision ? varying.precision : "", varying.type, varying.name, semanticNames[varying.semantic], varying.index); } C("void main() {\n"); break; @@ -218,7 +226,7 @@ void ShaderWriter::BeginFSMain(Slice uniforms, Slice var // Let's do the varyings as parameters to main, no struct. C("vec4 main("); for (auto &varying : varyings) { - F(" %s %s : %s, ", varying.type, varying.name, varying.semantic); + F(" %s %s : %s, ", varying.type, varying.name, semanticNames[varying.semantic]); } // Erase the last comma Rewind(2); @@ -232,7 +240,7 @@ void ShaderWriter::BeginFSMain(Slice uniforms, Slice var // Let's do the varyings as parameters to main, no struct. C("vec4 main("); for (auto &varying : varyings) { - F(" %s %s : %s, ", varying.type, varying.name, varying.semantic); + F(" %s %s : %s, ", varying.type, varying.name, semanticNames[varying.semantic]); } // Erase the last comma Rewind(2); @@ -241,7 +249,7 @@ void ShaderWriter::BeginFSMain(Slice uniforms, Slice var break; case GLSL_VULKAN: for (auto &varying : varyings) { - F("layout(location = %d) %s in %s %s; // %s\n", varying.index, varying.precision ? varying.precision : "", varying.type, varying.name, varying.semantic); + F("layout(location = %d) %s in %s %s; // %s\n", varying.index, varying.precision ? varying.precision : "", varying.type, varying.name, semanticNames[varying.semantic]); } C("layout(location = 0, index = 0) out vec4 fragColor0;\n"); if (!uniforms.is_empty()) { @@ -256,7 +264,7 @@ void ShaderWriter::BeginFSMain(Slice uniforms, Slice var default: // GLSL OpenGL for (auto &varying : varyings) { - F("%s %s %s %s; // %s\n", lang_.varying_fs, varying.precision ? varying.precision : "", varying.type, varying.name, varying.semantic); + F("%s %s %s %s; // %s\n", lang_.varying_fs, varying.precision ? varying.precision : "", varying.type, varying.name, semanticNames[varying.semantic]); } for (auto &uniform : uniforms) { F("uniform %s %s;\n", uniform.type, uniform.name); diff --git a/Common/GPU/ShaderWriter.h b/Common/GPU/ShaderWriter.h index 5e7f488723..b4a4beb3cc 100644 --- a/Common/GPU/ShaderWriter.h +++ b/Common/GPU/ShaderWriter.h @@ -19,7 +19,7 @@ struct InputDef { const char *type; const char *name; - const char *semantic; + int semantic; }; struct UniformDef { @@ -31,7 +31,7 @@ struct UniformDef { struct VaryingDef { const char *type; const char *name; - const char *semantic; + int semantic; int index; const char *precision; }; diff --git a/GPU/Common/Draw2D.cpp b/GPU/Common/Draw2D.cpp index 7d22dd817f..3f9ac35df4 100644 --- a/GPU/Common/Draw2D.cpp +++ b/GPU/Common/Draw2D.cpp @@ -27,12 +27,12 @@ #include "GPU/Common/TextureCacheCommon.h" static const InputDef inputs[2] = { - { "vec2", "a_position", "POSITION" }, - { "vec2", "a_texcoord", "TEXCOORD0" }, + { "vec2", "a_position", Draw::SEM_POSITION }, + { "vec2", "a_texcoord", Draw::SEM_TEXCOORD0 }, }; static const VaryingDef varyings[1] = { - { "vec2", "v_texcoord", "TEXCOORD0", 0, "highp" }, + { "vec2", "v_texcoord", Draw::SEM_TEXCOORD0, 0, "highp" }, }; void GenerateDraw2DFs(char *buffer, const ShaderLanguageDesc &lang, const Draw::Bugs &bugs) { diff --git a/GPU/Common/ReinterpretFramebuffer.cpp b/GPU/Common/ReinterpretFramebuffer.cpp index 1f0f23b8c0..aa1cd89a1b 100644 --- a/GPU/Common/ReinterpretFramebuffer.cpp +++ b/GPU/Common/ReinterpretFramebuffer.cpp @@ -3,10 +3,11 @@ #include "Common/GPU/Shader.h" #include "Common/GPU/ShaderWriter.h" #include "Common/Log.h" +#include "Common/GPU/thin3d.h" #include "GPU/Common/ReinterpretFramebuffer.h" static const VaryingDef varyings[1] = { - { "vec2", "v_texcoord", "TEXCOORD0", 0, "highp" }, + { "vec2", "v_texcoord", Draw::SEM_TEXCOORD0, 0, "highp" }, }; // TODO: We could possibly have an option to preserve any extra color precision? But gonna start without it. diff --git a/GPU/Common/StencilCommon.cpp b/GPU/Common/StencilCommon.cpp index ca661fa1f0..617880eb58 100644 --- a/GPU/Common/StencilCommon.cpp +++ b/GPU/Common/StencilCommon.cpp @@ -72,11 +72,11 @@ static const UniformDef uniforms[1] = { }; static const InputDef inputs[1] = { - { "vec2", "a_position", "POSITION", } + { "vec2", "a_position", Draw::SEM_POSITION, } }; static const VaryingDef varyings[1] = { - { "vec2", "v_texcoord", "TEXCOORD0", 0, "highp" }, + { "vec2", "v_texcoord", Draw::SEM_TEXCOORD0, 0, "highp" }, }; void GenerateStencilFs(char *buffer, const ShaderLanguageDesc &lang, const Draw::Bugs &bugs) { diff --git a/GPU/Vulkan/FramebufferManagerVulkan.cpp b/GPU/Vulkan/FramebufferManagerVulkan.cpp index 707c20da6e..b0dd6e956d 100644 --- a/GPU/Vulkan/FramebufferManagerVulkan.cpp +++ b/GPU/Vulkan/FramebufferManagerVulkan.cpp @@ -170,66 +170,6 @@ void FramebufferManagerVulkan::NotifyClear(bool clearColor, bool clearAlpha, boo } } -void FramebufferManagerVulkan::DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) { - float texCoords[8] = { - u0,v0, - u1,v0, - u1,v1, - u0,v1, - }; - - if (uvRotation != ROTATION_LOCKED_HORIZONTAL) { - float temp[8]; - int rotation = 0; - switch (uvRotation) { - case ROTATION_LOCKED_HORIZONTAL180: rotation = 4; break; - case ROTATION_LOCKED_VERTICAL: rotation = 2; break; - case ROTATION_LOCKED_VERTICAL180: rotation = 6; break; - } - for (int i = 0; i < 8; i++) { - temp[i] = texCoords[(i + rotation) & 7]; - } - memcpy(texCoords, temp, sizeof(temp)); - } - - Vulkan2D::Vertex vtx[4] = { - {x, y, 0, texCoords[0], texCoords[1]}, - {x + w, y, 0, texCoords[2], texCoords[3]}, - {x, y + h, 0, texCoords[6], texCoords[7]}, - {x + w, y + h, 0, texCoords[4], texCoords[5]}, - }; - - float invDestW = 1.0f / (destW * 0.5f); - float invDestH = 1.0f / (destH * 0.5f); - for (int i = 0; i < 4; i++) { - vtx[i].x = vtx[i].x * invDestW - 1.0f; - vtx[i].y = vtx[i].y * invDestH - 1.0f; - } - - if ((flags & DRAWTEX_TO_BACKBUFFER) && g_display_rotation != DisplayRotation::ROTATE_0) { - for (int i = 0; i < 4; i++) { - Lin::Vec3 v(vtx[i].x, vtx[i].y, 0.0f); - // backwards notation, should fix that... - v = v * g_display_rot_matrix; - vtx[i].x = v.x; - vtx[i].y = v.y; - } - } - - draw_->FlushState(); - - // TODO: Should probably use draw_ directly and not go low level - - VulkanRenderManager *renderManager = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER); - - VkImageView view = (VkImageView)draw_->GetNativeObject(Draw::NativeObject::BOUND_TEXTURE0_IMAGEVIEW); - VkDescriptorSet descSet = vulkan2D_->GetDescriptorSet(view, (flags & DRAWTEX_LINEAR) ? linearSampler_ : nearestSampler_, VK_NULL_HANDLE, VK_NULL_HANDLE); - VkBuffer vbuffer; - VkDeviceSize offset = push_->Push(vtx, sizeof(vtx), &vbuffer); - renderManager->BindPipeline(cur2DPipeline_, (PipelineFlags)0); - renderManager->Draw(vulkan2D_->GetPipelineLayout(), descSet, 0, nullptr, vbuffer, offset, 4); -} - void FramebufferManagerVulkan::Bind2DShader() { VkRenderPass rp = (VkRenderPass)draw_->GetNativeObject(Draw::NativeObject::COMPATIBLE_RENDERPASS); cur2DPipeline_ = vulkan2D_->GetPipeline(rp, vsBasicTex_, fsBasicTex_); diff --git a/GPU/Vulkan/FramebufferManagerVulkan.h b/GPU/Vulkan/FramebufferManagerVulkan.h index 45a07ef9ed..6f60b12652 100644 --- a/GPU/Vulkan/FramebufferManagerVulkan.h +++ b/GPU/Vulkan/FramebufferManagerVulkan.h @@ -43,9 +43,6 @@ public: void SetVulkan2D(Vulkan2D *vk2d) { vulkan2D_ = vk2d; } void SetPushBuffer(VulkanPushBuffer *push) { push_ = push; } - // x,y,w,h are relative to destW, destH which fill out the target completely. - void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) override; - void BeginFrameVulkan(); // there's a BeginFrame in the base class, which this calls void EndFrame(); From 192e331b1195ea3bcd7ec1a3c4a41ee60d022c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 3 Aug 2022 12:35:40 +0200 Subject: [PATCH 6/8] Delete the OpenGL implementation of DrawActiveTexture. --- GPU/GLES/FramebufferManagerGLES.cpp | 77 ----------------------------- GPU/GLES/FramebufferManagerGLES.h | 3 -- 2 files changed, 80 deletions(-) diff --git a/GPU/GLES/FramebufferManagerGLES.cpp b/GPU/GLES/FramebufferManagerGLES.cpp index 42245c4d2e..0e8077e60f 100644 --- a/GPU/GLES/FramebufferManagerGLES.cpp +++ b/GPU/GLES/FramebufferManagerGLES.cpp @@ -162,83 +162,6 @@ FramebufferManagerGLES::~FramebufferManagerGLES() { delete [] convBuf_; } -// x, y, w, h are relative coordinates against destW/destH, which is not very intuitive. -// TODO: This could totally use fbo_blit in many cases. -void FramebufferManagerGLES::DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) { - float texCoords[8] = { - u0,v0, - u1,v0, - u1,v1, - u0,v1, - }; - - if (uvRotation != ROTATION_LOCKED_HORIZONTAL) { - float temp[8]; - int rotation = 0; - // Vertical and Vertical180 needed swapping after we changed the coordinate system. - switch (uvRotation) { - case ROTATION_LOCKED_HORIZONTAL180: rotation = 4; break; - case ROTATION_LOCKED_VERTICAL: rotation = 6; break; - case ROTATION_LOCKED_VERTICAL180: rotation = 2; break; - } - for (int i = 0; i < 8; i++) { - temp[i] = texCoords[(i + rotation) & 7]; - } - memcpy(texCoords, temp, sizeof(temp)); - } - - float pos[12] = { - x,y,0, - x + w,y,0, - x + w,y + h,0, - x,y + h,0 - }; - - float invDestW = 1.0f / (destW * 0.5f); - float invDestH = 1.0f / (destH * 0.5f); - for (int i = 0; i < 4; i++) { - pos[i * 3] = pos[i * 3] * invDestW - 1.0f; - pos[i * 3 + 1] = pos[i * 3 + 1] * invDestH - 1.0f; - } - - // We always want a plain state here, well, except for when it's used by the stencil stuff... - render_->SetDepth(false, false, GL_ALWAYS); - render_->SetRaster(false, GL_CCW, GL_FRONT, GL_FALSE, GL_FALSE); - if (!(flags & DRAWTEX_KEEP_STENCIL_ALPHA)) { - render_->SetNoBlendAndMask(0xF); - render_->SetStencilDisabled(); - } - - // Upscaling postshaders don't look well with linear - if (flags & DRAWTEX_LINEAR) { - render_->SetTextureSampler(0, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_LINEAR, GL_LINEAR, 0.0f); - } else { - render_->SetTextureSampler(0, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST, 0.0f); - } - - Simple2DVertex verts[4]; - memcpy(verts[0].pos, &pos[0], 12); - memcpy(verts[1].pos, &pos[3], 12); - memcpy(verts[3].pos, &pos[6], 12); - memcpy(verts[2].pos, &pos[9], 12); - memcpy(verts[0].uv, &texCoords[0], 8); - memcpy(verts[1].uv, &texCoords[2], 8); - memcpy(verts[3].uv, &texCoords[4], 8); - memcpy(verts[2].uv, &texCoords[6], 8); - - uint32_t bindOffset; - GLRBuffer *buffer; - - // Workaround: This might accidentally get called from ReportScreen screenshot-taking, and in that case - // the push buffer is not mapped. This only happens if framebuffer blit support is not available. - if (drawEngineGL_->GetPushVertexBuffer()->IsReady()) { - void *dest = drawEngineGL_->GetPushVertexBuffer()->Push(sizeof(verts), &bindOffset, &buffer); - memcpy(dest, verts, sizeof(verts)); - render_->BindVertexBuffer(simple2DInputLayout_, buffer, bindOffset); - render_->Draw(GL_TRIANGLE_STRIP, 0, 4); - } -} - void FramebufferManagerGLES::UpdateDownloadTempBuffer(VirtualFramebuffer *nvfb) { _assert_msg_(nvfb->fbo, "Expecting a valid nvfb in UpdateDownloadTempBuffer"); diff --git a/GPU/GLES/FramebufferManagerGLES.h b/GPU/GLES/FramebufferManagerGLES.h index 94b06155e0..fc69c77341 100644 --- a/GPU/GLES/FramebufferManagerGLES.h +++ b/GPU/GLES/FramebufferManagerGLES.h @@ -40,9 +40,6 @@ public: void SetShaderManager(ShaderManagerGLES *sm); void SetDrawEngine(DrawEngineGLES *td); - // x,y,w,h are relative to destW, destH which fill out the target completely. - void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) override; - virtual void Init() override; void EndFrame(); void Resized() override; From 0f9bf47429b55d27fe59e07b0b0c100b3e8d37c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 3 Aug 2022 12:39:53 +0200 Subject: [PATCH 7/8] Reimplement filtering in DrawActiveTexture --- GPU/Common/Draw2D.cpp | 12 ++++++++++-- GPU/Common/FramebufferManagerCommon.cpp | 3 ++- GPU/Common/FramebufferManagerCommon.h | 6 +++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/GPU/Common/Draw2D.cpp b/GPU/Common/Draw2D.cpp index 3f9ac35df4..a9ba00ab4f 100644 --- a/GPU/Common/Draw2D.cpp +++ b/GPU/Common/Draw2D.cpp @@ -57,7 +57,7 @@ void GenerateDraw2DVS(char *buffer, const ShaderLanguageDesc &lang) { } // verts have positions in clip coordinates. -void FramebufferManagerCommon::DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount) { +void FramebufferManagerCommon::DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount, bool linearFilter) { using namespace Draw; if (!draw2DPipelineLinear_) { @@ -112,12 +112,20 @@ void FramebufferManagerCommon::DrawStrip2D(Draw::Texture *tex, Draw2DVertex *ver descLinear.wrapU = TextureAddressMode::CLAMP_TO_EDGE; descLinear.wrapV = TextureAddressMode::CLAMP_TO_EDGE; draw2DSamplerLinear_= draw_->CreateSamplerState(descLinear); + + SamplerStateDesc descNearest{}; + descLinear.magFilter = TextureFilter::NEAREST; + descLinear.minFilter = TextureFilter::NEAREST; + descLinear.mipFilter = TextureFilter::NEAREST; + descLinear.wrapU = TextureAddressMode::CLAMP_TO_EDGE; + descLinear.wrapV = TextureAddressMode::CLAMP_TO_EDGE; + draw2DSamplerNearest_ = draw_->CreateSamplerState(descNearest); } draw_->BindPipeline(draw2DPipelineLinear_); if (tex) { draw_->BindTextures(TEX_SLOT_PSP_TEXTURE, 1, &tex); } - draw_->BindSamplerStates(TEX_SLOT_PSP_TEXTURE, 1, &draw2DSamplerLinear_); + draw_->BindSamplerStates(TEX_SLOT_PSP_TEXTURE, 1, linearFilter ? &draw2DSamplerLinear_ : &draw2DSamplerNearest_); draw_->DrawUP(verts, vertexCount); } diff --git a/GPU/Common/FramebufferManagerCommon.cpp b/GPU/Common/FramebufferManagerCommon.cpp index 1bdf8c0265..3c6315e863 100644 --- a/GPU/Common/FramebufferManagerCommon.cpp +++ b/GPU/Common/FramebufferManagerCommon.cpp @@ -2495,6 +2495,7 @@ void FramebufferManagerCommon::DeviceLost() { DoRelease(stencilUploadSampler_); DoRelease(stencilUploadPipeline_); DoRelease(draw2DPipelineLinear_); + DoRelease(draw2DSamplerNearest_); DoRelease(draw2DSamplerLinear_); DoRelease(draw2DVs_); DoRelease(draw2DFs_); @@ -2555,7 +2556,7 @@ void FramebufferManagerCommon::DrawActiveTexture(float x, float y, float w, floa // Rearrange to strip form. std::swap(coord[2], coord[3]); - DrawStrip2D(nullptr, coord, 4); + DrawStrip2D(nullptr, coord, 4, (flags & DRAWTEX_LINEAR) != 0); gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS | DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE); } diff --git a/GPU/Common/FramebufferManagerCommon.h b/GPU/Common/FramebufferManagerCommon.h index 9140ed18e9..86b1dd96a1 100644 --- a/GPU/Common/FramebufferManagerCommon.h +++ b/GPU/Common/FramebufferManagerCommon.h @@ -164,7 +164,6 @@ enum BindFramebufferColorFlags { enum DrawTextureFlags { DRAWTEX_NEAREST = 0, DRAWTEX_LINEAR = 1, - DRAWTEX_KEEP_STENCIL_ALPHA = 4, DRAWTEX_TO_BACKBUFFER = 8, }; @@ -354,10 +353,10 @@ protected: virtual void PackFramebufferSync_(VirtualFramebuffer *vfb, int x, int y, int w, int h); void SetViewport2D(int x, int y, int w, int h); Draw::Texture *MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height); - virtual void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags); + void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags); virtual void Bind2DShader() = 0; - void DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount); + void DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCount, bool linearFilter); bool UpdateSize(); @@ -479,6 +478,7 @@ protected: // Draw2D pipelines Draw::Pipeline *draw2DPipelineLinear_ = nullptr; Draw::SamplerState *draw2DSamplerLinear_ = nullptr; + Draw::SamplerState *draw2DSamplerNearest_ = nullptr; Draw::ShaderModule *draw2DVs_ = nullptr; Draw::ShaderModule *draw2DFs_ = nullptr; }; From 8ded76650967b4462f0500616e74425eca07a87d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 3 Aug 2022 14:31:24 +0200 Subject: [PATCH 8/8] UWP buildfix --- UWP/GPU_UWP/GPU_UWP.vcxproj | 2 ++ UWP/GPU_UWP/GPU_UWP.vcxproj.filters | 2 ++ 2 files changed, 4 insertions(+) diff --git a/UWP/GPU_UWP/GPU_UWP.vcxproj b/UWP/GPU_UWP/GPU_UWP.vcxproj index 81ab4c82a0..acda27f60f 100644 --- a/UWP/GPU_UWP/GPU_UWP.vcxproj +++ b/UWP/GPU_UWP/GPU_UWP.vcxproj @@ -380,6 +380,7 @@ + @@ -439,6 +440,7 @@ + diff --git a/UWP/GPU_UWP/GPU_UWP.vcxproj.filters b/UWP/GPU_UWP/GPU_UWP.vcxproj.filters index f1f1de951b..9dd217c598 100644 --- a/UWP/GPU_UWP/GPU_UWP.vcxproj.filters +++ b/UWP/GPU_UWP/GPU_UWP.vcxproj.filters @@ -57,6 +57,7 @@ + @@ -116,5 +117,6 @@ + \ No newline at end of file