| /* | |
| * Copyright 2024 Rive | |
| */ | |
| #include "common/render_context_null.hpp" | |
| #include "rive/math/math_types.hpp" | |
| #include "rive/renderer/draw.hpp" | |
| #include "rive/renderer/gpu.hpp" | |
| #include "rive/renderer/rive_render_paint.hpp" | |
| #include "rive_render_path.hpp" | |
| #include "shaders/constants.glsl" | |
| #ifdef RIVE_VULKAN | |
| #include "vulkan/pipeline_manager_vulkan.hpp" | |
| #endif | |
| #include <catch.hpp> | |
| #include <cstring> | |
| #include <map> | |
| #include <set> | |
| namespace rive | |
| { | |
| TEST_CASE("find_transformed_area", "[gpu]") | |
| { | |
| AABB unitSquare{0, 0, 1, 1}; | |
| CHECK(gpu::find_transformed_area(unitSquare, Mat2D()) == 1); | |
| CHECK(gpu::find_transformed_area(unitSquare, Mat2D::fromScale(2, 2)) == 4); | |
| CHECK(gpu::find_transformed_area(unitSquare, Mat2D::fromScale(2, 1)) == 2); | |
| CHECK(gpu::find_transformed_area(unitSquare, Mat2D::fromScale(0, 1)) == 0); | |
| CHECK( | |
| gpu::find_transformed_area( | |
| unitSquare, | |
| Mat2D::fromRotation(math::PI / 4)) == Approx(1.f)); | |
| CHECK( | |
| gpu::find_transformed_area( | |
| unitSquare, | |
| Mat2D::fromRotation(math::PI / 8).scale({2, 2})) == Approx(4.f)); | |
| CHECK( | |
| gpu::find_transformed_area( | |
| unitSquare, | |
| Mat2D::fromRotation(math::PI / 16).scale({2, 1})) == Approx(2.f)); | |
| CHECK( | |
| gpu::find_transformed_area(unitSquare, {1, .87f, 8, 8 * .87f, 0, 0}) == | |
| Approx(0.f).margin(math::EPSILON)); | |
| } | |
| TEST_CASE("gaussian_integral_table", "[gpu]") | |
| { | |
| float gaussianTable[gpu::GAUSSIAN_TABLE_SIZE]; | |
| for (int i = 0; i < gpu::GAUSSIAN_TABLE_SIZE; i += 4) | |
| { | |
| float4 f32s = gpu::cast_f16_to_f32( | |
| simd::load<uint16_t, 4>(gpu::g_gaussianIntegralTableF16 + i)); | |
| simd::store(gaussianTable + i, f32s); | |
| }; | |
| CHECK(gaussianTable[0] == 0.0f); | |
| CHECK(gaussianTable[gpu::GAUSSIAN_TABLE_SIZE - 1] == 1.0f); | |
| if (gpu::GAUSSIAN_TABLE_SIZE & 1) | |
| { | |
| CHECK(gaussianTable[gpu::GAUSSIAN_TABLE_SIZE / 2] == .5f); | |
| } | |
| else | |
| { | |
| CHECK(gaussianTable[gpu::GAUSSIAN_TABLE_SIZE / 2 - 1] <= .5f); | |
| CHECK(gaussianTable[gpu::GAUSSIAN_TABLE_SIZE / 2] >= .5f); | |
| CHECK( | |
| (gaussianTable[gpu::GAUSSIAN_TABLE_SIZE / 2 - 1] + | |
| gaussianTable[gpu::GAUSSIAN_TABLE_SIZE / 2]) / | |
| 2 == | |
| Approx(.5f).margin(1e-3f)); | |
| } | |
| for (int i = 1; i < gpu::GAUSSIAN_TABLE_SIZE; ++i) | |
| { | |
| CHECK(gaussianTable[i - 1] <= gaussianTable[i]); | |
| } | |
| for (int i = 0; i < (gpu::GAUSSIAN_TABLE_SIZE + 1) / 2; ++i) | |
| { | |
| CHECK( | |
| gaussianTable[i] + gaussianTable[gpu::GAUSSIAN_TABLE_SIZE - 1 - i] == | |
| Approx(1).margin(1e-3f)); | |
| } | |
| } | |
| // Looks up the value of "x" in the given function table, with linear filtering. | |
| static float function_table_lookup(float x, const float* table, float tableSize) | |
| { | |
| x = fminf(fmaxf(0, x), 1); | |
| float sampleRegionLeft = x * tableSize - .5f; | |
| int rightIdx = static_cast<int>(fminf(sampleRegionLeft + 1, tableSize - 1)); | |
| int leftIdx = std::max(rightIdx - 1, 0); | |
| float t = fminf(fmaxf(0, sampleRegionLeft - leftIdx), 1); | |
| return lerp(table[leftIdx], table[rightIdx], t); | |
| } | |
| TEST_CASE("inverse_gaussian_integral_table", "[gpu]") | |
| { | |
| float gaussianTable[gpu::GAUSSIAN_TABLE_SIZE]; | |
| for (int i = 0; i < gpu::GAUSSIAN_TABLE_SIZE; i += 4) | |
| { | |
| float4 f32s = gpu::cast_f16_to_f32( | |
| simd::load<uint16_t, 4>(gpu::g_gaussianIntegralTableF16 + i)); | |
| simd::store(gaussianTable + i, f32s); | |
| }; | |
| auto gaussianIntegral = [&gaussianTable](float x) { | |
| return function_table_lookup(x, gaussianTable, std::size(gaussianTable)); | |
| }; | |
| float inverseGaussianTable[gpu::GAUSSIAN_TABLE_SIZE]; | |
| for (int i = 0; i < gpu::GAUSSIAN_TABLE_SIZE; i += 4) | |
| { | |
| float4 f32s = gpu::cast_f16_to_f32( | |
| simd::load<uint16_t, 4>(gpu::g_inverseGaussianIntegralTableF16 + i)); | |
| simd::store(inverseGaussianTable + i, f32s); | |
| }; | |
| auto inverseGaussianIntegral = [&inverseGaussianTable](float y) { | |
| return function_table_lookup( | |
| y, | |
| inverseGaussianTable, | |
| std::size(inverseGaussianTable)); | |
| }; | |
| CHECK(inverseGaussianTable[0] == 0); | |
| CHECK(inverseGaussianTable[gpu::GAUSSIAN_TABLE_SIZE - 1] == 1); | |
| if (gpu::GAUSSIAN_TABLE_SIZE & 1) | |
| { | |
| CHECK(inverseGaussianTable[gpu::GAUSSIAN_TABLE_SIZE / 2] == .5f); | |
| } | |
| else | |
| { | |
| CHECK( | |
| (inverseGaussianTable[gpu::GAUSSIAN_TABLE_SIZE / 2 - 1] + | |
| inverseGaussianTable[gpu::GAUSSIAN_TABLE_SIZE / 2]) / | |
| 2 == | |
| Approx(.5f).margin(1e-4f)); | |
| } | |
| for (int i = 1; i < gpu::GAUSSIAN_TABLE_SIZE; ++i) | |
| { | |
| CHECK(inverseGaussianTable[i - 1] <= inverseGaussianTable[i]); | |
| } | |
| for (int i = 0; i < (gpu::GAUSSIAN_TABLE_SIZE + 1) / 2 - 4; ++i) | |
| { | |
| CHECK( | |
| inverseGaussianTable[i] + | |
| inverseGaussianTable[gpu::GAUSSIAN_TABLE_SIZE - 1 - i] == | |
| Approx(1).margin( | |
| i > 100 ? 5e-4f | |
| : i > 4 ? 1e-3f | |
| : 1e-2f)); | |
| } | |
| // Check that the inverse table is actually an inverse of the gaussian | |
| // integral. | |
| float M = 21; | |
| for (float x = 0; x <= 1; x += 1.f / (gpu::GAUSSIAN_TABLE_SIZE * M)) | |
| { | |
| float y = gaussianIntegral(x); | |
| // The inverse table loses precision at the inner and outer cells. | |
| float margin = x > .125f && x < .875f ? 1.f / 512 | |
| : x > .04f && x < .96f ? 1.f / 256 | |
| : x > .02f && x < .98f ? 1.f / 128 | |
| : 1.f / 90; | |
| CHECK(inverseGaussianIntegral(y) == Approx(x).margin(margin)); | |
| } | |
| // Check inverse_gaussian_integral edge cases. | |
| CHECK(inverseGaussianIntegral(-1) == 0); | |
| CHECK(inverseGaussianIntegral(2) == 1); | |
| CHECK(inverseGaussianIntegral(-std::numeric_limits<float>::infinity()) == 0); | |
| CHECK(inverseGaussianIntegral(std::numeric_limits<float>::infinity()) == 1); | |
| CHECK(inverseGaussianIntegral(std::numeric_limits<float>::quiet_NaN()) == 0); | |
| } | |
| // ForEachUbershaderPermutation() has to enumerate every DrawType a backend can | |
| // actually bind. The dynamic-state DrawTypes were missing from msaa's | |
| // list, so no ubershader ever got built for them and a draw that fell back to | |
| // the async path had nothing to fall back to. | |
| TEST_CASE("ForEachUbershaderPermutation", "[gpu]") | |
| { | |
| auto enumerateDrawTypes = [](bool supportsPipelineDynamicState) { | |
| gpu::PlatformFeatures platformFeatures; | |
| platformFeatures | |
| .supportsPipelineDynamicState = supportsPipelineDynamicState; | |
| std::set<gpu::DrawType> drawTypes; | |
| gpu::ForEachUbershaderPermutation( | |
| gpu::InterlockMode::depthStencil, | |
| platformFeatures, | |
| [&drawTypes]( | |
| gpu::DrawType drawType, | |
| gpu::ShaderFeatures, | |
| gpu::ShaderMiscFlags) { | |
| drawTypes.insert(drawType); | |
| return true; // Keep iterating. | |
| }); | |
| return drawTypes; | |
| }; | |
| const std::set<gpu::DrawType> withDynamicState = enumerateDrawTypes(true); | |
| CHECK(withDynamicState.count(gpu::DrawType::stencilDynamicMidpointFans) == 1); | |
| CHECK(withDynamicState.count(gpu::DrawType::stencilDynamicOuterCubics) == 1); | |
| // Backends that can't switch depth/stencil/cull/color-write without | |
| // rebinding a pipeline never bind these, so they don't precompile them. | |
| const std::set<gpu::DrawType> withoutDynamicState = enumerateDrawTypes(false); | |
| CHECK( | |
| withoutDynamicState.count(gpu::DrawType::stencilDynamicMidpointFans) == | |
| 0); | |
| CHECK( | |
| withoutDynamicState.count(gpu::DrawType::stencilDynamicOuterCubics) == 0); | |
| // The passes they collapse are enumerated either way. | |
| for (auto drawType : | |
| {gpu::DrawType::stencilMidpointFanBorrowedCoverage, | |
| gpu::DrawType::stencilMidpointFans, | |
| gpu::DrawType::stencilMidpointFanReset}) | |
| { | |
| CHECK(withDynamicState.count(drawType) == 1); | |
| CHECK(withoutDynamicState.count(drawType) == 1); | |
| } | |
| } | |
| #ifdef RIVE_VULKAN | |
| // Exercise the backend's enumeration, including render-pass options. The | |
| // generic shader enumerator cannot tell whether a Vulkan subpass exists. | |
| TEST_CASE( | |
| "Vulkan warmup pipeline states and resolve subpasses", | |
| "[gpu][vulkan_warmup]") | |
| { | |
| using namespace gpu; | |
| PlatformFeatures features; | |
| features.msaaColorPreserveNeedsDraw = true; | |
| features.supportsPipelineDynamicState = true; | |
| for (auto mode : | |
| {InterlockMode::rasterOrdering, | |
| InterlockMode::atomics, | |
| InterlockMode::depthStencil}) | |
| { | |
| for (auto load : | |
| {LoadAction::clear, | |
| LoadAction::preserveRenderTarget, | |
| LoadAction::dontCare}) | |
| { | |
| for (VkImageUsageFlags usage : | |
| {VkImageUsageFlags(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT), | |
| VkImageUsageFlags( | |
| VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | | |
| VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)}) | |
| { | |
| CAPTURE(mode, load, usage); | |
| std::set<bool> resolveSampleCounts; | |
| size_t ordinaryDraws = 0; | |
| PipelineManagerVulkan::forEachUbershaderPermutation( | |
| mode, | |
| VK_FORMAT_R8G8B8A8_UNORM, | |
| usage, | |
| load, | |
| DrawPipelineVulkan::Options::none, | |
| features, | |
| [&](const DrawPipelineVulkan::PipelineProps& props) { | |
| CAPTURE( | |
| props.drawType, | |
| props.drawContents, | |
| props.shaderMiscFlags, | |
| props.renderPassOptions); | |
| // Exercise the same state construction as DrawPipelineVulkan, | |
| // including its invariants, without needing a Vulkan device. | |
| const auto state = get_pipeline_state( | |
| props.drawType, | |
| props.interlockMode, | |
| props.shaderMiscFlags, | |
| props.drawContents, | |
| enums::is_flag_set( | |
| props.renderPassOptions, | |
| RenderPassOptionsVulkan::fixedFunctionColorOutput), | |
| props.blendMode, | |
| features); | |
| if (mode == InterlockMode::depthStencil && | |
| (props.drawType == DrawType::renderPassInitialize || | |
| props.drawType == DrawType::renderPassResolve)) | |
| { | |
| // These fullscreen copies replace color, including alpha. | |
| // Blending would incorrectly combine it with the destination. | |
| CHECK(enums::is_flag_set( | |
| props.drawContents, | |
| DrawContents::opaquePaint)); | |
| CHECK(state.blendEquation == BlendEquation::none); | |
| } | |
| if (props.drawType == DrawType::renderPassResolve) | |
| { | |
| // RenderPassVulkan appends a resolve subpass only for these | |
| // cases. Fixed-function MSAA resolve uses the main subpass. | |
| CHECK( | |
| (mode == InterlockMode::atomics || | |
| enums::is_flag_set( | |
| props.renderPassOptions, | |
| RenderPassOptionsVulkan::manuallyResolved))); | |
| resolveSampleCounts.insert(enums::is_flag_set( | |
| props.renderPassOptions, | |
| RenderPassOptionsVulkan::msaa)); | |
| } | |
| else if (props.drawType != DrawType::renderPassInitialize) | |
| { | |
| ++ordinaryDraws; | |
| } | |
| return true; | |
| }); | |
| CHECK(ordinaryDraws > 0); | |
| // Prevent a blanket resolve skip from satisfying the negative check. | |
| CHECK(resolveSampleCounts.count(false) == 1); | |
| CHECK( | |
| resolveSampleCounts.count(true) == | |
| (mode == InterlockMode::depthStencil ? 1 : 0)); | |
| } | |
| } | |
| } | |
| } | |
| TEST_CASE("Vulkan warmup color seed subpasses", "[gpu][vulkan_warmup]") | |
| { | |
| using namespace gpu; | |
| PlatformFeatures features; | |
| features.msaaColorPreserveNeedsDraw = true; | |
| features.supportsPipelineDynamicState = true; | |
| for (auto load : | |
| {LoadAction::clear, | |
| LoadAction::preserveRenderTarget, | |
| LoadAction::dontCare}) | |
| { | |
| CAPTURE(load); | |
| std::set<bool> seedSources; | |
| PipelineManagerVulkan::forEachUbershaderPermutation( | |
| InterlockMode::depthStencil, | |
| VK_FORMAT_R8G8B8A8_UNORM, | |
| VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | | |
| VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT, | |
| load, | |
| DrawPipelineVulkan::Options::none, | |
| features, | |
| [&](const DrawPipelineVulkan::PipelineProps& props) { | |
| if (props.drawType == DrawType::renderPassInitialize) | |
| { | |
| CHECK(load == LoadAction::preserveRenderTarget); | |
| CHECK(enums::is_flag_set( | |
| props.renderPassOptions, | |
| RenderPassOptionsVulkan::msaa)); | |
| seedSources.insert(enums::is_flag_set( | |
| props.renderPassOptions, | |
| RenderPassOptionsVulkan::msaaSeedFromOffscreenTexture)); | |
| } | |
| return true; | |
| }); | |
| if (load == LoadAction::preserveRenderTarget) | |
| { | |
| // Both the destination image and an offscreen image can seed MSAA. | |
| CHECK(seedSources == std::set<bool>{false, true}); | |
| } | |
| else | |
| { | |
| CHECK(seedSources.empty()); | |
| } | |
| } | |
| } | |
| #endif | |
| // ShaderUniqueKey() packs shaderMiscFlags densely, using only the bits that are | |
| // relevant to the interlockMode. Draw types that share a drawTypeKey therefore | |
| // have to agree on that layout -- otherwise the same packed value means | |
| // different flags for each, and two different shaders land on one key. | |
| TEST_CASE("shader_unique_keys_do_not_collide", "[gpu]") | |
| { | |
| gpu::PlatformFeatures platformFeatures; | |
| platformFeatures.supportsPipelineDynamicState = true; | |
| for (auto interlockMode : | |
| {gpu::InterlockMode::rasterOrdering, | |
| gpu::InterlockMode::atomics, | |
| gpu::InterlockMode::clockwise, | |
| gpu::InterlockMode::clockwiseAtomic, | |
| gpu::InterlockMode::depthStencil}) | |
| { | |
| // Two draws may share a key only if they compile to the same shader, | |
| // which requires identical shaderMiscFlags. | |
| std::map<uint32_t, gpu::ShaderMiscFlags> keyToMiscFlags; | |
| gpu::ForEachUbershaderPermutation( | |
| interlockMode, | |
| platformFeatures, | |
| [&](gpu::DrawType drawType, | |
| gpu::ShaderFeatures shaderFeatures, | |
| gpu::ShaderMiscFlags shaderMiscFlags) { | |
| uint32_t key = gpu::ShaderUniqueKey( | |
| drawType, | |
| shaderFeatures, | |
| interlockMode, | |
| shaderMiscFlags); | |
| auto [it, inserted] = keyToMiscFlags.insert({key, shaderMiscFlags}); | |
| if (!inserted) | |
| { | |
| INFO( | |
| "interlockMode " | |
| << static_cast<uint32_t>(interlockMode) << ", drawType " | |
| << static_cast<uint32_t>(drawType) << ", key " << key); | |
| CHECK(it->second == shaderMiscFlags); | |
| } | |
| return true; // Keep iterating. | |
| }); | |
| } | |
| } | |
| TEST_CASE("storage_texture_allocation_caps_its_height", "[gpu]") | |
| { | |
| // A buffer ring is sized for the whole frame (plus room to grow), which can | |
| // be more than a single flush ever uploads to its polyfill texture. | |
| auto [width, height] = gpu::storageTextureAllocationSize( | |
| size_t(STORAGE_TEXTURE_WIDTH) * gpu::MaxStorageTextureHeight * 2 * | |
| sizeof(uint32_t), | |
| gpu::StorageBufferStructure::uint32x1); | |
| CHECK(width == STORAGE_TEXTURE_WIDTH); | |
| CHECK(height == gpu::MaxStorageTextureHeight); | |
| } | |
| } // namespace rive |