Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit 52f3c80

Browse files
committed
use spirv from glslang
1 parent 9ff8a1a commit 52f3c80

File tree

5 files changed

+227
-55
lines changed

5 files changed

+227
-55
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
run: |
2020
mkdir build
2121
cd build
22-
cmake ..
22+
cmake -DMGL_BUILD_TEST=TRUE ..
2323
2424
- name: Build
2525
run: |

CMakeLists.txt

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,47 @@
11
cmake_minimum_required(VERSION 3.15)
22
project(MGL LANGUAGES C CXX OBJC)
33

4-
execute_process(COMMAND "bash" "clone_external.sh" WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/external" COMMAND_ECHO STDOUT)
54
execute_process(COMMAND "bash" "build_external.sh" WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/external" COMMAND_ECHO STDOUT)
65

76
set(CMAKE_C_STANDARD 17)
87
set(CMAKE_CXX_STANDARD 20)
98

10-
# Include paths
119
include_directories(
1210
MGL/SPIRV/SPIRV-Cross
1311
MGL/include
1412
MGL/include/GL
1513
external/SPIRV-Cross
16-
external/SPIRV-Tools/include
14+
external/glslang/External/spirv-tools/include
15+
external/glslang/SPIRV
1716
external/glslang/glslang/Include
17+
external/glslang/glslang/Public
1818
)
1919

20-
# Gather all source files
2120
file(GLOB_RECURSE MGL_SOURCES
2221
MGL/src/*.c
2322
MGL/src/*.m
2423
MGL/spirv_cross_c.cpp
2524
)
2625

27-
# Static library target
2826
add_library(mgl STATIC ${MGL_SOURCES})
2927

30-
# Link libraries
3128
target_link_libraries(mgl PUBLIC
32-
"${CMAKE_CURRENT_LIST_DIR}/external/glslang/build/glslang/libGenericCodeGen.a"
33-
"${CMAKE_CURRENT_LIST_DIR}/external/glslang/build/glslang/libMachineIndependent.a"
34-
"${CMAKE_CURRENT_LIST_DIR}/external/glslang/build/glslang/libglslang-default-resource-limits.a"
35-
"${CMAKE_CURRENT_LIST_DIR}/external/glslang/build/glslang/libglslang.a"
36-
"${CMAKE_CURRENT_LIST_DIR}/external/glslang/build/glslang/OSDependent/Unix/libOSDependent.a"
37-
"${CMAKE_CURRENT_LIST_DIR}/external/glslang/build/SPIRV/libSPIRV.a"
38-
"${CMAKE_CURRENT_LIST_DIR}/external/SPIRV-Cross/build/libspirv-cross-c.a"
39-
"${CMAKE_CURRENT_LIST_DIR}/external/SPIRV-Cross/build/libspirv-cross-core.a"
40-
"${CMAKE_CURRENT_LIST_DIR}/external/SPIRV-Cross/build/libspirv-cross-cpp.a"
41-
"${CMAKE_CURRENT_LIST_DIR}/external/SPIRV-Cross/build/libspirv-cross-glsl.a"
42-
"${CMAKE_CURRENT_LIST_DIR}/external/SPIRV-Cross/build/libspirv-cross-hlsl.a"
43-
"${CMAKE_CURRENT_LIST_DIR}/external/SPIRV-Cross/build/libspirv-cross-msl.a"
44-
"${CMAKE_CURRENT_LIST_DIR}/external/SPIRV-Cross/build/libspirv-cross-reflect.a"
45-
"${CMAKE_CURRENT_LIST_DIR}/external/SPIRV-Cross/build/libspirv-cross-util.a"
29+
"${CMAKE_CURRENT_SOURCE_DIR}/external/SPIRV-Cross/build/libspirv-cross-c.a"
30+
"${CMAKE_CURRENT_SOURCE_DIR}/external/SPIRV-Cross/build/libspirv-cross-core.a"
31+
"${CMAKE_CURRENT_SOURCE_DIR}/external/SPIRV-Cross/build/libspirv-cross-cpp.a"
32+
"${CMAKE_CURRENT_SOURCE_DIR}/external/SPIRV-Cross/build/libspirv-cross-glsl.a"
33+
"${CMAKE_CURRENT_SOURCE_DIR}/external/SPIRV-Cross/build/libspirv-cross-hlsl.a"
34+
"${CMAKE_CURRENT_SOURCE_DIR}/external/SPIRV-Cross/build/libspirv-cross-msl.a"
35+
"${CMAKE_CURRENT_SOURCE_DIR}/external/SPIRV-Cross/build/libspirv-cross-reflect.a"
36+
"${CMAKE_CURRENT_SOURCE_DIR}/external/SPIRV-Cross/build/libspirv-cross-util.a"
37+
"${CMAKE_CURRENT_SOURCE_DIR}/external/glslang/build/SPIRV/libSPIRV.a"
38+
"${CMAKE_CURRENT_SOURCE_DIR}/external/glslang/build/glslang/OSDependent/Unix/libOSDependent.a"
39+
"${CMAKE_CURRENT_SOURCE_DIR}/external/glslang/build/glslang/libGenericCodeGen.a"
40+
"${CMAKE_CURRENT_SOURCE_DIR}/external/glslang/build/glslang/libMachineIndependent.a"
41+
"${CMAKE_CURRENT_SOURCE_DIR}/external/glslang/build/glslang/libglslang-default-resource-limits.a"
42+
"${CMAKE_CURRENT_SOURCE_DIR}/external/glslang/build/glslang/libglslang.a"
4643
)
4744

48-
# macOS Frameworks
4945
target_link_libraries(mgl PUBLIC
5046
"-framework AppKit"
5147
"-framework Cocoa"
@@ -56,3 +52,20 @@ target_link_libraries(mgl PUBLIC
5652
"-framework OpenGL"
5753
"-framework QuartzCore"
5854
)
55+
56+
if(MGL_BUILD_TEST)
57+
add_library(glfw-imp STATIC IMPORTED)
58+
set_target_properties(glfw-imp PROPERTIES IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/external/glfw/build/src/libglfw3.a")
59+
60+
add_library(glfw INTERFACE)
61+
target_link_libraries(glfw INTERFACE glfw-imp mgl)
62+
target_include_directories(glfw INTERFACE "external/glfw/include")
63+
64+
add_dependencies(glfw mgl)
65+
66+
add_library(glm INTERFACE)
67+
target_include_directories(glm INTERFACE "mgl/MGL/include")
68+
69+
add_executable(mgl_test mgl_test.cpp)
70+
target_link_libraries(mgl_test glfw glm)
71+
endif()

external/build_external.sh

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
set SDKROOT=`xcrun --show-sdk-path`
1+
#!/bin/bash
22

3-
cp ../MGL/include/MGLContext.h glfw/src
4-
cp ../MGL/include/MGLRenderer.h glfw/src
5-
cd SPIRV-Tools
6-
mkdir build
7-
cd build
8-
cmake ..
9-
make -j 4
10-
cd ../..
3+
# Optional: for macOS SDK if you're using system frameworks
4+
# export SDKROOT=$(xcrun --show-sdk-path)
5+
6+
# Clone only the required repositories
7+
git clone https://github.com/KhronosGroup/glslang.git --depth 1
8+
git clone https://github.com/r58Playz/SPIRV-Cross.git -b uniform-constants
9+
10+
# === Build SPIRV-Cross ===
1111
cd SPIRV-Cross
12-
mkdir build
13-
cd build
12+
mkdir -p build && cd build
1413
cmake ..
15-
make -j 4
16-
cd ../..
17-
cd SPIRV-Headers
18-
mkdir build
19-
cd build
20-
cmake ..
21-
make -j 4
14+
make -j4
2215
cd ../..
16+
17+
# === Build glslang ===
2318
cd glslang
2419
./update_glslang_sources.py
25-
mkdir build
26-
cd build
20+
mkdir -p build && cd build
2721
cmake ..
28-
make -j 4
22+
make -j4
2923
cd ../..
24+
25+
# === Build glfw ===
26+
cp ../MGL/include/MGLContext.h glfw/src
27+
cp ../MGL/include/MGLRenderer.h glfw/src
3028
cd glfw
31-
mkdir build
32-
cd build
29+
mkdir -p build && cd build
3330
cmake ..
34-
make -j 4 glfw
31+
make -j4
3532
cd ../..

external/clone_external.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

mgl_test.cpp

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
//========================================================================
2+
// OpenGL triangle example
3+
// Copyright (c) Camilla Löwy <[email protected]>
4+
//
5+
// This software is provided 'as-is', without any express or implied
6+
// warranty. In no event will the authors be held liable for any damages
7+
// arising from the use of this software.
8+
//
9+
// Permission is granted to anyone to use this software for any purpose,
10+
// including commercial applications, and to alter it and redistribute it
11+
// freely, subject to the following restrictions:
12+
//
13+
// 1. The origin of this software must not be misrepresented; you must not
14+
// claim that you wrote the original software. If you use this software
15+
// in a product, an acknowledgment in the product documentation would
16+
// be appreciated but is not required.
17+
//
18+
// 2. Altered source versions must be plainly marked as such, and must not
19+
// be misrepresented as being the original software.
20+
//
21+
// 3. This notice may not be removed or altered from any source
22+
// distribution.
23+
//
24+
//========================================================================
25+
//! [code]
26+
27+
#define GLAD_GL_IMPLEMENTATION
28+
#include <glad/gl.h>
29+
#define GLFW_INCLUDE_NONE
30+
#include <GLFW/glfw3.h>
31+
32+
#include "linmath.h"
33+
34+
#include <stdlib.h>
35+
#include <stddef.h>
36+
#include <stdio.h>
37+
38+
typedef struct Vertex
39+
{
40+
vec2 pos;
41+
vec3 col;
42+
} Vertex;
43+
44+
static const Vertex vertices[3] =
45+
{
46+
{ { -0.6f, -0.4f }, { 1.f, 0.f, 0.f } },
47+
{ { 0.6f, -0.4f }, { 0.f, 1.f, 0.f } },
48+
{ { 0.f, 0.6f }, { 0.f, 0.f, 1.f } }
49+
};
50+
51+
static const char* vertex_shader_text =
52+
"#version 330\n"
53+
"uniform mat4 MVP;\n"
54+
"in vec3 vCol;\n"
55+
"in vec2 vPos;\n"
56+
"out vec3 color;\n"
57+
"void main()\n"
58+
"{\n"
59+
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
60+
" color = vCol;\n"
61+
"}\n";
62+
63+
static const char* fragment_shader_text =
64+
"#version 330\n"
65+
"in vec3 color;\n"
66+
"out vec4 fragment;\n"
67+
"void main()\n"
68+
"{\n"
69+
" fragment = vec4(color, 1.0);\n"
70+
"}\n";
71+
72+
static void error_callback(int error, const char* description)
73+
{
74+
fprintf(stderr, "Error: %s\n", description);
75+
}
76+
77+
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
78+
{
79+
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
80+
glfwSetWindowShouldClose(window, GLFW_TRUE);
81+
}
82+
83+
int main(void)
84+
{
85+
glfwSetErrorCallback(error_callback);
86+
87+
if (!glfwInit())
88+
exit(EXIT_FAILURE);
89+
90+
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
91+
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
92+
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
93+
94+
GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Triangle", NULL, NULL);
95+
if (!window)
96+
{
97+
glfwTerminate();
98+
exit(EXIT_FAILURE);
99+
}
100+
101+
glfwSetKeyCallback(window, key_callback);
102+
103+
glfwMakeContextCurrent(window);
104+
gladLoadGL(glfwGetProcAddress);
105+
glfwSwapInterval(1);
106+
107+
// NOTE: OpenGL error checks have been omitted for brevity
108+
109+
GLuint vertex_buffer;
110+
glGenBuffers(1, &vertex_buffer);
111+
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
112+
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
113+
114+
const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
115+
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
116+
glCompileShader(vertex_shader);
117+
118+
const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
119+
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
120+
glCompileShader(fragment_shader);
121+
122+
const GLuint program = glCreateProgram();
123+
glAttachShader(program, vertex_shader);
124+
glAttachShader(program, fragment_shader);
125+
glLinkProgram(program);
126+
127+
const GLint mvp_location = glGetUniformLocation(program, "MVP");
128+
const GLint vpos_location = glGetAttribLocation(program, "vPos");
129+
const GLint vcol_location = glGetAttribLocation(program, "vCol");
130+
131+
GLuint vertex_array;
132+
glGenVertexArrays(1, &vertex_array);
133+
glBindVertexArray(vertex_array);
134+
glEnableVertexAttribArray(vpos_location);
135+
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
136+
sizeof(Vertex), (void*) offsetof(Vertex, pos));
137+
glEnableVertexAttribArray(vcol_location);
138+
glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
139+
sizeof(Vertex), (void*) offsetof(Vertex, col));
140+
141+
while (!glfwWindowShouldClose(window))
142+
{
143+
int width, height;
144+
glfwGetFramebufferSize(window, &width, &height);
145+
const float ratio = width / (float) height;
146+
147+
glViewport(0, 0, width, height);
148+
glClear(GL_COLOR_BUFFER_BIT);
149+
150+
mat4x4 m, p, mvp;
151+
mat4x4_identity(m);
152+
mat4x4_rotate_Z(m, m, (float) glfwGetTime());
153+
mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
154+
mat4x4_mul(mvp, p, m);
155+
156+
glUseProgram(program);
157+
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) &mvp);
158+
glBindVertexArray(vertex_array);
159+
glDrawArrays(GL_TRIANGLES, 0, 3);
160+
161+
glfwSwapBuffers(window);
162+
glfwPollEvents();
163+
}
164+
165+
glfwDestroyWindow(window);
166+
167+
glfwTerminate();
168+
exit(EXIT_SUCCESS);
169+
}
170+
171+
//! [code]

0 commit comments

Comments
 (0)