Skip to content

Commit

Permalink
Upgrade Asteroids code to use MethaneKit v0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
egorodet committed Jan 1, 2025
1 parent dade401 commit c00fac8
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 81 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ on:

env:
product_ver_major: 0
product_ver_minor: 7
product_ver_patch: 3
product_ver_minor: 8
product_ver_patch: 0
product_ver_build: ${{ github.run_number }}
tracy_release_version: "0.9.1"
vulkan_sdk_version: "1.3.250.1"
tracy_release_version: "0.11.2.1"
vulkan_sdk_version: "1.3.290.0"

jobs:
build-asteroids:
Expand Down
11 changes: 6 additions & 5 deletions App/AsteroidsApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Sample demonstrating parallel rendering of the distinct asteroids massive
#include <thread>
#include <array>
#include <map>
#include <magic_enum.hpp>

namespace Methane::Samples
{
Expand Down Expand Up @@ -353,7 +352,7 @@ void AsteroidsApp::Init()
frame.asteroids.uniforms_buffer.SetName(fmt::format("Asteroids Array Uniforms Buffer {}", frame.index));

// Resource bindings for Sky-Box rendering
frame.sky_box.program_bindings = m_sky_box.CreateProgramBindings(frame.sky_box.uniforms_buffer, frame.index);
std::tie(frame.sky_box.program_bindings, frame.sky_box.uniforms_argument_binding_ptr) = m_sky_box.CreateProgramBindings(frame.index);
frame.sky_box.program_bindings.SetName(fmt::format("Space Sky-Box Bindings {}", frame.index));

// Resource bindings for Planet rendering
Expand Down Expand Up @@ -410,12 +409,14 @@ bool AsteroidsApp::Update()
if (!UserInterfaceApp::Update())
return false;

const AsteroidsFrame& frame = GetCurrentFrame();

// Update scene uniforms
m_scene_uniforms.view_proj_matrix = hlslpp::transpose(m_view_camera.GetViewProjMatrix());
m_scene_uniforms.eye_position = m_view_camera.GetOrientation().eye;
m_scene_uniforms.light_position = m_light_camera.GetOrientation().eye;

m_sky_box.Update();
m_sky_box.Update(*frame.sky_box.uniforms_argument_binding_ptr);
return true;
}

Expand Down Expand Up @@ -453,7 +454,7 @@ bool AsteroidsApp::Render()

// Draw planet and sky-box after asteroids to minimize pixel overdraw
m_planet_ptr->Draw(frame.final_cmd_list, frame.planet, GetViewState());
m_sky_box.Draw(frame.final_cmd_list, frame.sky_box, GetViewState());
m_sky_box.Draw(frame.final_cmd_list, frame.sky_box.program_bindings, GetViewState());

RenderOverlay(frame.final_cmd_list);
frame.final_cmd_list.Commit();
Expand Down Expand Up @@ -537,7 +538,7 @@ void AsteroidsApp::SetParallelRenderingEnabled(bool is_parallel_rendering_enable
AsteroidsArray& AsteroidsApp::GetAsteroidsArray() const
{
META_FUNCTION_TASK();
META_CHECK_ARG_NOT_NULL(m_asteroids_array_ptr);
META_CHECK_NOT_NULL(m_asteroids_array_ptr);
return *m_asteroids_array_ptr;
}

Expand Down
9 changes: 7 additions & 2 deletions App/AsteroidsApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ namespace pin = Platform::Input;

struct AsteroidsFrame final : gfx::AppFrame
{
struct MeshBufferBindings : gfx::MeshBufferBindings
{
rhi::IProgramArgumentBinding* uniforms_argument_binding_ptr;
};

rhi::RenderPass asteroids_pass;
rhi::ParallelRenderCommandList parallel_cmd_list;
rhi::RenderCommandList serial_cmd_list;
rhi::RenderCommandList final_cmd_list;
rhi::CommandListSet execute_cmd_list_set;
rhi::Buffer scene_uniforms_buffer;
gfx::MeshBufferBindings sky_box;
gfx::MeshBufferBindings planet;
MeshBufferBindings sky_box;
MeshBufferBindings planet;
gfx::InstancedMeshBufferBindings asteroids;

using gfx::AppFrame::AppFrame;
Expand Down
4 changes: 2 additions & 2 deletions App/AsteroidsAppController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void AsteroidsAppController::OnKeyboardStateAction(AsteroidsAppAction action)
break;

default:
META_UNEXPECTED_ARG(action);
META_UNEXPECTED(action);
}
}

Expand All @@ -115,7 +115,7 @@ std::string AsteroidsAppController::GetKeyboardActionName(AsteroidsAppAction act
case AsteroidsAppAction::SetComplexity7: return "set 7 scene complexity";
case AsteroidsAppAction::SetComplexity8: return "set 8 scene complexity";
case AsteroidsAppAction::SetComplexity9: return "set 9 scene complexity";
default: META_UNEXPECTED_ARG_RETURN(action, "");
default: META_UNEXPECTED_RETURN(action, "");
}
}

Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.21.0)

# Methane version, build & product info
set(ASTEROIDS_VERSION_MAJOR 0 CACHE STRING "Asteroids major version")
set(ASTEROIDS_VERSION_MINOR 7 CACHE STRING "Asteroids minor version")
set(ASTEROIDS_VERSION_PATCH 3 CACHE STRING "Asteroids patch version")
set(ASTEROIDS_VERSION_MINOR 8 CACHE STRING "Asteroids minor version")
set(ASTEROIDS_VERSION_PATCH 0 CACHE STRING "Asteroids patch version")
set(ASTEROIDS_VERSION_BUILD 0 CACHE STRING "Asteroids build version")

project(METHANE_ASTEROIDS
Expand Down
1 change: 1 addition & 0 deletions Externals/CMRC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CPMAddPackage(
NAME CMRC
GITHUB_REPOSITORY MethanePowered/CMRC
GIT_TAG 2.0.2
DOWNLOAD_ONLY YES
)

list(APPEND CMAKE_MODULE_PATH "${CMRC_SOURCE_DIR}")
9 changes: 5 additions & 4 deletions Externals/CPM.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(CPM_DOWNLOAD_VERSION 0.40.0)
set(CPM_DOWNLOAD_VERSION 0.40.5)

if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
Expand All @@ -14,9 +14,9 @@ get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)
function(download_cpm)
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endfunction()

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
Expand All @@ -27,6 +27,7 @@ else()
if("${check}" STREQUAL "")
download_cpm()
endif()
unset(check)
endif()

include(${CPM_DOWNLOAD_LOCATION})
1 change: 1 addition & 0 deletions Externals/FastNoise2.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CPMAddPackage(
NAME FastNoise2
GITHUB_REPOSITORY MethanePowered/FastNoise2
GIT_TAG f8facbad699a51f0b5a0800223d0813cca1d34be
VERSION 0.10.0-alpha
OPTIONS
"FASTNOISE2_NOISETOOL OFF"
Expand Down
1 change: 0 additions & 1 deletion Externals/MethaneKit.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
CPMAddPackage(
NAME MethaneKit
GITHUB_REPOSITORY MethanePowered/MethaneKit
GIT_TAG 4c66c858c5b08f4e20717c4e5f4e09b641a42c73 # Last commit on master branch with latest hot-fixes for v0.7.3
VERSION ${ASTEROIDS_VERSION_MAJOR}.${ASTEROIDS_VERSION_MINOR}.${ASTEROIDS_VERSION_PATCH}
OPTIONS
"METHANE_APPS_BUILD_ENABLED OFF"
Expand Down
10 changes: 3 additions & 7 deletions Externals/iOS-Toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,8 @@ elseif(DEFINED CMAKE_OSX_SYSROOT_INT)
endif()

# Use bitcode or not
if(NOT DEFINED ENABLE_BITCODE AND NOT ARCHS MATCHES "((^|;|, )(i386|x86_64))+")
# Unless specified, enable bitcode support by default
message(STATUS "[DEFAULTS] Enabling bitcode support by default. ENABLE_BITCODE not provided!")
set(ENABLE_BITCODE ON)
elseif(NOT DEFINED ENABLE_BITCODE)
message(STATUS "[DEFAULTS] Disabling bitcode support by default on simulators. ENABLE_BITCODE not provided for override!")
if(NOT DEFINED ENABLE_BITCODE)
message(STATUS "[DEFAULTS] Disabling bitcode support by default.")
set(ENABLE_BITCODE OFF)
endif()
set(ENABLE_BITCODE_INT ${ENABLE_BITCODE} CACHE BOOL
Expand Down Expand Up @@ -743,7 +739,7 @@ if(${CMAKE_VERSION} VERSION_LESS "3.11")
endif()
elseif(NOT PLATFORM_INT MATCHES "^MAC_CATALYST")
# Newer versions of CMake sets the version min flags correctly, skip this for Mac Catalyst targets
set(CMAKE_OSX_DEPLOYMENT_TARGET ${DEPLOYMENT_TARGET})
set(CMAKE_OSX_DEPLOYMENT_TARGET "${DEPLOYMENT_TARGET}")
endif()

if(DEFINED APPLE_TARGET_TRIPLE_INT)
Expand Down
12 changes: 6 additions & 6 deletions Modules/Simulation/Asteroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ Asteroid::Colors Asteroid::GetAsteroidRockColors(uint32_t deep_color_index, uint
} };
static const AsteroidColorSchema s_linear_shallow_rock_colors = TransformSrgbToLinear(s_srgb_shallow_rock_colors);

META_CHECK_ARG_LESS(deep_color_index, s_linear_deep_rock_colors.size());
META_CHECK_ARG_LESS(shallow_color_index, s_linear_shallow_rock_colors.size());
META_CHECK_LESS(deep_color_index, s_linear_deep_rock_colors.size());
META_CHECK_LESS(shallow_color_index, s_linear_shallow_rock_colors.size());
return Asteroid::Colors{ s_linear_deep_rock_colors[deep_color_index], s_linear_shallow_rock_colors[shallow_color_index] };
}

Expand Down Expand Up @@ -207,8 +207,8 @@ Asteroid::Colors Asteroid::GetAsteroidIceColors(uint32_t deep_color_index, uint3
} };
static const AsteroidColorSchema s_linear_shallow_ice_colors = TransformSrgbToLinear(s_srgb_shallow_ice_colors);

META_CHECK_ARG_LESS(deep_color_index, s_linear_deep_ice_colors.size());
META_CHECK_ARG_LESS(shallow_color_index, s_linear_shallow_ice_colors.size());
META_CHECK_LESS(deep_color_index, s_linear_deep_ice_colors.size());
META_CHECK_LESS(shallow_color_index, s_linear_shallow_ice_colors.size());
return Asteroid::Colors{ s_linear_deep_ice_colors[deep_color_index], s_linear_shallow_ice_colors[shallow_color_index] };
}

Expand All @@ -235,8 +235,8 @@ Asteroid::Colors Asteroid::GetAsteroidLodColors(uint32_t lod_index)
} };
static const AsteroidColorSchema s_linear_lod_shallow_colors = TransformSrgbToLinear(s_srgb_lod_shallow_colors);

META_CHECK_ARG_LESS(lod_index, s_linear_lod_deep_colors.size());
META_CHECK_ARG_LESS(lod_index, s_linear_lod_shallow_colors.size());
META_CHECK_LESS(lod_index, s_linear_lod_deep_colors.size());
META_CHECK_LESS(lod_index, s_linear_lod_shallow_colors.size());
return Asteroid::Colors{ s_linear_lod_deep_colors[lod_index], s_linear_lod_shallow_colors[lod_index] };
}

Expand Down
48 changes: 21 additions & 27 deletions Modules/Simulation/AsteroidsArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,27 @@ AsteroidsArray::UberMesh::UberMesh(tf::Executor& parallel_executor, uint32_t ins
uint32_t AsteroidsArray::UberMesh::GetSubsetIndex(uint32_t instance_index, uint32_t subdivision_index) const
{
META_FUNCTION_TASK();
META_CHECK_ARG_LESS(instance_index, m_instance_count);
META_CHECK_ARG_LESS(subdivision_index, m_subdivisions_count);
META_CHECK_LESS(instance_index, m_instance_count);
META_CHECK_LESS(subdivision_index, m_subdivisions_count);

return subdivision_index * m_instance_count + instance_index;
}

uint32_t AsteroidsArray::UberMesh::GetSubsetSubdivision(uint32_t subset_index) const
{
META_FUNCTION_TASK();
META_CHECK_ARG_LESS(subset_index, GetSubsetCount());
META_CHECK_LESS(subset_index, GetSubsetCount());

const uint32_t subdivision_index = subset_index / m_instance_count;
META_CHECK_ARG_LESS(subdivision_index, m_subdivisions_count);
META_CHECK_LESS(subdivision_index, m_subdivisions_count);

return subdivision_index;
}

const Asteroid::Mesh::DepthRange& AsteroidsArray::UberMesh::GetSubsetDepthRange(uint32_t subset_index) const
{
META_FUNCTION_TASK();
META_CHECK_ARG_LESS(subset_index, GetSubsetCount());
META_CHECK_LESS(subset_index, GetSubsetCount());
assert(subset_index < m_depth_ranges.size());
return m_depth_ranges[subset_index];
}
Expand Down Expand Up @@ -255,13 +255,7 @@ AsteroidsArray::AsteroidsArray(const rhi::CommandQueue& render_cmd_queue,
},
rhi::Program::ArgumentAccessors
{
{ { rhi::ShaderType::All, "g_mesh_uniforms" }, rhi::Program::ArgumentAccessor::Type::Mutable, true },
{ { rhi::ShaderType::All, "g_scene_uniforms" }, rhi::Program::ArgumentAccessor::Type::FrameConstant },
{ { rhi::ShaderType::Pixel, "g_constants" }, rhi::Program::ArgumentAccessor::Type::Constant },
{ { rhi::ShaderType::Pixel, "g_texture_sampler"}, rhi::Program::ArgumentAccessor::Type::Constant },
{ { rhi::ShaderType::Pixel, "g_face_textures" }, m_settings.textures_array_enabled
? rhi::Program::ArgumentAccessor::Type::Constant
: rhi::Program::ArgumentAccessor::Type::Mutable },
META_PROGRAM_ARG_BUFFER_ADDRESS_MUTABLE(rhi::ShaderType::All, "g_mesh_uniforms")
},
render_pattern.GetAttachmentFormats()
}
Expand Down Expand Up @@ -294,7 +288,7 @@ AsteroidsArray::AsteroidsArray(const rhi::CommandQueue& render_cmd_queue,
for (uint32_t subset_index = 0; subset_index < m_content_state_ptr->mesh_subset_texture_indices.size(); ++subset_index)
{
const uint32_t subset_texture_index = m_content_state_ptr->mesh_subset_texture_indices[subset_index];
META_CHECK_ARG_LESS(subset_texture_index, m_unique_textures.size());
META_CHECK_LESS(subset_texture_index, m_unique_textures.size());
SetSubsetTexture(m_unique_textures[subset_texture_index], subset_index);
}

Expand Down Expand Up @@ -327,11 +321,11 @@ std::vector<rhi::ProgramBindings> AsteroidsArray::CreateProgramBindings(const rh

program_bindings_array.resize(m_settings.instance_count);
program_bindings_array[0] = m_render_state.GetProgram().CreateBindings({
{ { rhi::ShaderType::All, "g_mesh_uniforms" }, { { asteroids_uniforms_buffer.GetInterface(), GetUniformsBufferOffset(0), uniform_data_size } } },
{ { rhi::ShaderType::All, "g_scene_uniforms" }, { { scene_uniforms_buffer.GetInterface() } } },
{ { rhi::ShaderType::Pixel, "g_constants" }, { { constants_buffer.GetInterface() } } },
{ { rhi::ShaderType::Pixel, "g_face_textures" }, face_texture_locations },
{ { rhi::ShaderType::Pixel, "g_texture_sampler"}, { { m_texture_sampler.GetInterface() } } },
{ { rhi::ShaderType::All, "g_mesh_uniforms" }, asteroids_uniforms_buffer.GetBufferView(GetUniformsBufferOffset(0), uniform_data_size) },
{ { rhi::ShaderType::All, "g_scene_uniforms" }, scene_uniforms_buffer.GetResourceView() },
{ { rhi::ShaderType::Pixel, "g_constants" }, constants_buffer.GetResourceView() },
{ { rhi::ShaderType::Pixel, "g_face_textures" }, face_texture_locations },
{ { rhi::ShaderType::Pixel, "g_texture_sampler"}, m_texture_sampler.GetResourceView() },
}, frame_index);
program_bindings_array[0].SetName(fmt::format("Asteroids[0] Bindings {}", frame_index));

Expand All @@ -341,14 +335,14 @@ std::vector<rhi::ProgramBindings> AsteroidsArray::CreateProgramBindings(const rh
{
META_UNUSED(uniform_data_size); // workaround for Clang error unused-lambda-capture uniform_data_size (false positive)
const Data::Size asteroid_uniform_offset = GetUniformsBufferOffset(asteroid_index);
META_CHECK_ARG_EQUAL(asteroid_uniform_offset % 256, 0);
rhi::ProgramBindings::ResourceViewsByArgument set_resource_view_by_argument{
{ { rhi::ShaderType::All, "g_mesh_uniforms" }, { { asteroids_uniforms_buffer.GetInterface(), asteroid_uniform_offset, uniform_data_size } } },
META_CHECK_EQUAL(asteroid_uniform_offset % 256, 0);
rhi::ProgramBindingValueByArgument set_resource_view_by_argument{
{ { rhi::ShaderType::All, "g_mesh_uniforms" }, asteroids_uniforms_buffer.GetBufferView(asteroid_uniform_offset, uniform_data_size) }
};
if (!m_settings.textures_array_enabled)
{
set_resource_view_by_argument.insert(
{ { rhi::ShaderType::Pixel, "g_face_textures" }, { { GetInstanceTexture(asteroid_index).GetInterface() } } }
{ { rhi::ShaderType::Pixel, "g_face_textures" }, GetInstanceTexture(asteroid_index).GetResourceView() }
);
}
program_bindings_array[asteroid_index] = rhi::ProgramBindings(program_bindings_array[0], set_resource_view_by_argument, frame_index);
Expand Down Expand Up @@ -385,7 +379,7 @@ void AsteroidsArray::Draw(const rhi::RenderCommandList& cmd_list,
{
META_FUNCTION_TASK();
META_SCOPE_TIMER("AsteroidsArray::Draw");
META_CHECK_ARG_GREATER_OR_EQUAL(buffer_bindings.uniforms_buffer.GetDataSize(), GetUniformsBufferSize());
META_CHECK_GREATER_OR_EQUAL(buffer_bindings.uniforms_buffer.GetDataSize(), GetUniformsBufferSize());

// Upload uniforms buffer data to GPU asynchronously while encoding drawing commands on CPU
auto uniforms_update_future = std::async([this, &buffer_bindings]() {
Expand All @@ -396,7 +390,7 @@ void AsteroidsArray::Draw(const rhi::RenderCommandList& cmd_list,
cmd_list.ResetWithState(m_render_state, &s_debug_group);
cmd_list.SetViewState(view_state);

META_CHECK_ARG_EQUAL(buffer_bindings.program_bindings_per_instance.size(), m_settings.instance_count);
META_CHECK_EQUAL(buffer_bindings.program_bindings_per_instance.size(), m_settings.instance_count);
BaseBuffers::Draw(
cmd_list,
buffer_bindings.program_bindings_per_instance,
Expand All @@ -416,7 +410,7 @@ void AsteroidsArray::DrawParallel(const rhi::ParallelRenderCommandList& parallel
{
META_FUNCTION_TASK();
META_SCOPE_TIMER("AsteroidsArray::DrawParallel");
META_CHECK_ARG_GREATER_OR_EQUAL(buffer_bindings.uniforms_buffer.GetDataSize(), GetUniformsBufferSize());
META_CHECK_GREATER_OR_EQUAL(buffer_bindings.uniforms_buffer.GetDataSize(), GetUniformsBufferSize());

// Upload uniforms buffer data to GPU asynchronously while encoding drawing commands on CPU
auto uniforms_update_future = std::async([this, &buffer_bindings]() {
Expand All @@ -427,7 +421,7 @@ void AsteroidsArray::DrawParallel(const rhi::ParallelRenderCommandList& parallel
parallel_cmd_list.ResetWithState(m_render_state, &s_debug_group);
parallel_cmd_list.SetViewState(view_state);

META_CHECK_ARG_EQUAL(buffer_bindings.program_bindings_per_instance.size(), m_settings.instance_count);
META_CHECK_EQUAL(buffer_bindings.program_bindings_per_instance.size(), m_settings.instance_count);
BaseBuffers::DrawParallel(
parallel_cmd_list,
buffer_bindings.program_bindings_per_instance,
Expand All @@ -453,7 +447,7 @@ void AsteroidsArray::SetMinMeshLodScreenSize(float mesh_lod_min_screen_size)
uint32_t AsteroidsArray::GetSubsetByInstanceIndex(uint32_t instance_index) const
{
META_FUNCTION_TASK();
META_CHECK_ARG_LESS(instance_index, m_mesh_subset_by_instance_index.size());
META_CHECK_LESS(instance_index, m_mesh_subset_by_instance_index.size());
return m_mesh_subset_by_instance_index[instance_index];
}

Expand Down
Loading

0 comments on commit c00fac8

Please sign in to comment.