diff options
| author | 2020-02-18 11:31:06 -0400 | |
|---|---|---|
| committer | 2020-02-18 11:31:06 -0400 | |
| commit | 93acfbd3a5c0e52a0031cc2d0475c82449e0f340 (patch) | |
| tree | 5f49c1d148aa64551bfb792f9702e567a2e5547f /src | |
| parent | Merge pull request #3412 from Morph1984/aspect-ratio (diff) | |
| parent | query_cache: Address feedback (diff) | |
| download | yuzu-93acfbd3a5c0e52a0031cc2d0475c82449e0f340.tar.gz yuzu-93acfbd3a5c0e52a0031cc2d0475c82449e0f340.tar.xz yuzu-93acfbd3a5c0e52a0031cc2d0475c82449e0f340.zip | |
Merge pull request #3409 from ReinUsesLisp/host-queries
query_cache: Implement a query cache and query 21 (samples passed)
Diffstat (limited to 'src')
18 files changed, 1043 insertions, 51 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index db9332d00..4b0c6346f 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt | |||
| @@ -37,6 +37,7 @@ add_library(video_core STATIC | |||
| 37 | memory_manager.h | 37 | memory_manager.h |
| 38 | morton.cpp | 38 | morton.cpp |
| 39 | morton.h | 39 | morton.h |
| 40 | query_cache.h | ||
| 40 | rasterizer_accelerated.cpp | 41 | rasterizer_accelerated.cpp |
| 41 | rasterizer_accelerated.h | 42 | rasterizer_accelerated.h |
| 42 | rasterizer_cache.cpp | 43 | rasterizer_cache.cpp |
| @@ -74,6 +75,8 @@ add_library(video_core STATIC | |||
| 74 | renderer_opengl/gl_stream_buffer.h | 75 | renderer_opengl/gl_stream_buffer.h |
| 75 | renderer_opengl/gl_texture_cache.cpp | 76 | renderer_opengl/gl_texture_cache.cpp |
| 76 | renderer_opengl/gl_texture_cache.h | 77 | renderer_opengl/gl_texture_cache.h |
| 78 | renderer_opengl/gl_query_cache.cpp | ||
| 79 | renderer_opengl/gl_query_cache.h | ||
| 77 | renderer_opengl/maxwell_to_gl.h | 80 | renderer_opengl/maxwell_to_gl.h |
| 78 | renderer_opengl/renderer_opengl.cpp | 81 | renderer_opengl/renderer_opengl.cpp |
| 79 | renderer_opengl/renderer_opengl.h | 82 | renderer_opengl/renderer_opengl.h |
| @@ -177,6 +180,8 @@ if (ENABLE_VULKAN) | |||
| 177 | renderer_vulkan/vk_memory_manager.h | 180 | renderer_vulkan/vk_memory_manager.h |
| 178 | renderer_vulkan/vk_pipeline_cache.cpp | 181 | renderer_vulkan/vk_pipeline_cache.cpp |
| 179 | renderer_vulkan/vk_pipeline_cache.h | 182 | renderer_vulkan/vk_pipeline_cache.h |
| 183 | renderer_vulkan/vk_query_cache.cpp | ||
| 184 | renderer_vulkan/vk_query_cache.h | ||
| 180 | renderer_vulkan/vk_rasterizer.cpp | 185 | renderer_vulkan/vk_rasterizer.cpp |
| 181 | renderer_vulkan/vk_rasterizer.h | 186 | renderer_vulkan/vk_rasterizer.h |
| 182 | renderer_vulkan/vk_renderpass_cache.cpp | 187 | renderer_vulkan/vk_renderpass_cache.cpp |
diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp index 0b3e8749b..842cdcbcf 100644 --- a/src/video_core/engines/maxwell_3d.cpp +++ b/src/video_core/engines/maxwell_3d.cpp | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #include <cinttypes> | 5 | #include <cinttypes> |
| 6 | #include <cstring> | 6 | #include <cstring> |
| 7 | #include <optional> | ||
| 7 | #include "common/assert.h" | 8 | #include "common/assert.h" |
| 8 | #include "core/core.h" | 9 | #include "core/core.h" |
| 9 | #include "core/core_timing.h" | 10 | #include "core/core_timing.h" |
| @@ -16,6 +17,8 @@ | |||
| 16 | 17 | ||
| 17 | namespace Tegra::Engines { | 18 | namespace Tegra::Engines { |
| 18 | 19 | ||
| 20 | using VideoCore::QueryType; | ||
| 21 | |||
| 19 | /// First register id that is actually a Macro call. | 22 | /// First register id that is actually a Macro call. |
| 20 | constexpr u32 MacroRegistersStart = 0xE00; | 23 | constexpr u32 MacroRegistersStart = 0xE00; |
| 21 | 24 | ||
| @@ -400,6 +403,10 @@ void Maxwell3D::CallMethod(const GPU::MethodCall& method_call) { | |||
| 400 | ProcessQueryCondition(); | 403 | ProcessQueryCondition(); |
| 401 | break; | 404 | break; |
| 402 | } | 405 | } |
| 406 | case MAXWELL3D_REG_INDEX(counter_reset): { | ||
| 407 | ProcessCounterReset(); | ||
| 408 | break; | ||
| 409 | } | ||
| 403 | case MAXWELL3D_REG_INDEX(sync_info): { | 410 | case MAXWELL3D_REG_INDEX(sync_info): { |
| 404 | ProcessSyncPoint(); | 411 | ProcessSyncPoint(); |
| 405 | break; | 412 | break; |
| @@ -544,40 +551,28 @@ void Maxwell3D::ProcessQueryGet() { | |||
| 544 | "Units other than CROP are unimplemented"); | 551 | "Units other than CROP are unimplemented"); |
| 545 | 552 | ||
| 546 | switch (regs.query.query_get.operation) { | 553 | switch (regs.query.query_get.operation) { |
| 547 | case Regs::QueryOperation::Release: { | 554 | case Regs::QueryOperation::Release: |
| 548 | const u64 result = regs.query.query_sequence; | 555 | StampQueryResult(regs.query.query_sequence, regs.query.query_get.short_query == 0); |
| 549 | StampQueryResult(result, regs.query.query_get.short_query == 0); | ||
| 550 | break; | 556 | break; |
| 551 | } | 557 | case Regs::QueryOperation::Acquire: |
| 552 | case Regs::QueryOperation::Acquire: { | 558 | // TODO(Blinkhawk): Under this operation, the GPU waits for the CPU to write a value that |
| 553 | // Todo(Blinkhawk): Under this operation, the GPU waits for the CPU | 559 | // matches the current payload. |
| 554 | // to write a value that matches the current payload. | ||
| 555 | UNIMPLEMENTED_MSG("Unimplemented query operation ACQUIRE"); | 560 | UNIMPLEMENTED_MSG("Unimplemented query operation ACQUIRE"); |
| 556 | break; | 561 | break; |
| 557 | } | 562 | case Regs::QueryOperation::Counter: |
| 558 | case Regs::QueryOperation::Counter: { | 563 | if (const std::optional<u64> result = GetQueryResult()) { |
| 559 | u64 result{}; | 564 | // If the query returns an empty optional it means it's cached and deferred. |
| 560 | switch (regs.query.query_get.select) { | 565 | // In this case we have a non-empty result, so we stamp it immediately. |
| 561 | case Regs::QuerySelect::Zero: | 566 | StampQueryResult(*result, regs.query.query_get.short_query == 0); |
| 562 | result = 0; | ||
| 563 | break; | ||
| 564 | default: | ||
| 565 | result = 1; | ||
| 566 | UNIMPLEMENTED_MSG("Unimplemented query select type {}", | ||
| 567 | static_cast<u32>(regs.query.query_get.select.Value())); | ||
| 568 | } | 567 | } |
| 569 | StampQueryResult(result, regs.query.query_get.short_query == 0); | ||
| 570 | break; | 568 | break; |
| 571 | } | 569 | case Regs::QueryOperation::Trap: |
| 572 | case Regs::QueryOperation::Trap: { | ||
| 573 | UNIMPLEMENTED_MSG("Unimplemented query operation TRAP"); | 570 | UNIMPLEMENTED_MSG("Unimplemented query operation TRAP"); |
| 574 | break; | 571 | break; |
| 575 | } | 572 | default: |
| 576 | default: { | ||
| 577 | UNIMPLEMENTED_MSG("Unknown query operation"); | 573 | UNIMPLEMENTED_MSG("Unknown query operation"); |
| 578 | break; | 574 | break; |
| 579 | } | 575 | } |
| 580 | } | ||
| 581 | } | 576 | } |
| 582 | 577 | ||
| 583 | void Maxwell3D::ProcessQueryCondition() { | 578 | void Maxwell3D::ProcessQueryCondition() { |
| @@ -593,20 +588,20 @@ void Maxwell3D::ProcessQueryCondition() { | |||
| 593 | } | 588 | } |
| 594 | case Regs::ConditionMode::ResNonZero: { | 589 | case Regs::ConditionMode::ResNonZero: { |
| 595 | Regs::QueryCompare cmp; | 590 | Regs::QueryCompare cmp; |
| 596 | memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp)); | 591 | memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp)); |
| 597 | execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U; | 592 | execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U; |
| 598 | break; | 593 | break; |
| 599 | } | 594 | } |
| 600 | case Regs::ConditionMode::Equal: { | 595 | case Regs::ConditionMode::Equal: { |
| 601 | Regs::QueryCompare cmp; | 596 | Regs::QueryCompare cmp; |
| 602 | memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp)); | 597 | memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp)); |
| 603 | execute_on = | 598 | execute_on = |
| 604 | cmp.initial_sequence == cmp.current_sequence && cmp.initial_mode == cmp.current_mode; | 599 | cmp.initial_sequence == cmp.current_sequence && cmp.initial_mode == cmp.current_mode; |
| 605 | break; | 600 | break; |
| 606 | } | 601 | } |
| 607 | case Regs::ConditionMode::NotEqual: { | 602 | case Regs::ConditionMode::NotEqual: { |
| 608 | Regs::QueryCompare cmp; | 603 | Regs::QueryCompare cmp; |
| 609 | memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp)); | 604 | memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp)); |
| 610 | execute_on = | 605 | execute_on = |
| 611 | cmp.initial_sequence != cmp.current_sequence || cmp.initial_mode != cmp.current_mode; | 606 | cmp.initial_sequence != cmp.current_sequence || cmp.initial_mode != cmp.current_mode; |
| 612 | break; | 607 | break; |
| @@ -619,6 +614,18 @@ void Maxwell3D::ProcessQueryCondition() { | |||
| 619 | } | 614 | } |
| 620 | } | 615 | } |
| 621 | 616 | ||
| 617 | void Maxwell3D::ProcessCounterReset() { | ||
| 618 | switch (regs.counter_reset) { | ||
| 619 | case Regs::CounterReset::SampleCnt: | ||
| 620 | rasterizer.ResetCounter(QueryType::SamplesPassed); | ||
| 621 | break; | ||
| 622 | default: | ||
| 623 | LOG_WARNING(Render_OpenGL, "Unimplemented counter reset={}", | ||
| 624 | static_cast<int>(regs.counter_reset)); | ||
| 625 | break; | ||
| 626 | } | ||
| 627 | } | ||
| 628 | |||
| 622 | void Maxwell3D::ProcessSyncPoint() { | 629 | void Maxwell3D::ProcessSyncPoint() { |
| 623 | const u32 sync_point = regs.sync_info.sync_point.Value(); | 630 | const u32 sync_point = regs.sync_info.sync_point.Value(); |
| 624 | const u32 increment = regs.sync_info.increment.Value(); | 631 | const u32 increment = regs.sync_info.increment.Value(); |
| @@ -661,6 +668,22 @@ void Maxwell3D::DrawArrays() { | |||
| 661 | } | 668 | } |
| 662 | } | 669 | } |
| 663 | 670 | ||
| 671 | std::optional<u64> Maxwell3D::GetQueryResult() { | ||
| 672 | switch (regs.query.query_get.select) { | ||
| 673 | case Regs::QuerySelect::Zero: | ||
| 674 | return 0; | ||
| 675 | case Regs::QuerySelect::SamplesPassed: | ||
| 676 | // Deferred. | ||
| 677 | rasterizer.Query(regs.query.QueryAddress(), VideoCore::QueryType::SamplesPassed, | ||
| 678 | system.GPU().GetTicks()); | ||
| 679 | return {}; | ||
| 680 | default: | ||
| 681 | UNIMPLEMENTED_MSG("Unimplemented query select type {}", | ||
| 682 | static_cast<u32>(regs.query.query_get.select.Value())); | ||
| 683 | return 1; | ||
| 684 | } | ||
| 685 | } | ||
| 686 | |||
| 664 | void Maxwell3D::ProcessCBBind(std::size_t stage_index) { | 687 | void Maxwell3D::ProcessCBBind(std::size_t stage_index) { |
| 665 | // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage. | 688 | // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage. |
| 666 | auto& shader = state.shader_stages[stage_index]; | 689 | auto& shader = state.shader_stages[stage_index]; |
diff --git a/src/video_core/engines/maxwell_3d.h b/src/video_core/engines/maxwell_3d.h index 0a2af54e5..26939be3f 100644 --- a/src/video_core/engines/maxwell_3d.h +++ b/src/video_core/engines/maxwell_3d.h | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <bitset> | 8 | #include <bitset> |
| 9 | #include <optional> | ||
| 9 | #include <type_traits> | 10 | #include <type_traits> |
| 10 | #include <unordered_map> | 11 | #include <unordered_map> |
| 11 | #include <vector> | 12 | #include <vector> |
| @@ -409,6 +410,27 @@ public: | |||
| 409 | Linear = 1, | 410 | Linear = 1, |
| 410 | }; | 411 | }; |
| 411 | 412 | ||
| 413 | enum class CounterReset : u32 { | ||
| 414 | SampleCnt = 0x01, | ||
| 415 | Unk02 = 0x02, | ||
| 416 | Unk03 = 0x03, | ||
| 417 | Unk04 = 0x04, | ||
| 418 | EmittedPrimitives = 0x10, // Not tested | ||
| 419 | Unk11 = 0x11, | ||
| 420 | Unk12 = 0x12, | ||
| 421 | Unk13 = 0x13, | ||
| 422 | Unk15 = 0x15, | ||
| 423 | Unk16 = 0x16, | ||
| 424 | Unk17 = 0x17, | ||
| 425 | Unk18 = 0x18, | ||
| 426 | Unk1A = 0x1A, | ||
| 427 | Unk1B = 0x1B, | ||
| 428 | Unk1C = 0x1C, | ||
| 429 | Unk1D = 0x1D, | ||
| 430 | Unk1E = 0x1E, | ||
| 431 | GeneratedPrimitives = 0x1F, | ||
| 432 | }; | ||
| 433 | |||
| 412 | struct Cull { | 434 | struct Cull { |
| 413 | enum class FrontFace : u32 { | 435 | enum class FrontFace : u32 { |
| 414 | ClockWise = 0x0900, | 436 | ClockWise = 0x0900, |
| @@ -857,7 +879,7 @@ public: | |||
| 857 | BitField<7, 1, u32> c7; | 879 | BitField<7, 1, u32> c7; |
| 858 | } clip_distance_enabled; | 880 | } clip_distance_enabled; |
| 859 | 881 | ||
| 860 | INSERT_UNION_PADDING_WORDS(0x1); | 882 | u32 samplecnt_enable; |
| 861 | 883 | ||
| 862 | float point_size; | 884 | float point_size; |
| 863 | 885 | ||
| @@ -865,7 +887,11 @@ public: | |||
| 865 | 887 | ||
| 866 | u32 point_sprite_enable; | 888 | u32 point_sprite_enable; |
| 867 | 889 | ||
| 868 | INSERT_UNION_PADDING_WORDS(0x5); | 890 | INSERT_UNION_PADDING_WORDS(0x3); |
| 891 | |||
| 892 | CounterReset counter_reset; | ||
| 893 | |||
| 894 | INSERT_UNION_PADDING_WORDS(0x1); | ||
| 869 | 895 | ||
| 870 | u32 zeta_enable; | 896 | u32 zeta_enable; |
| 871 | 897 | ||
| @@ -1412,12 +1438,15 @@ private: | |||
| 1412 | /// Handles a write to the QUERY_GET register. | 1438 | /// Handles a write to the QUERY_GET register. |
| 1413 | void ProcessQueryGet(); | 1439 | void ProcessQueryGet(); |
| 1414 | 1440 | ||
| 1415 | // Writes the query result accordingly | 1441 | /// Writes the query result accordingly. |
| 1416 | void StampQueryResult(u64 payload, bool long_query); | 1442 | void StampQueryResult(u64 payload, bool long_query); |
| 1417 | 1443 | ||
| 1418 | // Handles Conditional Rendering | 1444 | /// Handles conditional rendering. |
| 1419 | void ProcessQueryCondition(); | 1445 | void ProcessQueryCondition(); |
| 1420 | 1446 | ||
| 1447 | /// Handles counter resets. | ||
| 1448 | void ProcessCounterReset(); | ||
| 1449 | |||
| 1421 | /// Handles writes to syncing register. | 1450 | /// Handles writes to syncing register. |
| 1422 | void ProcessSyncPoint(); | 1451 | void ProcessSyncPoint(); |
| 1423 | 1452 | ||
| @@ -1434,6 +1463,9 @@ private: | |||
| 1434 | 1463 | ||
| 1435 | // Handles a instance drawcall from MME | 1464 | // Handles a instance drawcall from MME |
| 1436 | void StepInstance(MMEDrawMode expected_mode, u32 count); | 1465 | void StepInstance(MMEDrawMode expected_mode, u32 count); |
| 1466 | |||
| 1467 | /// Returns a query's value or an empty object if the value will be deferred through a cache. | ||
| 1468 | std::optional<u64> GetQueryResult(); | ||
| 1437 | }; | 1469 | }; |
| 1438 | 1470 | ||
| 1439 | #define ASSERT_REG_POSITION(field_name, position) \ | 1471 | #define ASSERT_REG_POSITION(field_name, position) \ |
| @@ -1499,8 +1531,10 @@ ASSERT_REG_POSITION(screen_y_control, 0x4EB); | |||
| 1499 | ASSERT_REG_POSITION(vb_element_base, 0x50D); | 1531 | ASSERT_REG_POSITION(vb_element_base, 0x50D); |
| 1500 | ASSERT_REG_POSITION(vb_base_instance, 0x50E); | 1532 | ASSERT_REG_POSITION(vb_base_instance, 0x50E); |
| 1501 | ASSERT_REG_POSITION(clip_distance_enabled, 0x544); | 1533 | ASSERT_REG_POSITION(clip_distance_enabled, 0x544); |
| 1534 | ASSERT_REG_POSITION(samplecnt_enable, 0x545); | ||
| 1502 | ASSERT_REG_POSITION(point_size, 0x546); | 1535 | ASSERT_REG_POSITION(point_size, 0x546); |
| 1503 | ASSERT_REG_POSITION(point_sprite_enable, 0x548); | 1536 | ASSERT_REG_POSITION(point_sprite_enable, 0x548); |
| 1537 | ASSERT_REG_POSITION(counter_reset, 0x54C); | ||
| 1504 | ASSERT_REG_POSITION(zeta_enable, 0x54E); | 1538 | ASSERT_REG_POSITION(zeta_enable, 0x54E); |
| 1505 | ASSERT_REG_POSITION(multisample_control, 0x54F); | 1539 | ASSERT_REG_POSITION(multisample_control, 0x54F); |
| 1506 | ASSERT_REG_POSITION(condition, 0x554); | 1540 | ASSERT_REG_POSITION(condition, 0x554); |
diff --git a/src/video_core/query_cache.h b/src/video_core/query_cache.h new file mode 100644 index 000000000..e66054ed0 --- /dev/null +++ b/src/video_core/query_cache.h | |||
| @@ -0,0 +1,359 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <array> | ||
| 9 | #include <cstring> | ||
| 10 | #include <iterator> | ||
| 11 | #include <memory> | ||
| 12 | #include <mutex> | ||
| 13 | #include <optional> | ||
| 14 | #include <unordered_map> | ||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | #include "common/assert.h" | ||
| 18 | #include "core/core.h" | ||
| 19 | #include "video_core/engines/maxwell_3d.h" | ||
| 20 | #include "video_core/gpu.h" | ||
| 21 | #include "video_core/memory_manager.h" | ||
| 22 | #include "video_core/rasterizer_interface.h" | ||
| 23 | |||
| 24 | namespace VideoCommon { | ||
| 25 | |||
| 26 | template <class QueryCache, class HostCounter> | ||
| 27 | class CounterStreamBase { | ||
| 28 | public: | ||
| 29 | explicit CounterStreamBase(QueryCache& cache, VideoCore::QueryType type) | ||
| 30 | : cache{cache}, type{type} {} | ||
| 31 | |||
| 32 | /// Updates the state of the stream, enabling or disabling as needed. | ||
| 33 | void Update(bool enabled) { | ||
| 34 | if (enabled) { | ||
| 35 | Enable(); | ||
| 36 | } else { | ||
| 37 | Disable(); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | /// Resets the stream to zero. It doesn't disable the query after resetting. | ||
| 42 | void Reset() { | ||
| 43 | if (current) { | ||
| 44 | current->EndQuery(); | ||
| 45 | |||
| 46 | // Immediately start a new query to avoid disabling its state. | ||
| 47 | current = cache.Counter(nullptr, type); | ||
| 48 | } | ||
| 49 | last = nullptr; | ||
| 50 | } | ||
| 51 | |||
| 52 | /// Returns the current counter slicing as needed. | ||
| 53 | std::shared_ptr<HostCounter> Current() { | ||
| 54 | if (!current) { | ||
| 55 | return nullptr; | ||
| 56 | } | ||
| 57 | current->EndQuery(); | ||
| 58 | last = std::move(current); | ||
| 59 | current = cache.Counter(last, type); | ||
| 60 | return last; | ||
| 61 | } | ||
| 62 | |||
| 63 | /// Returns true when the counter stream is enabled. | ||
| 64 | bool IsEnabled() const { | ||
| 65 | return current != nullptr; | ||
| 66 | } | ||
| 67 | |||
| 68 | private: | ||
| 69 | /// Enables the stream. | ||
| 70 | void Enable() { | ||
| 71 | if (current) { | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | current = cache.Counter(last, type); | ||
| 75 | } | ||
| 76 | |||
| 77 | // Disables the stream. | ||
| 78 | void Disable() { | ||
| 79 | if (current) { | ||
| 80 | current->EndQuery(); | ||
| 81 | } | ||
| 82 | last = std::exchange(current, nullptr); | ||
| 83 | } | ||
| 84 | |||
| 85 | QueryCache& cache; | ||
| 86 | const VideoCore::QueryType type; | ||
| 87 | |||
| 88 | std::shared_ptr<HostCounter> current; | ||
| 89 | std::shared_ptr<HostCounter> last; | ||
| 90 | }; | ||
| 91 | |||
| 92 | template <class QueryCache, class CachedQuery, class CounterStream, class HostCounter, | ||
| 93 | class QueryPool> | ||
| 94 | class QueryCacheBase { | ||
| 95 | public: | ||
| 96 | explicit QueryCacheBase(Core::System& system, VideoCore::RasterizerInterface& rasterizer) | ||
| 97 | : system{system}, rasterizer{rasterizer}, streams{{CounterStream{ | ||
| 98 | static_cast<QueryCache&>(*this), | ||
| 99 | VideoCore::QueryType::SamplesPassed}}} {} | ||
| 100 | |||
| 101 | void InvalidateRegion(CacheAddr addr, std::size_t size) { | ||
| 102 | std::unique_lock lock{mutex}; | ||
| 103 | FlushAndRemoveRegion(addr, size); | ||
| 104 | } | ||
| 105 | |||
| 106 | void FlushRegion(CacheAddr addr, std::size_t size) { | ||
| 107 | std::unique_lock lock{mutex}; | ||
| 108 | FlushAndRemoveRegion(addr, size); | ||
| 109 | } | ||
| 110 | |||
| 111 | /** | ||
| 112 | * Records a query in GPU mapped memory, potentially marked with a timestamp. | ||
| 113 | * @param gpu_addr GPU address to flush to when the mapped memory is read. | ||
| 114 | * @param type Query type, e.g. SamplesPassed. | ||
| 115 | * @param timestamp Timestamp, when empty the flushed query is assumed to be short. | ||
| 116 | */ | ||
| 117 | void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) { | ||
| 118 | std::unique_lock lock{mutex}; | ||
| 119 | auto& memory_manager = system.GPU().MemoryManager(); | ||
| 120 | const auto host_ptr = memory_manager.GetPointer(gpu_addr); | ||
| 121 | |||
| 122 | CachedQuery* query = TryGet(ToCacheAddr(host_ptr)); | ||
| 123 | if (!query) { | ||
| 124 | const auto cpu_addr = memory_manager.GpuToCpuAddress(gpu_addr); | ||
| 125 | ASSERT_OR_EXECUTE(cpu_addr, return;); | ||
| 126 | |||
| 127 | query = Register(type, *cpu_addr, host_ptr, timestamp.has_value()); | ||
| 128 | } | ||
| 129 | |||
| 130 | query->BindCounter(Stream(type).Current(), timestamp); | ||
| 131 | } | ||
| 132 | |||
| 133 | /// Updates counters from GPU state. Expected to be called once per draw, clear or dispatch. | ||
| 134 | void UpdateCounters() { | ||
| 135 | std::unique_lock lock{mutex}; | ||
| 136 | const auto& regs = system.GPU().Maxwell3D().regs; | ||
| 137 | Stream(VideoCore::QueryType::SamplesPassed).Update(regs.samplecnt_enable); | ||
| 138 | } | ||
| 139 | |||
| 140 | /// Resets a counter to zero. It doesn't disable the query after resetting. | ||
| 141 | void ResetCounter(VideoCore::QueryType type) { | ||
| 142 | std::unique_lock lock{mutex}; | ||
| 143 | Stream(type).Reset(); | ||
| 144 | } | ||
| 145 | |||
| 146 | /// Disable all active streams. Expected to be called at the end of a command buffer. | ||
| 147 | void DisableStreams() { | ||
| 148 | std::unique_lock lock{mutex}; | ||
| 149 | for (auto& stream : streams) { | ||
| 150 | stream.Update(false); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | /// Returns a new host counter. | ||
| 155 | std::shared_ptr<HostCounter> Counter(std::shared_ptr<HostCounter> dependency, | ||
| 156 | VideoCore::QueryType type) { | ||
| 157 | return std::make_shared<HostCounter>(static_cast<QueryCache&>(*this), std::move(dependency), | ||
| 158 | type); | ||
| 159 | } | ||
| 160 | |||
| 161 | /// Returns the counter stream of the specified type. | ||
| 162 | CounterStream& Stream(VideoCore::QueryType type) { | ||
| 163 | return streams[static_cast<std::size_t>(type)]; | ||
| 164 | } | ||
| 165 | |||
| 166 | /// Returns the counter stream of the specified type. | ||
| 167 | const CounterStream& Stream(VideoCore::QueryType type) const { | ||
| 168 | return streams[static_cast<std::size_t>(type)]; | ||
| 169 | } | ||
| 170 | |||
| 171 | protected: | ||
| 172 | std::array<QueryPool, VideoCore::NumQueryTypes> query_pools; | ||
| 173 | |||
| 174 | private: | ||
| 175 | /// Flushes a memory range to guest memory and removes it from the cache. | ||
| 176 | void FlushAndRemoveRegion(CacheAddr addr, std::size_t size) { | ||
| 177 | const u64 addr_begin = static_cast<u64>(addr); | ||
| 178 | const u64 addr_end = addr_begin + static_cast<u64>(size); | ||
| 179 | const auto in_range = [addr_begin, addr_end](CachedQuery& query) { | ||
| 180 | const u64 cache_begin = query.GetCacheAddr(); | ||
| 181 | const u64 cache_end = cache_begin + query.SizeInBytes(); | ||
| 182 | return cache_begin < addr_end && addr_begin < cache_end; | ||
| 183 | }; | ||
| 184 | |||
| 185 | const u64 page_end = addr_end >> PAGE_SHIFT; | ||
| 186 | for (u64 page = addr_begin >> PAGE_SHIFT; page <= page_end; ++page) { | ||
| 187 | const auto& it = cached_queries.find(page); | ||
| 188 | if (it == std::end(cached_queries)) { | ||
| 189 | continue; | ||
| 190 | } | ||
| 191 | auto& contents = it->second; | ||
| 192 | for (auto& query : contents) { | ||
| 193 | if (!in_range(query)) { | ||
| 194 | continue; | ||
| 195 | } | ||
| 196 | rasterizer.UpdatePagesCachedCount(query.CpuAddr(), query.SizeInBytes(), -1); | ||
| 197 | query.Flush(); | ||
| 198 | } | ||
| 199 | contents.erase(std::remove_if(std::begin(contents), std::end(contents), in_range), | ||
| 200 | std::end(contents)); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | /// Registers the passed parameters as cached and returns a pointer to the stored cached query. | ||
| 205 | CachedQuery* Register(VideoCore::QueryType type, VAddr cpu_addr, u8* host_ptr, bool timestamp) { | ||
| 206 | rasterizer.UpdatePagesCachedCount(cpu_addr, CachedQuery::SizeInBytes(timestamp), 1); | ||
| 207 | const u64 page = static_cast<u64>(ToCacheAddr(host_ptr)) >> PAGE_SHIFT; | ||
| 208 | return &cached_queries[page].emplace_back(static_cast<QueryCache&>(*this), type, cpu_addr, | ||
| 209 | host_ptr); | ||
| 210 | } | ||
| 211 | |||
| 212 | /// Tries to a get a cached query. Returns nullptr on failure. | ||
| 213 | CachedQuery* TryGet(CacheAddr addr) { | ||
| 214 | const u64 page = static_cast<u64>(addr) >> PAGE_SHIFT; | ||
| 215 | const auto it = cached_queries.find(page); | ||
| 216 | if (it == std::end(cached_queries)) { | ||
| 217 | return nullptr; | ||
| 218 | } | ||
| 219 | auto& contents = it->second; | ||
| 220 | const auto found = | ||
| 221 | std::find_if(std::begin(contents), std::end(contents), | ||
| 222 | [addr](auto& query) { return query.GetCacheAddr() == addr; }); | ||
| 223 | return found != std::end(contents) ? &*found : nullptr; | ||
| 224 | } | ||
| 225 | |||
| 226 | static constexpr std::uintptr_t PAGE_SIZE = 4096; | ||
| 227 | static constexpr unsigned PAGE_SHIFT = 12; | ||
| 228 | |||
| 229 | Core::System& system; | ||
| 230 | VideoCore::RasterizerInterface& rasterizer; | ||
| 231 | |||
| 232 | std::recursive_mutex mutex; | ||
| 233 | |||
| 234 | std::unordered_map<u64, std::vector<CachedQuery>> cached_queries; | ||
| 235 | |||
| 236 | std::array<CounterStream, VideoCore::NumQueryTypes> streams; | ||
| 237 | }; | ||
| 238 | |||
| 239 | template <class QueryCache, class HostCounter> | ||
| 240 | class HostCounterBase { | ||
| 241 | public: | ||
| 242 | explicit HostCounterBase(std::shared_ptr<HostCounter> dependency_) | ||
| 243 | : dependency{std::move(dependency_)}, depth{dependency ? (dependency->Depth() + 1) : 0} { | ||
| 244 | // Avoid nesting too many dependencies to avoid a stack overflow when these are deleted. | ||
| 245 | constexpr u64 depth_threshold = 96; | ||
| 246 | if (depth > depth_threshold) { | ||
| 247 | depth = 0; | ||
| 248 | base_result = dependency->Query(); | ||
| 249 | dependency = nullptr; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | virtual ~HostCounterBase() = default; | ||
| 253 | |||
| 254 | /// Returns the current value of the query. | ||
| 255 | u64 Query() { | ||
| 256 | if (result) { | ||
| 257 | return *result; | ||
| 258 | } | ||
| 259 | |||
| 260 | u64 value = BlockingQuery() + base_result; | ||
| 261 | if (dependency) { | ||
| 262 | value += dependency->Query(); | ||
| 263 | dependency = nullptr; | ||
| 264 | } | ||
| 265 | |||
| 266 | result = value; | ||
| 267 | return *result; | ||
| 268 | } | ||
| 269 | |||
| 270 | /// Returns true when flushing this query will potentially wait. | ||
| 271 | bool WaitPending() const noexcept { | ||
| 272 | return result.has_value(); | ||
| 273 | } | ||
| 274 | |||
| 275 | u64 Depth() const noexcept { | ||
| 276 | return depth; | ||
| 277 | } | ||
| 278 | |||
| 279 | protected: | ||
| 280 | /// Returns the value of query from the backend API blocking as needed. | ||
| 281 | virtual u64 BlockingQuery() const = 0; | ||
| 282 | |||
| 283 | private: | ||
| 284 | std::shared_ptr<HostCounter> dependency; ///< Counter to add to this value. | ||
| 285 | std::optional<u64> result; ///< Filled with the already returned value. | ||
| 286 | u64 depth; ///< Number of nested dependencies. | ||
| 287 | u64 base_result = 0; ///< Equivalent to nested dependencies value. | ||
| 288 | }; | ||
| 289 | |||
| 290 | template <class HostCounter> | ||
| 291 | class CachedQueryBase { | ||
| 292 | public: | ||
| 293 | explicit CachedQueryBase(VAddr cpu_addr, u8* host_ptr) | ||
| 294 | : cpu_addr{cpu_addr}, host_ptr{host_ptr} {} | ||
| 295 | virtual ~CachedQueryBase() = default; | ||
| 296 | |||
| 297 | CachedQueryBase(CachedQueryBase&&) noexcept = default; | ||
| 298 | CachedQueryBase(const CachedQueryBase&) = delete; | ||
| 299 | |||
| 300 | CachedQueryBase& operator=(CachedQueryBase&&) noexcept = default; | ||
| 301 | CachedQueryBase& operator=(const CachedQueryBase&) = delete; | ||
| 302 | |||
| 303 | /// Flushes the query to guest memory. | ||
| 304 | virtual void Flush() { | ||
| 305 | // When counter is nullptr it means that it's just been reseted. We are supposed to write a | ||
| 306 | // zero in these cases. | ||
| 307 | const u64 value = counter ? counter->Query() : 0; | ||
| 308 | std::memcpy(host_ptr, &value, sizeof(u64)); | ||
| 309 | |||
| 310 | if (timestamp) { | ||
| 311 | std::memcpy(host_ptr + TIMESTAMP_OFFSET, &*timestamp, sizeof(u64)); | ||
| 312 | } | ||
| 313 | } | ||
| 314 | |||
| 315 | /// Binds a counter to this query. | ||
| 316 | void BindCounter(std::shared_ptr<HostCounter> counter_, std::optional<u64> timestamp_) { | ||
| 317 | if (counter) { | ||
| 318 | // If there's an old counter set it means the query is being rewritten by the game. | ||
| 319 | // To avoid losing the data forever, flush here. | ||
| 320 | Flush(); | ||
| 321 | } | ||
| 322 | counter = std::move(counter_); | ||
| 323 | timestamp = timestamp_; | ||
| 324 | } | ||
| 325 | |||
| 326 | VAddr CpuAddr() const noexcept { | ||
| 327 | return cpu_addr; | ||
| 328 | } | ||
| 329 | |||
| 330 | CacheAddr GetCacheAddr() const noexcept { | ||
| 331 | return ToCacheAddr(host_ptr); | ||
| 332 | } | ||
| 333 | |||
| 334 | u64 SizeInBytes() const noexcept { | ||
| 335 | return SizeInBytes(timestamp.has_value()); | ||
| 336 | } | ||
| 337 | |||
| 338 | static constexpr u64 SizeInBytes(bool with_timestamp) noexcept { | ||
| 339 | return with_timestamp ? LARGE_QUERY_SIZE : SMALL_QUERY_SIZE; | ||
| 340 | } | ||
| 341 | |||
| 342 | protected: | ||
| 343 | /// Returns true when querying the counter may potentially block. | ||
| 344 | bool WaitPending() const noexcept { | ||
| 345 | return counter && counter->WaitPending(); | ||
| 346 | } | ||
| 347 | |||
| 348 | private: | ||
| 349 | static constexpr std::size_t SMALL_QUERY_SIZE = 8; // Query size without timestamp. | ||
| 350 | static constexpr std::size_t LARGE_QUERY_SIZE = 16; // Query size with timestamp. | ||
| 351 | static constexpr std::intptr_t TIMESTAMP_OFFSET = 8; // Timestamp offset in a large query. | ||
| 352 | |||
| 353 | VAddr cpu_addr; ///< Guest CPU address. | ||
| 354 | u8* host_ptr; ///< Writable host pointer. | ||
| 355 | std::shared_ptr<HostCounter> counter; ///< Host counter to query, owns the dependency tree. | ||
| 356 | std::optional<u64> timestamp; ///< Timestamp to flush to guest memory. | ||
| 357 | }; | ||
| 358 | |||
| 359 | } // namespace VideoCommon | ||
diff --git a/src/video_core/rasterizer_interface.h b/src/video_core/rasterizer_interface.h index c586cd6fe..e9f1436f0 100644 --- a/src/video_core/rasterizer_interface.h +++ b/src/video_core/rasterizer_interface.h | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include <atomic> | 7 | #include <atomic> |
| 8 | #include <functional> | 8 | #include <functional> |
| 9 | #include <optional> | ||
| 9 | #include "common/common_types.h" | 10 | #include "common/common_types.h" |
| 10 | #include "video_core/engines/fermi_2d.h" | 11 | #include "video_core/engines/fermi_2d.h" |
| 11 | #include "video_core/gpu.h" | 12 | #include "video_core/gpu.h" |
| @@ -17,6 +18,11 @@ class MemoryManager; | |||
| 17 | 18 | ||
| 18 | namespace VideoCore { | 19 | namespace VideoCore { |
| 19 | 20 | ||
| 21 | enum class QueryType { | ||
| 22 | SamplesPassed, | ||
| 23 | }; | ||
| 24 | constexpr std::size_t NumQueryTypes = 1; | ||
| 25 | |||
| 20 | enum class LoadCallbackStage { | 26 | enum class LoadCallbackStage { |
| 21 | Prepare, | 27 | Prepare, |
| 22 | Decompile, | 28 | Decompile, |
| @@ -41,6 +47,12 @@ public: | |||
| 41 | /// Dispatches a compute shader invocation | 47 | /// Dispatches a compute shader invocation |
| 42 | virtual void DispatchCompute(GPUVAddr code_addr) = 0; | 48 | virtual void DispatchCompute(GPUVAddr code_addr) = 0; |
| 43 | 49 | ||
| 50 | /// Resets the counter of a query | ||
| 51 | virtual void ResetCounter(QueryType type) = 0; | ||
| 52 | |||
| 53 | /// Records a GPU query and caches it | ||
| 54 | virtual void Query(GPUVAddr gpu_addr, QueryType type, std::optional<u64> timestamp) = 0; | ||
| 55 | |||
| 44 | /// Notify rasterizer that all caches should be flushed to Switch memory | 56 | /// Notify rasterizer that all caches should be flushed to Switch memory |
| 45 | virtual void FlushAll() = 0; | 57 | virtual void FlushAll() = 0; |
| 46 | 58 | ||
diff --git a/src/video_core/renderer_opengl/gl_query_cache.cpp b/src/video_core/renderer_opengl/gl_query_cache.cpp new file mode 100644 index 000000000..f12e9f55f --- /dev/null +++ b/src/video_core/renderer_opengl/gl_query_cache.cpp | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstring> | ||
| 7 | #include <memory> | ||
| 8 | #include <unordered_map> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include <glad/glad.h> | ||
| 13 | |||
| 14 | #include "common/assert.h" | ||
| 15 | #include "core/core.h" | ||
| 16 | #include "video_core/engines/maxwell_3d.h" | ||
| 17 | #include "video_core/memory_manager.h" | ||
| 18 | #include "video_core/renderer_opengl/gl_query_cache.h" | ||
| 19 | #include "video_core/renderer_opengl/gl_rasterizer.h" | ||
| 20 | |||
| 21 | namespace OpenGL { | ||
| 22 | |||
| 23 | namespace { | ||
| 24 | |||
| 25 | constexpr std::array<GLenum, VideoCore::NumQueryTypes> QueryTargets = {GL_SAMPLES_PASSED}; | ||
| 26 | |||
| 27 | constexpr GLenum GetTarget(VideoCore::QueryType type) { | ||
| 28 | return QueryTargets[static_cast<std::size_t>(type)]; | ||
| 29 | } | ||
| 30 | |||
| 31 | } // Anonymous namespace | ||
| 32 | |||
| 33 | QueryCache::QueryCache(Core::System& system, RasterizerOpenGL& gl_rasterizer) | ||
| 34 | : VideoCommon::QueryCacheBase< | ||
| 35 | QueryCache, CachedQuery, CounterStream, HostCounter, | ||
| 36 | std::vector<OGLQuery>>{system, | ||
| 37 | static_cast<VideoCore::RasterizerInterface&>(gl_rasterizer)}, | ||
| 38 | gl_rasterizer{gl_rasterizer} {} | ||
| 39 | |||
| 40 | QueryCache::~QueryCache() = default; | ||
| 41 | |||
| 42 | OGLQuery QueryCache::AllocateQuery(VideoCore::QueryType type) { | ||
| 43 | auto& reserve = query_pools[static_cast<std::size_t>(type)]; | ||
| 44 | OGLQuery query; | ||
| 45 | if (reserve.empty()) { | ||
| 46 | query.Create(GetTarget(type)); | ||
| 47 | return query; | ||
| 48 | } | ||
| 49 | |||
| 50 | query = std::move(reserve.back()); | ||
| 51 | reserve.pop_back(); | ||
| 52 | return query; | ||
| 53 | } | ||
| 54 | |||
| 55 | void QueryCache::Reserve(VideoCore::QueryType type, OGLQuery&& query) { | ||
| 56 | query_pools[static_cast<std::size_t>(type)].push_back(std::move(query)); | ||
| 57 | } | ||
| 58 | |||
| 59 | bool QueryCache::AnyCommandQueued() const noexcept { | ||
| 60 | return gl_rasterizer.AnyCommandQueued(); | ||
| 61 | } | ||
| 62 | |||
| 63 | HostCounter::HostCounter(QueryCache& cache, std::shared_ptr<HostCounter> dependency, | ||
| 64 | VideoCore::QueryType type) | ||
| 65 | : VideoCommon::HostCounterBase<QueryCache, HostCounter>{std::move(dependency)}, cache{cache}, | ||
| 66 | type{type}, query{cache.AllocateQuery(type)} { | ||
| 67 | glBeginQuery(GetTarget(type), query.handle); | ||
| 68 | } | ||
| 69 | |||
| 70 | HostCounter::~HostCounter() { | ||
| 71 | cache.Reserve(type, std::move(query)); | ||
| 72 | } | ||
| 73 | |||
| 74 | void HostCounter::EndQuery() { | ||
| 75 | if (!cache.AnyCommandQueued()) { | ||
| 76 | // There are chances a query waited on without commands (glDraw, glClear, glDispatch). Not | ||
| 77 | // having any of these causes a lock. glFlush is considered a command, so we can safely wait | ||
| 78 | // for this. Insert to the OpenGL command stream a flush. | ||
| 79 | glFlush(); | ||
| 80 | } | ||
| 81 | glEndQuery(GetTarget(type)); | ||
| 82 | } | ||
| 83 | |||
| 84 | u64 HostCounter::BlockingQuery() const { | ||
| 85 | GLint64 value; | ||
| 86 | glGetQueryObjecti64v(query.handle, GL_QUERY_RESULT, &value); | ||
| 87 | return static_cast<u64>(value); | ||
| 88 | } | ||
| 89 | |||
| 90 | CachedQuery::CachedQuery(QueryCache& cache, VideoCore::QueryType type, VAddr cpu_addr, u8* host_ptr) | ||
| 91 | : VideoCommon::CachedQueryBase<HostCounter>{cpu_addr, host_ptr}, cache{&cache}, type{type} {} | ||
| 92 | |||
| 93 | CachedQuery::CachedQuery(CachedQuery&& rhs) noexcept | ||
| 94 | : VideoCommon::CachedQueryBase<HostCounter>(std::move(rhs)), cache{rhs.cache}, type{rhs.type} {} | ||
| 95 | |||
| 96 | CachedQuery& CachedQuery::operator=(CachedQuery&& rhs) noexcept { | ||
| 97 | VideoCommon::CachedQueryBase<HostCounter>::operator=(std::move(rhs)); | ||
| 98 | cache = rhs.cache; | ||
| 99 | type = rhs.type; | ||
| 100 | return *this; | ||
| 101 | } | ||
| 102 | |||
| 103 | void CachedQuery::Flush() { | ||
| 104 | // Waiting for a query while another query of the same target is enabled locks Nvidia's driver. | ||
| 105 | // To avoid this disable and re-enable keeping the dependency stream. | ||
| 106 | // But we only have to do this if we have pending waits to be done. | ||
| 107 | auto& stream = cache->Stream(type); | ||
| 108 | const bool slice_counter = WaitPending() && stream.IsEnabled(); | ||
| 109 | if (slice_counter) { | ||
| 110 | stream.Update(false); | ||
| 111 | } | ||
| 112 | |||
| 113 | VideoCommon::CachedQueryBase<HostCounter>::Flush(); | ||
| 114 | |||
| 115 | if (slice_counter) { | ||
| 116 | stream.Update(true); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | } // namespace OpenGL | ||
diff --git a/src/video_core/renderer_opengl/gl_query_cache.h b/src/video_core/renderer_opengl/gl_query_cache.h new file mode 100644 index 000000000..d8e7052a1 --- /dev/null +++ b/src/video_core/renderer_opengl/gl_query_cache.h | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <memory> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "video_core/query_cache.h" | ||
| 13 | #include "video_core/rasterizer_interface.h" | ||
| 14 | #include "video_core/renderer_opengl/gl_resource_manager.h" | ||
| 15 | |||
| 16 | namespace Core { | ||
| 17 | class System; | ||
| 18 | } | ||
| 19 | |||
| 20 | namespace OpenGL { | ||
| 21 | |||
| 22 | class CachedQuery; | ||
| 23 | class HostCounter; | ||
| 24 | class QueryCache; | ||
| 25 | class RasterizerOpenGL; | ||
| 26 | |||
| 27 | using CounterStream = VideoCommon::CounterStreamBase<QueryCache, HostCounter>; | ||
| 28 | |||
| 29 | class QueryCache final : public VideoCommon::QueryCacheBase<QueryCache, CachedQuery, CounterStream, | ||
| 30 | HostCounter, std::vector<OGLQuery>> { | ||
| 31 | public: | ||
| 32 | explicit QueryCache(Core::System& system, RasterizerOpenGL& rasterizer); | ||
| 33 | ~QueryCache(); | ||
| 34 | |||
| 35 | OGLQuery AllocateQuery(VideoCore::QueryType type); | ||
| 36 | |||
| 37 | void Reserve(VideoCore::QueryType type, OGLQuery&& query); | ||
| 38 | |||
| 39 | bool AnyCommandQueued() const noexcept; | ||
| 40 | |||
| 41 | private: | ||
| 42 | RasterizerOpenGL& gl_rasterizer; | ||
| 43 | }; | ||
| 44 | |||
| 45 | class HostCounter final : public VideoCommon::HostCounterBase<QueryCache, HostCounter> { | ||
| 46 | public: | ||
| 47 | explicit HostCounter(QueryCache& cache, std::shared_ptr<HostCounter> dependency, | ||
| 48 | VideoCore::QueryType type); | ||
| 49 | ~HostCounter(); | ||
| 50 | |||
| 51 | void EndQuery(); | ||
| 52 | |||
| 53 | private: | ||
| 54 | u64 BlockingQuery() const override; | ||
| 55 | |||
| 56 | QueryCache& cache; | ||
| 57 | const VideoCore::QueryType type; | ||
| 58 | OGLQuery query; | ||
| 59 | }; | ||
| 60 | |||
| 61 | class CachedQuery final : public VideoCommon::CachedQueryBase<HostCounter> { | ||
| 62 | public: | ||
| 63 | explicit CachedQuery(QueryCache& cache, VideoCore::QueryType type, VAddr cpu_addr, | ||
| 64 | u8* host_ptr); | ||
| 65 | CachedQuery(CachedQuery&& rhs) noexcept; | ||
| 66 | CachedQuery(const CachedQuery&) = delete; | ||
| 67 | |||
| 68 | CachedQuery& operator=(CachedQuery&& rhs) noexcept; | ||
| 69 | CachedQuery& operator=(const CachedQuery&) = delete; | ||
| 70 | |||
| 71 | void Flush() override; | ||
| 72 | |||
| 73 | private: | ||
| 74 | QueryCache* cache; | ||
| 75 | VideoCore::QueryType type; | ||
| 76 | }; | ||
| 77 | |||
| 78 | } // namespace OpenGL | ||
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index b0eb14c8b..4bdc8db85 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp | |||
| @@ -25,6 +25,7 @@ | |||
| 25 | #include "video_core/engines/maxwell_3d.h" | 25 | #include "video_core/engines/maxwell_3d.h" |
| 26 | #include "video_core/engines/shader_type.h" | 26 | #include "video_core/engines/shader_type.h" |
| 27 | #include "video_core/memory_manager.h" | 27 | #include "video_core/memory_manager.h" |
| 28 | #include "video_core/renderer_opengl/gl_query_cache.h" | ||
| 28 | #include "video_core/renderer_opengl/gl_rasterizer.h" | 29 | #include "video_core/renderer_opengl/gl_rasterizer.h" |
| 29 | #include "video_core/renderer_opengl/gl_shader_cache.h" | 30 | #include "video_core/renderer_opengl/gl_shader_cache.h" |
| 30 | #include "video_core/renderer_opengl/gl_shader_gen.h" | 31 | #include "video_core/renderer_opengl/gl_shader_gen.h" |
| @@ -92,8 +93,8 @@ std::size_t GetConstBufferSize(const Tegra::Engines::ConstBufferInfo& buffer, | |||
| 92 | RasterizerOpenGL::RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window, | 93 | RasterizerOpenGL::RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window, |
| 93 | ScreenInfo& info) | 94 | ScreenInfo& info) |
| 94 | : RasterizerAccelerated{system.Memory()}, texture_cache{system, *this, device}, | 95 | : RasterizerAccelerated{system.Memory()}, texture_cache{system, *this, device}, |
| 95 | shader_cache{*this, system, emu_window, device}, system{system}, screen_info{info}, | 96 | shader_cache{*this, system, emu_window, device}, query_cache{system, *this}, system{system}, |
| 96 | buffer_cache{*this, system, device, STREAM_BUFFER_SIZE} { | 97 | screen_info{info}, buffer_cache{*this, system, device, STREAM_BUFFER_SIZE} { |
| 97 | shader_program_manager = std::make_unique<GLShader::ProgramManager>(); | 98 | shader_program_manager = std::make_unique<GLShader::ProgramManager>(); |
| 98 | state.draw.shader_program = 0; | 99 | state.draw.shader_program = 0; |
| 99 | state.Apply(); | 100 | state.Apply(); |
| @@ -541,11 +542,16 @@ void RasterizerOpenGL::Clear() { | |||
| 541 | } else if (use_stencil) { | 542 | } else if (use_stencil) { |
| 542 | glClearBufferiv(GL_STENCIL, 0, ®s.clear_stencil); | 543 | glClearBufferiv(GL_STENCIL, 0, ®s.clear_stencil); |
| 543 | } | 544 | } |
| 545 | |||
| 546 | ++num_queued_commands; | ||
| 544 | } | 547 | } |
| 545 | 548 | ||
| 546 | void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) { | 549 | void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) { |
| 547 | MICROPROFILE_SCOPE(OpenGL_Drawing); | 550 | MICROPROFILE_SCOPE(OpenGL_Drawing); |
| 548 | auto& gpu = system.GPU().Maxwell3D(); | 551 | auto& gpu = system.GPU().Maxwell3D(); |
| 552 | const auto& regs = gpu.regs; | ||
| 553 | |||
| 554 | query_cache.UpdateCounters(); | ||
| 549 | 555 | ||
| 550 | SyncRasterizeEnable(state); | 556 | SyncRasterizeEnable(state); |
| 551 | SyncColorMask(); | 557 | SyncColorMask(); |
| @@ -638,6 +644,8 @@ void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) { | |||
| 638 | glTextureBarrier(); | 644 | glTextureBarrier(); |
| 639 | } | 645 | } |
| 640 | 646 | ||
| 647 | ++num_queued_commands; | ||
| 648 | |||
| 641 | const GLuint base_instance = static_cast<GLuint>(gpu.regs.vb_base_instance); | 649 | const GLuint base_instance = static_cast<GLuint>(gpu.regs.vb_base_instance); |
| 642 | const GLsizei num_instances = | 650 | const GLsizei num_instances = |
| 643 | static_cast<GLsizei>(is_instanced ? gpu.mme_draw.instance_count : 1); | 651 | static_cast<GLsizei>(is_instanced ? gpu.mme_draw.instance_count : 1); |
| @@ -707,6 +715,16 @@ void RasterizerOpenGL::DispatchCompute(GPUVAddr code_addr) { | |||
| 707 | state.ApplyProgramPipeline(); | 715 | state.ApplyProgramPipeline(); |
| 708 | 716 | ||
| 709 | glDispatchCompute(launch_desc.grid_dim_x, launch_desc.grid_dim_y, launch_desc.grid_dim_z); | 717 | glDispatchCompute(launch_desc.grid_dim_x, launch_desc.grid_dim_y, launch_desc.grid_dim_z); |
| 718 | ++num_queued_commands; | ||
| 719 | } | ||
| 720 | |||
| 721 | void RasterizerOpenGL::ResetCounter(VideoCore::QueryType type) { | ||
| 722 | query_cache.ResetCounter(type); | ||
| 723 | } | ||
| 724 | |||
| 725 | void RasterizerOpenGL::Query(GPUVAddr gpu_addr, VideoCore::QueryType type, | ||
| 726 | std::optional<u64> timestamp) { | ||
| 727 | query_cache.Query(gpu_addr, type, timestamp); | ||
| 710 | } | 728 | } |
| 711 | 729 | ||
| 712 | void RasterizerOpenGL::FlushAll() {} | 730 | void RasterizerOpenGL::FlushAll() {} |
| @@ -718,6 +736,7 @@ void RasterizerOpenGL::FlushRegion(CacheAddr addr, u64 size) { | |||
| 718 | } | 736 | } |
| 719 | texture_cache.FlushRegion(addr, size); | 737 | texture_cache.FlushRegion(addr, size); |
| 720 | buffer_cache.FlushRegion(addr, size); | 738 | buffer_cache.FlushRegion(addr, size); |
| 739 | query_cache.FlushRegion(addr, size); | ||
| 721 | } | 740 | } |
| 722 | 741 | ||
| 723 | void RasterizerOpenGL::InvalidateRegion(CacheAddr addr, u64 size) { | 742 | void RasterizerOpenGL::InvalidateRegion(CacheAddr addr, u64 size) { |
| @@ -728,6 +747,7 @@ void RasterizerOpenGL::InvalidateRegion(CacheAddr addr, u64 size) { | |||
| 728 | texture_cache.InvalidateRegion(addr, size); | 747 | texture_cache.InvalidateRegion(addr, size); |
| 729 | shader_cache.InvalidateRegion(addr, size); | 748 | shader_cache.InvalidateRegion(addr, size); |
| 730 | buffer_cache.InvalidateRegion(addr, size); | 749 | buffer_cache.InvalidateRegion(addr, size); |
| 750 | query_cache.InvalidateRegion(addr, size); | ||
| 731 | } | 751 | } |
| 732 | 752 | ||
| 733 | void RasterizerOpenGL::FlushAndInvalidateRegion(CacheAddr addr, u64 size) { | 753 | void RasterizerOpenGL::FlushAndInvalidateRegion(CacheAddr addr, u64 size) { |
| @@ -738,10 +758,18 @@ void RasterizerOpenGL::FlushAndInvalidateRegion(CacheAddr addr, u64 size) { | |||
| 738 | } | 758 | } |
| 739 | 759 | ||
| 740 | void RasterizerOpenGL::FlushCommands() { | 760 | void RasterizerOpenGL::FlushCommands() { |
| 761 | // Only flush when we have commands queued to OpenGL. | ||
| 762 | if (num_queued_commands == 0) { | ||
| 763 | return; | ||
| 764 | } | ||
| 765 | num_queued_commands = 0; | ||
| 741 | glFlush(); | 766 | glFlush(); |
| 742 | } | 767 | } |
| 743 | 768 | ||
| 744 | void RasterizerOpenGL::TickFrame() { | 769 | void RasterizerOpenGL::TickFrame() { |
| 770 | // Ticking a frame means that buffers will be swapped, calling glFlush implicitly. | ||
| 771 | num_queued_commands = 0; | ||
| 772 | |||
| 745 | buffer_cache.TickFrame(); | 773 | buffer_cache.TickFrame(); |
| 746 | } | 774 | } |
| 747 | 775 | ||
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index 0501f3828..c772fd4ba 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h | |||
| @@ -24,6 +24,7 @@ | |||
| 24 | #include "video_core/renderer_opengl/gl_buffer_cache.h" | 24 | #include "video_core/renderer_opengl/gl_buffer_cache.h" |
| 25 | #include "video_core/renderer_opengl/gl_device.h" | 25 | #include "video_core/renderer_opengl/gl_device.h" |
| 26 | #include "video_core/renderer_opengl/gl_framebuffer_cache.h" | 26 | #include "video_core/renderer_opengl/gl_framebuffer_cache.h" |
| 27 | #include "video_core/renderer_opengl/gl_query_cache.h" | ||
| 27 | #include "video_core/renderer_opengl/gl_resource_manager.h" | 28 | #include "video_core/renderer_opengl/gl_resource_manager.h" |
| 28 | #include "video_core/renderer_opengl/gl_sampler_cache.h" | 29 | #include "video_core/renderer_opengl/gl_sampler_cache.h" |
| 29 | #include "video_core/renderer_opengl/gl_shader_cache.h" | 30 | #include "video_core/renderer_opengl/gl_shader_cache.h" |
| @@ -61,6 +62,8 @@ public: | |||
| 61 | bool DrawMultiBatch(bool is_indexed) override; | 62 | bool DrawMultiBatch(bool is_indexed) override; |
| 62 | void Clear() override; | 63 | void Clear() override; |
| 63 | void DispatchCompute(GPUVAddr code_addr) override; | 64 | void DispatchCompute(GPUVAddr code_addr) override; |
| 65 | void ResetCounter(VideoCore::QueryType type) override; | ||
| 66 | void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override; | ||
| 64 | void FlushAll() override; | 67 | void FlushAll() override; |
| 65 | void FlushRegion(CacheAddr addr, u64 size) override; | 68 | void FlushRegion(CacheAddr addr, u64 size) override; |
| 66 | void InvalidateRegion(CacheAddr addr, u64 size) override; | 69 | void InvalidateRegion(CacheAddr addr, u64 size) override; |
| @@ -75,6 +78,11 @@ public: | |||
| 75 | void LoadDiskResources(const std::atomic_bool& stop_loading, | 78 | void LoadDiskResources(const std::atomic_bool& stop_loading, |
| 76 | const VideoCore::DiskResourceLoadCallback& callback) override; | 79 | const VideoCore::DiskResourceLoadCallback& callback) override; |
| 77 | 80 | ||
| 81 | /// Returns true when there are commands queued to the OpenGL server. | ||
| 82 | bool AnyCommandQueued() const { | ||
| 83 | return num_queued_commands > 0; | ||
| 84 | } | ||
| 85 | |||
| 78 | private: | 86 | private: |
| 79 | /// Configures the color and depth framebuffer states. | 87 | /// Configures the color and depth framebuffer states. |
| 80 | void ConfigureFramebuffers(); | 88 | void ConfigureFramebuffers(); |
| @@ -180,10 +188,23 @@ private: | |||
| 180 | /// Syncs the alpha test state to match the guest state | 188 | /// Syncs the alpha test state to match the guest state |
| 181 | void SyncAlphaTest(); | 189 | void SyncAlphaTest(); |
| 182 | 190 | ||
| 183 | /// Check for extension that are not strictly required | 191 | /// Check for extension that are not strictly required but are needed for correct emulation |
| 184 | /// but are needed for correct emulation | ||
| 185 | void CheckExtensions(); | 192 | void CheckExtensions(); |
| 186 | 193 | ||
| 194 | std::size_t CalculateVertexArraysSize() const; | ||
| 195 | |||
| 196 | std::size_t CalculateIndexBufferSize() const; | ||
| 197 | |||
| 198 | /// Updates and returns a vertex array object representing current vertex format | ||
| 199 | GLuint SetupVertexFormat(); | ||
| 200 | |||
| 201 | void SetupVertexBuffer(GLuint vao); | ||
| 202 | void SetupVertexInstances(GLuint vao); | ||
| 203 | |||
| 204 | GLintptr SetupIndexBuffer(); | ||
| 205 | |||
| 206 | void SetupShaders(GLenum primitive_mode); | ||
| 207 | |||
| 187 | const Device device; | 208 | const Device device; |
| 188 | OpenGLState state; | 209 | OpenGLState state; |
| 189 | 210 | ||
| @@ -191,6 +212,7 @@ private: | |||
| 191 | ShaderCacheOpenGL shader_cache; | 212 | ShaderCacheOpenGL shader_cache; |
| 192 | SamplerCacheOpenGL sampler_cache; | 213 | SamplerCacheOpenGL sampler_cache; |
| 193 | FramebufferCacheOpenGL framebuffer_cache; | 214 | FramebufferCacheOpenGL framebuffer_cache; |
| 215 | QueryCache query_cache; | ||
| 194 | 216 | ||
| 195 | Core::System& system; | 217 | Core::System& system; |
| 196 | ScreenInfo& screen_info; | 218 | ScreenInfo& screen_info; |
| @@ -208,19 +230,8 @@ private: | |||
| 208 | BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER}; | 230 | BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER}; |
| 209 | BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER}; | 231 | BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER}; |
| 210 | 232 | ||
| 211 | std::size_t CalculateVertexArraysSize() const; | 233 | /// Number of commands queued to the OpenGL driver. Reseted on flush. |
| 212 | 234 | std::size_t num_queued_commands = 0; | |
| 213 | std::size_t CalculateIndexBufferSize() const; | ||
| 214 | |||
| 215 | /// Updates and returns a vertex array object representing current vertex format | ||
| 216 | GLuint SetupVertexFormat(); | ||
| 217 | |||
| 218 | void SetupVertexBuffer(GLuint vao); | ||
| 219 | void SetupVertexInstances(GLuint vao); | ||
| 220 | |||
| 221 | GLintptr SetupIndexBuffer(); | ||
| 222 | |||
| 223 | void SetupShaders(GLenum primitive_mode); | ||
| 224 | }; | 235 | }; |
| 225 | 236 | ||
| 226 | } // namespace OpenGL | 237 | } // namespace OpenGL |
diff --git a/src/video_core/renderer_opengl/gl_resource_manager.cpp b/src/video_core/renderer_opengl/gl_resource_manager.cpp index 5c96c1d46..f0ddfb276 100644 --- a/src/video_core/renderer_opengl/gl_resource_manager.cpp +++ b/src/video_core/renderer_opengl/gl_resource_manager.cpp | |||
| @@ -207,4 +207,21 @@ void OGLFramebuffer::Release() { | |||
| 207 | handle = 0; | 207 | handle = 0; |
| 208 | } | 208 | } |
| 209 | 209 | ||
| 210 | void OGLQuery::Create(GLenum target) { | ||
| 211 | if (handle != 0) | ||
| 212 | return; | ||
| 213 | |||
| 214 | MICROPROFILE_SCOPE(OpenGL_ResourceCreation); | ||
| 215 | glCreateQueries(target, 1, &handle); | ||
| 216 | } | ||
| 217 | |||
| 218 | void OGLQuery::Release() { | ||
| 219 | if (handle == 0) | ||
| 220 | return; | ||
| 221 | |||
| 222 | MICROPROFILE_SCOPE(OpenGL_ResourceDeletion); | ||
| 223 | glDeleteQueries(1, &handle); | ||
| 224 | handle = 0; | ||
| 225 | } | ||
| 226 | |||
| 210 | } // namespace OpenGL | 227 | } // namespace OpenGL |
diff --git a/src/video_core/renderer_opengl/gl_resource_manager.h b/src/video_core/renderer_opengl/gl_resource_manager.h index 3a85a1d4c..514d1d165 100644 --- a/src/video_core/renderer_opengl/gl_resource_manager.h +++ b/src/video_core/renderer_opengl/gl_resource_manager.h | |||
| @@ -266,4 +266,29 @@ public: | |||
| 266 | GLuint handle = 0; | 266 | GLuint handle = 0; |
| 267 | }; | 267 | }; |
| 268 | 268 | ||
| 269 | class OGLQuery : private NonCopyable { | ||
| 270 | public: | ||
| 271 | OGLQuery() = default; | ||
| 272 | |||
| 273 | OGLQuery(OGLQuery&& o) noexcept : handle(std::exchange(o.handle, 0)) {} | ||
| 274 | |||
| 275 | ~OGLQuery() { | ||
| 276 | Release(); | ||
| 277 | } | ||
| 278 | |||
| 279 | OGLQuery& operator=(OGLQuery&& o) noexcept { | ||
| 280 | Release(); | ||
| 281 | handle = std::exchange(o.handle, 0); | ||
| 282 | return *this; | ||
| 283 | } | ||
| 284 | |||
| 285 | /// Creates a new internal OpenGL resource and stores the handle | ||
| 286 | void Create(GLenum target); | ||
| 287 | |||
| 288 | /// Deletes the internal OpenGL resource | ||
| 289 | void Release(); | ||
| 290 | |||
| 291 | GLuint handle = 0; | ||
| 292 | }; | ||
| 293 | |||
| 269 | } // namespace OpenGL | 294 | } // namespace OpenGL |
diff --git a/src/video_core/renderer_vulkan/vk_device.cpp b/src/video_core/renderer_vulkan/vk_device.cpp index 9840f26e5..588a6835f 100644 --- a/src/video_core/renderer_vulkan/vk_device.cpp +++ b/src/video_core/renderer_vulkan/vk_device.cpp | |||
| @@ -104,6 +104,7 @@ bool VKDevice::Create(const vk::DispatchLoaderDynamic& dldi, vk::Instance instan | |||
| 104 | features.depthBiasClamp = true; | 104 | features.depthBiasClamp = true; |
| 105 | features.geometryShader = true; | 105 | features.geometryShader = true; |
| 106 | features.tessellationShader = true; | 106 | features.tessellationShader = true; |
| 107 | features.occlusionQueryPrecise = true; | ||
| 107 | features.fragmentStoresAndAtomics = true; | 108 | features.fragmentStoresAndAtomics = true; |
| 108 | features.shaderImageGatherExtended = true; | 109 | features.shaderImageGatherExtended = true; |
| 109 | features.shaderStorageImageWriteWithoutFormat = true; | 110 | features.shaderStorageImageWriteWithoutFormat = true; |
| @@ -117,6 +118,10 @@ bool VKDevice::Create(const vk::DispatchLoaderDynamic& dldi, vk::Instance instan | |||
| 117 | bit8_storage.uniformAndStorageBuffer8BitAccess = true; | 118 | bit8_storage.uniformAndStorageBuffer8BitAccess = true; |
| 118 | SetNext(next, bit8_storage); | 119 | SetNext(next, bit8_storage); |
| 119 | 120 | ||
| 121 | vk::PhysicalDeviceHostQueryResetFeaturesEXT host_query_reset; | ||
| 122 | host_query_reset.hostQueryReset = true; | ||
| 123 | SetNext(next, host_query_reset); | ||
| 124 | |||
| 120 | vk::PhysicalDeviceFloat16Int8FeaturesKHR float16_int8; | 125 | vk::PhysicalDeviceFloat16Int8FeaturesKHR float16_int8; |
| 121 | if (is_float16_supported) { | 126 | if (is_float16_supported) { |
| 122 | float16_int8.shaderFloat16 = true; | 127 | float16_int8.shaderFloat16 = true; |
| @@ -273,6 +278,7 @@ bool VKDevice::IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDev | |||
| 273 | VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME, | 278 | VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME, |
| 274 | VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, | 279 | VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, |
| 275 | VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, | 280 | VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, |
| 281 | VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME, | ||
| 276 | }; | 282 | }; |
| 277 | std::bitset<required_extensions.size()> available_extensions{}; | 283 | std::bitset<required_extensions.size()> available_extensions{}; |
| 278 | 284 | ||
| @@ -340,6 +346,7 @@ bool VKDevice::IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDev | |||
| 340 | std::make_pair(features.depthBiasClamp, "depthBiasClamp"), | 346 | std::make_pair(features.depthBiasClamp, "depthBiasClamp"), |
| 341 | std::make_pair(features.geometryShader, "geometryShader"), | 347 | std::make_pair(features.geometryShader, "geometryShader"), |
| 342 | std::make_pair(features.tessellationShader, "tessellationShader"), | 348 | std::make_pair(features.tessellationShader, "tessellationShader"), |
| 349 | std::make_pair(features.occlusionQueryPrecise, "occlusionQueryPrecise"), | ||
| 343 | std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"), | 350 | std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"), |
| 344 | std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"), | 351 | std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"), |
| 345 | std::make_pair(features.shaderStorageImageWriteWithoutFormat, | 352 | std::make_pair(features.shaderStorageImageWriteWithoutFormat, |
| @@ -376,7 +383,7 @@ std::vector<const char*> VKDevice::LoadExtensions(const vk::DispatchLoaderDynami | |||
| 376 | } | 383 | } |
| 377 | }; | 384 | }; |
| 378 | 385 | ||
| 379 | extensions.reserve(13); | 386 | extensions.reserve(14); |
| 380 | extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); | 387 | extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); |
| 381 | extensions.push_back(VK_KHR_16BIT_STORAGE_EXTENSION_NAME); | 388 | extensions.push_back(VK_KHR_16BIT_STORAGE_EXTENSION_NAME); |
| 382 | extensions.push_back(VK_KHR_8BIT_STORAGE_EXTENSION_NAME); | 389 | extensions.push_back(VK_KHR_8BIT_STORAGE_EXTENSION_NAME); |
| @@ -384,6 +391,7 @@ std::vector<const char*> VKDevice::LoadExtensions(const vk::DispatchLoaderDynami | |||
| 384 | extensions.push_back(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME); | 391 | extensions.push_back(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME); |
| 385 | extensions.push_back(VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME); | 392 | extensions.push_back(VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME); |
| 386 | extensions.push_back(VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME); | 393 | extensions.push_back(VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME); |
| 394 | extensions.push_back(VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME); | ||
| 387 | 395 | ||
| 388 | [[maybe_unused]] const bool nsight = | 396 | [[maybe_unused]] const bool nsight = |
| 389 | std::getenv("NVTX_INJECTION64_PATH") || std::getenv("NSIGHT_LAUNCHED"); | 397 | std::getenv("NVTX_INJECTION64_PATH") || std::getenv("NSIGHT_LAUNCHED"); |
diff --git a/src/video_core/renderer_vulkan/vk_query_cache.cpp b/src/video_core/renderer_vulkan/vk_query_cache.cpp new file mode 100644 index 000000000..ffbf60dda --- /dev/null +++ b/src/video_core/renderer_vulkan/vk_query_cache.cpp | |||
| @@ -0,0 +1,122 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "video_core/renderer_vulkan/declarations.h" | ||
| 12 | #include "video_core/renderer_vulkan/vk_device.h" | ||
| 13 | #include "video_core/renderer_vulkan/vk_query_cache.h" | ||
| 14 | #include "video_core/renderer_vulkan/vk_resource_manager.h" | ||
| 15 | #include "video_core/renderer_vulkan/vk_scheduler.h" | ||
| 16 | |||
| 17 | namespace Vulkan { | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | constexpr std::array QUERY_TARGETS = {vk::QueryType::eOcclusion}; | ||
| 22 | |||
| 23 | constexpr vk::QueryType GetTarget(VideoCore::QueryType type) { | ||
| 24 | return QUERY_TARGETS[static_cast<std::size_t>(type)]; | ||
| 25 | } | ||
| 26 | |||
| 27 | } // Anonymous namespace | ||
| 28 | |||
| 29 | QueryPool::QueryPool() : VKFencedPool{GROW_STEP} {} | ||
| 30 | |||
| 31 | QueryPool::~QueryPool() = default; | ||
| 32 | |||
| 33 | void QueryPool::Initialize(const VKDevice& device_, VideoCore::QueryType type_) { | ||
| 34 | device = &device_; | ||
| 35 | type = type_; | ||
| 36 | } | ||
| 37 | |||
| 38 | std::pair<vk::QueryPool, std::uint32_t> QueryPool::Commit(VKFence& fence) { | ||
| 39 | std::size_t index; | ||
| 40 | do { | ||
| 41 | index = CommitResource(fence); | ||
| 42 | } while (usage[index]); | ||
| 43 | usage[index] = true; | ||
| 44 | |||
| 45 | return {*pools[index / GROW_STEP], static_cast<std::uint32_t>(index % GROW_STEP)}; | ||
| 46 | } | ||
| 47 | |||
| 48 | void QueryPool::Allocate(std::size_t begin, std::size_t end) { | ||
| 49 | usage.resize(end); | ||
| 50 | |||
| 51 | const auto dev = device->GetLogical(); | ||
| 52 | const u32 size = static_cast<u32>(end - begin); | ||
| 53 | const vk::QueryPoolCreateInfo query_pool_ci({}, GetTarget(type), size, {}); | ||
| 54 | pools.push_back(dev.createQueryPoolUnique(query_pool_ci, nullptr, device->GetDispatchLoader())); | ||
| 55 | } | ||
| 56 | |||
| 57 | void QueryPool::Reserve(std::pair<vk::QueryPool, std::uint32_t> query) { | ||
| 58 | const auto it = | ||
| 59 | std::find_if(std::begin(pools), std::end(pools), | ||
| 60 | [query_pool = query.first](auto& pool) { return query_pool == *pool; }); | ||
| 61 | ASSERT(it != std::end(pools)); | ||
| 62 | |||
| 63 | const std::ptrdiff_t pool_index = std::distance(std::begin(pools), it); | ||
| 64 | usage[pool_index * GROW_STEP + static_cast<std::ptrdiff_t>(query.second)] = false; | ||
| 65 | } | ||
| 66 | |||
| 67 | VKQueryCache::VKQueryCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer, | ||
| 68 | const VKDevice& device, VKScheduler& scheduler) | ||
| 69 | : VideoCommon::QueryCacheBase<VKQueryCache, CachedQuery, CounterStream, HostCounter, | ||
| 70 | QueryPool>{system, rasterizer}, | ||
| 71 | device{device}, scheduler{scheduler} { | ||
| 72 | for (std::size_t i = 0; i < static_cast<std::size_t>(VideoCore::NumQueryTypes); ++i) { | ||
| 73 | query_pools[i].Initialize(device, static_cast<VideoCore::QueryType>(i)); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | VKQueryCache::~VKQueryCache() = default; | ||
| 78 | |||
| 79 | std::pair<vk::QueryPool, std::uint32_t> VKQueryCache::AllocateQuery(VideoCore::QueryType type) { | ||
| 80 | return query_pools[static_cast<std::size_t>(type)].Commit(scheduler.GetFence()); | ||
| 81 | } | ||
| 82 | |||
| 83 | void VKQueryCache::Reserve(VideoCore::QueryType type, | ||
| 84 | std::pair<vk::QueryPool, std::uint32_t> query) { | ||
| 85 | query_pools[static_cast<std::size_t>(type)].Reserve(query); | ||
| 86 | } | ||
| 87 | |||
| 88 | HostCounter::HostCounter(VKQueryCache& cache, std::shared_ptr<HostCounter> dependency, | ||
| 89 | VideoCore::QueryType type) | ||
| 90 | : VideoCommon::HostCounterBase<VKQueryCache, HostCounter>{std::move(dependency)}, cache{cache}, | ||
| 91 | type{type}, query{cache.AllocateQuery(type)}, ticks{cache.Scheduler().Ticks()} { | ||
| 92 | const auto dev = cache.Device().GetLogical(); | ||
| 93 | cache.Scheduler().Record([dev, query = query](vk::CommandBuffer cmdbuf, auto& dld) { | ||
| 94 | dev.resetQueryPoolEXT(query.first, query.second, 1, dld); | ||
| 95 | cmdbuf.beginQuery(query.first, query.second, vk::QueryControlFlagBits::ePrecise, dld); | ||
| 96 | }); | ||
| 97 | } | ||
| 98 | |||
| 99 | HostCounter::~HostCounter() { | ||
| 100 | cache.Reserve(type, query); | ||
| 101 | } | ||
| 102 | |||
| 103 | void HostCounter::EndQuery() { | ||
| 104 | cache.Scheduler().Record([query = query](auto cmdbuf, auto& dld) { | ||
| 105 | cmdbuf.endQuery(query.first, query.second, dld); | ||
| 106 | }); | ||
| 107 | } | ||
| 108 | |||
| 109 | u64 HostCounter::BlockingQuery() const { | ||
| 110 | if (ticks >= cache.Scheduler().Ticks()) { | ||
| 111 | cache.Scheduler().Flush(); | ||
| 112 | } | ||
| 113 | |||
| 114 | const auto dev = cache.Device().GetLogical(); | ||
| 115 | const auto& dld = cache.Device().GetDispatchLoader(); | ||
| 116 | u64 value; | ||
| 117 | dev.getQueryPoolResults(query.first, query.second, 1, sizeof(value), &value, sizeof(value), | ||
| 118 | vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait, dld); | ||
| 119 | return value; | ||
| 120 | } | ||
| 121 | |||
| 122 | } // namespace Vulkan | ||
diff --git a/src/video_core/renderer_vulkan/vk_query_cache.h b/src/video_core/renderer_vulkan/vk_query_cache.h new file mode 100644 index 000000000..c3092ee96 --- /dev/null +++ b/src/video_core/renderer_vulkan/vk_query_cache.h | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <cstddef> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <memory> | ||
| 10 | #include <utility> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "common/common_types.h" | ||
| 14 | #include "video_core/query_cache.h" | ||
| 15 | #include "video_core/renderer_vulkan/declarations.h" | ||
| 16 | #include "video_core/renderer_vulkan/vk_resource_manager.h" | ||
| 17 | |||
| 18 | namespace VideoCore { | ||
| 19 | class RasterizerInterface; | ||
| 20 | } | ||
| 21 | |||
| 22 | namespace Vulkan { | ||
| 23 | |||
| 24 | class CachedQuery; | ||
| 25 | class HostCounter; | ||
| 26 | class VKDevice; | ||
| 27 | class VKQueryCache; | ||
| 28 | class VKScheduler; | ||
| 29 | |||
| 30 | using CounterStream = VideoCommon::CounterStreamBase<VKQueryCache, HostCounter>; | ||
| 31 | |||
| 32 | class QueryPool final : public VKFencedPool { | ||
| 33 | public: | ||
| 34 | explicit QueryPool(); | ||
| 35 | ~QueryPool() override; | ||
| 36 | |||
| 37 | void Initialize(const VKDevice& device, VideoCore::QueryType type); | ||
| 38 | |||
| 39 | std::pair<vk::QueryPool, std::uint32_t> Commit(VKFence& fence); | ||
| 40 | |||
| 41 | void Reserve(std::pair<vk::QueryPool, std::uint32_t> query); | ||
| 42 | |||
| 43 | protected: | ||
| 44 | void Allocate(std::size_t begin, std::size_t end) override; | ||
| 45 | |||
| 46 | private: | ||
| 47 | static constexpr std::size_t GROW_STEP = 512; | ||
| 48 | |||
| 49 | const VKDevice* device = nullptr; | ||
| 50 | VideoCore::QueryType type = {}; | ||
| 51 | |||
| 52 | std::vector<UniqueQueryPool> pools; | ||
| 53 | std::vector<bool> usage; | ||
| 54 | }; | ||
| 55 | |||
| 56 | class VKQueryCache final | ||
| 57 | : public VideoCommon::QueryCacheBase<VKQueryCache, CachedQuery, CounterStream, HostCounter, | ||
| 58 | QueryPool> { | ||
| 59 | public: | ||
| 60 | explicit VKQueryCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer, | ||
| 61 | const VKDevice& device, VKScheduler& scheduler); | ||
| 62 | ~VKQueryCache(); | ||
| 63 | |||
| 64 | std::pair<vk::QueryPool, std::uint32_t> AllocateQuery(VideoCore::QueryType type); | ||
| 65 | |||
| 66 | void Reserve(VideoCore::QueryType type, std::pair<vk::QueryPool, std::uint32_t> query); | ||
| 67 | |||
| 68 | const VKDevice& Device() const noexcept { | ||
| 69 | return device; | ||
| 70 | } | ||
| 71 | |||
| 72 | VKScheduler& Scheduler() const noexcept { | ||
| 73 | return scheduler; | ||
| 74 | } | ||
| 75 | |||
| 76 | private: | ||
| 77 | const VKDevice& device; | ||
| 78 | VKScheduler& scheduler; | ||
| 79 | }; | ||
| 80 | |||
| 81 | class HostCounter final : public VideoCommon::HostCounterBase<VKQueryCache, HostCounter> { | ||
| 82 | public: | ||
| 83 | explicit HostCounter(VKQueryCache& cache, std::shared_ptr<HostCounter> dependency, | ||
| 84 | VideoCore::QueryType type); | ||
| 85 | ~HostCounter(); | ||
| 86 | |||
| 87 | void EndQuery(); | ||
| 88 | |||
| 89 | private: | ||
| 90 | u64 BlockingQuery() const override; | ||
| 91 | |||
| 92 | VKQueryCache& cache; | ||
| 93 | const VideoCore::QueryType type; | ||
| 94 | const std::pair<vk::QueryPool, std::uint32_t> query; | ||
| 95 | const u64 ticks; | ||
| 96 | }; | ||
| 97 | |||
| 98 | class CachedQuery : public VideoCommon::CachedQueryBase<HostCounter> { | ||
| 99 | public: | ||
| 100 | explicit CachedQuery(VKQueryCache&, VideoCore::QueryType, VAddr cpu_addr, u8* host_ptr) | ||
| 101 | : VideoCommon::CachedQueryBase<HostCounter>{cpu_addr, host_ptr} {} | ||
| 102 | }; | ||
| 103 | |||
| 104 | } // namespace Vulkan | ||
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp index aada38702..79aa121ed 100644 --- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp +++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp | |||
| @@ -289,7 +289,9 @@ RasterizerVulkan::RasterizerVulkan(Core::System& system, Core::Frontend::EmuWind | |||
| 289 | staging_pool), | 289 | staging_pool), |
| 290 | pipeline_cache(system, *this, device, scheduler, descriptor_pool, update_descriptor_queue), | 290 | pipeline_cache(system, *this, device, scheduler, descriptor_pool, update_descriptor_queue), |
| 291 | buffer_cache(*this, system, device, memory_manager, scheduler, staging_pool), | 291 | buffer_cache(*this, system, device, memory_manager, scheduler, staging_pool), |
| 292 | sampler_cache(device) {} | 292 | sampler_cache(device), query_cache(system, *this, device, scheduler) { |
| 293 | scheduler.SetQueryCache(query_cache); | ||
| 294 | } | ||
| 293 | 295 | ||
| 294 | RasterizerVulkan::~RasterizerVulkan() = default; | 296 | RasterizerVulkan::~RasterizerVulkan() = default; |
| 295 | 297 | ||
| @@ -308,6 +310,8 @@ void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) { | |||
| 308 | 310 | ||
| 309 | FlushWork(); | 311 | FlushWork(); |
| 310 | 312 | ||
| 313 | query_cache.UpdateCounters(); | ||
| 314 | |||
| 311 | const auto& gpu = system.GPU().Maxwell3D(); | 315 | const auto& gpu = system.GPU().Maxwell3D(); |
| 312 | GraphicsPipelineCacheKey key{GetFixedPipelineState(gpu.regs)}; | 316 | GraphicsPipelineCacheKey key{GetFixedPipelineState(gpu.regs)}; |
| 313 | 317 | ||
| @@ -362,6 +366,8 @@ void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) { | |||
| 362 | void RasterizerVulkan::Clear() { | 366 | void RasterizerVulkan::Clear() { |
| 363 | MICROPROFILE_SCOPE(Vulkan_Clearing); | 367 | MICROPROFILE_SCOPE(Vulkan_Clearing); |
| 364 | 368 | ||
| 369 | query_cache.UpdateCounters(); | ||
| 370 | |||
| 365 | const auto& gpu = system.GPU().Maxwell3D(); | 371 | const auto& gpu = system.GPU().Maxwell3D(); |
| 366 | if (!system.GPU().Maxwell3D().ShouldExecute()) { | 372 | if (!system.GPU().Maxwell3D().ShouldExecute()) { |
| 367 | return; | 373 | return; |
| @@ -429,6 +435,8 @@ void RasterizerVulkan::DispatchCompute(GPUVAddr code_addr) { | |||
| 429 | sampled_views.clear(); | 435 | sampled_views.clear(); |
| 430 | image_views.clear(); | 436 | image_views.clear(); |
| 431 | 437 | ||
| 438 | query_cache.UpdateCounters(); | ||
| 439 | |||
| 432 | const auto& launch_desc = system.GPU().KeplerCompute().launch_description; | 440 | const auto& launch_desc = system.GPU().KeplerCompute().launch_description; |
| 433 | const ComputePipelineCacheKey key{ | 441 | const ComputePipelineCacheKey key{ |
| 434 | code_addr, | 442 | code_addr, |
| @@ -471,17 +479,28 @@ void RasterizerVulkan::DispatchCompute(GPUVAddr code_addr) { | |||
| 471 | }); | 479 | }); |
| 472 | } | 480 | } |
| 473 | 481 | ||
| 482 | void RasterizerVulkan::ResetCounter(VideoCore::QueryType type) { | ||
| 483 | query_cache.ResetCounter(type); | ||
| 484 | } | ||
| 485 | |||
| 486 | void RasterizerVulkan::Query(GPUVAddr gpu_addr, VideoCore::QueryType type, | ||
| 487 | std::optional<u64> timestamp) { | ||
| 488 | query_cache.Query(gpu_addr, type, timestamp); | ||
| 489 | } | ||
| 490 | |||
| 474 | void RasterizerVulkan::FlushAll() {} | 491 | void RasterizerVulkan::FlushAll() {} |
| 475 | 492 | ||
| 476 | void RasterizerVulkan::FlushRegion(CacheAddr addr, u64 size) { | 493 | void RasterizerVulkan::FlushRegion(CacheAddr addr, u64 size) { |
| 477 | texture_cache.FlushRegion(addr, size); | 494 | texture_cache.FlushRegion(addr, size); |
| 478 | buffer_cache.FlushRegion(addr, size); | 495 | buffer_cache.FlushRegion(addr, size); |
| 496 | query_cache.FlushRegion(addr, size); | ||
| 479 | } | 497 | } |
| 480 | 498 | ||
| 481 | void RasterizerVulkan::InvalidateRegion(CacheAddr addr, u64 size) { | 499 | void RasterizerVulkan::InvalidateRegion(CacheAddr addr, u64 size) { |
| 482 | texture_cache.InvalidateRegion(addr, size); | 500 | texture_cache.InvalidateRegion(addr, size); |
| 483 | pipeline_cache.InvalidateRegion(addr, size); | 501 | pipeline_cache.InvalidateRegion(addr, size); |
| 484 | buffer_cache.InvalidateRegion(addr, size); | 502 | buffer_cache.InvalidateRegion(addr, size); |
| 503 | query_cache.InvalidateRegion(addr, size); | ||
| 485 | } | 504 | } |
| 486 | 505 | ||
| 487 | void RasterizerVulkan::FlushAndInvalidateRegion(CacheAddr addr, u64 size) { | 506 | void RasterizerVulkan::FlushAndInvalidateRegion(CacheAddr addr, u64 size) { |
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.h b/src/video_core/renderer_vulkan/vk_rasterizer.h index 7be71e734..add1ad88c 100644 --- a/src/video_core/renderer_vulkan/vk_rasterizer.h +++ b/src/video_core/renderer_vulkan/vk_rasterizer.h | |||
| @@ -24,6 +24,7 @@ | |||
| 24 | #include "video_core/renderer_vulkan/vk_descriptor_pool.h" | 24 | #include "video_core/renderer_vulkan/vk_descriptor_pool.h" |
| 25 | #include "video_core/renderer_vulkan/vk_memory_manager.h" | 25 | #include "video_core/renderer_vulkan/vk_memory_manager.h" |
| 26 | #include "video_core/renderer_vulkan/vk_pipeline_cache.h" | 26 | #include "video_core/renderer_vulkan/vk_pipeline_cache.h" |
| 27 | #include "video_core/renderer_vulkan/vk_query_cache.h" | ||
| 27 | #include "video_core/renderer_vulkan/vk_renderpass_cache.h" | 28 | #include "video_core/renderer_vulkan/vk_renderpass_cache.h" |
| 28 | #include "video_core/renderer_vulkan/vk_resource_manager.h" | 29 | #include "video_core/renderer_vulkan/vk_resource_manager.h" |
| 29 | #include "video_core/renderer_vulkan/vk_sampler_cache.h" | 30 | #include "video_core/renderer_vulkan/vk_sampler_cache.h" |
| @@ -96,7 +97,7 @@ struct ImageView { | |||
| 96 | vk::ImageLayout* layout = nullptr; | 97 | vk::ImageLayout* layout = nullptr; |
| 97 | }; | 98 | }; |
| 98 | 99 | ||
| 99 | class RasterizerVulkan : public VideoCore::RasterizerAccelerated { | 100 | class RasterizerVulkan final : public VideoCore::RasterizerAccelerated { |
| 100 | public: | 101 | public: |
| 101 | explicit RasterizerVulkan(Core::System& system, Core::Frontend::EmuWindow& render_window, | 102 | explicit RasterizerVulkan(Core::System& system, Core::Frontend::EmuWindow& render_window, |
| 102 | VKScreenInfo& screen_info, const VKDevice& device, | 103 | VKScreenInfo& screen_info, const VKDevice& device, |
| @@ -108,6 +109,8 @@ public: | |||
| 108 | bool DrawMultiBatch(bool is_indexed) override; | 109 | bool DrawMultiBatch(bool is_indexed) override; |
| 109 | void Clear() override; | 110 | void Clear() override; |
| 110 | void DispatchCompute(GPUVAddr code_addr) override; | 111 | void DispatchCompute(GPUVAddr code_addr) override; |
| 112 | void ResetCounter(VideoCore::QueryType type) override; | ||
| 113 | void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override; | ||
| 111 | void FlushAll() override; | 114 | void FlushAll() override; |
| 112 | void FlushRegion(CacheAddr addr, u64 size) override; | 115 | void FlushRegion(CacheAddr addr, u64 size) override; |
| 113 | void InvalidateRegion(CacheAddr addr, u64 size) override; | 116 | void InvalidateRegion(CacheAddr addr, u64 size) override; |
| @@ -247,6 +250,7 @@ private: | |||
| 247 | VKPipelineCache pipeline_cache; | 250 | VKPipelineCache pipeline_cache; |
| 248 | VKBufferCache buffer_cache; | 251 | VKBufferCache buffer_cache; |
| 249 | VKSamplerCache sampler_cache; | 252 | VKSamplerCache sampler_cache; |
| 253 | VKQueryCache query_cache; | ||
| 250 | 254 | ||
| 251 | std::array<View, Maxwell::NumRenderTargets> color_attachments; | 255 | std::array<View, Maxwell::NumRenderTargets> color_attachments; |
| 252 | View zeta_attachment; | 256 | View zeta_attachment; |
diff --git a/src/video_core/renderer_vulkan/vk_scheduler.cpp b/src/video_core/renderer_vulkan/vk_scheduler.cpp index d66133ad1..92bd6c344 100644 --- a/src/video_core/renderer_vulkan/vk_scheduler.cpp +++ b/src/video_core/renderer_vulkan/vk_scheduler.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include "common/microprofile.h" | 6 | #include "common/microprofile.h" |
| 7 | #include "video_core/renderer_vulkan/declarations.h" | 7 | #include "video_core/renderer_vulkan/declarations.h" |
| 8 | #include "video_core/renderer_vulkan/vk_device.h" | 8 | #include "video_core/renderer_vulkan/vk_device.h" |
| 9 | #include "video_core/renderer_vulkan/vk_query_cache.h" | ||
| 9 | #include "video_core/renderer_vulkan/vk_resource_manager.h" | 10 | #include "video_core/renderer_vulkan/vk_resource_manager.h" |
| 10 | #include "video_core/renderer_vulkan/vk_scheduler.h" | 11 | #include "video_core/renderer_vulkan/vk_scheduler.h" |
| 11 | 12 | ||
| @@ -139,6 +140,8 @@ void VKScheduler::SubmitExecution(vk::Semaphore semaphore) { | |||
| 139 | } | 140 | } |
| 140 | 141 | ||
| 141 | void VKScheduler::AllocateNewContext() { | 142 | void VKScheduler::AllocateNewContext() { |
| 143 | ++ticks; | ||
| 144 | |||
| 142 | std::unique_lock lock{mutex}; | 145 | std::unique_lock lock{mutex}; |
| 143 | current_fence = next_fence; | 146 | current_fence = next_fence; |
| 144 | next_fence = &resource_manager.CommitFence(); | 147 | next_fence = &resource_manager.CommitFence(); |
| @@ -146,6 +149,10 @@ void VKScheduler::AllocateNewContext() { | |||
| 146 | current_cmdbuf = resource_manager.CommitCommandBuffer(*current_fence); | 149 | current_cmdbuf = resource_manager.CommitCommandBuffer(*current_fence); |
| 147 | current_cmdbuf.begin({vk::CommandBufferUsageFlagBits::eOneTimeSubmit}, | 150 | current_cmdbuf.begin({vk::CommandBufferUsageFlagBits::eOneTimeSubmit}, |
| 148 | device.GetDispatchLoader()); | 151 | device.GetDispatchLoader()); |
| 152 | // Enable counters once again. These are disabled when a command buffer is finished. | ||
| 153 | if (query_cache) { | ||
| 154 | query_cache->UpdateCounters(); | ||
| 155 | } | ||
| 149 | } | 156 | } |
| 150 | 157 | ||
| 151 | void VKScheduler::InvalidateState() { | 158 | void VKScheduler::InvalidateState() { |
| @@ -159,6 +166,7 @@ void VKScheduler::InvalidateState() { | |||
| 159 | } | 166 | } |
| 160 | 167 | ||
| 161 | void VKScheduler::EndPendingOperations() { | 168 | void VKScheduler::EndPendingOperations() { |
| 169 | query_cache->DisableStreams(); | ||
| 162 | EndRenderPass(); | 170 | EndRenderPass(); |
| 163 | } | 171 | } |
| 164 | 172 | ||
diff --git a/src/video_core/renderer_vulkan/vk_scheduler.h b/src/video_core/renderer_vulkan/vk_scheduler.h index bcdffbba0..62fd7858b 100644 --- a/src/video_core/renderer_vulkan/vk_scheduler.h +++ b/src/video_core/renderer_vulkan/vk_scheduler.h | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <atomic> | ||
| 7 | #include <condition_variable> | 8 | #include <condition_variable> |
| 8 | #include <memory> | 9 | #include <memory> |
| 9 | #include <optional> | 10 | #include <optional> |
| @@ -18,6 +19,7 @@ namespace Vulkan { | |||
| 18 | 19 | ||
| 19 | class VKDevice; | 20 | class VKDevice; |
| 20 | class VKFence; | 21 | class VKFence; |
| 22 | class VKQueryCache; | ||
| 21 | class VKResourceManager; | 23 | class VKResourceManager; |
| 22 | 24 | ||
| 23 | class VKFenceView { | 25 | class VKFenceView { |
| @@ -67,6 +69,11 @@ public: | |||
| 67 | /// Binds a pipeline to the current execution context. | 69 | /// Binds a pipeline to the current execution context. |
| 68 | void BindGraphicsPipeline(vk::Pipeline pipeline); | 70 | void BindGraphicsPipeline(vk::Pipeline pipeline); |
| 69 | 71 | ||
| 72 | /// Assigns the query cache. | ||
| 73 | void SetQueryCache(VKQueryCache& query_cache_) { | ||
| 74 | query_cache = &query_cache_; | ||
| 75 | } | ||
| 76 | |||
| 70 | /// Returns true when viewports have been set in the current command buffer. | 77 | /// Returns true when viewports have been set in the current command buffer. |
| 71 | bool TouchViewports() { | 78 | bool TouchViewports() { |
| 72 | return std::exchange(state.viewports, true); | 79 | return std::exchange(state.viewports, true); |
| @@ -112,6 +119,11 @@ public: | |||
| 112 | return current_fence; | 119 | return current_fence; |
| 113 | } | 120 | } |
| 114 | 121 | ||
| 122 | /// Returns the current command buffer tick. | ||
| 123 | u64 Ticks() const { | ||
| 124 | return ticks; | ||
| 125 | } | ||
| 126 | |||
| 115 | private: | 127 | private: |
| 116 | class Command { | 128 | class Command { |
| 117 | public: | 129 | public: |
| @@ -205,6 +217,8 @@ private: | |||
| 205 | 217 | ||
| 206 | const VKDevice& device; | 218 | const VKDevice& device; |
| 207 | VKResourceManager& resource_manager; | 219 | VKResourceManager& resource_manager; |
| 220 | VKQueryCache* query_cache = nullptr; | ||
| 221 | |||
| 208 | vk::CommandBuffer current_cmdbuf; | 222 | vk::CommandBuffer current_cmdbuf; |
| 209 | VKFence* current_fence = nullptr; | 223 | VKFence* current_fence = nullptr; |
| 210 | VKFence* next_fence = nullptr; | 224 | VKFence* next_fence = nullptr; |
| @@ -227,6 +241,7 @@ private: | |||
| 227 | Common::SPSCQueue<std::unique_ptr<CommandChunk>> chunk_reserve; | 241 | Common::SPSCQueue<std::unique_ptr<CommandChunk>> chunk_reserve; |
| 228 | std::mutex mutex; | 242 | std::mutex mutex; |
| 229 | std::condition_variable cv; | 243 | std::condition_variable cv; |
| 244 | std::atomic<u64> ticks = 0; | ||
| 230 | bool quit = false; | 245 | bool quit = false; |
| 231 | }; | 246 | }; |
| 232 | 247 | ||