Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/MetalPetal/MetalPetal
Browse files Browse the repository at this point in the history
  • Loading branch information
YuAo committed Apr 18, 2018
2 parents 7710628 + 8e07c28 commit a0002e4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Frameworks/MetalPetal/Filters/MTILensBlurFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#import "MTIFilter.h"
#import "MTIMask.h"

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -15,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface MTILensBlurFilter : NSObject <MTIFilter>

@property (nonatomic, strong, nullable) MTIImage *inputImage;
@property (nonatomic, strong, nullable) MTIImage *inputMaskImage;
@property (nonatomic, strong, nullable) MTIMask *inputMask;

@property (nonatomic) float radius;
@property (nonatomic) float brightness;
Expand Down
15 changes: 9 additions & 6 deletions Frameworks/MetalPetal/Filters/MTILensBlurFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ - (MTIImage *)outputImage {
return self.inputImage;
}

MTIImage *maskImage = self.inputMaskImage;
if (!maskImage) {
maskImage = [[MTIImage alloc] initWithColor:MTIColorMake(1, 1, 1, 1) sRGB:NO size:CGSizeMake(1, 1)];
MTIMask *mask = self.inputMask;
if (!mask) {
MTIImage *maskImage = [[MTIImage alloc] initWithColor:MTIColorMake(1, 1, 1, 1) sRGB:NO size:CGSizeMake(1, 1)];
mask = [[MTIMask alloc] initWithContent:maskImage component:MTIColorComponentRed mode:MTIMaskModeNormal];
}

MTIVector * deltas[3];
Expand All @@ -69,9 +70,11 @@ - (MTIImage *)outputImage {
}

float power = pow(10, MIN(MAX(self.brightness, -1), 1));

MTIImage *prepassOutputImage = [[MTILensBlurFilter prepassKernel] applyToInputImages:@[self.inputImage, maskImage]
parameters:@{@"power": @(power)}
BOOL usesOneMinusMaskValue = mask.mode == MTIMaskModeOneMinusMaskValue;
MTIImage *prepassOutputImage = [[MTILensBlurFilter prepassKernel] applyToInputImages:@[self.inputImage, mask.content]
parameters:@{@"power": @(power),
@"maskComponent": @((int)mask.component),
@"usesOneMinusMaskValue": @(usesOneMinusMaskValue)}
outputTextureDimensions:MTITextureDimensionsMake2DFromCGSize(self.inputImage.size)
outputPixelFormat:MTLPixelFormatRGBA16Float];
NSArray<MTIImage *> *alphaOutputs = [[MTILensBlurFilter alphaPassKernel] applyToInputImages:@[prepassOutputImage]
Expand Down
2 changes: 1 addition & 1 deletion Frameworks/MetalPetal/MTIError.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

NSError * _MTIErrorCreate(MTIError code, NSString *defaultDescription, NSDictionary *userInfo) {
if (userInfo[NSLocalizedDescriptionKey] == nil) {
NSMutableDictionary *info = [userInfo mutableCopy];
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithDictionary:userInfo];
info[NSLocalizedDescriptionKey] = defaultDescription;
return [NSError errorWithDomain:MTIErrorDomain code:code userInfo:info];
} else {
Expand Down
12 changes: 6 additions & 6 deletions Frameworks/MetalPetal/Shaders/LensBlur.metal
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ namespace metalpetal {
return blurAmount < 0.01 ? texture.sample(textureSampler, position) : float4(color / blurAmount, 1.0);
}

fragment float4 lensBlurPre(
VertexOut vertexIn [[ stage_in ]],
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) ]]
) {
float coc = maskTexture.sample(maskSampler, vertexIn.textureCoordinate).r;
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, coc);
return float4(textureColor.rgb, usesOneMinusMaskValue ? 1.0 - coc : coc);
}

typedef struct {
Expand Down

0 comments on commit a0002e4

Please sign in to comment.