Skip to content

Commit

Permalink
Merge pull request #31 from irisengine/feature/macos_arm
Browse files Browse the repository at this point in the history
Feature/macos arm
  • Loading branch information
nathan-baggs authored Aug 25, 2022
2 parents ec4648e + cce7742 commit 2bea5b7
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 47 deletions.
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.18)

project(
iris
VERSION "3.0.0"
VERSION "3.0.1"
DESCRIPTION "Cross-platform game engine"
LANGUAGES C CXX)

Expand All @@ -14,15 +14,19 @@ include(GenerateExportHeader)
# set options for library
option(IRIS_BUILD_UNIT_TESTS "whether to build unit tests" ON)

set(CMAKE_CXX_STANDARD 23)
set(ASM_OPTIONS "-x assembler-with-cpp")

# if a platform wasn't supplied then default to current platform
if(NOT IRIS_PLATFORM)
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(CMAKE_CXX_STANDARD 20)
set(IRIS_PLATFORM "MACOS")
set(IRIS_ARCH "X86_64")

if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64")
set(IRIS_ARCH "ARM64")
else()
set(IRIS_ARCH "X86_64")
endif()
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(IRIS_PLATFORM "WIN32")
set(IRIS_ARCH "X86_64")
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Iris is a cross-platform game engine written in modern C++
1. [Options](#options)
2. [Command line](#command-line)
3. [Visual Studio Code / Visual Studio](#visual-studio-code-visual-studio)
5. [Xcode](#xcode)
7. [Examples](#examples)
8. [Design](#design)
1. [Versioning](#versioning)
Expand All @@ -40,6 +39,7 @@ Iris is a cross-platform game engine written in modern C++
---

## Screenshots
![trinket](media/trinket.png)
![zombie](media/zombie.png)
![physics](media/physics.png)

Expand Down Expand Up @@ -68,6 +68,7 @@ The following compilers have been tested
| Platform | Version | Compiler |
| -------- | ------- | -------- |
| macOS | 14.0.5 | clang |
| macOS | 13.1.6 | Apple clang (Xcode) |
| linux | 14.0.5 | clang |
| linux | 12.1.0 | g++ |
| windows | 19.32.31332 | msvc |
Expand Down
2 changes: 1 addition & 1 deletion include/iris/core/error_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ inline void check_and_handle(
#define IRIS_DEBUG_BREAK() \
do \
{ \
asm("BKPT"); \
__builtin_debugtrap(); \
} while (false)
#else
#error unsupported architecture
Expand Down
3 changes: 3 additions & 0 deletions include/iris/graphics/metal/metal_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ class MetalRenderer : public Renderer

/** Buffer for bindless sampler table. */
std::unique_ptr<MetalConstantBuffer> sampler_table_;

/** Collection of resources that need to be made resident before rendering. */
std::vector<id<MTLResource>> resident_resources_;
};

}
Binary file added media/trinket.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions samples/sample_browser/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ void go(int, char **)
iris::Looper looper{
0ms,
16ms,
[&sample = sample](auto, auto) {
[&sample = sample](auto, auto)
{
sample->fixed_update();
return true;
},
[&, &sample = sample](std::chrono::microseconds elapsed, auto) {
[&, &sample = sample](std::chrono::microseconds elapsed, auto)
{
auto running = true;
auto event = window->pump_event();
while (event)
Expand Down
2 changes: 1 addition & 1 deletion shaders/glsl/vertex_node_chunk.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
vertex_pos{{swizzle}}
{% else if type == 1 %}
norm{{swizzle}}
{% else if type == 2%}
{% else if type == 2 %}
vec3(tex_coord{{swizzle}}, 0.0)
{% endif %}
6 changes: 4 additions & 2 deletions shaders/msl/texture_node_chunk.msl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% if vertex_data %}
{% if uv_source == 0 %}
texture_table[{{texture_index}}].texture.sample(sampler_table[{{sampler_index}}].smplr, in.tex.xy)
{% else %}
{% else if uv_source == 1 %}
texture_table[{{texture_index}}].texture.sample(sampler_table[{{sampler_index}}].smplr, in.position.xy * float2({{reciprocal_width}}, {{reciprocal_height}}))
{% else if uv_source == 2 %}
texture_table[{{texture_index}}].texture.sample(sampler_table[{{sampler_index}}].smplr, {{tex_coord}})
{% endif %}
4 changes: 3 additions & 1 deletion shaders/msl/vertex_node_chunk.msl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% if type == 0 %}
in.vertex_position{{swizzle}}
{% else %}
{% else if type == 1 %}
in.normal{{swizzle}}
{% else if type == 2 %}
float3(in.tex.xy, 0.0)
{% endif %}
10 changes: 9 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ add_subdirectory("scripting")
add_library(iris::iris ALIAS iris)
generate_export_header(iris)

target_compile_features(iris PUBLIC cxx_std_20)

# hide symbols
set_target_properties(iris PROPERTIES
CMAKE_CXX_VISIBILITY_PRESET hidden
Expand Down Expand Up @@ -51,7 +53,13 @@ set(IRIS_LINKED_LIBS_PRIVATE)
# handle platform specific setup including setting default graphics apis
if(IRIS_PLATFORM MATCHES "MACOS")
target_compile_definitions(iris PUBLIC IRIS_PLATFORM_MACOS)
target_compile_definitions(iris PUBLIC IRIS_ARCH_X86_64)

if(IRIS_ARCH MATCHES "ARM64")
target_compile_definitions(iris PUBLIC IRIS_ARCH_ARM64)
else()
target_compile_definitions(iris PUBLIC IRIS_ARCH_X86_64)
endif()

target_compile_options(iris PRIVATE -Wall -Werror -pedantic -glldb -fobjc-arc)
elseif(IRIS_PLATFORM MATCHES "IOS")
target_compile_definitions(iris PUBLIC IRIS_PLATFORM_IOS)
Expand Down
14 changes: 13 additions & 1 deletion src/core/macos/start.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
#include "graphics/metal/metal_render_target_manager.h"
#include "graphics/metal/metal_texture_manager.h"
#include "iris_version.h"
#include "jobs/fiber/fiber_job_system_manager.h"
#include "jobs/thread/thread_job_system_manager.h"
#include "log/emoji_formatter.h"
#include "log/log.h"
#include "log/logger.h"
#include "physics/bullet/bullet_physics_manager.h"

#if defined(IRIS_ARCH_X86_64)
#include "jobs/fiber/fiber_job_system_manager.h"
#endif

namespace
{

Expand All @@ -38,9 +42,14 @@ void register_apis()
iris::Root::register_physics_api("bullet", std::make_unique<iris::BulletPhysicsManager>());
iris::Root::set_physics_api("bullet");

#if defined(IRIS_ARCH_X86_64)
iris::Root::register_jobs_api("thread", std::make_unique<iris::ThreadJobSystemManager>());
iris::Root::register_jobs_api("fiber", std::make_unique<iris::FiberJobSystemManager>());
iris::Root::set_jobs_api("fiber");
#else
iris::Root::register_jobs_api("thread", std::make_unique<iris::ThreadJobSystemManager>());
iris::Root::set_jobs_api("thread");
#endif
}

}
Expand All @@ -50,6 +59,8 @@ void register_apis()

void start(int argc, char **argv, std::function<void(int, char **)> entry)
{
Logger::instance().set_Formatter<EmojiFormatter>();

LOG_ERROR("start", "engine start {}", IRIS_VERSION_STR);

register_apis();
Expand All @@ -62,6 +73,7 @@ void start(int argc, char **argv, std::function<void(int, char **)> entry)
void start_debug(int argc, char **argv, std::function<void(int, char **)> entry)
{
// enable engine logging
Logger::instance().set_Formatter<EmojiFormatter>();
Logger::instance().set_log_engine(true);

LOG_ENGINE_INFO("start", "engine start (with debugging) {}", IRIS_VERSION_STR);
Expand Down
1 change: 1 addition & 0 deletions src/graphics/metal/metal_cube_map.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
[MTLTextureDescriptor textureCubeDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm_sRGB
size:width
mipmapped:NO];
texture_descriptor.resourceOptions = MTLResourceStorageModeShared;
texture_ = [device newTextureWithDescriptor:texture_descriptor];

const std::byte *data_ptrs[] = {
Expand Down
26 changes: 22 additions & 4 deletions src/graphics/metal/metal_renderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,13 @@
/**
* Helper function to build the texture table - a global GPU buffer of all textures (used for bindless rendering)
*
* @param resident_resources
* Collection to add any resources that should be made resident before rendering.
*
* @returns
* Metal buffer with all loaded textures.
*/
std::unique_ptr<iris::MetalConstantBuffer> create_texture_table()
std::unique_ptr<iris::MetalConstantBuffer> create_texture_table(std::vector<id<MTLResource>> &resident_resources)
{
const auto *device = iris::core::utility::metal_device();

Expand All @@ -117,6 +120,8 @@
const auto *blank_texture = static_cast<const iris::MetalTexture *>(iris::Root::texture_manager().blank_texture());
auto iter = std::cbegin(textures);

resident_resources.push_back(blank_texture->handle());

// create descriptor for arguments we want to write
auto *argument_descriptor = [MTLArgumentDescriptor argumentDescriptor];
[argument_descriptor setIndex:0];
Expand All @@ -143,6 +148,7 @@
const auto *metal_texture = static_cast<const iris::MetalTexture *>(*iter);
[texture_table_argument_encoder setTexture:metal_texture->handle() atIndex:i];
++iter;
resident_resources.push_back(metal_texture->handle());
}
else
{
Expand All @@ -157,10 +163,13 @@
/**
* Helper function to build the cube map table - a global GPU buffer of all cube maps (used for bindless rendering)
*
* @param resident_resources
* Collection to add any resources that should be made resident before rendering.
*
* @returns
* Metal buffer with all loaded cube map.
*/
std::unique_ptr<iris::MetalConstantBuffer> create_cube_map_table()
std::unique_ptr<iris::MetalConstantBuffer> create_cube_map_table(std::vector<id<MTLResource>> &resident_resources)
{
const auto *device = iris::core::utility::metal_device();

Expand All @@ -169,6 +178,7 @@
const auto *blank_cube_map =
static_cast<const iris::MetalCubeMap *>(iris::Root::texture_manager().blank_cube_map());
auto iter = std::cbegin(cube_maps);
resident_resources.push_back(blank_cube_map->handle());

// create descriptor for arguments we want to write
auto *argument_descriptor = [MTLArgumentDescriptor argumentDescriptor];
Expand Down Expand Up @@ -196,6 +206,7 @@
const auto *metal_cube_map = static_cast<const iris::MetalCubeMap *>(*iter);
[cube_map_table_argument_encoder setTexture:metal_cube_map->handle() atIndex:i];
++iter;
resident_resources.push_back(metal_cube_map->handle());
}
else
{
Expand Down Expand Up @@ -392,8 +403,9 @@
}
}

texture_table_ = create_texture_table();
cube_map_table_ = create_cube_map_table();
resident_resources_.clear();
texture_table_ = create_texture_table(resident_resources_);
cube_map_table_ = create_cube_map_table(resident_resources_);
sampler_table_ = create_sampler_table();
}

Expand Down Expand Up @@ -480,6 +492,12 @@

render_encoder_ = render_encoders_[colour_target];

// make resident any resources we need to
for (const auto &resource : resident_resources_)
{
[render_encoder_ useResource:resource usage:MTLResourceUsageRead stages:MTLRenderStageFragment];
}

// if we haven't seen this entity yet this pass then create buffers for its data
if (!frame.bone_data.contains(entity))
{
Expand Down
3 changes: 3 additions & 0 deletions src/graphics/metal/metal_texture.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import <Metal/Metal.h>

#include "core/data_buffer.h"
#include "core/error_handling.h"
#include "core/exception.h"
#include "core/macos/macos_ios_utility.h"
#include "core/resource_loader.h"
Expand Down Expand Up @@ -47,6 +48,7 @@
texture_descriptor.width = width;
texture_descriptor.height = height;
texture_descriptor.pixelFormat = MTLPixelFormatRGBA8Unorm_sRGB;
texture_descriptor.resourceOptions = MTLResourceStorageModeShared;
texture_descriptor.usage = MTLTextureUsageShaderRead;

return texture_descriptor;
Expand All @@ -72,6 +74,7 @@
texture_descriptor.width = width;
texture_descriptor.height = height;
texture_descriptor.pixelFormat = MTLPixelFormatRGBA8Unorm;
texture_descriptor.resourceOptions = MTLResourceStorageModeShared;
texture_descriptor.usage = MTLTextureUsageShaderRead;

return texture_descriptor;
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/render_graph/shader_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ void ShaderCompiler::visit(const ComponentNode &node)
const auto value = stream_stack_.top().str();
stream_stack_.pop();

const ::inja::json args{{"value", value, {"component", node.component()}}};
const ::inja::json args{{"value", value}, {"component", node.component()}};

stream_stack_.top() << ::inja::render(
language_string(language_, hlsl::component_node_chunk, glsl::component_node_chunk, msl::component_node_chunk),
Expand Down
Loading

0 comments on commit 2bea5b7

Please sign in to comment.