Skip to content

Fix macos build and run issues #1777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions assets/shaders/identity.vert.glsl
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#version 120
#version 330 core

// no-transformation texture mapping vertex shader
// Input vertex position
layout(location = 0) in vec4 vertex_position;

// the position of this vertex
attribute vec4 vertex_position;

// interpolated texture coordinates sent to fragment shader
varying vec2 tex_position;
// Output to fragment shader
out vec2 tex_position;

void main(void) {
gl_Position = vertex_position;
gl_Position = vertex_position;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaik you don't need this statement since gl_Position by definition contains the current vertex position already. You can remove that line.

Suggested change
gl_Position = vertex_position;

// convert from screen coordinates (-1, 1) to texture coordinates (0, 1)
tex_position = (vertex_position.xy + vec2(1.f)) / 2.f;
// Convert from clip space (-1, 1) to texture space (0, 1)
tex_position = (vertex_position.xy + vec2(1.0)) * 0.5;
}
15 changes: 6 additions & 9 deletions assets/shaders/maptexture.frag.glsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#version 120
// total basic standard texture drawing fragment shader
#version 330 core

// the texture data
uniform sampler2D texture;
uniform sampler2D texture_;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better rename it tex than using underscores because that aligns with other shader source code definitions.

Suggested change
uniform sampler2D texture_;
uniform sampler2D tex;

// interpolated texture coordinates received from vertex shader
varying vec2 tex_position;
in vec2 tex_position;
out vec4 frag_color;

void main (void) {
// this sets the fragment color to the corresponding texel.
gl_FragColor = texture2D(texture, tex_position);
void main(void) {
frag_color = texture(texture_, tex_position);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
frag_color = texture(texture_, tex_position);
frag_color = texture(tex, tex_position);

25 changes: 10 additions & 15 deletions assets/shaders/maptexture.vert.glsl
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
//total basic standard texture mapping vertex shader
#version 330 core

//modelview*projection matrix
uniform mat4 mvp_matrix;

//the position of this vertex
attribute vec4 vertex_position;
// Input attributes (set via glVertexAttribPointer)
layout(location = 0) in vec4 vertex_position;
layout(location = 1) in vec2 tex_coordinates;

//the texture coordinates assigned to this vertex
attribute vec2 tex_coordinates;
// Output to fragment shader
out vec2 tex_position;

//interpolated texture coordinates sent to fragment shader
varying vec2 tex_position;
// Uniform matrix
uniform mat4 mvp_matrix;

void main(void) {
//transform the vertex coordinates
gl_Position = gl_ModelViewProjectionMatrix * vertex_position;

//pass the fix points for texture coordinates set at this vertex
tex_position = tex_coordinates;
gl_Position = mvp_matrix * vertex_position;
tex_position = tex_coordinates;
}
31 changes: 12 additions & 19 deletions assets/shaders/world2d.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,18 @@ vec2 uv = vec2(
void main() {
vec4 tex_val = texture(tex, uv);
int alpha = int(round(tex_val.a * 255));
switch (alpha) {
case 0:
col = tex_val;
discard;

// do not save the ID
return;
case 254:
col = vec4(1.0f, 0.0f, 0.0f, 1.0f);
break;
case 252:
col = vec4(0.0f, 1.0f, 0.0f, 1.0f);
break;
case 250:
col = vec4(0.0f, 0.0f, 1.0f, 1.0f);
break;
default:
col = tex_val;
break;
if (alpha == 0) {
col = tex_val;
discard;
return;
} else if (alpha == 254) {
col = vec4(1.0f, 0.0f, 0.0f, 1.0f);
} else if (alpha == 252) {
col = vec4(0.0f, 1.0f, 0.0f, 1.0f);
} else if (alpha == 250) {
col = vec4(0.0f, 0.0f, 1.0f, 1.0f);
} else {
col = tex_val;
Comment on lines +22 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch statements should definitely work in this place, so I would revisit this shader once you have fixed all the other comments.

}
id = u_id;
}
26 changes: 11 additions & 15 deletions assets/test/shaders/demo_2_obj.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@ layout(location=1) out uint id;
void main() {
vec4 tex_val = texture(tex, v_uv);
int alpha = int(round(tex_val.a * 255));
switch (alpha) {
case 0:
discard;
break;
case 254:
col = vec4(1.0f, 0.0f, 0.0f, 1.0f);
break;
case 252:
col = vec4(0.0f, 1.0f, 0.0f, 1.0f);
break;
case 250:
col = vec4(0.0f, 0.0f, 1.0f, 1.0f);
break;
default:

if (alpha == 0) {
discard;
} else if (alpha == 254) {
col = vec4(1.0, 0.0, 0.0, 1.0);
} else if (alpha == 252) {
col = vec4(0.0, 1.0, 0.0, 1.0);
} else if (alpha == 250) {
col = vec4(0.0, 0.0, 1.0, 1.0);
} else {
Comment on lines +14 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

col = tex_val;
break;
}

id = u_id;
}
26 changes: 11 additions & 15 deletions assets/test/shaders/demo_4_obj.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@ layout(location=1) out uint id;
void main() {
vec4 tex_val = texture(tex, v_uv);
int alpha = int(round(tex_val.a * 255));
switch (alpha) {
case 0:
discard;
break;
case 254:
col = vec4(1.0f, 0.0f, 0.0f, 1.0f);
break;
case 252:
col = vec4(0.0f, 1.0f, 0.0f, 1.0f);
break;
case 250:
col = vec4(0.0f, 0.0f, 1.0f, 1.0f);
break;
default:

if (alpha == 0) {
discard;
} else if (alpha == 254) {
col = vec4(1.0, 0.0, 0.0, 1.0);
} else if (alpha == 252) {
col = vec4(0.0, 1.0, 0.0, 1.0);
} else if (alpha == 250) {
col = vec4(0.0, 0.0, 1.0, 1.0);
} else {
Comment on lines +14 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

col = tex_val;
break;
}

id = u_id;
}
2 changes: 2 additions & 0 deletions copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ _the openage authors_ are:
| Jordan Sutton | jsutCodes | jsutcodes à gmail dawt com |
| Daniel Wieczorek | Danio | danielwieczorek96 à gmail dawt com |
| | bytegrrrl | bytegrrrl à proton dawt me |
| | hellvoid | hell à void dawt lan |


If you're a first-time committer, add yourself to the above list. This is not
just for legal reasons, but also to keep an overview of all those nicknames.
Expand Down
36 changes: 23 additions & 13 deletions libopenage/engine/engine.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 the openage authors. See copying.md for legal info.
// Copyright 2023-2025 the openage authors. See copying.md for legal info.

#include "engine.h"

Expand All @@ -20,6 +20,7 @@ Engine::Engine(mode mode,
running{true},
run_mode{mode},
root_dir{root_dir},
window_settings{window_settings},
threads{} {
log::log(INFO
<< "launching engine with root directory"
Expand Down Expand Up @@ -53,31 +54,40 @@ Engine::Engine(mode mode,
this->time_loop.reset();
});

// if presenter is used, run it in a separate thread
log::log(INFO << "Using " << this->threads.size() + 1 << " threads "
<< "(" << std::jthread::hardware_concurrency() << " available)");
}
Comment on lines +57 to +58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number is now no longer correct because simulation/presenter are started later. That's why I made the recommendation to start all threads in loop() in the other comment.


void Engine::loop() {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The structure with loop() and loop_simulation() is a bit too convoluted now I think. I recommend we organize this in a different way:

  1. Move all thread functions (time loop, simulation loop, presenter loop) into a separate function definition (maybe even outside Engine class?)
  2. Move the thread spawning to Engine::loop() (so that alll threads are started there)
  3. In Engine::loop() it should be much clearer why we are running different functions in the main thread depending on headless mode. The comments should explain that this is because of the Qt requirements.

// if presenter is used, run the simulation in a separate thread
if (this->run_mode == mode::FULL) {
this->threads.emplace_back([&]() {
this->presenter->run(window_settings);

// Make sure that the presenter gets destructed in the same thread
// otherwise OpenGL complains about missing contexts
this->presenter.reset();
this->running = false;
this->threads.emplace_back([&]() {
this->loop_simulation();
});
}

log::log(INFO << "Using " << this->threads.size() + 1 << " threads "
<< "(" << std::jthread::hardware_concurrency() << " available)");
this->presenter->run(this->window_settings);
log::log(MSG(info) << "presenter exited");
// Make sure that the presenter gets destructed in the same thread
// otherwise OpenGL complains about missing contexts
this->presenter.reset();
this->running = false;
}
else {
this->loop_simulation();
}
}

void Engine::loop() {
void Engine::loop_simulation() {
// Run the main game simulation loop:
this->simulation->run();

// After stopping, clean up the simulation
this->simulation.reset();
if (this->run_mode != mode::FULL) {
this->running = false;
}

}

} // namespace openage::engine
13 changes: 12 additions & 1 deletion libopenage/engine/engine.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2024 the openage authors. See copying.md for legal info.
// Copyright 2023-2025 the openage authors. See copying.md for legal info.

#pragma once

Expand Down Expand Up @@ -100,6 +100,12 @@ class Engine {
bool running;

private:

/**
* Run the simulation loop
*/
void loop_simulation();

/**
* The run mode to use.
*/
Expand All @@ -110,6 +116,11 @@ class Engine {
*/
util::Path root_dir;

/**
* Window settings
*/
const renderer::window_settings window_settings;

/**
Comment on lines +119 to +123
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably better to store them in the presenter class and pass them in the constructor.

* The threads used by the engine.
*/
Expand Down
2 changes: 1 addition & 1 deletion libopenage/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2024 the openage authors. See copying.md for legal info.
// Copyright 2015-2025 the openage authors. See copying.md for legal info.

#include "main.h"

Expand Down
2 changes: 1 addition & 1 deletion libopenage/main/demo/pong/gui.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2024 the openage authors. See copying.md for legal info.
// Copyright 2019-2025 the openage authors. See copying.md for legal info.

#include "gui.h"

Expand Down
7 changes: 4 additions & 3 deletions libopenage/renderer/gui/gui.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2024 the openage authors. See copying.md for legal info.
// Copyright 2015-2025 the openage authors. See copying.md for legal info.

#include "gui.h"

Expand All @@ -16,6 +16,7 @@
#include "renderer/window.h"
#include "util/path.h"

static constexpr const char* texture_ = "texture_";

namespace openage::renderer::gui {

Expand Down Expand Up @@ -87,7 +88,7 @@ void GUI::initialize_render_pass(size_t width,
resources::Texture2dInfo(width, height, resources::pixel_format::rgba8));
auto fbo = this->renderer->create_texture_target({output_texture});

this->texture_unif = maptex_shader->new_uniform_input("texture", this->texture);
this->texture_unif = maptex_shader->new_uniform_input(texture_, this->texture);
Renderable display_obj{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a static variable should not be necessary for new_uniform_input(..), since all other calls just pass an in-place string. You should just be able to pass the string without any problems.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a constexpr as "texture" was used in more than one place. IMO it is better to have a constexpr/define rather than a hardcoded value, especially if it is used in more than one place.

However if you rather want it hardcoded I will object to it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree in general, but for these WIP parts of the engine it's probably better to hardcode it for now. My reason would be that it then becomes more "visible" what is done with the shader until it's properly defined. For the more mature shaders, we have a solution that is similar to what you propose where we store the uniform ID and use that

/**
* Shader uniform IDs for setting uniform values.
*/
inline static uniform_id_t obj_world_position;
inline static uniform_id_t flip_x;
inline static uniform_id_t flip_y;
inline static uniform_id_t tex;
inline static uniform_id_t tile_params;
inline static uniform_id_t scale;
inline static uniform_id_t subtex_size;
inline static uniform_id_t anchor_offset;

this->texture_unif,
quad,
Expand Down Expand Up @@ -119,7 +120,7 @@ void GUI::resize(size_t width, size_t height) {
auto fbo = renderer->create_texture_target({output_texture});

// pass the new texture to shader uniform
this->texture_unif->update("texture", this->texture);
this->texture_unif->update(texture_, this->texture);

this->render_pass->set_target(fbo);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2023 the openage authors. See copying.md for legal info.
// Copyright 2015-2025 the openage authors. See copying.md for legal info.

#include "gui_application_impl.h"

Expand Down Expand Up @@ -35,9 +35,7 @@ GuiApplicationImpl::~GuiApplicationImpl() {

void GuiApplicationImpl::processEvents() {
assert(std::this_thread::get_id() == this->owner);
#ifndef __APPLE__
this->app.processEvents();
#endif
}

namespace {
Expand Down
Loading