-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
800 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
Oops, something went wrong.