Skip to content

Commit

Permalink
Use texture for clipping
Browse files Browse the repository at this point in the history
  • Loading branch information
avu committed May 7, 2020
1 parent c41010c commit db99069
Show file tree
Hide file tree
Showing 7 changed files with 800 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ add_subdirectory(ex5_aarect)
add_subdirectory(ex6_mstxt)
add_subdirectory(ex7_aarect2)
add_subdirectory(ex8_clip)
add_subdirectory(ex9_aaclip)
add_subdirectory(ex9_aaclip)
add_subdirectory(ex10_aaclip2)
22 changes: 22 additions & 0 deletions ex10_aaclip2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.13)
project(aaclip2)

set(CMAKE_CXX_STANDARD 17)

add_executable(aaclip2 aaclip2.mm aaclip2.metal)

target_link_libraries(aaclip2 ${APPKIT_LIBRARY})
target_link_libraries(aaclip2 "-framework Cocoa")
target_link_libraries(aaclip2 "-framework Metal")
target_link_libraries(aaclip2 "-framework MetalKit")
target_link_libraries(aaclip2 "-framework QuartzCore")

add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/aaclip2sh.metallib_
WORKING_DIR ${PROJECT_BINARY_DIR}
COMMAND ${METAL} -o aaclip2sh.air ${CMAKE_SOURCE_DIR}/ex10_aaclip2/aaclip2.metal
COMMAND ${METALLIB} -o aaclip2sh.metallib_ aaclip2sh.air
COMMAND ${CMAKE_COMMAND} -E copy aaclip2sh.metallib_ aaclip2sh.metallib
MAIN_DEPENDENCY aaclip2.metal
VERBATIM
)
File renamed without changes.
93 changes: 93 additions & 0 deletions ex10_aaclip2/aaclip2.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <simd/simd.h>
#include "aaclip2.h"

using namespace metal;

struct VertexInput {
float3 position [[attribute(VertexAttributePosition)]];
half4 color [[attribute(VertexAttributeColor)]];
};

struct TxVertexInput {
float3 position [[attribute(VertexAttributePosition)]];
half4 color [[attribute(VertexAttributeColor)]];
float2 texCoords [[attribute(VertexAttributeTexPos)]];
};

struct ShaderInOut {
float4 position [[position]];
half4 color;
};

struct TxShaderInOut {
float4 position [[position]];
half4 color;
float2 texCoords [[attribute(VertexAttributeTexPos)]];
};

struct StencilShaderInOut {
float4 position [[position]];
char color;
};

vertex ShaderInOut vert(VertexInput in [[stage_in]],
constant FrameUniforms& uniforms [[buffer(FrameUniformBuffer)]]) {
ShaderInOut out;
float4 pos4 = float4(in.position, 1.0);
out.position = uniforms.projectionViewModel * pos4;
out.color = in.color / 255.0;
return out;
}

vertex StencilShaderInOut vert_stencil(VertexInput in [[stage_in]],
constant FrameUniforms& uniforms [[buffer(FrameUniformBuffer)]]) {
StencilShaderInOut out;
float4 pos4 = float4(in.position, 1.0);
out.position = uniforms.projectionViewModel * pos4;
out.color = 0xFF;
return out;
}

fragment half4 frag(ShaderInOut in [[stage_in]]) {
return in.color;
}

fragment unsigned int frag_stencil(StencilShaderInOut in [[stage_in]]) {
return in.color;
}

vertex TxShaderInOut tx_vert(TxVertexInput in [[stage_in]],
constant FrameUniforms& uniforms [[buffer(FrameUniformBuffer)]]) {
TxShaderInOut out;
float4 pos4 = float4(in.position, 1.0);
out.position = uniforms.projectionViewModel * pos4;
out.color = in.color / 255.0;
out.texCoords = in.texCoords;
return out;
}

fragment half4 tx_frag(
TxShaderInOut vert [[stage_in]],
texture2d<float, access::sample> renderTexture [[texture(0)]],
texture2d<float, access::sample> stencilTexture [[texture(1)]]
)
{
constexpr sampler textureSampler (mag_filter::linear,
min_filter::linear);
float4 pixelColor = renderTexture.sample(textureSampler, vert.texCoords);
float4 stencil = stencilTexture.sample(textureSampler, vert.texCoords);
if (stencil.r == 0.0 && stencil.g == 0.0 && stencil.b == 0.0 && stencil.a == 0.0) {
discard_fragment();
}
return half4(pixelColor.r*pixelColor.a, pixelColor.g*pixelColor.a, pixelColor.b*pixelColor.a, 1.0);
}

kernel void stencil2tex(const device uchar *imageBuffer [[buffer(0)]],
device uchar4 *outputBuffer [[buffer(1)]],
uint gid [[thread_position_in_grid]])
{
uchar p = imageBuffer[gid];
outputBuffer[gid] = uchar4(p, p, p, p);
//outputBuffer[gid] = uchar4(255, 255, 255, 255);

}
Loading

0 comments on commit db99069

Please sign in to comment.