diff --git a/Common/GPU/Vulkan/VulkanRenderManager.h b/Common/GPU/Vulkan/VulkanRenderManager.h index d4885a8e48..95f9658510 100644 --- a/Common/GPU/Vulkan/VulkanRenderManager.h +++ b/Common/GPU/Vulkan/VulkanRenderManager.h @@ -303,7 +303,8 @@ public: // return value and skip the draw if we're in a bad state. In that case, call ReportBadState. // The old assert wasn't very helpful in figuring out what caused it anyway... bool BindPipeline(VKRGraphicsPipeline *pipeline, PipelineFlags flags, VKRPipelineLayout *pipelineLayout) { - _dbg_assert_(curRenderStep_ && curRenderStep_->stepType == VKRStepType::RENDER && pipeline != nullptr); + _dbg_assert_(pipeline != nullptr); + _dbg_assert_(curRenderStep_ && curRenderStep_->stepType == VKRStepType::RENDER); if (!curRenderStep_ || curRenderStep_->stepType != VKRStepType::RENDER) { return false; } diff --git a/Common/GPU/Vulkan/thin3d_vulkan.cpp b/Common/GPU/Vulkan/thin3d_vulkan.cpp index 4ac1bcb8d7..2af76fb00a 100644 --- a/Common/GPU/Vulkan/thin3d_vulkan.cpp +++ b/Common/GPU/Vulkan/thin3d_vulkan.cpp @@ -474,6 +474,7 @@ public: void BindPipeline(Pipeline *pipeline) override { curPipeline_ = (VKPipeline *)pipeline; + _dbg_assert_(curPipeline_->pipeline); } void BindVertexBuffer(Buffer *vertexBuffer, int offset) override { @@ -1246,8 +1247,10 @@ Pipeline *VKContext::CreateGraphicsPipeline(const PipelineDesc &desc, const char vkshader->AddRef(); pipeline->deps.push_back(vkshader); if (vkshader->GetStage() == ShaderStage::Vertex) { + _dbg_assert_(!gDesc.vertexShader); // can't have two gDesc.vertexShader = vkshader->Get(); } else if (vkshader->GetStage() == ShaderStage::Fragment) { + _dbg_assert_(!gDesc.fragmentShader); // can't have two gDesc.fragmentShader = vkshader->Get(); } else { ERROR_LOG(Log::G3D, "Bad stage"); @@ -1301,6 +1304,7 @@ Pipeline *VKContext::CreateGraphicsPipeline(const PipelineDesc &desc, const char } pipeline->pipeline = renderManager_.CreateGraphicsPipeline(&gDesc, pipelineFlags, 1 << (size_t)RenderPassType::BACKBUFFER, VK_SAMPLE_COUNT_1_BIT, false, tag ? tag : "thin3d"); + _dbg_assert_(pipeline->pipeline); if (desc.uniformDesc) { pipeline->dynamicUniformSize = (int)desc.uniformDesc->uniformBufferSize; diff --git a/Common/GPU/thin3d.cpp b/Common/GPU/thin3d.cpp index 3091c12eae..ea6542b4de 100644 --- a/Common/GPU/thin3d.cpp +++ b/Common/GPU/thin3d.cpp @@ -210,11 +210,14 @@ const UniformDef g_uniforms[] = { { "vec2", "TintSaturation", 1 }, }; -static ShaderModule *GenerateVShader(DrawContext *draw, bool texCoords, bool tint) { +static ShaderModule *GenerateVShader(DrawContext *draw, VertexShaderPreset preset, bool tintSupported) { const ShaderLanguageDesc &shaderLanguageDesc = draw->GetShaderLanguageDesc(); char code[2048]; ShaderWriter vsWriter(code, shaderLanguageDesc, ShaderStage::Vertex); + const bool texCoords = preset != VS_COLOR_2D; + const bool tint = tintSupported && (preset == VS_TEXTURE_COLOR_2D || preset == VS_COLOR_2D); + if (tint) { vsWriter.C(R"( vec3 rgb2hsv(vec3 c) { @@ -252,10 +255,14 @@ vec3 hsv2rgb(vec3 c) { return draw->CreateShaderModule(ShaderStage::Vertex, shaderLanguageDesc.shaderLanguage, (const uint8_t *)code, strlen(code)); } -static ShaderModule *GenerateFShader(DrawContext *draw, bool texturing, bool rbSwizzle) { +static ShaderModule *GenerateFShader(DrawContext *draw, FragmentShaderPreset preset) { const ShaderLanguageDesc &shaderLanguageDesc = draw->GetShaderLanguageDesc(); char code[2048]; ShaderWriter fsWriter(code, shaderLanguageDesc, ShaderStage::Fragment); + + const bool texturing = preset != FS_COLOR_2D; + const bool rbSwizzle = preset == FS_TEXTURE_COLOR_2D_RB_SWIZZLE; + fsWriter.DeclareSamplers(g_samplers); fsWriter.BeginFSMain(g_uniforms, texturing ? Slice(g_varyingsTex) : Slice(g_varyings)); if (texturing) { @@ -266,6 +273,9 @@ static ShaderModule *GenerateFShader(DrawContext *draw, bool texturing, bool rbS } else { fsWriter.C(" * oColor0;\n"); } + if (preset == FS_TEXTURE_COLOR_2D_ALPHA_TO_GRAY) { + fsWriter.C("col.rgb = splat3(col.a);\n"); + } } else { fsWriter.C("vec4 col = oColor0;\n"); } @@ -275,19 +285,23 @@ static ShaderModule *GenerateFShader(DrawContext *draw, bool texturing, bool rbS } bool DrawContext::CreatePresets() { - bool tintSupported = true; - if (bugs_.Has(Bugs::RASPBERRY_SHADER_COMP_HANG)) { - tintSupported = false; + bool tintSupported = !bugs_.Has(Bugs::RASPBERRY_SHADER_COMP_HANG); + + for (int i = 0; i < VS_MAX_PRESET; i++) { + vsPresets_[i] = GenerateVShader(this, (VertexShaderPreset)i, tintSupported); + if (!vsPresets_[i]) { + WARN_LOG(Log::G3D, "Failed to create vertex shader preset %d", i); + return false; + } } - - vsPresets_[VS_TEXTURE_COLOR_2D] = GenerateVShader(this, true, tintSupported); - vsPresets_[VS_COLOR_2D] = GenerateVShader(this, false, tintSupported); - - fsPresets_[FS_TEXTURE_COLOR_2D] = GenerateFShader(this, true, false); - fsPresets_[FS_COLOR_2D] = GenerateFShader(this, false, false); - fsPresets_[FS_TEXTURE_COLOR_2D_RB_SWIZZLE] = GenerateFShader(this, true, true); - - return vsPresets_[VS_TEXTURE_COLOR_2D] && vsPresets_[VS_COLOR_2D] && fsPresets_[FS_TEXTURE_COLOR_2D] && fsPresets_[FS_COLOR_2D] && fsPresets_[FS_TEXTURE_COLOR_2D_RB_SWIZZLE]; + for (int i = 0; i < FS_MAX_PRESET; i++) { + fsPresets_[i] = GenerateFShader(this, (FragmentShaderPreset)i); + if (!fsPresets_[i]) { + WARN_LOG(Log::G3D, "Failed to create fragment shader preset %d", i); + return false; + } + } + return true; } void DrawContext::DestroyPresets() { diff --git a/Common/GPU/thin3d.h b/Common/GPU/thin3d.h index b5d8019abf..32a184db96 100644 --- a/Common/GPU/thin3d.h +++ b/Common/GPU/thin3d.h @@ -148,6 +148,7 @@ enum class Primitive { enum VertexShaderPreset : int { VS_COLOR_2D, VS_TEXTURE_COLOR_2D, + VS_TEXTURE_COLOR_2D_NO_TINT, VS_MAX_PRESET, }; @@ -155,6 +156,7 @@ enum FragmentShaderPreset : int { FS_COLOR_2D, FS_TEXTURE_COLOR_2D, FS_TEXTURE_COLOR_2D_RB_SWIZZLE, + FS_TEXTURE_COLOR_2D_ALPHA_TO_GRAY, FS_MAX_PRESET, }; @@ -770,8 +772,16 @@ public: virtual Pipeline *CreateGraphicsPipeline(const PipelineDesc &desc, const char *tag) = 0; // Note that these DO NOT AddRef so you must not ->Release presets unless you manually AddRef them. - ShaderModule *GetVshaderPreset(VertexShaderPreset preset) { return vsPresets_[preset]; } - ShaderModule *GetFshaderPreset(FragmentShaderPreset preset) { return fsPresets_[preset]; } + ShaderModule *GetVshaderPreset(VertexShaderPreset preset) { + ShaderModule *module = vsPresets_[preset]; + _dbg_assert_(module->GetStage() == ShaderStage::Vertex); + return module; + } + ShaderModule *GetFshaderPreset(FragmentShaderPreset preset) { + ShaderModule *module = fsPresets_[preset]; + _dbg_assert_(module->GetStage() == ShaderStage::Fragment); + return module; + } // Resources virtual Buffer *CreateBuffer(size_t size, uint32_t usageFlags) = 0; @@ -911,13 +921,13 @@ public: protected: HistoryBuffer frameTimeHistory_; - ShaderModule *vsPresets_[VS_MAX_PRESET]; - ShaderModule *fsPresets_[FS_MAX_PRESET]; + ShaderModule *vsPresets_[VS_MAX_PRESET]{}; + ShaderModule *fsPresets_[FS_MAX_PRESET]{}; ShaderLanguageDesc shaderLanguageDesc_; - int targetWidth_; - int targetHeight_; + int targetWidth_ = 0; + int targetHeight_ = 0; Bugs bugs_; }; diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index 675c6ed857..3f239901c0 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -543,12 +543,6 @@ bool DrawEngineCommon::TestBoundingBoxFast(const float *cullMatrix, const void * // 2D bounding box test against scissor. No indexing yet. // Only supports non-indexed draws with float positions. TODO: Add more float formats. bool DrawEngineCommon::TestBoundingBoxThrough(GEPrimitiveType prim, const void *vdata, const void *idata, int vertexCount, const VertexDecoder *dec, u32 vertType, int *bytesRead, ClipInfoFlags *flags) { - // Although this may lead to drawing that shouldn't happen, the viewport is more complex on VR. - // Let's always say objects are within bounds. - if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) { - return true; - } - // For through mode, we only check FlatZ. *flags |= ClipInfoFlags::Valid; @@ -628,6 +622,13 @@ bool DrawEngineCommon::TestBoundingBoxThrough(GEPrimitiveType prim, const void * } } } + + // Although this may lead to drawing that shouldn't happen, the viewport is more complex on VR. + // Let's always say objects are within bounds. + if (gstate_c.Use(GPU_USE_VIRTUAL_REALITY)) { + return true; + } + if (allOutsideLeft || allOutsideTop || allOutsideRight || allOutsideBottom) { return false; } diff --git a/GPU/Common/FragmentShaderGenerator.cpp b/GPU/Common/FragmentShaderGenerator.cpp index 4d74670013..a9c5939ef9 100644 --- a/GPU/Common/FragmentShaderGenerator.cpp +++ b/GPU/Common/FragmentShaderGenerator.cpp @@ -1204,12 +1204,12 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu if (writeDepth) { if (gstate_c.Use(GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT)) { if (fsDepthClamp) { - WRITE(p, " gl_FragDepth = clamp(floor(projZ), 0.0, 65536.0) / 65536.0;\n"); + WRITE(p, " gl_FragDepth = clamp(floor(projZ), 0.0, 65535.0) / 65535.0;\n"); } else { - WRITE(p, " gl_FragDepth = floor(gl_FragCoord.z) / 65536.0;\n"); + WRITE(p, " gl_FragDepth = floor(gl_FragCoord.z * 65535.0) / 65535.0;\n"); } } else if (fsDepthClamp) { - WRITE(p, " gl_FragDepth = clamp(projZ, 0.0, 65536.0) / 65536.0;\n"); + WRITE(p, " gl_FragDepth = clamp(projZ, 0.0, 65535.0) / 65535.0;\n"); } else if (useDiscardStencilBugWorkaround) { // Adreno and some Mali drivers apply early frag tests even with discard in the shader, // when only stencil is used. The exact situation seems to vary by driver. diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index a804e2851d..5a59ea51da 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -975,12 +975,14 @@ bool CreateGlobalPipelines() { colorPipeline = g_draw->CreateGraphicsPipeline(colorDesc, "global_color"); if (!colorPipeline) { + _dbg_assert_(false); // Something really critical is wrong, don't care much about correct releasing of the states. return false; } texColorPipeline = g_draw->CreateGraphicsPipeline(texColorDesc, "global_texcolor"); if (!texColorPipeline) { + _dbg_assert_(false); // Something really critical is wrong, don't care much about correct releasing of the states. return false; } diff --git a/assets/compat.ini b/assets/compat.ini index c0007c4b4a..95a945938a 100644 --- a/assets/compat.ini +++ b/assets/compat.ini @@ -88,6 +88,7 @@ ULES00785 = true ULJS00242 = true [PixelDepthRounding] + # Heroes Phantasia requires pixel depth rounding. #6485 (flickering overlaid sprites) NPJH50558 = true ULJS00456 = true diff --git a/ext/imgui/imgui_impl_thin3d.cpp b/ext/imgui/imgui_impl_thin3d.cpp index 4bef970ed8..6ff8590086 100644 --- a/ext/imgui/imgui_impl_thin3d.cpp +++ b/ext/imgui/imgui_impl_thin3d.cpp @@ -34,7 +34,7 @@ struct RegisteredTexture { struct BackendData { Draw::SamplerState *fontSampler = nullptr; Draw::Texture *fontImage = nullptr; - Draw::Pipeline *pipelines[2]{}; + Draw::Pipeline *pipelines[(int)ImGuiPipeline::Count]{}; std::vector tempTextures; }; @@ -220,12 +220,9 @@ bool ImGui_ImplThin3d_CreateDeviceObjects(Draw::DrawContext *draw) { DepthStencilState *depthStencil = draw->CreateDepthStencilState(dsDesc); RasterState *rasterNoCull = draw->CreateRasterState({}); - ShaderModule *vs_texture_color_2d = draw->GetVshaderPreset(VS_TEXTURE_COLOR_2D); - ShaderModule *fs_texture_color_2d = draw->GetFshaderPreset(FS_TEXTURE_COLOR_2D); - PipelineDesc pipelineDesc{ Primitive::TRIANGLE_LIST, - { vs_texture_color_2d, fs_texture_color_2d }, + { draw->GetVshaderPreset(VS_TEXTURE_COLOR_2D_NO_TINT), draw->GetFshaderPreset(FS_TEXTURE_COLOR_2D) }, inputLayout, depthStencil, blend, @@ -233,9 +230,13 @@ bool ImGui_ImplThin3d_CreateDeviceObjects(Draw::DrawContext *draw) { &vsTexColBufDesc }; - bd->pipelines[0] = draw->CreateGraphicsPipeline(pipelineDesc, "imgui-pipeline"); + bd->pipelines[(int)ImGuiPipeline::TexturedAlphaBlend] = draw->CreateGraphicsPipeline(pipelineDesc, "imgui-pipeline"); pipelineDesc.blend = blendOpaque; - bd->pipelines[1] = draw->CreateGraphicsPipeline(pipelineDesc, "imgui-pipeline-opaque"); + bd->pipelines[(int)ImGuiPipeline::TexturedOpaque] = draw->CreateGraphicsPipeline(pipelineDesc, "imgui-pipeline-opaque"); + + pipelineDesc.shaders[1] = draw->GetFshaderPreset(FS_TEXTURE_COLOR_2D_ALPHA_TO_GRAY); + + bd->pipelines[(int)ImGuiPipeline::TexturedAlphaToGrayscale] = draw->CreateGraphicsPipeline(pipelineDesc, "imgui-pipeline-alpha-to-gray"); inputLayout->Release(); blend->Release(); diff --git a/ext/imgui/imgui_impl_thin3d.h b/ext/imgui/imgui_impl_thin3d.h index 26f8386d59..a403e57ed8 100644 --- a/ext/imgui/imgui_impl_thin3d.h +++ b/ext/imgui/imgui_impl_thin3d.h @@ -46,6 +46,8 @@ IMGUI_IMPL_API void ImGui_ImplThin3d_DestroyDeviceObjects(); enum class ImGuiPipeline { TexturedAlphaBlend = 0, TexturedOpaque = 1, + TexturedAlphaToGrayscale = 2, + Count, }; // These register a texture for imgui drawing, but just for the current frame.