Skip to content

Commit

Permalink
ReducePIX
Browse files Browse the repository at this point in the history
  • Loading branch information
heestand-xyz committed Jul 3, 2020
1 parent 2851044 commit 7322f35
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 75 deletions.
116 changes: 62 additions & 54 deletions PixelKitShaders.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
3A68D78824AF71D6007C4CC3 /* EffectSingleReducePIX.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3A68D78724AF71D6007C4CC3 /* EffectSingleReducePIX.metal */; };
3A68D78924AF71D6007C4CC3 /* EffectSingleReducePIX.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3A68D78724AF71D6007C4CC3 /* EffectSingleReducePIX.metal */; };
3A68D78A24AF71D6007C4CC3 /* EffectSingleReducePIX.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3A68D78724AF71D6007C4CC3 /* EffectSingleReducePIX.metal */; };
3AA1F93E24ABE87100C3317E /* ContentResourceImagePIX.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3AA1F8ED24ABE87100C3317E /* ContentResourceImagePIX.metal */; };
3AA1F93F24ABE87100C3317E /* ContentResourceImagePIX.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3AA1F8ED24ABE87100C3317E /* ContentResourceImagePIX.metal */; };
3AA1F94024ABE87100C3317E /* ContentResourceImagePIX.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3AA1F8ED24ABE87100C3317E /* ContentResourceImagePIX.metal */; };
Expand Down Expand Up @@ -234,16 +237,17 @@
/* Begin PBXCopyFilesBuildPhase section */
748CB48D249E442F00460A84 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 8;
buildActionMask = 12;
dstPath = /Users/anton/Code/Packages/Swift/PixelKit/Source/Metal;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
3A68D78724AF71D6007C4CC3 /* EffectSingleReducePIX.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = EffectSingleReducePIX.metal; sourceTree = "<group>"; };
3AA1F8ED24ABE87100C3317E /* ContentResourceImagePIX.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = ContentResourceImagePIX.metal; sourceTree = "<group>"; };
3AA1F8EE24ABE87100C3317E /* EffectSingleTransformPIX.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = EffectSingleTransformPIX.metal; sourceTree = "<group>"; };
3AA1F8EF24ABE87100C3317E /* NilPIX.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = NilPIX.metal; sourceTree = "<group>"; };
Expand Down Expand Up @@ -320,75 +324,76 @@
3AA1F93B24ABE87100C3317E /* ContentGeneratorRectanglePIX.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = ContentGeneratorRectanglePIX.metal; sourceTree = "<group>"; };
3AA1F93C24ABE87100C3317E /* ContentGeneratorLinePIX.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = ContentGeneratorLinePIX.metal; sourceTree = "<group>"; };
3AA1F93D24ABE87100C3317E /* EffectSingleCropPIX.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = EffectSingleCropPIX.metal; sourceTree = "<group>"; };
7473A66A210F4FD0003CF329 /* PixelKitShaders.metallib */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "archive.metal-library"; name = PixelKitShaders.metallib; path = "/Users/anton/Code/Packages/Swift/PixelKit/build/Debug-iphoneos/PixelKitShaders.metallib"; sourceTree = "<absolute>"; };
74E9F6C72193813800F541DE /* PixelKitShaders-macOS.metallib */ = {isa = PBXFileReference; explicitFileType = "archive.metal-library"; includeInIndex = 0; name = "PixelKitShaders-macOS.metallib"; path = "/Users/anton/Code/Packages/Swift/PixelKit/build/Debug/PixelKitShaders-macOS.metallib"; sourceTree = "<absolute>"; };
7473A66A210F4FD0003CF329 /* PixelKitShaders.metallib */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "archive.metal-library"; path = PixelKitShaders.metallib; sourceTree = BUILT_PRODUCTS_DIR; };
74E9F6C72193813800F541DE /* PixelKitShaders-macOS.metallib */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "archive.metal-library"; path = "PixelKitShaders-macOS.metallib"; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

/* Begin PBXGroup section */
3AA1F8EC24ABE87100C3317E /* Shaders */ = {
isa = PBXGroup;
children = (
3AA1F8ED24ABE87100C3317E /* ContentResourceImagePIX.metal */,
3AA1F8EE24ABE87100C3317E /* EffectSingleTransformPIX.metal */,
3AA1F8EF24ABE87100C3317E /* NilPIX.metal */,
3AA1F8F024ABE87100C3317E /* EffectMergerReorderPIX.metal */,
3AA1F92D24ABE87100C3317E /* ContentGeneratorArcPIX.metal */,
3AA1F93A24ABE87100C3317E /* ContentGeneratorCirclePIX.metal */,
3AA1F90C24ABE87100C3317E /* ContentGeneratorColorPIX.metal */,
3AA1F90024ABE87100C3317E /* ContentGeneratorGradientPIX.metal */,
3AA1F93C24ABE87100C3317E /* ContentGeneratorLinePIX.metal */,
3AA1F8F124ABE87100C3317E /* ContentGeneratorNoisePIX.metal */,
3AA1F8F224ABE87100C3317E /* EffectSingleSepiaPIX.metal */,
3AA1F8F324ABE87100C3317E /* EffectSingleAveragePIX.metal */,
3AA1F8F424ABE87100C3317E /* EffectMergerLumaBlurPIX.metal */,
3AA1F8F524ABE87100C3317E /* ContentGeneratorPolygonPIX.metal */,
3AA1F8F624ABE87100C3317E /* EffectSingleThresholdPIX.metal */,
3AA1F8F724ABE87100C3317E /* EffectMultiBlendsPIX.metal */,
3AA1F8F824ABE87100C3317E /* EffectSingleFlipFlopPIX.metal */,
3AA1F8F924ABE87100C3317E /* EffectSingleSlopePIX.metal */,
3AA1F8FA24ABE87100C3317E /* EffectSingleKaleidoscopePIX.metal */,
3AA1F93B24ABE87100C3317E /* ContentGeneratorRectanglePIX.metal */,
3AA1F91024ABE87100C3317E /* ContentResourceBackgroundPIX.metal */,
3AA1F93124ABE87100C3317E /* ContentResourceBGRPIX.metal */,
3AA1F93024ABE87100C3317E /* ContentResourceCameraPIX.metal */,
3AA1F91124ABE87100C3317E /* ContentResourceFlipPIX.metal */,
3AA1F8ED24ABE87100C3317E /* ContentResourceImagePIX.metal */,
3AA1F92E24ABE87100C3317E /* ContentResourcePIX.metal */,
3AA1F93724ABE87100C3317E /* ContentResourceRedToWhitePIX.metal */,
3AA1F91524ABE87100C3317E /* EffectMergerBlendPIX.metal */,
3AA1F8FB24ABE87100C3317E /* EffectMergerCrossPIX.metal */,
3AA1F8FC24ABE87100C3317E /* EffectSingleFlarePIX.metal */,
3AA1F8FD24ABE87100C3317E /* EffectSingleChromaKeyPIX.metal */,
3AA1F8FE24ABE87100C3317E /* EffectSingleEdgePIX.metal */,
3AA1F8FF24ABE87100C3317E /* EffectMergerDisplacePIX.metal */,
3AA1F90024ABE87100C3317E /* ContentGeneratorGradientPIX.metal */,
3AA1F90124ABE87100C3317E /* SpritePIX.metal */,
3AA1F90224ABE87100C3317E /* EffectSingleSharpenPIX.metal */,
3AA1F90324ABE87100C3317E /* EffectMergerTimeMachinePIX.metal */,
3AA1F90B24ABE87100C3317E /* EffectMergerLookupPIX.metal */,
3AA1F8F424ABE87100C3317E /* EffectMergerLumaBlurPIX.metal */,
3AA1F93924ABE87100C3317E /* EffectMergerLumaColorShiftPIX.metal */,
3AA1F92F24ABE87100C3317E /* EffectMergerLumaLevelsPIX.metal */,
3AA1F93624ABE87100C3317E /* EffectMergerLumaRainbowBlurPIX.metal */,
3AA1F90F24ABE87100C3317E /* EffectMergerLumaTransformPIX.metal */,
3AA1F90424ABE87100C3317E /* EffectMergerLUTPIX.metal */,
3AA1F91624ABE87100C3317E /* EffectMergerRemapPIX.metal */,
3AA1F8F024ABE87100C3317E /* EffectMergerReorderPIX.metal */,
3AA1F90324ABE87100C3317E /* EffectMergerTimeMachinePIX.metal */,
3AA1F91424ABE87100C3317E /* EffectMultiArrayPIX.metal */,
3AA1F8F724ABE87100C3317E /* EffectMultiBlendsPIX.metal */,
3AA1F93224ABE87100C3317E /* EffectMultiHDRPIX.metal */,
3AA1F90524ABE87100C3317E /* EffectMultiStackPIX.metal */,
3AA1F90624ABE87100C3317E /* EffectSingleTwirlPIX.metal */,
3AA1F90724ABE87100C3317E /* EffectSingleConvertPIX.metal */,
3AA1F90824ABE87100C3317E /* TemplatePIX.metal */,
3AA1F8F324ABE87100C3317E /* EffectSingleAveragePIX.metal */,
3AA1F90924ABE87100C3317E /* EffectSingleBlurPIX.metal */,
3AA1F90A24ABE87100C3317E /* EffectSingleQuantizePIX.metal */,
3AA1F90B24ABE87100C3317E /* EffectMergerLookupPIX.metal */,
3AA1F90C24ABE87100C3317E /* ContentGeneratorColorPIX.metal */,
3AA1F90D24ABE87100C3317E /* EffectSingleLevelsPIX.metal */,
3AA1F90E24ABE87100C3317E /* EffectSingleClampPIX.metal */,
3AA1F90F24ABE87100C3317E /* EffectMergerLumaTransformPIX.metal */,
3AA1F91024ABE87100C3317E /* ContentResourceBackgroundPIX.metal */,
3AA1F91124ABE87100C3317E /* ContentResourceFlipPIX.metal */,
3AA1F91224ABE87100C3317E /* EffectSingleChannelMixPIX.metal */,
3AA1F8FD24ABE87100C3317E /* EffectSingleChromaKeyPIX.metal */,
3AA1F90E24ABE87100C3317E /* EffectSingleClampPIX.metal */,
3AA1F93824ABE87100C3317E /* EffectSingleColorConvertPIX.metal */,
3AA1F91324ABE87100C3317E /* EffectSingleColorShiftPIX.metal */,
3AA1F91424ABE87100C3317E /* EffectMultiArrayPIX.metal */,
3AA1F91524ABE87100C3317E /* EffectMergerBlendPIX.metal */,
3AA1F91624ABE87100C3317E /* EffectMergerRemapPIX.metal */,
3AA1F91724ABE87100C3317E /* Shaders */,
3AA1F90724ABE87100C3317E /* EffectSingleConvertPIX.metal */,
3AA1F93D24ABE87100C3317E /* EffectSingleCropPIX.metal */,
3AA1F8FE24ABE87100C3317E /* EffectSingleEdgePIX.metal */,
3AA1F8FC24ABE87100C3317E /* EffectSingleFlarePIX.metal */,
3AA1F8F824ABE87100C3317E /* EffectSingleFlipFlopPIX.metal */,
3AA1F8FA24ABE87100C3317E /* EffectSingleKaleidoscopePIX.metal */,
3AA1F90D24ABE87100C3317E /* EffectSingleLevelsPIX.metal */,
3AA1F90A24ABE87100C3317E /* EffectSingleQuantizePIX.metal */,
3AA1F93524ABE87100C3317E /* EffectSingleRainbowBlurPIX.metal */,
3AA1F92C24ABE87100C3317E /* EffectSingleRangePIX.metal */,
3AA1F92D24ABE87100C3317E /* ContentGeneratorArcPIX.metal */,
3AA1F92E24ABE87100C3317E /* ContentResourcePIX.metal */,
3AA1F92F24ABE87100C3317E /* EffectMergerLumaLevelsPIX.metal */,
3AA1F93024ABE87100C3317E /* ContentResourceCameraPIX.metal */,
3AA1F93124ABE87100C3317E /* ContentResourceBGRPIX.metal */,
3AA1F93224ABE87100C3317E /* EffectMultiHDRPIX.metal */,
3AA1F93324ABE87100C3317E /* EffectSingleSlicePIX.metal */,
3A68D78724AF71D6007C4CC3 /* EffectSingleReducePIX.metal */,
3AA1F93424ABE87100C3317E /* EffectSingleResPIX.metal */,
3AA1F93524ABE87100C3317E /* EffectSingleRainbowBlurPIX.metal */,
3AA1F93624ABE87100C3317E /* EffectMergerLumaRainbowBlurPIX.metal */,
3AA1F93724ABE87100C3317E /* ContentResourceRedToWhitePIX.metal */,
3AA1F93824ABE87100C3317E /* EffectSingleColorConvertPIX.metal */,
3AA1F93924ABE87100C3317E /* EffectMergerLumaColorShiftPIX.metal */,
3AA1F93A24ABE87100C3317E /* ContentGeneratorCirclePIX.metal */,
3AA1F93B24ABE87100C3317E /* ContentGeneratorRectanglePIX.metal */,
3AA1F93C24ABE87100C3317E /* ContentGeneratorLinePIX.metal */,
3AA1F93D24ABE87100C3317E /* EffectSingleCropPIX.metal */,
3AA1F8F224ABE87100C3317E /* EffectSingleSepiaPIX.metal */,
3AA1F90224ABE87100C3317E /* EffectSingleSharpenPIX.metal */,
3AA1F93324ABE87100C3317E /* EffectSingleSlicePIX.metal */,
3AA1F8F924ABE87100C3317E /* EffectSingleSlopePIX.metal */,
3AA1F8F624ABE87100C3317E /* EffectSingleThresholdPIX.metal */,
3AA1F8EE24ABE87100C3317E /* EffectSingleTransformPIX.metal */,
3AA1F90624ABE87100C3317E /* EffectSingleTwirlPIX.metal */,
3AA1F8EF24ABE87100C3317E /* NilPIX.metal */,
3AA1F91724ABE87100C3317E /* Shaders */,
3AA1F90124ABE87100C3317E /* SpritePIX.metal */,
3AA1F90824ABE87100C3317E /* TemplatePIX.metal */,
);
path = Shaders;
sourceTree = "<group>";
Expand Down Expand Up @@ -575,6 +580,7 @@
3AA1F96D24ABE87100C3317E /* EffectSingleFlarePIX.metal in Sources */,
3AA1FA1B24ABE87200C3317E /* EffectSingleCropPIX.metal in Sources */,
3AA1F99124ABE87100C3317E /* TemplatePIX.metal in Sources */,
3A68D78A24AF71D6007C4CC3 /* EffectSingleReducePIX.metal in Sources */,
3AA1F99A24ABE87100C3317E /* EffectMergerLookupPIX.metal in Sources */,
3AA1F9F424ABE87200C3317E /* ContentResourceCameraPIX.metal in Sources */,
3AA1F9D624ABE87200C3317E /* blend_header.metal in Sources */,
Expand Down Expand Up @@ -656,6 +662,7 @@
3AA1F96B24ABE87100C3317E /* EffectSingleFlarePIX.metal in Sources */,
3AA1FA1924ABE87200C3317E /* EffectSingleCropPIX.metal in Sources */,
3AA1F98F24ABE87100C3317E /* TemplatePIX.metal in Sources */,
3A68D78824AF71D6007C4CC3 /* EffectSingleReducePIX.metal in Sources */,
3AA1F99824ABE87100C3317E /* EffectMergerLookupPIX.metal in Sources */,
3AA1F9F224ABE87200C3317E /* ContentResourceCameraPIX.metal in Sources */,
3AA1F9D424ABE87200C3317E /* blend_header.metal in Sources */,
Expand Down Expand Up @@ -737,6 +744,7 @@
3AA1F96C24ABE87100C3317E /* EffectSingleFlarePIX.metal in Sources */,
3AA1FA1A24ABE87200C3317E /* EffectSingleCropPIX.metal in Sources */,
3AA1F99024ABE87100C3317E /* TemplatePIX.metal in Sources */,
3A68D78924AF71D6007C4CC3 /* EffectSingleReducePIX.metal in Sources */,
3AA1F99924ABE87100C3317E /* EffectMergerLookupPIX.metal in Sources */,
3AA1F9F324ABE87200C3317E /* ContentResourceCameraPIX.metal in Sources */,
3AA1F9D524ABE87200C3317E /* blend_header.metal in Sources */,
Expand Down
38 changes: 38 additions & 0 deletions Shaders/EffectSingleReducePIX.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// EffectSingleReducePIX.metal
// VoxelKitShaders
//
// Created by Anton Heestand on 2020-07-03.
// Copyright © 2019 Hexagons. All rights reserved.
//

#include <metal_stdlib>
using namespace metal;

struct VertexOut {
float4 position [[position]];
float2 texCoord;
};

struct Uniforms {
float isAvg;
float count;
};

fragment float4 effectSingleReducePIX(VertexOut out [[stage_in]],
const device Uniforms& in [[ buffer(0) ]],
texture2d<float> inTex [[ texture(0) ]],
sampler s [[ sampler(0) ]]) {

float u = out.texCoord[0];
float v = out.texCoord[1];
float2 uv = float2(u, v);

float4 c = inTex.sample(s, uv);
if (in.isAvg) {
c /= float(in.count);
}

return c;
}

Binary file modified Source/Metal/PixelKitShaders-macOS.metallib
Binary file not shown.
91 changes: 79 additions & 12 deletions Source/PIX/PIXs/Effects/Single/ReducePIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,70 @@ import MetalPerformanceShaders
@available(tvOS 11.3, *)
@available(iOS 11.3, *)
@available(OSX 10.13.4, *)
class ReducePIX: PIXSingleEffect, PIXAuto, CustomRenderDelegate {
public class ReducePIX: PIXSingleEffect, PIXAuto, CustomRenderDelegate {

override open var shaderName: String { return "effectSingleReducePIX" }

override var customResolution: Resolution? {
guard let inputResolution: Resolution = (input as! PIX?)?.realResolution else { return nil }
return getCustomResolution(from: inputResolution)
}

// MARK: - Public Properties

public enum Axis {
case row
case column
case horizontal
case vertical
}

public var axis: Axis = .row { didSet { applyResolution { self.setNeedsRender() } } }
/// final output axis of pixels
///
/// to get one pixel row, use `.horizontal` *(default)*
public var axis: Axis = .horizontal { didSet { applyResolution { self.setNeedsRender() } } }

public enum Method {
// case avg
case mean
case min
case max
case sum
/// enable 16 bit fore better accuracy: `PixelKit.main.render.bits = ._16`
case average
}

public var method: Method = .mean { didSet { setNeedsRender() } }
public var method: Method = .mean {
didSet {
setNeedsRender()
if method == .average && pixelKit.render.bits == ._8 {
pixelKit.logger.log(.info, .render, "Reduce with .average is better in 16 bit mode. `PixelKit.main.render.bits = ._16`")
}
}
}

// public override var overrideBits: LiveColor.Bits? { method == .average ? ._16 : nil }

public required init() {
super.init(name: "Reduce", typeName: "pix-effect-single-reduce")
customRenderActive = true
customRenderDelegate = self
}

// MARK: - Property Helpers

var resolutionCount: Int {
guard let inputResolution: Resolution = (input as! PIX?)?.realResolution else { return 1 }
return getResolutionCount(from: inputResolution)
}

public override var uniforms: [CGFloat] {
[
method == .average ? 1.0 : 0.0,
CGFloat(resolutionCount),
]
}

// MARK: - Custom Render

func customRender(_ texture: MTLTexture, with commandBuffer: MTLCommandBuffer) -> MTLTexture? {
public func customRender(_ texture: MTLTexture, with commandBuffer: MTLCommandBuffer) -> MTLTexture? {
let resolution: Resolution = getCustomResolution(from: texture.resolution)
guard let destinationTexture: MTLTexture = try? Texture.emptyTexture(size: resolution.size.cg,
bits: pixelKit.render.bits,
Expand All @@ -55,7 +84,7 @@ class ReducePIX: PIXSingleEffect, PIXAuto, CustomRenderDelegate {
pixelKit.logger.log(node: self, .error, .generator, "Guassian Blur: Make texture faild.")
return nil
}
let reduceKernel = MPSImageReduceRowMean(device: pixelKit.render.metalDevice)
let reduceKernel: MPSImageReduceUnary = getKernel(with: pixelKit.render.metalDevice)
#if !os(tvOS)
reduceKernel.edgeMode = extend.mps!
#endif
Expand All @@ -65,15 +94,53 @@ class ReducePIX: PIXSingleEffect, PIXAuto, CustomRenderDelegate {
return destinationTexture
}

// MARK: - Custom Resolution
// MARK: - Resolution

func getCustomResolution(from resolution: Resolution) -> Resolution {
switch axis {
case .row:
case .horizontal:
return .custom(w: resolution.w, h: 1)
case .column:
case .vertical:
return .custom(w: 1, h: resolution.h)
}
}

func getResolutionCount(from resolution: Resolution) -> Int {
switch axis {
case .horizontal:
return resolution.h
case .vertical:
return resolution.w
}
}

// MARK: - Kernel

func getKernel(with device: MTLDevice) -> MPSImageReduceUnary {
switch axis {
case .horizontal:
switch method {
case .mean:
return MPSImageReduceColumnMean(device: device)
case .min:
return MPSImageReduceColumnMin(device: device)
case .max:
return MPSImageReduceColumnMax(device: device)
case .sum, .average:
return MPSImageReduceColumnSum(device: device)
}
case .vertical:
switch method {
case .mean:
return MPSImageReduceRowMean(device: device)
case .min:
return MPSImageReduceRowMin(device: device)
case .max:
return MPSImageReduceRowMax(device: device)
case .sum, .average:
return MPSImageReduceRowSum(device: device)
}
}
}

}
Loading

0 comments on commit 7322f35

Please sign in to comment.