Non-clang build fixes * <mm_malloc.h> is clang only. * __builtin_clz is clang only. * Generate preprocessor defines for shader feature sets as a bitfield instead of strings. * Use float literals with simd expressions, which are more compatible with the non-clang simd workaround class. * Remove C++20 designated initializer lists. Diffs= ffa49c5b9 Non-clang build fixes (#5397) Co-authored-by: Chris Dalton <99840794+csmartdalton@users.noreply.github.com> Co-authored-by: Scott Redig <scott@pocketwatchgames.com>
diff --git a/.rive_head b/.rive_head index 4b9413e..5f63853 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -6bb88136d85122356c2731bf96d47b07e41471a9 +ffa49c5b9c08c0f9c86ab901bc987e96a0eee794
diff --git a/include/rive/pls/aligned_buffer.hpp b/include/rive/pls/aligned_buffer.hpp index ad356f8..724b90b 100644 --- a/include/rive/pls/aligned_buffer.hpp +++ b/include/rive/pls/aligned_buffer.hpp
@@ -5,12 +5,18 @@ #pragma once #include "rive/rive_types.hpp" + +#ifdef __clang__ #include <mm_malloc.h> +#else +#include <cstdlib> +#endif namespace rive::pls { // Wraps a memory allocation whose address is aligned at a multiple of // "AlignmentInElements * sizeof(T)" bytes. +// This allows for optimal SSE/NEON loads. template <size_t AlignmentInElements, typename T> class AlignedBuffer { public: @@ -23,17 +29,38 @@ { if (m_capacity != capacity) { - // Align at 128 bits for optimal SSE/NEON loads. - _mm_free(m_buffer); + freeBuffer(); + // Round "capacity" up to a multiple of "AlignmentInElements". + capacity = + ((capacity + AlignmentInElements - 1) / AlignmentInElements) * AlignmentInElements; +#if defined(__clang__) m_buffer = reinterpret_cast<T*>( _mm_malloc(capacity * sizeof(T), AlignmentInElements * sizeof(T))); +#elif defined(_WIN32) + m_buffer = reinterpret_cast<T*>( + _aligned_malloc(capacity * sizeof(T), AlignmentInElements * sizeof(T))); +#else + m_buffer = reinterpret_cast<T*>( + std::aligned_alloc(AlignmentInElements * sizeof(T), capacity * sizeof(T))); +#endif m_capacity = capacity; } } - ~AlignedBuffer() { _mm_free(m_buffer); } + ~AlignedBuffer() { freeBuffer(); } private: + void freeBuffer() + { +#if defined(__clang__) + _mm_free(m_buffer); +#elif defined(_WIN32) + _aligned_free(m_buffer); +#else + std::free(m_buffer); +#endif + } + size_t m_capacity = 0; T* m_buffer = nullptr; };
diff --git a/include/rive/pls/pls.hpp b/include/rive/pls/pls.hpp index a5d2b0b..9636ec8 100644 --- a/include/rive/pls/pls.hpp +++ b/include/rive/pls/pls.hpp
@@ -74,7 +74,7 @@ // Tessellation is performed by rendering vertices into a data texture. These values define the // dimensions of the tessellation data texture. constexpr static size_t kTessTextureWidth = 2048; // GL_MAX_TEXTURE_SIZE spec minimum on ES3/WebGL2. -constexpr static size_t kTessTextureWidthLog2 = 31 - __builtin_clz(kTessTextureWidth); +constexpr static size_t kTessTextureWidthLog2 = 11; static_assert(1 << kTessTextureWidthLog2 == kTessTextureWidth); // Gradients are implemented by sampling a horizontal ramp of pixels allocated in a global gradient
diff --git a/include/rive/pls/pls_render_context.hpp b/include/rive/pls/pls_render_context.hpp index 5c6f767..6eb01d6 100644 --- a/include/rive/pls/pls_render_context.hpp +++ b/include/rive/pls/pls_render_context.hpp
@@ -280,12 +280,17 @@ // Indicates which "uber shader" features to enable in the draw shader. struct ShaderFeatures { - // Returns a key that uniquely identifies the current feature set. - uint64_t uniqueKey(SourceType) const; + enum PreprocessorDefines : uint64_t + { + ENABLE_ADVANCED_BLEND = 1 << 0, + ENABLE_PATH_CLIPPING = 1 << 1, + ENABLE_EVEN_ODD = 1 << 2, + ENABLE_HSL_BLEND_MODES = 1 << 3, + }; - // Issues the callback with the set of preprocessor macros that must be defined in order to - // support the current feature set. - void generatePreprocessorDefines(SourceType, const std::function<void(const char*)>&) const; + // Returns a bitmask of which preprocessor macros must be defined in order to support the + // current feature set. + uint64_t getPreprocessorDefines(SourceType) const; struct {
diff --git a/renderer/gl/pls_render_context_gl.cpp b/renderer/gl/pls_render_context_gl.cpp index 4ca6bdd..06e212c 100644 --- a/renderer/gl/pls_render_context_gl.cpp +++ b/renderer/gl/pls_render_context_gl.cpp
@@ -210,9 +210,23 @@ std::vector<const char*> defines; defines.push_back(plsImpl->shaderDefineName()); - shaderFeatures.generatePreprocessorDefines(sourceType, [&defines](const char* macro) { - defines.push_back(macro); - }); + uint64_t shaderFeatureDefines = shaderFeatures.getPreprocessorDefines(sourceType); + if (shaderFeatureDefines & ShaderFeatures::PreprocessorDefines::ENABLE_ADVANCED_BLEND) + { + defines.push_back(GLSL_ENABLE_ADVANCED_BLEND); + } + if (shaderFeatureDefines & ShaderFeatures::PreprocessorDefines::ENABLE_PATH_CLIPPING) + { + defines.push_back(GLSL_ENABLE_PATH_CLIPPING); + } + if (shaderFeatureDefines & ShaderFeatures::PreprocessorDefines::ENABLE_EVEN_ODD) + { + defines.push_back(GLSL_ENABLE_EVEN_ODD); + } + if (shaderFeatureDefines & ShaderFeatures::PreprocessorDefines::ENABLE_HSL_BLEND_MODES) + { + defines.push_back(GLSL_ENABLE_HSL_BLEND_MODES); + } std::vector<const char*> sources; sources.push_back(glsl::math); @@ -255,14 +269,14 @@ // Not every vertex shader is unique. Cache them by just the vertex features and reuse when // possible. - const DrawShader& vertexShader = - context->m_vertexShaders - .try_emplace(shaderFeatures.uniqueKey(SourceType::vertexOnly), - GL_VERTEX_SHADER, - shaderFeatures, - context->m_platformFeatures, - context->m_plsImpl.get()) - .first->second; + uint64_t vertexShaderKey = shaderFeatures.getPreprocessorDefines(SourceType::vertexOnly); + const DrawShader& vertexShader = context->m_vertexShaders + .try_emplace(vertexShaderKey, + GL_VERTEX_SHADER, + shaderFeatures, + context->m_platformFeatures, + context->m_plsImpl.get()) + .first->second; glAttachShader(m_id, vertexShader.id()); // Every fragment shader is unique. @@ -354,10 +368,9 @@ // Compile the draw program before activating pixel local storage. // Cache specific compilations of draw.glsl by ShaderFeatures. + uint64_t fragmentShaderKey = shaderFeatures.getPreprocessorDefines(SourceType::wholeProgram); const DrawProgram& drawProgram = - m_drawPrograms - .try_emplace(shaderFeatures.uniqueKey(SourceType::wholeProgram), this, shaderFeatures) - .first->second; + m_drawPrograms.try_emplace(fragmentShaderKey, this, shaderFeatures).first->second; if (frameDescriptor().wireframe) {
diff --git a/renderer/metal/pls_render_context_metal.mm b/renderer/metal/pls_render_context_metal.mm index a676275..889ee94 100644 --- a/renderer/metal/pls_render_context_metal.mm +++ b/renderer/metal/pls_render_context_metal.mm
@@ -113,29 +113,28 @@ const ShaderFeatures& shaderFeatures) { char namespaceName[] = "r0000"; - shaderFeatures.generatePreprocessorDefines(sourceType, [&namespaceName](const char* macro) { - // draw.metal uses the following bits in the namespace names for each flag: - // ENABLE_ADVANCED_BLEND: r0001 - // ENABLE_PATH_CLIPPING: r0010 - // ENABLE_EVEN_ODD: r0100 - // ENABLE_HSL_BLEND_MODES: r1000 - if (strcmp(macro, GLSL_ENABLE_ADVANCED_BLEND) == 0) - { - namespaceName[4] = '1'; - } - if (strcmp(macro, GLSL_ENABLE_PATH_CLIPPING) == 0) - { - namespaceName[3] = '1'; - } - if (strcmp(macro, GLSL_ENABLE_EVEN_ODD) == 0) - { - namespaceName[2] = '1'; - } - if (strcmp(macro, GLSL_ENABLE_HSL_BLEND_MODES) == 0) - { - namespaceName[1] = '1'; - } - }); + uint64_t shaderFeatureDefines = shaderFeatures.getPreprocessorDefines(sourceType); + // draw.metal uses the following bits in the namespace names for each flag: + // ENABLE_ADVANCED_BLEND: r0001 + // ENABLE_PATH_CLIPPING: r0010 + // ENABLE_EVEN_ODD: r0100 + // ENABLE_HSL_BLEND_MODES: r1000 + if (shaderFeatureDefines & ShaderFeatures::PreprocessorDefines::ENABLE_ADVANCED_BLEND) + { + namespaceName[4] = '1'; + } + if (shaderFeatureDefines & ShaderFeatures::PreprocessorDefines::ENABLE_PATH_CLIPPING) + { + namespaceName[3] = '1'; + } + if (shaderFeatureDefines & ShaderFeatures::PreprocessorDefines::ENABLE_EVEN_ODD) + { + namespaceName[2] = '1'; + } + if (shaderFeatureDefines & ShaderFeatures::PreprocessorDefines::ENABLE_HSL_BLEND_MODES) + { + namespaceName[1] = '1'; + } const char* mainFunctionName = sourceType == SourceType::vertexOnly ? GLSL_drawVertexMain : GLSL_drawFragmentMain; NSString* fullyQualifiedName = @@ -403,11 +402,9 @@ // Cache specific compilations of draw.glsl by ShaderFeatures. // TODO: Reuse vertex shader compilations where possible. // TODO: Precompile these shaders and only ship bytecode. + uint64_t shaderKey = shaderFeatures.getPreprocessorDefines(SourceType::wholeProgram); const DrawPipeline& drawPipeline = - m_drawPipelines - .try_emplace( - shaderFeatures.uniqueKey(SourceType::wholeProgram), this, shaderFeatures) - .first->second; + m_drawPipelines.try_emplace(shaderKey, this, shaderFeatures).first->second; [encoder setViewport:(MTLViewport){0.f, 0.f,
diff --git a/renderer/path_utils.cpp b/renderer/path_utils.cpp index 00a924b..08d39a6 100644 --- a/renderer/path_utils.cpp +++ b/renderer/path_utils.cpp
@@ -23,9 +23,9 @@ float2 p1 = simd::load2f(p + 1); float2 p2 = simd::load2f(p + 2); float2 p3 = simd::load2f(p + 3); - float2 a = p3 + 3 * (p1 - p2) - p0; - float2 b = 3 * (p2 - 2 * p1 + p0); - float2 c = 3 * (p1 - p0); + float2 a = p3 + 3.f * (p1 - p2) - p0; + float2 b = 3.f * (p2 - 2.f * p1 + p0); + float2 c = 3.f * (p1 - p0); float2 d = p0; return math::bit_cast<Vec2D>(((a * t + b) * t + c) * t + d); }
diff --git a/renderer/pls.cpp b/renderer/pls.cpp index 84b4c91..8faa2bd 100644 --- a/renderer/pls.cpp +++ b/renderer/pls.cpp
@@ -14,29 +14,14 @@ { if (wedgeType == WedgeType::centerStroke) { - vertices[vertexCount++] = {.localVertexID = static_cast<float>(i), - .outset = 1, - .fillCoverage = 0, - .vertexType = flags::kStrokeVertex}; - vertices[vertexCount++] = {.localVertexID = static_cast<float>(i), - .outset = 0, - .fillCoverage = .5f, - .vertexType = flags::kStrokeVertex}; - vertices[vertexCount++] = {.localVertexID = static_cast<float>(i), - .outset = -1, - .fillCoverage = 0, - .vertexType = flags::kStrokeVertex}; + vertices[vertexCount++] = {static_cast<float>(i), 1, 0, flags::kStrokeVertex}; + vertices[vertexCount++] = {static_cast<float>(i), 0, .5f, flags::kStrokeVertex}; + vertices[vertexCount++] = {static_cast<float>(i), -1, 0, flags::kStrokeVertex}; } else { - vertices[vertexCount++] = {.localVertexID = static_cast<float>(i), - .outset = -1, - .fillCoverage = 1, - .vertexType = flags::kStrokeVertex}; - vertices[vertexCount++] = {.localVertexID = static_cast<float>(i), - .outset = 1, - .fillCoverage = 0, - .vertexType = flags::kStrokeVertex}; + vertices[vertexCount++] = {static_cast<float>(i), -1, 1, flags::kStrokeVertex}; + vertices[vertexCount++] = {static_cast<float>(i), 1, 0, flags::kStrokeVertex}; } } @@ -44,18 +29,15 @@ size_t fanVerticesIdx = vertexCount; for (int i = 0; i <= kWedgeSize; ++i) { - vertices[vertexCount++] = {.localVertexID = static_cast<float>(i), - .outset = wedgeType == WedgeType::centerStroke ? 0.f : -1.f, - .fillCoverage = 1, - .vertexType = flags::kFanVertex}; + vertices[vertexCount++] = {static_cast<float>(i), + wedgeType == WedgeType::centerStroke ? 0.f : -1.f, + 1, + flags::kFanVertex}; } // Midpoint vertex. size_t midpointIdx = vertexCount; - vertices[vertexCount++] = {.localVertexID = 0, - .outset = 0, - .fillCoverage = 1, - .vertexType = flags::kFanMidpointVertex}; + vertices[vertexCount++] = {0, 0, 1, flags::kFanMidpointVertex}; assert(vertexCount == (wedgeType == WedgeType::centerStroke ? kCenterStrokeWedgeVertexCount : kOuterStrokeWedgeVertexCount));
diff --git a/renderer/pls_render_context.cpp b/renderer/pls_render_context.cpp index 3598d75..1c79982 100644 --- a/renderer/pls_render_context.cpp +++ b/renderer/pls_render_context.cpp
@@ -8,9 +8,6 @@ #include "pls_paint.hpp" #include "rive/math/math_types.hpp" -#include "../out/obj/generated/advanced_blend.exports.h" -#include "../out/obj/generated/draw.exports.h" - #include <string_view> // Overallocate GPU resources by 25% of current usage, in order to create padding for increase. @@ -21,44 +18,29 @@ namespace rive::pls { -uint64_t PLSRenderContext::ShaderFeatures::uniqueKey(SourceType sourceType) const +uint64_t PLSRenderContext::ShaderFeatures::getPreprocessorDefines(SourceType sourceType) const { - // The vertex shader key doesn't need to know the exact blend tier; it only cares if advanced - // blend is enabled at all or not. - uint64_t key = programFeatures.blendTier != BlendTier::srcOver; - key = (key << 1) | static_cast<uint64_t>(programFeatures.enablePathClipping); - if (sourceType != SourceType::vertexOnly) - { - assert(static_cast<uint64_t>(programFeatures.blendTier) < (1 << 2)); - key = (key << 2) | static_cast<uint64_t>(programFeatures.blendTier); - key = (key << 1) | static_cast<uint64_t>(fragmentFeatures.enableEvenOdd); - } - return key; -} - -void PLSRenderContext::ShaderFeatures::generatePreprocessorDefines( - SourceType sourceType, - const std::function<void(const char*)>& callback) const -{ + uint64_t defines = 0; if (programFeatures.blendTier != BlendTier::srcOver) { - callback(GLSL_ENABLE_ADVANCED_BLEND); + defines |= PreprocessorDefines::ENABLE_ADVANCED_BLEND; } if (programFeatures.enablePathClipping) { - callback(GLSL_ENABLE_PATH_CLIPPING); + defines |= PreprocessorDefines::ENABLE_PATH_CLIPPING; } if (sourceType != SourceType::vertexOnly) { if (fragmentFeatures.enableEvenOdd) { - callback(GLSL_ENABLE_EVEN_ODD); + defines |= PreprocessorDefines::ENABLE_EVEN_ODD; } if (programFeatures.blendTier == BlendTier::advancedHSL) { - callback(GLSL_ENABLE_HSL_BLEND_MODES); + defines |= PreprocessorDefines::ENABLE_HSL_BLEND_MODES; } } + return defines; } PLSRenderContext::BlendTier PLSRenderContext::BlendTierForBlendMode(PLSBlendMode blendMode)