Skip to content

Commit a6e3e93

Browse files
committed
preparation for metal implementation
1 parent da114da commit a6e3e93

11 files changed

+1108
-102
lines changed

CMakeLists.txt

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ project(
1313
SamplinSafari
1414
DESCRIPTION "A research tool to visualize and interactively inspect high-dimensional (quasi) Monte Carlo samplers."
1515
VERSION ${VERSION}
16-
LANGUAGES C CXX
16+
LANGUAGES C CXX OBJC
1717
)
1818

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

181-
set(HELLOIMGUI_WITH_GLFW ON)
182-
CPMAddPackage("gh:wkjarosz/hello_imgui#745ced9d52be601097e4b7d0837ad67a0b93db32")
181+
# set(HELLOIMGUI_USE_GLFW_METAL ON)
182+
set(HELLOIMGUI_USE_GLFW_OPENGL3 ON)
183+
CPMAddPackage("gh:pthom/hello_imgui#0c8e5e848738dd7160485d96da3aa9e9033c62ad")
183184
if(hello_imgui_ADDED)
184185
message(STATUS "hello_imgui library added")
185186
endif()
@@ -334,9 +335,13 @@ hello_imgui_add_app(
334335
SamplinSafari
335336
src/app.cpp
336337
${CMAKE_CURRENT_BINARY_DIR}/src/common.cpp
338+
src/opengl_check.cpp
337339
src/shader.cpp
338340
src/shader_gl.cpp
341+
src/shader_metal.mm
339342
src/export_to_file.cpp
343+
src/renderpass_gl.cpp
344+
src/renderpass_metal.mm
340345
ASSETS_LOCATION
341346
${CMAKE_CURRENT_BINARY_DIR}/assets
342347
)

include/app.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ using namespace linalg::aliases;
3737
#include "arcball.h"
3838
#include "hello_imgui/hello_imgui.h"
3939
#include "misc/cpp/imgui_stdlib.h"
40+
#include "renderpass.h"
4041
#include "shader.h"
4142
#include <galois++/array2d.h>
4243
#include <map>
@@ -111,7 +112,7 @@ class SampleViewer
111112
void draw_text(const int2 &pos, const std::string &text, const float4 &col, ImFont *font = nullptr,
112113
int align = TextAlign_RIGHT | TextAlign_BOTTOM) const;
113114
void draw_points(const float4x4 &mvp, const float4x4 &smash, const float3 &color);
114-
void draw_grid(const float4x4 &mat, int2 size, float alpha) const;
115+
void draw_grid(const float4x4 &mat, int2 size, float alpha);
115116
void draw_trigrid(Shader *shader, const float4x4 &mvp, float alpha, const int2x3 &count);
116117
void draw_2D_points_and_grid(const float4x4 &mvp, int2 dims, int plotIndex);
117118
int2 get_draw_range() const;
@@ -139,9 +140,9 @@ class SampleViewer
139140
bool m_show_1d_projections = false, m_show_point_nums = false, m_show_point_coords = false,
140141
m_show_coarse_grid = false, m_show_fine_grid = false, m_show_custom_grid = false, m_show_bbox = false;
141142

142-
Shader *m_3d_point_shader = nullptr, *m_2d_point_shader = nullptr, *m_grid_shader = nullptr;
143+
RenderPass m_render_pass;
144+
Shader *m_3d_point_shader = nullptr, *m_2d_point_shader = nullptr, *m_grid_shader = nullptr;
143145

144-
int2 m_viewport_pos, m_viewport_pos_GL, m_viewport_size;
145146
float m_animate_start_time = 0.0f;
146147

147148
bool m_subset_by_index = false;

include/opengl_check.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
\file opengl_check.h
3+
*/
4+
#pragma once
5+
6+
bool check_glerror(const char *cmd);
7+
8+
#if defined(NDEBUG)
9+
#define CHK(cmd) cmd
10+
#else
11+
#define CHK(cmd) \
12+
do \
13+
{ \
14+
cmd; \
15+
while (check_glerror(#cmd)) \
16+
{ \
17+
} \
18+
} while (false)
19+
#endif

include/renderpass.h

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
\file renderpass.h
3+
*/
4+
#pragma once
5+
6+
#include "linalg.h"
7+
#include <unordered_map>
8+
9+
using namespace linalg::aliases;
10+
11+
/// An abstraction for rendering passes that work with OpenGL, OpenGL ES, and Metal.
12+
/*
13+
This is a greatly simplified version of NanoGUI's RenderPass class. Original copyright follows.
14+
----------
15+
NanoGUI was developed by Wenzel Jakob <[email protected]>.
16+
The widget drawing code is based on the NanoVG demo application
17+
by Mikko Mononen.
18+
19+
All rights reserved. Use of this source code is governed by a
20+
BSD-style license that can be found in the LICENSE.txt file.
21+
*/
22+
class RenderPass
23+
{
24+
public:
25+
/// Depth test
26+
enum class DepthTest
27+
{
28+
Never,
29+
Less,
30+
Equal,
31+
LessEqual,
32+
Greater,
33+
NotEqual,
34+
GreaterEqual,
35+
Always
36+
};
37+
38+
/// Culling mode
39+
enum class CullMode
40+
{
41+
Disabled,
42+
Front,
43+
Back
44+
};
45+
46+
/**
47+
* Create a new render pass for rendering to the main color and (optionally) depth buffer.
48+
*
49+
* \param write_depth
50+
* Should we write to the depth buffer?
51+
*
52+
* \param clear
53+
* Should \ref enter() begin by clearing all buffers?
54+
*/
55+
RenderPass(bool write_depth = true, bool clear = true);
56+
57+
~RenderPass();
58+
59+
/**
60+
* Begin the render pass
61+
*
62+
* The specified drawing state (e.g. depth tests, culling mode, blending mode) are automatically set up at this
63+
* point. Later changes between \ref begin() and \ref end() are possible but cause additional OpenGL/GLES/Metal API
64+
* calls.
65+
*/
66+
void begin();
67+
68+
/// Finish the render pass
69+
void end();
70+
71+
/// Return the clear color for a given color attachment
72+
const float4 &clear_color() const
73+
{
74+
return m_clear_color;
75+
}
76+
77+
/// Set the clear color for a given color attachment
78+
void set_clear_color(const float4 &color);
79+
80+
/// Return the clear depth for the depth attachment
81+
float clear_depth() const
82+
{
83+
return m_clear_depth;
84+
}
85+
86+
/// Set the clear depth for the depth attachment
87+
void set_clear_depth(float depth);
88+
89+
/// Specify the depth test and depth write mask of this render pass
90+
void set_depth_test(DepthTest depth_test, bool depth_write);
91+
92+
/// Return the depth test and depth write mask of this render pass
93+
std::pair<DepthTest, bool> depth_test() const
94+
{
95+
return {m_depth_test, m_depth_write};
96+
}
97+
98+
/// Set the pixel offset and size of the viewport region
99+
void set_viewport(const int2 &offset, const int2 &size);
100+
101+
/// Return the pixel offset and size of the viewport region
102+
std::pair<int2, int2> viewport()
103+
{
104+
return {m_viewport_offset, m_viewport_size};
105+
}
106+
107+
/// Specify the culling mode associated with the render pass
108+
void set_cull_mode(CullMode mode);
109+
110+
/// Return the culling mode associated with the render pass
111+
CullMode cull_mode() const
112+
{
113+
return m_cull_mode;
114+
}
115+
116+
/// Resize all texture targets attached to the render pass
117+
void resize(const int2 &size);
118+
119+
#if defined(HELLOIMGUI_HAS_METAL)
120+
void *command_encoder() const
121+
{
122+
return m_command_encoder;
123+
}
124+
void *command_buffer() const
125+
{
126+
return m_command_buffer;
127+
}
128+
#endif
129+
130+
protected:
131+
bool m_clear;
132+
float4 m_clear_color;
133+
float m_clear_depth;
134+
int2 m_viewport_offset;
135+
int2 m_viewport_size;
136+
int2 m_framebuffer_size;
137+
DepthTest m_depth_test;
138+
bool m_depth_write;
139+
CullMode m_cull_mode;
140+
bool m_active;
141+
#if defined(HELLOIMGUI_HAS_OPENGL)
142+
int4 m_viewport_backup, m_scissor_backup;
143+
bool m_depth_test_backup;
144+
bool m_depth_write_backup;
145+
bool m_scissor_test_backup;
146+
bool m_cull_face_backup;
147+
bool m_blend_backup;
148+
#elif defined(HELLOIMGUI_HAS_METAL)
149+
void *m_command_buffer;
150+
void *m_command_encoder;
151+
void *m_pass_descriptor;
152+
// ref<Shader> m_clear_shader;
153+
#endif
154+
};

include/shader.h

+12-15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <string>
99
#include <unordered_map>
1010

11+
class RenderPass;
12+
1113
/// An abstraction for shaders that work with OpenGL, OpenGL ES, and (at some point down the road, hopefully) Metal.
1214
/*
1315
This is adapted from NanoGUI's Shader class. Copyright follows.
@@ -44,6 +46,9 @@ class Shader
4446
/**
4547
Initialize the shader using the source files (read from the assets directory).
4648
49+
\param render_pass
50+
RenderPass object encoding targets to which color and depth information will be rendered.
51+
4752
\param name
4853
A name identifying this shader
4954
@@ -53,11 +58,14 @@ class Shader
5358
\param fs_filename
5459
Filename of the fragment shader source code.
5560
*/
56-
Shader(const std::string &name, const std::string &vs_filename, const std::string &fs_filename,
57-
BlendMode blend_mode = BlendMode::None);
61+
Shader(RenderPass *render_pass, const std::string &name, const std::string &vs_filename,
62+
const std::string &fs_filename, BlendMode blend_mode = BlendMode::None);
5863

5964
/// Return the render pass associated with this shader
60-
// RenderPass *render_pass() { return m_render_pass; }
65+
RenderPass *render_pass()
66+
{
67+
return m_render_pass;
68+
}
6169

6270
/// Return the name of this shader
6371
const std::string &name() const
@@ -256,7 +264,7 @@ class Shader
256264
virtual ~Shader();
257265

258266
protected:
259-
// RenderPass* m_render_pass;
267+
RenderPass *m_render_pass;
260268
std::string m_name;
261269
std::unordered_map<std::string, Buffer> m_buffers;
262270
BlendMode m_blend_mode;
@@ -271,14 +279,3 @@ class Shader
271279
void *m_pipeline_state;
272280
#endif
273281
};
274-
275-
bool check_glerror(const char *cmd);
276-
277-
#define CHK(cmd) \
278-
do \
279-
{ \
280-
cmd; \
281-
while (check_glerror(#cmd)) \
282-
{ \
283-
} \
284-
} while (false)

0 commit comments

Comments
 (0)