openglonmetal / MGL

OpenGL 4.6 on Metal
Apache License 2.0
769 stars 30 forks source link

Progress on CMakeLists build script #40

Open dokipen3d opened 2 years ago

dokipen3d commented 2 years ago

Hiya

I just discovered this project and am very excited as I had been waiting for mesa/zink to cover this area and now we have something concrete.

I've been having a go at putting together a cmake file to be able to compile (currently on my macbook pro m1).

I've managed to get it to compile and the easiest thing it seemed to do was to use the existing dependencies that come with the vulkan sdk that are already built.

Here is the CMakeLists.txt below. So do the usual

mkdir build cd build cmake .. make

I have llvm 14 installed through brew which is being picked up.

The only issue is that the test program fails with

./test_mgl_app 
creating window...
Cannot set swap interval without a current OpenGL or OpenGL ES context
setup complete. testing...
error: SPIRV-Tools Validation Errors
error: Invalid SPIR-V binary version 1.5 for target environment SPIR-V 1.0 (under OpenGL 4.5 semantics).

GL Error func: linkAndCompileProgramToMetal type: 1282
Assertion failed: (0), function error_func, file error.c, line 50.
zsh: abort      ./test_mgl_app

It's late right now, but I'm going to see if I can find out more tomorrow. I'm using glfw from homebrew as well so I'm not sure if the custom one is still needed (the makefile suggested otherwise). If there is definitely something custom I need to build then please let me know. Or I might have missed something from the make file when creating the cmake file.

I look forward to getting this working and thank you so much for creating this project and sharing it :)

project(mgl)
cmake_minimum_required(VERSION 3.18)
set(CMAKE_OSX_ARCHITECTURES "arm64")
#x86_64
#arm64
#set(SRC_FILES
#    MGL/src/*.c
#    MGL/src/*.m
#)

FILE(GLOB MyCSources MGL/src/*.c)
FILE(GLOB MyMSources MGL/src/*.m)

add_library(mgl ${MyCSources} ${MyMSources})
target_include_directories(mgl PUBLIC MGL/include MGL/include/GL)

target_compile_options(mgl PRIVATE -x objective-c)
#target_compile_definitions(mgl PUBLIC GLSLANG_SHADER_VULKAN_RULES_RELAXED=1)
target_compile_definitions(mgl PUBLIC 
                                ENABLE_OPT=0 
                                SPIRV_CROSS_C_API_MSL=1
                                SPIRV_CROSS_C_API_GLSL=1
                                SPIRV_CROSS_C_API_CPP=1
                                SPIRV_CROSS_C_API_REFLECT=1
                                )

#set_target_properties(mgl PROPERTIES COMPILE_FLAGS "-x objective-c")
#set_target_properties(mgl PROPERTIES COMPILE_FLAGS "-x objective-c++")

#set(CMAKE_C_FLAGS "-x objective-c")
#set(CMAKE_CXX_FLAGS "-x objective-c++")
set(CMAKE_EXE_LINKER_FLAGS "-framework Quartz -framework Quartzcore -framework IOKit -framework ApplicationServices -framework OpenGL -framework MetalKit -framework Metal -framework CoreGraphics -framework Cocoa  -framework AppKit -framework CoreData -framework Foundation")

#target_compile_options(mgl PUBLIC "-x objective-c" "-x objective-c++")
set_property(TARGET mgl PROPERTY CXX_STANDARD 17)

find_package(glm REQUIRED)
find_package(glfw3 CONFIG REQUIRED)
find_library(GLSLANG glslang $ENV{VULKAN_SDK}/lib)
find_library(SPIRVCROSS spirv-cross-core $ENV{VULKAN_SDK}/lib)
find_library(SPIRVCROSSC spirv-cross-c $ENV{VULKAN_SDK}/lib)
find_library(SPIRVCROSSCPP spirv-cross-cpp $ENV{VULKAN_SDK}/lib)
find_library(SPIRVCROSSMSL spirv-cross-msl $ENV{VULKAN_SDK}/lib)
find_library(SPIRVCROSSGLSL spirv-cross-glsl $ENV{VULKAN_SDK}/lib)
find_library(SPIRVCROSSHLSL spirv-cross-hlsl $ENV{VULKAN_SDK}/lib)
find_library(SPIRVCROSSREFLECT spirv-cross-reflect $ENV{VULKAN_SDK}/lib)
find_library(MACHINEINDEPENDENT MachineIndependent $ENV{VULKAN_SDK}/lib)
find_library(GENERICCODEGEN GenericCodeGen $ENV{VULKAN_SDK}/lib)
find_library(OGLCOMPILER OGLCompiler $ENV{VULKAN_SDK}/lib)
find_library(OSDEPENDENT OSDependent $ENV{VULKAN_SDK}/lib)
find_library(GLSLANGDEFAULT glslang-default-resource-limits $ENV{VULKAN_SDK}/lib)
find_library(SPIRVLIB SPIRV $ENV{VULKAN_SDK}/lib)
find_library(SPIRVTOOLS SPIRV-Tools $ENV{VULKAN_SDK}/lib)
find_library(SPIRVTOOLSOPT SPIRV-Tools-opt $ENV{VULKAN_SDK}/lib)

target_include_directories(${PROJECT_NAME} PUBLIC  
                            $ENV{VULKAN_SDK}/include/spirv_cross
                            $ENV{VULKAN_SDK}/include/glslang/Include
                            $ENV{VULKAN_SDK}/include
                            $ENV{VULKAN_SDK}/include/spirv-tools)

MESSAGE(STATUS "Found glslang in: ${GLSLANG}")
MESSAGE(STATUS "Found spirv-cross-core in: ${SPIRVCROSS}")
MESSAGE(STATUS "Found spirv-cross-c in: ${SPIRVCROSSC}")
MESSAGE(STATUS "Found spirv-cross-cpp in: ${SPIRVCROSSCPP}")
MESSAGE(STATUS "Found spirv-cross-msl in: ${SPIRVCROSSMSL}")
MESSAGE(STATUS "Found spirv-cross-glsl in: ${SPIRVCROSSGLSL}")
MESSAGE(STATUS "Found spirv-cross-hlsl in: ${SPIRVCROSSHLSL}")
MESSAGE(STATUS "Found spirv-cross-reflect in: ${SPIRVCROSSREFLECT}")
MESSAGE(STATUS "Found machine independent in: ${MACHINEINDEPENDENT}")
MESSAGE(STATUS "Found generic in: ${GENERICCODEGEN}")
MESSAGE(STATUS "Found ogl compiiler in: ${OGLCOMPILER}")
MESSAGE(STATUS "Found os dep in: ${OSDEPENDENT}")
MESSAGE(STATUS "Found glslang def in: ${GLSLANGDEFAULT}")
MESSAGE(STATUS "Found spirv in: ${SPIRVLIB}")
MESSAGE(STATUS "Found spirvtools in: ${SPIRVTOOLS}")
MESSAGE(STATUS "Found spirvtoolsopt in: ${SPIRVTOOLSOPT}")

#if (VULKAN_FOUND)
#    message(STATUS "Found Vulkan, Including and Linking now")
#    include_directories(${Vulkan_INCLUDE_DIRS})
#    target_link_libraries (${PROJECT_NAME} ${Vulkan_LIBRARIES} glfw ${SHADERC_LIB})
#endif (VULKAN_FOUND)

target_link_libraries(${PROJECT_NAME} 
                        ${SPIRVCROSS}
                        ${SPIRVCROSSC}
                        ${SPIRVCROSSCPP}
                        ${SPIRVCROSSMSL}
                        ${SPIRVCROSSGLSL}
                        ${SPIRVCROSSHLSL}
                        ${SPIRVCROSSREFLECT}
                        ${GLSLANG}
                        ${MACHINEINDEPENDENT}
                        ${GENERICCODEGEN}
                        ${OGLCOMPILER}
                        ${OSDEPENDENT}
                        ${GLSLANGDEFAULT}
                        ${SPIRVLIB}
                        ${SPIRVTOOLSOPT}
                        glfw
                        )

#test

add_executable(test_mgl_app test_mgl/main.cpp)
target_compile_options(test_mgl_app PRIVATE -x objective-c++)
target_compile_definitions(test_mgl_app PUBLIC 
                                DTEST_MGL_GLFW
                                ENABLE_OPT=0 
                                SPIRV_CROSS_C_API_MSL=1
                                SPIRV_CROSS_C_API_GLSL=1
                                SPIRV_CROSS_C_API_CPP=1
                                SPIRV_CROSS_C_API_REFLECT=1)

set_property(TARGET test_mgl_app PROPERTY CXX_STANDARD 17)
target_include_directories(test_mgl_app PUBLIC MGL/include)
#target_link_libraries(test_mgl_app glfw mgl)
target_link_libraries(test_mgl_app
                        PUBLIC
                        mgl
                        ${SPIRVLIB}
                        ${SPIRVCROSS}
                        ${SPIRVCROSSC}
                        ${SPIRVCROSSCPP}
                        ${SPIRVCROSSMSL}
                        ${SPIRVCROSSGLSL}
                        ${SPIRVCROSSHLSL}
                        ${SPIRVCROSSREFLECT}
                        ${GLSLANG}
                        ${MACHINEINDEPENDENT}
                        ${GENERICCODEGEN}
                        ${OGLCOMPILER}
                        ${OSDEPENDENT}
                        ${GLSLANGDEFAULT}
                        ${SPIRVTOOLS}
                        ${SPIRVTOOLSOPT}
                        glfw
                        )
r58Playz commented 2 years ago

I do have a working build (it does use xcode though) over at my repo. You probably need an exact commit of spirv. Also, MGL includes a patched GLFW so use that instead of the system glfw.

darkaegisagain commented 2 years ago

Hi,

Yes you have to use the GLFW provided… normally GLFW tried to load the default OpenGL framework.

I am on the water on the boat taking some time off, I will be home Wednesday if you have any additional problems.

Cheers

Mike

On Jul 25, 2022, at 4:58 AM, Daniel Elliott @.***> wrote:

Hiya

I just discovered this project and am very excited as I had been waiting for mesa/zink to cover this area and now we have something concrete.

I've been having a go at putting together a cmake file to be able to compile (currently on my macbook pro m1).

I've managed to get it to compile and the easiest thing it seemed to do was to use the existing dependencies that come with the vulkan sdk that are already built.

Here is the CMakeLists.txt below. So do the usual

mkdir build cd build cmake .. make

I have llvm 14 installed through brew which is being picked up.

The only issue is that the test program fails with

`./test_mgl_app creating window... Cannot set swap interval without a current OpenGL or OpenGL ES context setup complete. testing... error: SPIRV-Tools Validation Errors error: Invalid SPIR-V binary version 1.5 for target environment SPIR-V 1.0 (under OpenGL 4.5 semantics).

GL Error func: linkAndCompileProgramToMetal type: 1282 Assertion failed: (0), function error_func, file error.c, line 50. zsh: abort ./test_mgl_app`

It's late right now, but I'm going to see if I can find out more tomorrow. I'm using glfw from homebrew as well so I'm not sure if the custom one is still needed (the makefile suggested otherwise). If there is definitely something custom I need to build then please let me know. Or I might have missed something from the make file when creating the cmake file.

I look forward to getting this working and thank you so much for creating this project and sharing it :)

`project(mgl) cmake_minimum_required(VERSION 3.18) set(CMAKE_OSX_ARCHITECTURES "arm64")

x86_64

arm64

set(SRC_FILES

MGL/src/*.c

MGL/src/*.m

)

FILE(GLOB MyCSources MGL/src/.c) FILE(GLOB MyMSources MGL/src/.m)

add_library(mgl ${MyCSources} ${MyMSources}) target_include_directories(mgl PUBLIC MGL/include MGL/include/GL)

target_compile_options(mgl PRIVATE -x objective-c)

target_compile_definitions(mgl PUBLIC GLSLANG_SHADER_VULKAN_RULES_RELAXED=1)

target_compile_definitions(mgl PUBLIC ENABLE_OPT=0 SPIRV_CROSS_C_API_MSL=1 SPIRV_CROSS_C_API_GLSL=1 SPIRV_CROSS_C_API_CPP=1 SPIRV_CROSS_C_API_REFLECT=1 )

set_target_properties(mgl PROPERTIES COMPILE_FLAGS "-x objective-c")

set_target_properties(mgl PROPERTIES COMPILE_FLAGS "-x objective-c++")

set(CMAKE_C_FLAGS "-x objective-c")

set(CMAKE_CXX_FLAGS "-x objective-c++")

set(CMAKE_EXE_LINKER_FLAGS "-framework Quartz -framework Quartzcore -framework IOKit -framework ApplicationServices -framework OpenGL -framework MetalKit -framework Metal -framework CoreGraphics -framework Cocoa -framework AppKit -framework CoreData -framework Foundation")

target_compile_options(mgl PUBLIC "-x objective-c" "-x objective-c++")

set_property(TARGET mgl PROPERTY CXX_STANDARD 17)

find_package(glm REQUIRED) find_package(glfw3 CONFIG REQUIRED) find_library(GLSLANG glslang $ENV{VULKAN_SDK}/lib) find_library(SPIRVCROSS spirv-cross-core $ENV{VULKAN_SDK}/lib) find_library(SPIRVCROSSC spirv-cross-c $ENV{VULKAN_SDK}/lib) find_library(SPIRVCROSSCPP spirv-cross-cpp $ENV{VULKAN_SDK}/lib) find_library(SPIRVCROSSMSL spirv-cross-msl $ENV{VULKAN_SDK}/lib) find_library(SPIRVCROSSGLSL spirv-cross-glsl $ENV{VULKAN_SDK}/lib) find_library(SPIRVCROSSHLSL spirv-cross-hlsl $ENV{VULKAN_SDK}/lib) find_library(SPIRVCROSSREFLECT spirv-cross-reflect $ENV{VULKAN_SDK}/lib) find_library(MACHINEINDEPENDENT MachineIndependent $ENV{VULKAN_SDK}/lib) find_library(GENERICCODEGEN GenericCodeGen $ENV{VULKAN_SDK}/lib) find_library(OGLCOMPILER OGLCompiler $ENV{VULKAN_SDK}/lib) find_library(OSDEPENDENT OSDependent $ENV{VULKAN_SDK}/lib) find_library(GLSLANGDEFAULT glslang-default-resource-limits $ENV{VULKAN_SDK}/lib) find_library(SPIRVLIB SPIRV $ENV{VULKAN_SDK}/lib) find_library(SPIRVTOOLS SPIRV-Tools $ENV{VULKAN_SDK}/lib) find_library(SPIRVTOOLSOPT SPIRV-Tools-opt $ENV{VULKAN_SDK}/lib)

target_include_directories(${PROJECT_NAME} PUBLIC $ENV{VULKAN_SDK}/include/spirv_cross $ENV{VULKAN_SDK}/include/glslang/Include $ENV{VULKAN_SDK}/include $ENV{VULKAN_SDK}/include/spirv-tools)

MESSAGE(STATUS "Found glslang in: ${GLSLANG}") MESSAGE(STATUS "Found spirv-cross-core in: ${SPIRVCROSS}") MESSAGE(STATUS "Found spirv-cross-c in: ${SPIRVCROSSC}") MESSAGE(STATUS "Found spirv-cross-cpp in: ${SPIRVCROSSCPP}") MESSAGE(STATUS "Found spirv-cross-msl in: ${SPIRVCROSSMSL}") MESSAGE(STATUS "Found spirv-cross-glsl in: ${SPIRVCROSSGLSL}") MESSAGE(STATUS "Found spirv-cross-hlsl in: ${SPIRVCROSSHLSL}") MESSAGE(STATUS "Found spirv-cross-reflect in: ${SPIRVCROSSREFLECT}") MESSAGE(STATUS "Found machine independent in: ${MACHINEINDEPENDENT}") MESSAGE(STATUS "Found generic in: ${GENERICCODEGEN}") MESSAGE(STATUS "Found ogl compiiler in: ${OGLCOMPILER}") MESSAGE(STATUS "Found os dep in: ${OSDEPENDENT}") MESSAGE(STATUS "Found glslang def in: ${GLSLANGDEFAULT}") MESSAGE(STATUS "Found spirv in: ${SPIRVLIB}") MESSAGE(STATUS "Found spirvtools in: ${SPIRVTOOLS}") MESSAGE(STATUS "Found spirvtoolsopt in: ${SPIRVTOOLSOPT}")

if (VULKAN_FOUND)

message(STATUS "Found Vulkan, Including and Linking now")

include_directories(${Vulkan_INCLUDE_DIRS})

target_link_libraries (${PROJECT_NAME} ${Vulkan_LIBRARIES} glfw ${SHADERC_LIB})

endif (VULKAN_FOUND)

target_link_libraries(${PROJECT_NAME} ${SPIRVCROSS} ${SPIRVCROSSC} ${SPIRVCROSSCPP} ${SPIRVCROSSMSL} ${SPIRVCROSSGLSL} ${SPIRVCROSSHLSL} ${SPIRVCROSSREFLECT} ${GLSLANG} ${MACHINEINDEPENDENT} ${GENERICCODEGEN} ${OGLCOMPILER} ${OSDEPENDENT} ${GLSLANGDEFAULT} ${SPIRVLIB} ${SPIRVTOOLSOPT} glfw )

test

add_executable(test_mgl_app test_mgl/main.cpp) target_compile_options(test_mgl_app PRIVATE -x objective-c++) target_compile_definitions(test_mgl_app PUBLIC DTEST_MGL_GLFW ENABLE_OPT=0 SPIRV_CROSS_C_API_MSL=1 SPIRV_CROSS_C_API_GLSL=1 SPIRV_CROSS_C_API_CPP=1 SPIRV_CROSS_C_API_REFLECT=1)

set_property(TARGET test_mgl_app PROPERTY CXX_STANDARD 17) target_include_directories(test_mgl_app PUBLIC MGL/include)

target_link_libraries(test_mgl_app glfw mgl)

target_link_libraries(test_mgl_app PUBLIC mgl ${SPIRVLIB} ${SPIRVCROSS} ${SPIRVCROSSC} ${SPIRVCROSSCPP} ${SPIRVCROSSMSL} ${SPIRVCROSSGLSL} ${SPIRVCROSSHLSL} ${SPIRVCROSSREFLECT} ${GLSLANG} ${MACHINEINDEPENDENT} ${GENERICCODEGEN} ${OGLCOMPILER} ${OSDEPENDENT} ${GLSLANGDEFAULT} ${SPIRVTOOLS} ${SPIRVTOOLSOPT} glfw ) `

— Reply to this email directly, view it on GitHub https://github.com/openglonmetal/MGL/issues/40, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACOKD3YUFIRHXSJPV4H3ATLVVZ6PJANCNFSM54SARSWQ. You are receiving this because you are subscribed to this thread.

dokipen3d commented 2 years ago

cool thanks everyone. I'll build the patched glfw today and add it to the cmake progress.

I have lots of 4.6 test code from a tutorial series I've been writing so looking forward to getting this working.

it's a shame the multi draw indirect base instance trick doesn't work. is that a hard limitation or does it just require someone to work on it?

dokipen3d commented 2 years ago

I've managed to build the external deps and build libmgl.dylib okay now.

One thing that was difficult was to tell cmake how to compile mgl as objc and the test programs as objective c++ within the same cmake file. for now I've separated them out.

I'm able to compile the test program and link to everything, the only stumbling block I'm hitting on now is an exeption when creating the window. glfw goes to refresh context attributes and then tries to swap the metal buffer and something goes bad. I've just got vscode set up to debug so having a look at whats up.

I couldn't get the xcode projects to compile. I'm not the best at xcode at the moment.

Screen Shot 2022-07-26 at 2 36 31 PM
dokipen3d commented 2 years ago

I've built everything with symbols now so I can see the stack but for some reason, the debugger has no idea what kind of object _currentCommandBuffer is even thought its an

id<MTLCommandBuffer> _currentCommandBuffer;

I'm thinking its something to do with the way I've compiled it or I've missed a framework or something. I've spent a while on this so I'll have a break and come back to it another time.

dokipen3d commented 2 years ago

Oh my goodness! I have something displaying! This is magical....

I'll update my PR with the changes it took to get it to work. It was to do with Automatic Ref Counting flags it seems.

Screen Shot 2022-07-26 at 11 40 58 PM
conversy commented 2 years ago

good work @dokipen3d!

I'm not using the custom glfw anymore, but the one provided with brew, please have a look at the test_mgl/main.cpp and see how to setup an MGL context and pass it to callbacks...

I use SDL2, which is using the same scheme (also in test_mgl/main.cpp).

I'm looking forward to seeing your tutorial work with MGL, as it means it will improve correctness-wise...

As for the multidraw trick: I just could not find a way to make it work, and had to resort to another buffer, but feel free to give it a try.

My only regret so far is that I cannot think of a way to support bindless_texture easily, if you have the skills to look at it, that would be awesome...