Skip to content

Commit

Permalink
preparation for metal implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wkjarosz committed Dec 31, 2023
1 parent da114da commit a6e3e93
Show file tree
Hide file tree
Showing 11 changed files with 1,108 additions and 102 deletions.
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ project(
SamplinSafari
DESCRIPTION "A research tool to visualize and interactively inspect high-dimensional (quasi) Monte Carlo samplers."
VERSION ${VERSION}
LANGUAGES C CXX
LANGUAGES C CXX OBJC
)

message(STATUS "C++ compiler is: ${CMAKE_CXX_COMPILER_ID}")
Expand Down Expand Up @@ -178,8 +178,9 @@ if(portable-file-dialogs_ADDED)
target_include_directories(portable-file-dialogs INTERFACE "${portable-file-dialogs_SOURCE_DIR}")
endif()

set(HELLOIMGUI_WITH_GLFW ON)
CPMAddPackage("gh:wkjarosz/hello_imgui#745ced9d52be601097e4b7d0837ad67a0b93db32")
# set(HELLOIMGUI_USE_GLFW_METAL ON)
set(HELLOIMGUI_USE_GLFW_OPENGL3 ON)
CPMAddPackage("gh:pthom/hello_imgui#0c8e5e848738dd7160485d96da3aa9e9033c62ad")
if(hello_imgui_ADDED)
message(STATUS "hello_imgui library added")
endif()
Expand Down Expand Up @@ -334,9 +335,13 @@ hello_imgui_add_app(
SamplinSafari
src/app.cpp
${CMAKE_CURRENT_BINARY_DIR}/src/common.cpp
src/opengl_check.cpp
src/shader.cpp
src/shader_gl.cpp
src/shader_metal.mm
src/export_to_file.cpp
src/renderpass_gl.cpp
src/renderpass_metal.mm
ASSETS_LOCATION
${CMAKE_CURRENT_BINARY_DIR}/assets
)
Expand Down
7 changes: 4 additions & 3 deletions include/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ using namespace linalg::aliases;
#include "arcball.h"
#include "hello_imgui/hello_imgui.h"
#include "misc/cpp/imgui_stdlib.h"
#include "renderpass.h"
#include "shader.h"
#include <galois++/array2d.h>
#include <map>
Expand Down Expand Up @@ -111,7 +112,7 @@ class SampleViewer
void draw_text(const int2 &pos, const std::string &text, const float4 &col, ImFont *font = nullptr,
int align = TextAlign_RIGHT | TextAlign_BOTTOM) const;
void draw_points(const float4x4 &mvp, const float4x4 &smash, const float3 &color);
void draw_grid(const float4x4 &mat, int2 size, float alpha) const;
void draw_grid(const float4x4 &mat, int2 size, float alpha);
void draw_trigrid(Shader *shader, const float4x4 &mvp, float alpha, const int2x3 &count);
void draw_2D_points_and_grid(const float4x4 &mvp, int2 dims, int plotIndex);
int2 get_draw_range() const;
Expand Down Expand Up @@ -139,9 +140,9 @@ class SampleViewer
bool m_show_1d_projections = false, m_show_point_nums = false, m_show_point_coords = false,
m_show_coarse_grid = false, m_show_fine_grid = false, m_show_custom_grid = false, m_show_bbox = false;

Shader *m_3d_point_shader = nullptr, *m_2d_point_shader = nullptr, *m_grid_shader = nullptr;
RenderPass m_render_pass;
Shader *m_3d_point_shader = nullptr, *m_2d_point_shader = nullptr, *m_grid_shader = nullptr;

int2 m_viewport_pos, m_viewport_pos_GL, m_viewport_size;
float m_animate_start_time = 0.0f;

bool m_subset_by_index = false;
Expand Down
19 changes: 19 additions & 0 deletions include/opengl_check.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
\file opengl_check.h
*/
#pragma once

bool check_glerror(const char *cmd);

#if defined(NDEBUG)
#define CHK(cmd) cmd
#else
#define CHK(cmd) \
do \
{ \
cmd; \
while (check_glerror(#cmd)) \
{ \
} \
} while (false)
#endif
154 changes: 154 additions & 0 deletions include/renderpass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/**
\file renderpass.h
*/
#pragma once

#include "linalg.h"
#include <unordered_map>

using namespace linalg::aliases;

/// An abstraction for rendering passes that work with OpenGL, OpenGL ES, and Metal.
/*
This is a greatly simplified version of NanoGUI's RenderPass class. Original copyright follows.
----------
NanoGUI was developed by Wenzel Jakob <[email protected]>.
The widget drawing code is based on the NanoVG demo application
by Mikko Mononen.
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE.txt file.
*/
class RenderPass
{
public:
/// Depth test
enum class DepthTest
{
Never,
Less,
Equal,
LessEqual,
Greater,
NotEqual,
GreaterEqual,
Always
};

/// Culling mode
enum class CullMode
{
Disabled,
Front,
Back
};

/**
* Create a new render pass for rendering to the main color and (optionally) depth buffer.
*
* \param write_depth
* Should we write to the depth buffer?
*
* \param clear
* Should \ref enter() begin by clearing all buffers?
*/
RenderPass(bool write_depth = true, bool clear = true);

~RenderPass();

/**
* Begin the render pass
*
* The specified drawing state (e.g. depth tests, culling mode, blending mode) are automatically set up at this
* point. Later changes between \ref begin() and \ref end() are possible but cause additional OpenGL/GLES/Metal API
* calls.
*/
void begin();

/// Finish the render pass
void end();

/// Return the clear color for a given color attachment
const float4 &clear_color() const
{
return m_clear_color;
}

/// Set the clear color for a given color attachment
void set_clear_color(const float4 &color);

/// Return the clear depth for the depth attachment
float clear_depth() const
{
return m_clear_depth;
}

/// Set the clear depth for the depth attachment
void set_clear_depth(float depth);

/// Specify the depth test and depth write mask of this render pass
void set_depth_test(DepthTest depth_test, bool depth_write);

/// Return the depth test and depth write mask of this render pass
std::pair<DepthTest, bool> depth_test() const
{
return {m_depth_test, m_depth_write};
}

/// Set the pixel offset and size of the viewport region
void set_viewport(const int2 &offset, const int2 &size);

/// Return the pixel offset and size of the viewport region
std::pair<int2, int2> viewport()
{
return {m_viewport_offset, m_viewport_size};
}

/// Specify the culling mode associated with the render pass
void set_cull_mode(CullMode mode);

/// Return the culling mode associated with the render pass
CullMode cull_mode() const
{
return m_cull_mode;
}

/// Resize all texture targets attached to the render pass
void resize(const int2 &size);

#if defined(HELLOIMGUI_HAS_METAL)
void *command_encoder() const
{
return m_command_encoder;
}
void *command_buffer() const
{
return m_command_buffer;
}
#endif

protected:
bool m_clear;
float4 m_clear_color;
float m_clear_depth;
int2 m_viewport_offset;
int2 m_viewport_size;
int2 m_framebuffer_size;
DepthTest m_depth_test;
bool m_depth_write;
CullMode m_cull_mode;
bool m_active;
#if defined(HELLOIMGUI_HAS_OPENGL)
int4 m_viewport_backup, m_scissor_backup;
bool m_depth_test_backup;
bool m_depth_write_backup;
bool m_scissor_test_backup;
bool m_cull_face_backup;
bool m_blend_backup;
#elif defined(HELLOIMGUI_HAS_METAL)
void *m_command_buffer;
void *m_command_encoder;
void *m_pass_descriptor;
// ref<Shader> m_clear_shader;
#endif
};
27 changes: 12 additions & 15 deletions include/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <string>
#include <unordered_map>

class RenderPass;

/// An abstraction for shaders that work with OpenGL, OpenGL ES, and (at some point down the road, hopefully) Metal.
/*
This is adapted from NanoGUI's Shader class. Copyright follows.
Expand Down Expand Up @@ -44,6 +46,9 @@ class Shader
/**
Initialize the shader using the source files (read from the assets directory).
\param render_pass
RenderPass object encoding targets to which color and depth information will be rendered.
\param name
A name identifying this shader
Expand All @@ -53,11 +58,14 @@ class Shader
\param fs_filename
Filename of the fragment shader source code.
*/
Shader(const std::string &name, const std::string &vs_filename, const std::string &fs_filename,
BlendMode blend_mode = BlendMode::None);
Shader(RenderPass *render_pass, const std::string &name, const std::string &vs_filename,
const std::string &fs_filename, BlendMode blend_mode = BlendMode::None);

/// Return the render pass associated with this shader
// RenderPass *render_pass() { return m_render_pass; }
RenderPass *render_pass()
{
return m_render_pass;
}

/// Return the name of this shader
const std::string &name() const
Expand Down Expand Up @@ -256,7 +264,7 @@ class Shader
virtual ~Shader();

protected:
// RenderPass* m_render_pass;
RenderPass *m_render_pass;
std::string m_name;
std::unordered_map<std::string, Buffer> m_buffers;
BlendMode m_blend_mode;
Expand All @@ -271,14 +279,3 @@ class Shader
void *m_pipeline_state;
#endif
};

bool check_glerror(const char *cmd);

#define CHK(cmd) \
do \
{ \
cmd; \
while (check_glerror(#cmd)) \
{ \
} \
} while (false)
Loading

0 comments on commit a6e3e93

Please sign in to comment.