Skip to content

Commit

Permalink
Added initial project for clip
Browse files Browse the repository at this point in the history
Refactored cmake files
  • Loading branch information
avu committed Apr 18, 2020
1 parent 8b00d84 commit 37f40fa
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 21 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ execute_process(COMMAND xcode-select -p
OUTPUT_VARIABLE xcode_path
OUTPUT_STRIP_TRAILING_WHITESPACE)

set(METAL /usr/bin/xcrun -sdk macosx metal -c -std=osx-metal2.0)
set(METALLIB /usr/bin/xcrun -sdk macosx metallib)
set(xcode_usr_bin ${xcode_path}/Platforms/MacOSX.platform/usr/bin)

FIND_LIBRARY(APPKIT_LIBRARY AppKit)
Expand All @@ -19,4 +21,5 @@ add_subdirectory(ex3_txtriangle)
add_subdirectory(ex4_mpr)
add_subdirectory(ex5_aarect)
add_subdirectory(ex6_mstxt)
add_subdirectory(ex7_aarect2)
add_subdirectory(ex7_aarect2)
add_subdirectory(ex8_clip)
7 changes: 3 additions & 4 deletions ex2_triangle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(triangle)

set(CMAKE_CXX_STANDARD 17)

add_executable(triangle aarect.mm tngsh.metal)
add_executable(triangle clip.mm tngsh.metal)

target_link_libraries(triangle ${APPKIT_LIBRARY})
target_link_libraries(triangle "-framework Cocoa")
Expand All @@ -14,9 +14,8 @@ target_link_libraries(triangle "-framework QuartzCore")
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/tngsh.metallib_
WORKING_DIR ${PROJECT_BINARY_DIR}
COMMAND /usr/bin/xcrun -sdk macosx metal -c -o tngsh.air
${CMAKE_SOURCE_DIR}/ex2_triangle/tngsh.metal
COMMAND /usr/bin/xcrun -sdk macosx metallib -o tngsh.metallib_ tngsh.air
COMMAND ${METAL} -o tngsh.air ${CMAKE_SOURCE_DIR}/ex2_triangle/tngsh.metal
COMMAND ${METALLIB} -o tngsh.metallib_ tngsh.air
COMMAND ${CMAKE_COMMAND} -E copy tngsh.metallib_ tngsh.metallib
MAIN_DEPENDENCY tngsh.metal
VERBATIM
Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions ex3_txtriangle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ target_link_libraries(txtriangle "-framework QuartzCore")
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/txtngsh.metallib_
WORKING_DIR ${PROJECT_BINARY_DIR}
COMMAND /usr/bin/xcrun -sdk macosx metal -c -o txtngsh.air
${CMAKE_SOURCE_DIR}/ex3_txtriangle/txtngsh.metal
COMMAND /usr/bin/xcrun -sdk macosx metallib -o txtngsh.metallib_ txtngsh.air
COMMAND ${METAL} -o txtngsh.air ${CMAKE_SOURCE_DIR}/ex3_txtriangle/txtngsh.metal
COMMAND ${METALLIB} -o txtngsh.metallib_ txtngsh.air
COMMAND ${CMAKE_COMMAND} -E copy txtngsh.metallib_ txtngsh.metallib
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/ex_res/earth.png ${PROJECT_BINARY_DIR}/earth.png
MAIN_DEPENDENCY txtngsh.metal
Expand Down
5 changes: 2 additions & 3 deletions ex4_mpr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ target_link_libraries(mpr "-framework QuartzCore")
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/mpr.metallib_
WORKING_DIR ${PROJECT_BINARY_DIR}
COMMAND /usr/bin/xcrun -sdk macosx metal -c -o mpr.air
${CMAKE_SOURCE_DIR}/ex4_mpr/mpr.metal
COMMAND /usr/bin/xcrun -sdk macosx metallib -o mpr.metallib_ mpr.air
COMMAND ${METAL} -o mpr.air ${CMAKE_SOURCE_DIR}/ex4_mpr/mpr.metal
COMMAND ${METALLIB} -o mpr.metallib_ mpr.air
COMMAND ${CMAKE_COMMAND} -E copy mpr.metallib_ mpr.metallib
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/ex_res/earth.png ${PROJECT_BINARY_DIR}/earth.png
MAIN_DEPENDENCY mpr.metal
Expand Down
5 changes: 2 additions & 3 deletions ex5_aarect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ target_link_libraries(aarect "-framework QuartzCore")
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/aarect.metallib_
WORKING_DIR ${PROJECT_BINARY_DIR}
COMMAND /usr/bin/xcrun -sdk macosx metal -c -o aarect.air
${CMAKE_SOURCE_DIR}/ex5_aarect/aarect.metal
COMMAND /usr/bin/xcrun -sdk macosx metallib -o aarect.metallib_ aarect.air
COMMAND ${METAL} -o aarect.air ${CMAKE_SOURCE_DIR}/ex5_aarect/aarect.metal
COMMAND ${METALLIB} -o aarect.metallib_ aarect.air
COMMAND ${CMAKE_COMMAND} -E copy aarect.metallib_ aarect.metallib
MAIN_DEPENDENCY aarect.metal
VERBATIM
Expand Down
2 changes: 1 addition & 1 deletion ex5_aarect/aarect.metal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <simd/simd.h>
#include <metal_stdlib>
#include "aarect.h"
#include "triangle.h"

using namespace metal;

Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions ex6_mstxt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ target_link_libraries(mstxt "-framework QuartzCore")
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/txtngsh.metallib_
WORKING_DIR ${PROJECT_BINARY_DIR}
COMMAND /usr/bin/xcrun -sdk macosx metal -c -o mstxt.air
${CMAKE_SOURCE_DIR}/ex6_mstxt/mstxt.metal
COMMAND /usr/bin/xcrun -sdk macosx metallib -o mstxt.metallib_ mstxt.air
COMMAND ${METAL} -o mstxt.air ${CMAKE_SOURCE_DIR}/ex6_mstxt/mstxt.metal
COMMAND ${METALLIB} -o mstxt.metallib_ mstxt.air
COMMAND ${CMAKE_COMMAND} -E copy mstxt.metallib_ mstxt.metallib
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/ex_res/earth.png ${PROJECT_BINARY_DIR}/earth.png
MAIN_DEPENDENCY mstxt.metal
Expand Down
5 changes: 2 additions & 3 deletions ex7_aarect2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ target_link_libraries(aarect2 "-framework QuartzCore")
add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/aarect2.metallib_
WORKING_DIR ${PROJECT_BINARY_DIR}
COMMAND /usr/bin/xcrun -sdk macosx metal -c -o aarect2.air
${CMAKE_SOURCE_DIR}/ex7_aarect2/aarect2.metal
COMMAND /usr/bin/xcrun -sdk macosx metallib -o aarect2.metallib_ aarect2.air
COMMAND ${METAL} -o aarect2.air ${CMAKE_SOURCE_DIR}/ex7_aarect2/aarect2.metal
COMMAND ${METALLIB} -o aarect2.metallib_ aarect2.air
COMMAND ${CMAKE_COMMAND} -E copy aarect2.metallib_ aarect2.metallib
MAIN_DEPENDENCY aarect2.metal
VERBATIM
Expand Down
22 changes: 22 additions & 0 deletions ex8_clip/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.13)
project(triangle)

set(CMAKE_CXX_STANDARD 17)

add_executable(clip clip.mm clip.metal)

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

add_custom_command(
OUTPUT ${PROJECT_BINARY_DIR}/clipsh.metallib_
WORKING_DIR ${PROJECT_BINARY_DIR}
COMMAND ${METAL} -o clipsh.air ${CMAKE_SOURCE_DIR}/ex8_clip/clip.metal
COMMAND ${METALLIB} -o clipsh.metallib_ clipsh.air
COMMAND ${CMAKE_COMMAND} -E copy clipsh.metallib_ clipsh.metallib
MAIN_DEPENDENCY clip.metal
VERBATIM
)
20 changes: 20 additions & 0 deletions ex8_clip/clip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef COMMON_H
#define COMMON_H

#include <simd/simd.h>

enum VertexAttributes {
VertexAttributePosition = 0,
VertexAttributeColor = 1,
};

enum BufferIndex {
MeshVertexBuffer = 0,
FrameUniformBuffer = 1,
};

struct FrameUniforms {
simd::float4x4 projectionViewModel;
};

#endif
27 changes: 27 additions & 0 deletions ex8_clip/clip.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <simd/simd.h>
#include "clip.h"

using namespace metal;

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

struct ShaderInOut {
float4 position [[position]];
half4 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;
}

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

0 comments on commit 37f40fa

Please sign in to comment.