diff options
Diffstat (limited to '')
22 files changed, 390 insertions, 239 deletions
diff --git a/src/video_core/engines/fermi_2d.cpp b/src/video_core/engines/fermi_2d.cpp index 98a8b5337..7ff44f06d 100644 --- a/src/video_core/engines/fermi_2d.cpp +++ b/src/video_core/engines/fermi_2d.cpp @@ -29,8 +29,8 @@ void Fermi2D::CallMethod(const GPU::MethodCall& method_call) { } void Fermi2D::HandleSurfaceCopy() { - LOG_WARNING(HW_GPU, "Requested a surface copy with operation {}", - static_cast<u32>(regs.operation)); + LOG_DEBUG(HW_GPU, "Requested a surface copy with operation {}", + static_cast<u32>(regs.operation)); // TODO(Subv): Only raw copies are implemented. ASSERT(regs.operation == Operation::SrcCopy); diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp index c7a3c85a0..b318aedb8 100644 --- a/src/video_core/engines/maxwell_3d.cpp +++ b/src/video_core/engines/maxwell_3d.cpp @@ -92,6 +92,10 @@ void Maxwell3D::InitializeRegisterDefaults() { // Some games (like Super Mario Odyssey) assume that SRGB is enabled. regs.framebuffer_srgb = 1; + mme_inline[MAXWELL3D_REG_INDEX(draw.vertex_end_gl)] = true; + mme_inline[MAXWELL3D_REG_INDEX(draw.vertex_begin_gl)] = true; + mme_inline[MAXWELL3D_REG_INDEX(vertex_buffer.count)] = true; + mme_inline[MAXWELL3D_REG_INDEX(index_array.count)] = true; } #define DIRTY_REGS_POS(field_name) (offsetof(Maxwell3D::DirtyRegs, field_name)) @@ -256,6 +260,9 @@ void Maxwell3D::CallMacroMethod(u32 method, std::size_t num_parameters, const u3 // Execute the current macro. macro_interpreter.Execute(macro_positions[entry], num_parameters, parameters); + if (mme_draw.current_mode != MMEDrawMode::Undefined) { + FlushMMEInlineDraw(); + } } void Maxwell3D::CallMethod(const GPU::MethodCall& method_call) { @@ -416,6 +423,97 @@ void Maxwell3D::CallMethod(const GPU::MethodCall& method_call) { } } +void Maxwell3D::StepInstance(const MMEDrawMode expected_mode, const u32 count) { + if (mme_draw.current_mode == MMEDrawMode::Undefined) { + if (mme_draw.gl_begin_consume) { + mme_draw.current_mode = expected_mode; + mme_draw.current_count = count; + mme_draw.instance_count = 1; + mme_draw.gl_begin_consume = false; + mme_draw.gl_end_count = 0; + } + return; + } else { + if (mme_draw.current_mode == expected_mode && count == mme_draw.current_count && + mme_draw.instance_mode && mme_draw.gl_begin_consume) { + mme_draw.instance_count++; + mme_draw.gl_begin_consume = false; + return; + } else { + FlushMMEInlineDraw(); + } + } + // Tail call in case it needs to retry. + StepInstance(expected_mode, count); +} + +void Maxwell3D::CallMethodFromMME(const GPU::MethodCall& method_call) { + const u32 method = method_call.method; + if (mme_inline[method]) { + regs.reg_array[method] = method_call.argument; + if (method == MAXWELL3D_REG_INDEX(vertex_buffer.count) || + method == MAXWELL3D_REG_INDEX(index_array.count)) { + const MMEDrawMode expected_mode = method == MAXWELL3D_REG_INDEX(vertex_buffer.count) + ? MMEDrawMode::Array + : MMEDrawMode::Indexed; + StepInstance(expected_mode, method_call.argument); + } else if (method == MAXWELL3D_REG_INDEX(draw.vertex_begin_gl)) { + mme_draw.instance_mode = + (regs.draw.instance_next != 0) || (regs.draw.instance_cont != 0); + mme_draw.gl_begin_consume = true; + } else { + mme_draw.gl_end_count++; + } + } else { + if (mme_draw.current_mode != MMEDrawMode::Undefined) { + FlushMMEInlineDraw(); + } + CallMethod(method_call); + } +} + +void Maxwell3D::FlushMMEInlineDraw() { + LOG_DEBUG(HW_GPU, "called, topology={}, count={}", static_cast<u32>(regs.draw.topology.Value()), + regs.vertex_buffer.count); + ASSERT_MSG(!(regs.index_array.count && regs.vertex_buffer.count), "Both indexed and direct?"); + ASSERT(mme_draw.instance_count == mme_draw.gl_end_count); + + auto debug_context = system.GetGPUDebugContext(); + + if (debug_context) { + debug_context->OnEvent(Tegra::DebugContext::Event::IncomingPrimitiveBatch, nullptr); + } + + // Both instance configuration registers can not be set at the same time. + ASSERT_MSG(!regs.draw.instance_next || !regs.draw.instance_cont, + "Illegal combination of instancing parameters"); + + const bool is_indexed = mme_draw.current_mode == MMEDrawMode::Indexed; + if (ShouldExecute()) { + rasterizer.DrawMultiBatch(is_indexed); + } + + if (debug_context) { + debug_context->OnEvent(Tegra::DebugContext::Event::FinishedPrimitiveBatch, nullptr); + } + + // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if + // the game is trying to draw indexed or direct mode. This needs to be verified on HW still - + // it's possible that it is incorrect and that there is some other register used to specify the + // drawing mode. + if (is_indexed) { + regs.index_array.count = 0; + } else { + regs.vertex_buffer.count = 0; + } + mme_draw.current_mode = MMEDrawMode::Undefined; + mme_draw.current_count = 0; + mme_draw.instance_count = 0; + mme_draw.instance_mode = false; + mme_draw.gl_begin_consume = false; + mme_draw.gl_end_count = 0; +} + void Maxwell3D::ProcessMacroUpload(u32 data) { ASSERT_MSG(regs.macros.upload_address < macro_memory.size(), "upload_address exceeded macro_memory size!"); @@ -541,7 +639,7 @@ void Maxwell3D::ProcessSyncPoint() { } void Maxwell3D::DrawArrays() { - LOG_DEBUG(HW_GPU, "called, topology={}, count={}", static_cast<u32>(regs.draw.topology.Value()), + LOG_TRACE(HW_GPU, "called, topology={}, count={}", static_cast<u32>(regs.draw.topology.Value()), regs.vertex_buffer.count); ASSERT_MSG(!(regs.index_array.count && regs.vertex_buffer.count), "Both indexed and direct?"); @@ -564,7 +662,9 @@ void Maxwell3D::DrawArrays() { } const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count}; - rasterizer.AccelerateDrawBatch(is_indexed); + if (ShouldExecute()) { + rasterizer.DrawBatch(is_indexed); + } if (debug_context) { debug_context->OnEvent(Tegra::DebugContext::Event::FinishedPrimitiveBatch, nullptr); diff --git a/src/video_core/engines/maxwell_3d.h b/src/video_core/engines/maxwell_3d.h index e5ec90717..4c97759ed 100644 --- a/src/video_core/engines/maxwell_3d.h +++ b/src/video_core/engines/maxwell_3d.h @@ -811,8 +811,9 @@ public: INSERT_PADDING_WORDS(0x21); u32 vb_element_base; + u32 vb_base_instance; - INSERT_PADDING_WORDS(0x36); + INSERT_PADDING_WORDS(0x35); union { BitField<0, 1, u32> c0; @@ -1238,6 +1239,11 @@ public: /// Write the value to the register identified by method. void CallMethod(const GPU::MethodCall& method_call); + /// Write the value to the register identified by method. + void CallMethodFromMME(const GPU::MethodCall& method_call); + + void FlushMMEInlineDraw(); + /// Given a Texture Handle, returns the TSC and TIC entries. Texture::FullTextureInfo GetTextureInfo(const Texture::TextureHandle tex_handle, std::size_t offset) const; @@ -1263,6 +1269,21 @@ public: return execute_on; } + enum class MMEDrawMode : u32 { + Undefined, + Array, + Indexed, + }; + + struct MMEDrawState { + MMEDrawMode current_mode{MMEDrawMode::Undefined}; + u32 current_count{}; + u32 instance_count{}; + bool instance_mode{}; + bool gl_begin_consume{}; + u32 gl_end_count{}; + } mme_draw; + private: void InitializeRegisterDefaults(); @@ -1275,6 +1296,8 @@ private: /// Start offsets of each macro in macro_memory std::array<u32, 0x80> macro_positions = {}; + std::array<bool, Regs::NUM_REGS> mme_inline{}; + /// Memory for macro code MacroMemory macro_memory; @@ -1346,6 +1369,9 @@ private: /// Handles a write to the VERTEX_END_GL register, triggering a draw. void DrawArrays(); + + // Handles a instance drawcall from MME + void StepInstance(MMEDrawMode expected_mode, u32 count); }; #define ASSERT_REG_POSITION(field_name, position) \ @@ -1402,6 +1428,7 @@ ASSERT_REG_POSITION(stencil_front_mask, 0x4E7); ASSERT_REG_POSITION(frag_color_clamp, 0x4EA); ASSERT_REG_POSITION(screen_y_control, 0x4EB); ASSERT_REG_POSITION(vb_element_base, 0x50D); +ASSERT_REG_POSITION(vb_base_instance, 0x50E); ASSERT_REG_POSITION(clip_distance_enabled, 0x544); ASSERT_REG_POSITION(point_size, 0x546); ASSERT_REG_POSITION(zeta_enable, 0x54E); diff --git a/src/video_core/engines/shader_bytecode.h b/src/video_core/engines/shader_bytecode.h index b46fcf03d..7a6355ce2 100644 --- a/src/video_core/engines/shader_bytecode.h +++ b/src/video_core/engines/shader_bytecode.h @@ -950,6 +950,11 @@ union Instruction { } isetp; union { + BitField<48, 1, u64> is_signed; + BitField<49, 3, PredCondition> cond; + } icmp; + + union { BitField<0, 3, u64> pred0; BitField<3, 3, u64> pred3; BitField<12, 3, u64> pred12; @@ -1646,6 +1651,10 @@ public: SEL_C, SEL_R, SEL_IMM, + ICMP_RC, + ICMP_R, + ICMP_CR, + ICMP_IMM, MUFU, // Multi-Function Operator RRO_C, // Range Reduction Operator RRO_R, @@ -1912,6 +1921,10 @@ private: INST("0100110010100---", Id::SEL_C, Type::ArithmeticInteger, "SEL_C"), INST("0101110010100---", Id::SEL_R, Type::ArithmeticInteger, "SEL_R"), INST("0011100-10100---", Id::SEL_IMM, Type::ArithmeticInteger, "SEL_IMM"), + INST("010100110100----", Id::ICMP_RC, Type::ArithmeticInteger, "ICMP_RC"), + INST("010110110100----", Id::ICMP_R, Type::ArithmeticInteger, "ICMP_R"), + INST("010010110100----", Id::ICMP_CR, Type::ArithmeticInteger, "ICMP_CR"), + INST("0011011-0100----", Id::ICMP_IMM, Type::ArithmeticInteger, "ICMP_IMM"), INST("0101101111011---", Id::LEA_R2, Type::ArithmeticInteger, "LEA_R2"), INST("0101101111010---", Id::LEA_R1, Type::ArithmeticInteger, "LEA_R1"), INST("001101101101----", Id::LEA_IMM, Type::ArithmeticInteger, "LEA_IMM"), diff --git a/src/video_core/gpu.cpp b/src/video_core/gpu.cpp index 2c47541cb..76cfe8107 100644 --- a/src/video_core/gpu.cpp +++ b/src/video_core/gpu.cpp @@ -122,6 +122,7 @@ u32 RenderTargetBytesPerPixel(RenderTargetFormat format) { case RenderTargetFormat::RGBA16_UINT: case RenderTargetFormat::RGBA16_UNORM: case RenderTargetFormat::RGBA16_FLOAT: + case RenderTargetFormat::RGBX16_FLOAT: case RenderTargetFormat::RG32_FLOAT: case RenderTargetFormat::RG32_UINT: return 8; diff --git a/src/video_core/gpu.h b/src/video_core/gpu.h index 78bc0601a..29fa8e95b 100644 --- a/src/video_core/gpu.h +++ b/src/video_core/gpu.h @@ -42,6 +42,7 @@ enum class RenderTargetFormat : u32 { RGBA16_FLOAT = 0xCA, RG32_FLOAT = 0xCB, RG32_UINT = 0xCD, + RGBX16_FLOAT = 0xCE, BGRA8_UNORM = 0xCF, BGRA8_SRGB = 0xD0, RGB10_A2_UNORM = 0xD1, diff --git a/src/video_core/macro_interpreter.cpp b/src/video_core/macro_interpreter.cpp index 62afc0d11..dbaeac6db 100644 --- a/src/video_core/macro_interpreter.cpp +++ b/src/video_core/macro_interpreter.cpp @@ -257,7 +257,7 @@ void MacroInterpreter::SetMethodAddress(u32 address) { } void MacroInterpreter::Send(u32 value) { - maxwell3d.CallMethod({method_address.address, value}); + maxwell3d.CallMethodFromMME({method_address.address, value}); // Increment the method address by the method increment. method_address.address.Assign(method_address.address.Value() + method_address.increment.Value()); diff --git a/src/video_core/morton.cpp b/src/video_core/morton.cpp index 084f85e67..ab71870ab 100644 --- a/src/video_core/morton.cpp +++ b/src/video_core/morton.cpp @@ -83,6 +83,7 @@ static constexpr ConversionArray morton_to_linear_fns = { MortonCopy<true, PixelFormat::RG8U>, MortonCopy<true, PixelFormat::RG8S>, MortonCopy<true, PixelFormat::RG32UI>, + MortonCopy<true, PixelFormat::RGBX16F>, MortonCopy<true, PixelFormat::R32UI>, MortonCopy<true, PixelFormat::ASTC_2D_8X8>, MortonCopy<true, PixelFormat::ASTC_2D_8X5>, @@ -151,6 +152,7 @@ static constexpr ConversionArray linear_to_morton_fns = { MortonCopy<false, PixelFormat::RG8U>, MortonCopy<false, PixelFormat::RG8S>, MortonCopy<false, PixelFormat::RG32UI>, + MortonCopy<false, PixelFormat::RGBX16F>, MortonCopy<false, PixelFormat::R32UI>, nullptr, nullptr, diff --git a/src/video_core/rasterizer_interface.h b/src/video_core/rasterizer_interface.h index 6b3f2d50a..5b0eca9e2 100644 --- a/src/video_core/rasterizer_interface.h +++ b/src/video_core/rasterizer_interface.h @@ -29,7 +29,10 @@ public: virtual ~RasterizerInterface() {} /// Draw the current batch of vertex arrays - virtual void DrawArrays() = 0; + virtual bool DrawBatch(bool is_indexed) = 0; + + /// Draw the current batch of multiple instances of vertex arrays + virtual bool DrawMultiBatch(bool is_indexed) = 0; /// Clear the current framebuffer virtual void Clear() = 0; @@ -69,10 +72,6 @@ public: return false; } - virtual bool AccelerateDrawBatch(bool is_indexed) { - return false; - } - /// Increase/decrease the number of object in pages touching the specified region virtual void UpdatePagesCachedCount(VAddr addr, u64 size, int delta) {} diff --git a/src/video_core/renderer_opengl/gl_framebuffer_cache.cpp b/src/video_core/renderer_opengl/gl_framebuffer_cache.cpp index 7c926bd48..a5d69d78d 100644 --- a/src/video_core/renderer_opengl/gl_framebuffer_cache.cpp +++ b/src/video_core/renderer_opengl/gl_framebuffer_cache.cpp @@ -35,21 +35,16 @@ OGLFramebuffer FramebufferCacheOpenGL::CreateFramebuffer(const FramebufferCacheK local_state.draw.draw_framebuffer = framebuffer.handle; local_state.ApplyFramebufferState(); - if (key.is_single_buffer) { - if (key.color_attachments[0] != GL_NONE && key.colors[0]) { - key.colors[0]->Attach(key.color_attachments[0], GL_DRAW_FRAMEBUFFER); - glDrawBuffer(key.color_attachments[0]); - } else { - glDrawBuffer(GL_NONE); - } - } else { - for (std::size_t index = 0; index < Maxwell::NumRenderTargets; ++index) { - if (key.colors[index]) { - key.colors[index]->Attach(GL_COLOR_ATTACHMENT0 + static_cast<GLenum>(index), - GL_DRAW_FRAMEBUFFER); - } + for (std::size_t index = 0; index < Maxwell::NumRenderTargets; ++index) { + if (key.colors[index]) { + key.colors[index]->Attach(GL_COLOR_ATTACHMENT0 + static_cast<GLenum>(index), + GL_DRAW_FRAMEBUFFER); } + } + if (key.colors_count) { glDrawBuffers(key.colors_count, key.color_attachments.data()); + } else { + glDrawBuffer(GL_NONE); } if (key.zeta) { @@ -67,9 +62,9 @@ std::size_t FramebufferCacheKey::Hash() const { } bool FramebufferCacheKey::operator==(const FramebufferCacheKey& rhs) const { - return std::tie(is_single_buffer, stencil_enable, colors_count, color_attachments, colors, - zeta) == std::tie(rhs.is_single_buffer, rhs.stencil_enable, rhs.colors_count, - rhs.color_attachments, rhs.colors, rhs.zeta); + return std::tie(stencil_enable, colors_count, color_attachments, colors, zeta) == + std::tie(rhs.stencil_enable, rhs.colors_count, rhs.color_attachments, rhs.colors, + rhs.zeta); } } // namespace OpenGL diff --git a/src/video_core/renderer_opengl/gl_framebuffer_cache.h b/src/video_core/renderer_opengl/gl_framebuffer_cache.h index a3a996353..424344c48 100644 --- a/src/video_core/renderer_opengl/gl_framebuffer_cache.h +++ b/src/video_core/renderer_opengl/gl_framebuffer_cache.h @@ -19,7 +19,6 @@ namespace OpenGL { struct alignas(sizeof(u64)) FramebufferCacheKey { - bool is_single_buffer = false; bool stencil_enable = false; u16 colors_count = 0; diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 4dd08bccb..6a17bed72 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -49,40 +49,6 @@ MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(128, 128, 192)); MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Mgmt", MP_RGB(100, 255, 100)); MICROPROFILE_DEFINE(OpenGL_PrimitiveAssembly, "OpenGL", "Prim Asmbl", MP_RGB(255, 100, 100)); -struct DrawParameters { - GLenum primitive_mode; - GLsizei count; - GLint current_instance; - bool use_indexed; - - GLint vertex_first; - - GLenum index_format; - GLint base_vertex; - GLintptr index_buffer_offset; - - void DispatchDraw() const { - if (use_indexed) { - const auto index_buffer_ptr = reinterpret_cast<const void*>(index_buffer_offset); - if (current_instance > 0) { - glDrawElementsInstancedBaseVertexBaseInstance(primitive_mode, count, index_format, - index_buffer_ptr, 1, base_vertex, - current_instance); - } else { - glDrawElementsBaseVertex(primitive_mode, count, index_format, index_buffer_ptr, - base_vertex); - } - } else { - if (current_instance > 0) { - glDrawArraysInstancedBaseInstance(primitive_mode, vertex_first, count, 1, - current_instance); - } else { - glDrawArrays(primitive_mode, vertex_first, count); - } - } - } -}; - static std::size_t GetConstBufferSize(const Tegra::Engines::ConstBufferInfo& buffer, const GLShader::ConstBufferEntry& entry) { if (!entry.IsIndirect()) { @@ -270,29 +236,6 @@ GLintptr RasterizerOpenGL::SetupIndexBuffer() { return offset; } -DrawParameters RasterizerOpenGL::SetupDraw(GLintptr index_buffer_offset) { - const auto& gpu = system.GPU().Maxwell3D(); - const auto& regs = gpu.regs; - const bool is_indexed = accelerate_draw == AccelDraw::Indexed; - - DrawParameters params{}; - params.current_instance = gpu.state.current_instance; - - params.use_indexed = is_indexed; - params.primitive_mode = MaxwellToGL::PrimitiveTopology(regs.draw.topology); - - if (is_indexed) { - params.index_format = MaxwellToGL::IndexFormat(regs.index_array.format); - params.count = regs.index_array.count; - params.index_buffer_offset = index_buffer_offset; - params.base_vertex = static_cast<GLint>(regs.vb_element_base); - } else { - params.count = regs.vertex_buffer.count; - params.vertex_first = regs.vertex_buffer.first; - } - return params; -} - void RasterizerOpenGL::SetupShaders(GLenum primitive_mode) { MICROPROFILE_SCOPE(OpenGL_Shader); auto& gpu = system.GPU().Maxwell3D(); @@ -399,12 +342,6 @@ std::size_t RasterizerOpenGL::CalculateIndexBufferSize() const { static_cast<std::size_t>(regs.index_array.FormatSizeInBytes()); } -bool RasterizerOpenGL::AccelerateDrawBatch(bool is_indexed) { - accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays; - DrawArrays(); - return true; -} - template <typename Map, typename Interval> static constexpr auto RangeFromInterval(Map& map, const Interval& interval) { return boost::make_iterator_range(map.equal_range(interval)); @@ -445,99 +382,51 @@ void RasterizerOpenGL::LoadDiskResources(const std::atomic_bool& stop_loading, shader_cache.LoadDiskCache(stop_loading, callback); } -std::pair<bool, bool> RasterizerOpenGL::ConfigureFramebuffers( - OpenGLState& current_state, bool using_color_fb, bool using_depth_fb, bool preserve_contents, - std::optional<std::size_t> single_color_target) { +void RasterizerOpenGL::ConfigureFramebuffers() { MICROPROFILE_SCOPE(OpenGL_Framebuffer); auto& gpu = system.GPU().Maxwell3D(); - const auto& regs = gpu.regs; - - const FramebufferConfigState fb_config_state{using_color_fb, using_depth_fb, preserve_contents, - single_color_target}; - if (fb_config_state == current_framebuffer_config_state && !gpu.dirty.render_settings) { - // Only skip if the previous ConfigureFramebuffers call was from the same kind (multiple or - // single color targets). This is done because the guest registers may not change but the - // host framebuffer may contain different attachments - return current_depth_stencil_usage; + if (!gpu.dirty.render_settings) { + return; } gpu.dirty.render_settings = false; - current_framebuffer_config_state = fb_config_state; texture_cache.GuardRenderTargets(true); - View depth_surface{}; - if (using_depth_fb) { - depth_surface = texture_cache.GetDepthBufferSurface(preserve_contents); - } else { - texture_cache.SetEmptyDepthBuffer(); - } + View depth_surface = texture_cache.GetDepthBufferSurface(true); + const auto& regs = gpu.regs; + state.framebuffer_srgb.enabled = regs.framebuffer_srgb != 0; UNIMPLEMENTED_IF(regs.rt_separate_frag_data == 0); // Bind the framebuffer surfaces - current_state.framebuffer_srgb.enabled = regs.framebuffer_srgb != 0; - FramebufferCacheKey fbkey; + for (std::size_t index = 0; index < Maxwell::NumRenderTargets; ++index) { + View color_surface{texture_cache.GetColorBufferSurface(index, true)}; - if (using_color_fb) { - if (single_color_target) { - // Used when just a single color attachment is enabled, e.g. for clearing a color buffer - View color_surface{ - texture_cache.GetColorBufferSurface(*single_color_target, preserve_contents)}; - - if (color_surface) { - // Assume that a surface will be written to if it is used as a framebuffer, even if - // the shader doesn't actually write to it. - texture_cache.MarkColorBufferInUse(*single_color_target); - } - - fbkey.is_single_buffer = true; - fbkey.color_attachments[0] = - GL_COLOR_ATTACHMENT0 + static_cast<GLenum>(*single_color_target); - fbkey.colors[0] = color_surface; - for (std::size_t index = 0; index < Maxwell::NumRenderTargets; ++index) { - if (index != *single_color_target) { - texture_cache.SetEmptyColorBuffer(index); - } - } - } else { - // Multiple color attachments are enabled - for (std::size_t index = 0; index < Maxwell::NumRenderTargets; ++index) { - View color_surface{texture_cache.GetColorBufferSurface(index, preserve_contents)}; - - if (color_surface) { - // Assume that a surface will be written to if it is used as a framebuffer, even - // if the shader doesn't actually write to it. - texture_cache.MarkColorBufferInUse(index); - } - - fbkey.color_attachments[index] = - GL_COLOR_ATTACHMENT0 + regs.rt_control.GetMap(index); - fbkey.colors[index] = color_surface; - } - fbkey.is_single_buffer = false; - fbkey.colors_count = regs.rt_control.count; + if (color_surface) { + // Assume that a surface will be written to if it is used as a framebuffer, even + // if the shader doesn't actually write to it. + texture_cache.MarkColorBufferInUse(index); } - } else { - // No color attachments are enabled - leave them as zero - fbkey.is_single_buffer = true; + + fbkey.color_attachments[index] = GL_COLOR_ATTACHMENT0 + regs.rt_control.GetMap(index); + fbkey.colors[index] = std::move(color_surface); } + fbkey.colors_count = regs.rt_control.count; if (depth_surface) { // Assume that a surface will be written to if it is used as a framebuffer, even if // the shader doesn't actually write to it. texture_cache.MarkDepthBufferInUse(); - fbkey.zeta = depth_surface; fbkey.stencil_enable = depth_surface->GetSurfaceParams().type == SurfaceType::DepthStencil; + fbkey.zeta = std::move(depth_surface); } texture_cache.GuardRenderTargets(false); - current_state.draw.draw_framebuffer = framebuffer_cache.GetFramebuffer(fbkey); - SyncViewport(current_state); - - return current_depth_stencil_usage = {static_cast<bool>(depth_surface), fbkey.stencil_enable}; + state.draw.draw_framebuffer = framebuffer_cache.GetFramebuffer(fbkey); + SyncViewport(state); } void RasterizerOpenGL::ConfigureClearFramebuffer(OpenGLState& current_state, bool using_color_fb, @@ -688,17 +577,9 @@ void RasterizerOpenGL::Clear() { } } -void RasterizerOpenGL::DrawArrays() { - if (accelerate_draw == AccelDraw::Disabled) - return; - - MICROPROFILE_SCOPE(OpenGL_Drawing); +void RasterizerOpenGL::DrawPrelude() { auto& gpu = system.GPU().Maxwell3D(); - if (!gpu.ShouldExecute()) { - return; - } - SyncColorMask(); SyncFragmentColorClampState(); SyncMultiSampleState(); @@ -743,10 +624,7 @@ void RasterizerOpenGL::DrawArrays() { // Upload vertex and index data. SetupVertexBuffer(vao); SetupVertexInstances(vao); - const GLintptr index_buffer_offset = SetupIndexBuffer(); - - // Setup draw parameters. It will automatically choose what glDraw* method to use. - const DrawParameters params = SetupDraw(index_buffer_offset); + index_buffer_offset = SetupIndexBuffer(); // Prepare packed bindings. bind_ubo_pushbuffer.Setup(0); @@ -754,10 +632,11 @@ void RasterizerOpenGL::DrawArrays() { // Setup shaders and their used resources. texture_cache.GuardSamplers(true); - SetupShaders(params.primitive_mode); + const auto primitive_mode = MaxwellToGL::PrimitiveTopology(gpu.regs.draw.topology); + SetupShaders(primitive_mode); texture_cache.GuardSamplers(false); - ConfigureFramebuffers(state); + ConfigureFramebuffers(); // Signal the buffer cache that we are not going to upload more things. const bool invalidate = buffer_cache.Unmap(); @@ -778,11 +657,107 @@ void RasterizerOpenGL::DrawArrays() { if (texture_cache.TextureBarrier()) { glTextureBarrier(); } +} + +struct DrawParams { + bool is_indexed{}; + bool is_instanced{}; + GLenum primitive_mode{}; + GLint count{}; + GLint base_vertex{}; + + // Indexed settings + GLenum index_format{}; + GLintptr index_buffer_offset{}; + + // Instanced setting + GLint num_instances{}; + GLint base_instance{}; + + void DispatchDraw() { + if (is_indexed) { + const auto index_buffer_ptr = reinterpret_cast<const void*>(index_buffer_offset); + if (is_instanced) { + glDrawElementsInstancedBaseVertexBaseInstance(primitive_mode, count, index_format, + index_buffer_ptr, num_instances, + base_vertex, base_instance); + } else { + glDrawElementsBaseVertex(primitive_mode, count, index_format, index_buffer_ptr, + base_vertex); + } + } else { + if (is_instanced) { + glDrawArraysInstancedBaseInstance(primitive_mode, base_vertex, count, num_instances, + base_instance); + } else { + glDrawArrays(primitive_mode, base_vertex, count); + } + } + } +}; - params.DispatchDraw(); +bool RasterizerOpenGL::DrawBatch(bool is_indexed) { + accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays; + MICROPROFILE_SCOPE(OpenGL_Drawing); + + DrawPrelude(); + + auto& maxwell3d = system.GPU().Maxwell3D(); + const auto& regs = maxwell3d.regs; + const auto current_instance = maxwell3d.state.current_instance; + DrawParams draw_call{}; + draw_call.is_indexed = is_indexed; + draw_call.num_instances = static_cast<GLint>(1); + draw_call.base_instance = static_cast<GLint>(current_instance); + draw_call.is_instanced = current_instance > 0; + draw_call.primitive_mode = MaxwellToGL::PrimitiveTopology(regs.draw.topology); + if (draw_call.is_indexed) { + draw_call.count = static_cast<GLint>(regs.index_array.count); + draw_call.base_vertex = static_cast<GLint>(regs.vb_element_base); + draw_call.index_format = MaxwellToGL::IndexFormat(regs.index_array.format); + draw_call.index_buffer_offset = index_buffer_offset; + } else { + draw_call.count = static_cast<GLint>(regs.vertex_buffer.count); + draw_call.base_vertex = static_cast<GLint>(regs.vertex_buffer.first); + } + draw_call.DispatchDraw(); + + maxwell3d.dirty.memory_general = false; accelerate_draw = AccelDraw::Disabled; - gpu.dirty.memory_general = false; + return true; +} + +bool RasterizerOpenGL::DrawMultiBatch(bool is_indexed) { + accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays; + + MICROPROFILE_SCOPE(OpenGL_Drawing); + + DrawPrelude(); + + auto& maxwell3d = system.GPU().Maxwell3D(); + const auto& regs = maxwell3d.regs; + const auto& draw_setup = maxwell3d.mme_draw; + DrawParams draw_call{}; + draw_call.is_indexed = is_indexed; + draw_call.num_instances = static_cast<GLint>(draw_setup.instance_count); + draw_call.base_instance = static_cast<GLint>(regs.vb_base_instance); + draw_call.is_instanced = draw_setup.instance_count > 1; + draw_call.primitive_mode = MaxwellToGL::PrimitiveTopology(regs.draw.topology); + if (draw_call.is_indexed) { + draw_call.count = static_cast<GLint>(regs.index_array.count); + draw_call.base_vertex = static_cast<GLint>(regs.vb_element_base); + draw_call.index_format = MaxwellToGL::IndexFormat(regs.index_array.format); + draw_call.index_buffer_offset = index_buffer_offset; + } else { + draw_call.count = static_cast<GLint>(regs.vertex_buffer.count); + draw_call.base_vertex = static_cast<GLint>(regs.vertex_buffer.first); + } + draw_call.DispatchDraw(); + + maxwell3d.dirty.memory_general = false; + accelerate_draw = AccelDraw::Disabled; + return true; } void RasterizerOpenGL::DispatchCompute(GPUVAddr code_addr) { diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index eada752e0..9c10ebda3 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h @@ -57,7 +57,8 @@ public: ScreenInfo& info); ~RasterizerOpenGL() override; - void DrawArrays() override; + bool DrawBatch(bool is_indexed) override; + bool DrawMultiBatch(bool is_indexed) override; void Clear() override; void DispatchCompute(GPUVAddr code_addr) override; void FlushAll() override; @@ -71,45 +72,13 @@ public: const Tegra::Engines::Fermi2D::Config& copy_config) override; bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr, u32 pixel_stride) override; - bool AccelerateDrawBatch(bool is_indexed) override; void UpdatePagesCachedCount(VAddr addr, u64 size, int delta) override; void LoadDiskResources(const std::atomic_bool& stop_loading, const VideoCore::DiskResourceLoadCallback& callback) override; private: - struct FramebufferConfigState { - bool using_color_fb{}; - bool using_depth_fb{}; - bool preserve_contents{}; - std::optional<std::size_t> single_color_target; - - bool operator==(const FramebufferConfigState& rhs) const { - return std::tie(using_color_fb, using_depth_fb, preserve_contents, - single_color_target) == std::tie(rhs.using_color_fb, rhs.using_depth_fb, - rhs.preserve_contents, - rhs.single_color_target); - } - bool operator!=(const FramebufferConfigState& rhs) const { - return !operator==(rhs); - } - }; - - /** - * Configures the color and depth framebuffer states. - * - * @param current_state The current OpenGL state. - * @param using_color_fb If true, configure color framebuffers. - * @param using_depth_fb If true, configure the depth/stencil framebuffer. - * @param preserve_contents If true, tries to preserve data from a previously used - * framebuffer. - * @param single_color_target Specifies if a single color buffer target should be used. - * - * @returns If depth (first) or stencil (second) are being stored in the bound zeta texture - * (requires using_depth_fb to be true) - */ - std::pair<bool, bool> ConfigureFramebuffers( - OpenGLState& current_state, bool using_color_fb = true, bool using_depth_fb = true, - bool preserve_contents = true, std::optional<std::size_t> single_color_target = {}); + /// Configures the color and depth framebuffer states. + void ConfigureFramebuffers(); void ConfigureClearFramebuffer(OpenGLState& current_state, bool using_color_fb, bool using_depth_fb, bool using_stencil_fb); @@ -136,6 +105,9 @@ private: void SetupGlobalMemory(const GLShader::GlobalMemoryEntry& entry, GPUVAddr gpu_addr, std::size_t size); + /// Syncs all the state, shaders, render targets and textures setting before a draw call. + void DrawPrelude(); + /// Configures the current textures to use for the draw command. Returns shaders texture buffer /// usage. TextureBufferUsage SetupDrawTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, @@ -228,9 +200,6 @@ private: OGLVertexArray> vertex_array_cache; - FramebufferConfigState current_framebuffer_config_state; - std::pair<bool, bool> current_depth_stencil_usage{}; - static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024; OGLBufferCache buffer_cache; @@ -250,7 +219,7 @@ private: GLintptr SetupIndexBuffer(); - DrawParameters SetupDraw(GLintptr index_buffer_offset); + GLintptr index_buffer_offset; void SetupShaders(GLenum primitive_mode); diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp index 0f3a35f74..e6b36a0f2 100644 --- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp +++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp @@ -461,6 +461,14 @@ private: code.AddLine("float gl_PointSize;"); } + if (ir.UsesInstanceId()) { + code.AddLine("int gl_InstanceID;"); + } + + if (ir.UsesVertexId()) { + code.AddLine("int gl_VertexID;"); + } + --code.scope; code.AddLine("}};"); code.AddNewLine(); @@ -951,7 +959,7 @@ private: switch (element) { case 2: // Config pack's first value is instance_id. - return {"config_pack[0]", Type::Uint}; + return {"gl_InstanceID", Type::Int}; case 3: return {"gl_VertexID", Type::Int}; } diff --git a/src/video_core/renderer_opengl/gl_texture_cache.cpp b/src/video_core/renderer_opengl/gl_texture_cache.cpp index 4f135fe03..173b76c4e 100644 --- a/src/video_core/renderer_opengl/gl_texture_cache.cpp +++ b/src/video_core/renderer_opengl/gl_texture_cache.cpp @@ -97,6 +97,7 @@ constexpr std::array<FormatTuple, VideoCore::Surface::MaxPixelFormat> tex_format {GL_RG8, GL_RG, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // RG8U {GL_RG8, GL_RG, GL_BYTE, ComponentType::SNorm, false}, // RG8S {GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT, ComponentType::UInt, false}, // RG32UI + {GL_RGB16F, GL_RGBA16, GL_HALF_FLOAT, ComponentType::Float, false}, // RGBX16F {GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, ComponentType::UInt, false}, // R32UI {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_8X8 {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_8X5 diff --git a/src/video_core/renderer_opengl/maxwell_to_gl.h b/src/video_core/renderer_opengl/maxwell_to_gl.h index ea77dd211..9ed738171 100644 --- a/src/video_core/renderer_opengl/maxwell_to_gl.h +++ b/src/video_core/renderer_opengl/maxwell_to_gl.h @@ -145,7 +145,7 @@ inline GLenum TextureFilterMode(Tegra::Texture::TextureFilter filter_mode, case Tegra::Texture::TextureMipmapFilter::None: return GL_LINEAR; case Tegra::Texture::TextureMipmapFilter::Nearest: - return GL_NEAREST_MIPMAP_LINEAR; + return GL_LINEAR_MIPMAP_NEAREST; case Tegra::Texture::TextureMipmapFilter::Linear: return GL_LINEAR_MIPMAP_LINEAR; } @@ -157,7 +157,7 @@ inline GLenum TextureFilterMode(Tegra::Texture::TextureFilter filter_mode, case Tegra::Texture::TextureMipmapFilter::Nearest: return GL_NEAREST_MIPMAP_NEAREST; case Tegra::Texture::TextureMipmapFilter::Linear: - return GL_LINEAR_MIPMAP_NEAREST; + return GL_NEAREST_MIPMAP_LINEAR; } } } diff --git a/src/video_core/renderer_vulkan/maxwell_to_vk.cpp b/src/video_core/renderer_vulkan/maxwell_to_vk.cpp index 0bbbf6851..3c5acda3e 100644 --- a/src/video_core/renderer_vulkan/maxwell_to_vk.cpp +++ b/src/video_core/renderer_vulkan/maxwell_to_vk.cpp @@ -143,6 +143,7 @@ static constexpr std::array<FormatTuple, VideoCore::Surface::MaxPixelFormat> tex {vk::Format::eUndefined, ComponentType::Invalid, false}, // RG8U {vk::Format::eUndefined, ComponentType::Invalid, false}, // RG8S {vk::Format::eUndefined, ComponentType::Invalid, false}, // RG32UI + {vk::Format::eUndefined, ComponentType::Invalid, false}, // RGBX16F {vk::Format::eUndefined, ComponentType::Invalid, false}, // R32UI {vk::Format::eUndefined, ComponentType::Invalid, false}, // ASTC_2D_8X8 {vk::Format::eUndefined, ComponentType::Invalid, false}, // ASTC_2D_8X5 diff --git a/src/video_core/shader/decode/arithmetic_integer.cpp b/src/video_core/shader/decode/arithmetic_integer.cpp index c8c1a7f40..b73f6536e 100644 --- a/src/video_core/shader/decode/arithmetic_integer.cpp +++ b/src/video_core/shader/decode/arithmetic_integer.cpp @@ -138,6 +138,35 @@ u32 ShaderIR::DecodeArithmeticInteger(NodeBlock& bb, u32 pc) { SetRegister(bb, instr.gpr0, value); break; } + case OpCode::Id::ICMP_CR: + case OpCode::Id::ICMP_R: + case OpCode::Id::ICMP_RC: + case OpCode::Id::ICMP_IMM: { + const Node zero = Immediate(0); + + const auto [op_b, test] = [&]() -> std::pair<Node, Node> { + switch (opcode->get().GetId()) { + case OpCode::Id::ICMP_CR: + return {GetConstBuffer(instr.cbuf34.index, instr.cbuf34.offset), + GetRegister(instr.gpr39)}; + case OpCode::Id::ICMP_R: + return {GetRegister(instr.gpr20), GetRegister(instr.gpr39)}; + case OpCode::Id::ICMP_RC: + return {GetRegister(instr.gpr39), + GetConstBuffer(instr.cbuf34.index, instr.cbuf34.offset)}; + case OpCode::Id::ICMP_IMM: + return {Immediate(instr.alu.GetSignedImm20_20()), GetRegister(instr.gpr39)}; + default: + UNREACHABLE(); + return {zero, zero}; + } + }(); + const Node op_a = GetRegister(instr.gpr8); + const Node comparison = + GetPredicateComparisonInteger(instr.icmp.cond, instr.icmp.is_signed != 0, test, zero); + SetRegister(bb, instr.gpr0, Operation(OperationCode::Select, comparison, op_a, op_b)); + break; + } case OpCode::Id::LOP_C: case OpCode::Id::LOP_R: case OpCode::Id::LOP_IMM: { diff --git a/src/video_core/shader/shader_ir.cpp b/src/video_core/shader/shader_ir.cpp index bbbab0bca..2c357f310 100644 --- a/src/video_core/shader/shader_ir.cpp +++ b/src/video_core/shader/shader_ir.cpp @@ -114,6 +114,18 @@ Node ShaderIR::GetOutputAttribute(Attribute::Index index, u64 element, Node buff break; } } + if (index == Attribute::Index::TessCoordInstanceIDVertexID) { + switch (element) { + case 2: + uses_instance_id = true; + break; + case 3: + uses_vertex_id = true; + break; + default: + break; + } + } if (index == Attribute::Index::ClipDistances0123 || index == Attribute::Index::ClipDistances4567) { const auto clip_index = diff --git a/src/video_core/shader/shader_ir.h b/src/video_core/shader/shader_ir.h index c3e147eea..6f666ee30 100644 --- a/src/video_core/shader/shader_ir.h +++ b/src/video_core/shader/shader_ir.h @@ -124,6 +124,14 @@ public: return uses_point_size; } + bool UsesInstanceId() const { + return uses_instance_id; + } + + bool UsesVertexId() const { + return uses_vertex_id; + } + bool HasPhysicalAttributes() const { return uses_physical_attributes; } @@ -370,6 +378,8 @@ private: bool uses_viewport_index{}; bool uses_point_size{}; bool uses_physical_attributes{}; // Shader uses AL2P or physical attribute read/writes + bool uses_instance_id{}; + bool uses_vertex_id{}; Tegra::Shader::Header header; }; diff --git a/src/video_core/surface.cpp b/src/video_core/surface.cpp index 53d0142cb..250afc6d6 100644 --- a/src/video_core/surface.cpp +++ b/src/video_core/surface.cpp @@ -159,6 +159,8 @@ PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format) return PixelFormat::R32UI; case Tegra::RenderTargetFormat::RG32_UINT: return PixelFormat::RG32UI; + case Tegra::RenderTargetFormat::RGBX16_FLOAT: + return PixelFormat::RGBX16F; default: LOG_CRITICAL(HW_GPU, "Unimplemented format={}", static_cast<u32>(format)); UNREACHABLE(); @@ -415,6 +417,7 @@ ComponentType ComponentTypeFromRenderTarget(Tegra::RenderTargetFormat format) { case Tegra::RenderTargetFormat::RG8_SNORM: return ComponentType::SNorm; case Tegra::RenderTargetFormat::RGBA16_FLOAT: + case Tegra::RenderTargetFormat::RGBX16_FLOAT: case Tegra::RenderTargetFormat::R11G11B10_FLOAT: case Tegra::RenderTargetFormat::RGBA32_FLOAT: case Tegra::RenderTargetFormat::RG32_FLOAT: diff --git a/src/video_core/surface.h b/src/video_core/surface.h index 19268b7cd..1e1c432a5 100644 --- a/src/video_core/surface.h +++ b/src/video_core/surface.h @@ -57,36 +57,37 @@ enum class PixelFormat { RG8U = 39, RG8S = 40, RG32UI = 41, - R32UI = 42, - ASTC_2D_8X8 = 43, - ASTC_2D_8X5 = 44, - ASTC_2D_5X4 = 45, - BGRA8_SRGB = 46, - DXT1_SRGB = 47, - DXT23_SRGB = 48, - DXT45_SRGB = 49, - BC7U_SRGB = 50, - ASTC_2D_4X4_SRGB = 51, - ASTC_2D_8X8_SRGB = 52, - ASTC_2D_8X5_SRGB = 53, - ASTC_2D_5X4_SRGB = 54, - ASTC_2D_5X5 = 55, - ASTC_2D_5X5_SRGB = 56, - ASTC_2D_10X8 = 57, - ASTC_2D_10X8_SRGB = 58, + RGBX16F = 42, + R32UI = 43, + ASTC_2D_8X8 = 44, + ASTC_2D_8X5 = 45, + ASTC_2D_5X4 = 46, + BGRA8_SRGB = 47, + DXT1_SRGB = 48, + DXT23_SRGB = 49, + DXT45_SRGB = 50, + BC7U_SRGB = 51, + ASTC_2D_4X4_SRGB = 52, + ASTC_2D_8X8_SRGB = 53, + ASTC_2D_8X5_SRGB = 54, + ASTC_2D_5X4_SRGB = 55, + ASTC_2D_5X5 = 56, + ASTC_2D_5X5_SRGB = 57, + ASTC_2D_10X8 = 58, + ASTC_2D_10X8_SRGB = 59, MaxColorFormat, // Depth formats - Z32F = 59, - Z16 = 60, + Z32F = 60, + Z16 = 61, MaxDepthFormat, // DepthStencil formats - Z24S8 = 61, - S8Z24 = 62, - Z32FS8 = 63, + Z24S8 = 62, + S8Z24 = 63, + Z32FS8 = 64, MaxDepthStencilFormat, @@ -166,6 +167,7 @@ constexpr std::array<u32, MaxPixelFormat> compression_factor_shift_table = {{ 0, // RG8U 0, // RG8S 0, // RG32UI + 0, // RGBX16F 0, // R32UI 2, // ASTC_2D_8X8 2, // ASTC_2D_8X5 @@ -249,6 +251,7 @@ constexpr std::array<u32, MaxPixelFormat> block_width_table = {{ 1, // RG8U 1, // RG8S 1, // RG32UI + 1, // RGBX16F 1, // R32UI 8, // ASTC_2D_8X8 8, // ASTC_2D_8X5 @@ -324,6 +327,7 @@ constexpr std::array<u32, MaxPixelFormat> block_height_table = {{ 1, // RG8U 1, // RG8S 1, // RG32UI + 1, // RGBX16F 1, // R32UI 8, // ASTC_2D_8X8 5, // ASTC_2D_8X5 @@ -399,6 +403,7 @@ constexpr std::array<u32, MaxPixelFormat> bpp_table = {{ 16, // RG8U 16, // RG8S 64, // RG32UI + 64, // RGBX16F 32, // R32UI 128, // ASTC_2D_8X8 128, // ASTC_2D_8X5 @@ -489,6 +494,7 @@ constexpr std::array<SurfaceCompression, MaxPixelFormat> compression_type_table SurfaceCompression::None, // RG8U SurfaceCompression::None, // RG8S SurfaceCompression::None, // RG32UI + SurfaceCompression::None, // RGBX16F SurfaceCompression::None, // R32UI SurfaceCompression::Converted, // ASTC_2D_8X8 SurfaceCompression::Converted, // ASTC_2D_8X5 |