<commit_before>//
//  LensBlur.metal
//  MetalPetal
//
//  Created by <NAME> on 13/10/2017.
//

#include <metal_stdlib>
#include "MTIShaderLib.h"

using namespace metal;

namespace metalpetal {
    namespace lensblur {
        METAL_FUNC float randomize(float3 scale, float seed, float2 position) {
            return fract(sin(dot(float3(position, 0.0) + seed, scale)) * 43758.5453 + seed);
        }
        
        METAL_FUNC float4 sampleWithDelta(texture2d<float, access::sample> texture, sampler textureSampler, float2 position, float2 delta) {
            /* randomize the lookup values to hide the fixed number of samples */
            float offset = randomize(float3(delta, 151.7182), 0.0, position);
            constexpr int samples = 16;
            float3 color = float3(0.0);
            float blurAmount = 0;
            for (int t = 0.0; t <= samples; t++) {
                float percent = (float(t) + offset) / float(samples);
                float4 textureColor = texture.sample(textureSampler, position + delta * percent);
                blurAmount += textureColor.a;
                textureColor *= textureColor.a;
                color += textureColor.rgb;
            }
            return blurAmount < 0.01 ? texture.sample(textureSampler, position) : float4(color / blurAmount, 1.0);
        }
        
        fragment float4 lensBlurPre(VertexOut vertexIn [[ stage_in ]],
                                    texture2d<float, access::sample> colorTexture [[ texture(0) ]],
                                    sampler colorSampler [[ sampler(0) ]],
                                    texture2d<float, access::sample> maskTexture [[ texture(1) ]],
                                    sampler maskSampler [[ sampler(1) ]],
                                    constant float & power [[ buffer(0) ]],
                                    constant int &maskComponent [[ buffer(1) ]],
                                    constant bool &usesOneMinusMaskValue [[ buffer(2) ]]) {
            float coc = maskTexture.sample(maskSampler, vertexIn.textureCoordinate)[maskComponent];
            float4 textureColor = colorTexture.sample(colorSampler, vertexIn.textureCoordinate);
            textureColor.rgb = pow(textureColor.rgb, float3(power));
            return float4(textureColor.rgb, usesOneMinusMaskValue ? 1.0 - coc : coc);
        }
        
        typedef struct {
            float4 vertical [[color(0)]];
            float4 diagonal [[color(1)]];
        } LensBlurAlphaPassOutput;
        
        fragment LensBlurAlphaPassOutput lensBlurAlpha(VertexOut vertexIn [[ stage_in ]],
                                                       texture2d<float, access::sample> colorTexture [[ texture(0) ]],
                                                       sampler colorSampler [[ sampler(0) ]],
                                                       constant float2 & delta0 [[ buffer(0) ]],
                                                       constant float2 & delta1 [[ buffer(1) ]]) {
            float coc = colorTexture.sample(colorSampler, vertexIn.textureCoordinate).a;
            float4 color1 = sampleWithDelta(colorTexture, colorSampler, vertexIn.textureCoordinate, delta0 * coc);
            float4 color2 = sampleWithDelta(colorTexture, colorSampler, vertexIn.textureCoordinate, delta1 * coc);
            LensBlurAlphaPassOutput output;
            output.vertical = float4(color1.rgb, coc);
            output.diagonal = float4((color2 + color1).rgb * 0.5, coc);
            return output;
        }
        
        fragment float4 lensBlurBravoCharlie(VertexOut vertexIn [[ stage_in ]],
                                             texture2d<float, access::sample> verticalTexture [[ texture(0) ]],
                                             sampler verticalSampler [[ sampler(0) ]],
                                             texture2d<float, access::sample> diagonalTexture [[ texture(1) ]],
                                             sampler diagonalSampler [[ sampler(1) ]],
                                             constant float2 & delta0 [[ buffer(0) ]],
                                             constant float2 & delta1 [[ buffer(1) ]],<commit_msg>lens blur shader fix<commit_after>//
//  LensBlur.metal
//  MetalPetal
//
//  Created by <NAME> on 13/10/2017.
//

#include <metal_stdlib>
#include "MTIShaderLib.h"

using namespace metal;

namespace metalpetal {
    namespace lensblur {
        METAL_FUNC float randomize(float3 scale, float seed, float2 position) {
            return fract(sin(dot(float3(position, 0.0) + seed, scale)) * 43758.5453 + seed);
        }
        
        METAL_FUNC float4 sampleWithDelta(texture2d<float, access::sample> texture, sampler textureSampler, float2 position, float2 delta) {
            /* randomize the lookup values to hide the fixed number of samples */
            float offset = randomize(float3(delta, 151.7182), 0.0, position);
            constexpr int samples = 16;
            float3 color = float3(0.0);
            float blurAmount = 0;
            for (int t = 0.0; t <= samples; t++) {
                float percent = (float(t) + offset) / float(samples);
                float4 textureColor = texture.sample(textureSampler, position + delta * percent);
                blurAmount += textureColor.a;
                textureColor *= textureColor.a;
                color += textureColor.rgb;
            }
            return blurAmount < 0.01 ? texture.sample(textureSampler, position) : float4(color / blurAmount, 1.0);
        }
        
        fragment float4 lensBlurPre(VertexOut vertexIn [[ stage_in ]],
                                    texture2d<float, access::sample> colorTexture [[ texture(0) ]],
                                    sampler colorSampler [[ sampler(0) ]],
                                    texture2d<float, access::sample> maskTexture [[ texture(1) ]],
                                    sampler maskSampler [[ sampler(1) ]],
                                    constant float & power [[ buffer(0) ]],
                                    constant int &maskComponent [[ buffer(1) ]],
                                    constant bool &usesOneMinusMaskValue [[ buffer(2) ]]) {
            float coc = maskTexture.sample(maskSampler, vertexIn.textureCoordinate)[maskComponent];
            float4 textureColor = colorTexture.sample(colorSampler, vertexIn.textureCoordinate);
            textureColor.rgb = pow(textureColor.rgb, float3(power));
            return float4(textureColor.rgb, usesOneMinusMaskValue ? 1.0 - coc : coc);
        }
        
        typedef struct {
            float4 vertical [[color(0)]];
            float4 diagonal [[color(1)]];
        } LensBlurAlphaPassOutput;
        
        fragment LensBlurAlphaPassOutput lensBlurAlpha(VertexOut vertexIn [[ stage_in ]],
                                                       texture2d<float, access::sample> colorTexture [[ texture(0) ]],
                                                       sampler colorSampler [[ sampler(0) ]],
                                                       constant float2 & delta0 [[ buffer(0) ]],
                                                       constant float2 & delta1 [[ buffer(1) ]]) {
            float coc = colorTexture.sample(colorSampler, vertexIn.textureCoordinate).a;
            float4 color1 = sampleWithDelta(colorTexture, colorSampler, vertexIn.textureCoordinate, delta0 * coc);
            float4 color2 = sampleWithDelta(colorTexture, colorSampler, vertexIn.textureCoordinate, delta1 * coc);
            LensBlurAlphaPassOutput output;
            output.vertical = float4(color1.rgb, coc);
            output.diagonal = float4((color2 + color1).rgb, coc);
            return output;
        }
        
        fragment float4 lensBlurBravoCharlie(VertexOut vertexIn [[ stage_in ]],
                                             texture2d<float, access::sample> verticalTexture [[ texture(0) ]],
                                             sampler verticalSampler [[ sampler(0) ]],
                                             texture2d<float, access::sample> diagonalTexture [[ texture(1) ]],
                                             sampler diagonalSampler [[ sampler(1) ]],
                                             constant float2 & delta0 [[ buffer(0) ]],
                                             constant float2 & delta1 [[ buffer(1) ]],
{'fraction_non_alphanumeric': 0.0983295818031039, 'fraction_numerical': 0.02487856888994195, 'mean_word_length': 1.6015408320493065, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 54, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3822697', 'n_tokens_mistral': 2117, 'n_tokens_neox': 2019, 'n_words': 478}