From d2aae0aa0db1a35d8990d76f17814c9755da5f65 Mon Sep 17 00:00:00 2001 From: thoughton Date: Tue, 2 Oct 2018 20:31:10 +0100 Subject: [PATCH 1/5] Adding support for Linux+GLSL (GLCORE33) --- CMakeLists.txt | 30 +++++++++++++++++++++++++ source/application.c | 52 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..392d0f7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.12) +project(dod_playground) + +set(CMAKE_C_STANDARD 99) +set(CMAKE_CXX_STANDARD 11) + +find_package(X11 REQUIRED) +find_package(OpenGL REQUIRED) + +add_compile_definitions( + SOKOL_GLCORE33) + +include_directories( + ${X11_INCLUDE_DIR}) + +add_executable(dod_playground + source/external/sokol.c + source/external/sokol_app.h + source/external/sokol_gfx.h + source/external/sokol_time.h + source/external/stb_easy_font.h + source/external/stb_image.h + source/application.c + source/game.cpp + source/game.h) + +target_link_libraries(dod_playground + OpenGL::GL + ${X11_LIBRARIES} + dl) diff --git a/source/application.c b/source/application.c index c1378b7..44a7ac6 100644 --- a/source/application.c +++ b/source/application.c @@ -53,9 +53,22 @@ void init(void) { /* create shader */ sg_shader shd = sg_make_shader(&(sg_shader_desc) { .vs.uniform_blocks[0] = { +#if defined(SOKOL_GLCORE33) + .size = sizeof(vs_params_t), + .uniforms = { + [0] = { .name = "aspect", .type = SG_UNIFORMTYPE_FLOAT } + }, +#else .size = sizeof(vs_params_t) +#endif }, +#if defined(SOKOL_GLCORE33) + .fs.images = { + [0] = { .name="tex0", .type=SG_IMAGETYPE_2D }, + }, +#else .fs.images[0].type = SG_IMAGETYPE_2D, +#endif .vs.source = vs_src, .fs.source = fs_src, }); @@ -78,10 +91,17 @@ void init(void) { sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){ .layout = { .buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE, +#if defined(SOKOL_GLCORE33) + .attrs = { + [0] = { .name="posScale", .offset = 0, .format=SG_VERTEXFORMAT_FLOAT3, .buffer_index=0 }, // instance pos + scale + [1] = { .name="colorIndex", .offset = 12, .format=SG_VERTEXFORMAT_FLOAT4, .buffer_index=0 }, // instance color + }, +#else .attrs = { [0] = { .sem_name = "POSSCALE", .format=SG_VERTEXFORMAT_FLOAT3 }, // instance pos + scale [1] = { .sem_name = "COLORSPRITE", .format=SG_VERTEXFORMAT_FLOAT4 }, // instance color }, +#endif }, .shader = shd, .index_type = SG_INDEXTYPE_UINT16, @@ -272,6 +292,36 @@ const char* fs_src = " diffuse.rgb *= inp.color.rgb;\n" " return diffuse;\n" "}\n"; +#elif defined(SOKOL_GLCORE33) +const char* vs_src = + "#version 330\n" + "uniform float aspect;\n" + "in vec3 posScale;\n" + "in vec4 colorIndex;\n" + "out vec3 color;\n" + "out vec2 uv;\n" + "void main() {\n" + " float x = gl_VertexID / 2;\n" + " float y = gl_VertexID & 1;\n" + " gl_Position.x = posScale.x + (x-0.5f) * posScale.z;\n" + " gl_Position.y = posScale.y + (y-0.5f) * posScale.z * aspect;\n" + " gl_Position.z = 0.0f;\n" + " gl_Position.w = 1.0f;\n" + " uv = vec2((x + colorIndex.w)/8, 1-y);\n" + " color = colorIndex.rgb;\n" + "}\n"; +const char* fs_src = + "#version 330\n" + "uniform sampler2D tex0;\n" + "in vec3 color;\n" + "in vec2 uv;\n" + "out vec4 frag_color;\n" + "void main() {\n" + " frag_color = texture(tex0, uv);\n" + " float lum = dot(frag_color.rgb, vec3(0.333));\n" + " frag_color.rgb = mix(frag_color.rgb, vec3(lum), 0.8);\n" + " frag_color.rgb *= color.rgb;\n" + "}\n"; #else -#error Unknown graphics plaform +#error No shaders defined for specified graphics platform #endif From 3ecadb0e6fdc8396760f0fe9b17a60533f4296a2 Mon Sep 17 00:00:00 2001 From: thoughton Date: Tue, 2 Oct 2018 20:42:15 +0100 Subject: [PATCH 2/5] Updated readme about Linux support --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b4a42ae..78891d8 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,9 @@ in the process. Talk [**slides are here**](http://aras-p.info/texts/files/2018Academy%20-%20ECS-DoD.pdf) _(10MB pdf)_. -The app playground should work on Windows (uses D3D11, VS2017 project in `projects/vs2017/dod-playground.sln`) and -macOS (uses Metal, Xcode 9 project in `projects/xcode/dod-playground.xcodeproj`). +The app playground should work on Windows (uses D3D11, VS2017 project in `projects/vs2017/dod-playground.sln`), +macOS (uses Metal, Xcode 9 project in `projects/xcode/dod-playground.xcodeproj`) and Linux (uses GL, CMakeLists.txt +in root folder). I used some excellent other libraries/resources to make life easier for me here: From 7b4d9ea9984dd2f020e5cd72c4790f8361a5b606 Mon Sep 17 00:00:00 2001 From: thoughton Date: Tue, 2 Oct 2018 21:15:37 +0100 Subject: [PATCH 3/5] Added check for "Linux" platform in CMakeLists --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 392d0f7..851ba92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,10 @@ cmake_minimum_required(VERSION 3.12) project(dod_playground) +if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux") + message(FATAL_ERROR "This CMake setup currently only supports Linux. For Windows and macOS please see the ready-made projects in the projects/ directory.") +endif() + set(CMAKE_C_STANDARD 99) set(CMAKE_CXX_STANDARD 11) From 408d0a682082a7f8a57ea6c5766fc56454148f08 Mon Sep 17 00:00:00 2001 From: thoughton Date: Tue, 2 Oct 2018 21:26:56 +0100 Subject: [PATCH 4/5] Fixed some tabbing in my previous change --- source/application.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/application.c b/source/application.c index 44a7ac6..5b33063 100644 --- a/source/application.c +++ b/source/application.c @@ -64,7 +64,7 @@ void init(void) { }, #if defined(SOKOL_GLCORE33) .fs.images = { - [0] = { .name="tex0", .type=SG_IMAGETYPE_2D }, + [0] = { .name="tex0", .type=SG_IMAGETYPE_2D }, }, #else .fs.images[0].type = SG_IMAGETYPE_2D, From 1d92f226f078d2d1a2e497564211970a6155ac07 Mon Sep 17 00:00:00 2001 From: thoughton Date: Tue, 2 Oct 2018 21:42:36 +0100 Subject: [PATCH 5/5] Trying again to fix tabbing in previous change --- source/application.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/application.c b/source/application.c index 5b33063..f12148d 100644 --- a/source/application.c +++ b/source/application.c @@ -64,8 +64,8 @@ void init(void) { }, #if defined(SOKOL_GLCORE33) .fs.images = { - [0] = { .name="tex0", .type=SG_IMAGETYPE_2D }, - }, + [0] = { .name="tex0", .type=SG_IMAGETYPE_2D }, + }, #else .fs.images[0].type = SG_IMAGETYPE_2D, #endif