cgv
Loading...
Searching...
No Matches
a_buffer.cxx
1#include "a_buffer.h"
2
3namespace cgv {
4 namespace render {
5
6 void a_buffer::ensure_buffers(context& ctx)
7 {
8 unsigned num_pixels = ctx.get_width() * ctx.get_height();
9 // Generate buffers for the a-buffer transparency implementation
10 node_counter_buffer.create_or_resize(ctx, sizeof(GLuint));
11 head_pointer_buffer.create_or_resize(ctx, num_pixels * sizeof(GLuint));
12 node_buffer.create_or_resize(ctx, nodes_per_pixel * 3 * num_pixels * sizeof(GLuint));
13 }
14 void a_buffer::update_shader_program_options(shader_compile_options& options, bool include_binding_points)
15 {
16 options.define_macro_if_not_default("MAX_FRAGMENTS", fragments_per_pixel, 32u);
17 if(include_binding_points) {
18 options.define_macro_if_not_default("NODE_COUNTER_BINDING_POINT", node_counter_binding_point, 0);
19 options.define_macro_if_not_default("HEAD_POINTERS_BINDING_POINT", head_pointers_binding_point, 0);
20 options.define_macro_if_not_default("NODES_BINDING_POINT", nodes_binding_point, 1);
21 }
22 }
23 void a_buffer::update_shader_program_options(shader_compile_options& options)
24 {
25 update_shader_program_options(options, true);
26 }
27 a_buffer::a_buffer(unsigned _fragments_per_pixel, unsigned _nodes_per_pixel, int _depth_tex_unit,
28 int _node_counter_binding_point, int _head_pointers_binding_point, int _nodes_binding_point)
29 : depth_tex("[D]")
30 {
31 depth_tex.set_min_filter(TF_NEAREST);
32 depth_tex.set_mag_filter(TF_NEAREST);
33 fragments_per_pixel = _fragments_per_pixel;
34 nodes_per_pixel = _nodes_per_pixel;
35
36 depth_tex_unit = _depth_tex_unit;
37 node_counter_binding_point = _node_counter_binding_point;
38 head_pointers_binding_point = _head_pointers_binding_point;
39 nodes_binding_point = _nodes_binding_point;
40
41 init_frame_called = false;
42 }
44 {
45 if (!clear_ssbo_prog.build_files(ctx, "a_buffer_clear", true))
46 return false;
47 update_shader_program_options(prog_options, false);
48 if (!a_buffer_prog.build_program(ctx, "a_buffer.glpr", prog_options, true))
49 return false;
50 last_prog_options = prog_options;
51 return true;
52 }
54 {
55 clear_ssbo_prog.destruct(ctx);
56 a_buffer_prog.destruct(ctx);
59 head_pointer_buffer.destruct(ctx);
60 node_buffer.destruct(ctx);
61 }
63 {
64 // Ensure depth texture and node buffers of correct size
67
68 if (!depth_tex.is_created())
69 depth_tex.create(ctx, TT_2D, ctx.get_width(), ctx.get_height());
70
71 ensure_buffers(ctx);
72
73 // Clear the atomic counter
74 const GLuint zero = 0;
75 node_counter_buffer.replace(ctx, 0, &zero, 1);
76
77 // Clear the head pointer buffer using a compute shader
78 head_pointer_buffer.bind(ctx, 0);
79 unsigned buffer_size = ctx.get_width() * ctx.get_height();
80 clear_ssbo_prog.enable(ctx);
81 clear_ssbo_prog.set_uniform(ctx, "size", buffer_size);
82 clear_ssbo_prog.set_uniform(ctx, "clear_value", -1);
83 glDispatchCompute(GLuint(ceil(buffer_size / 4)), 1, 1);
84 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
85 clear_ssbo_prog.disable(ctx);
86 head_pointer_buffer.unbind(ctx, 0);
87
88 // Check if the shader program needs to be rebuilt
89 update_shader_program_options(prog_options, false);
90 if (prog_options != last_prog_options) {
91 if (a_buffer_prog.is_created())
92 a_buffer_prog.destruct(ctx);
93 if (a_buffer_prog.build_program(ctx, "a_buffer.glpr", prog_options, true)) {
94 last_prog_options = prog_options;
95 std::cout << "a_buffer: rebuilt shader program" << std::endl;
96 }
97 }
98 init_frame_called = true;
99 }
100 bool a_buffer::enable(context& ctx, shader_program& prog, int tex_unit)
101 {
102 // in first call after init_frame, copy depth buffer to depth texture
103 if (init_frame_called) {
104 depth_tex.replace_from_buffer(ctx, 0, 0, 0, 0, ctx.get_width(), ctx.get_height());
105 init_frame_called = false;
106 }
107 // set program uniforms
108 if (!prog.set_uniform(ctx, "viewport_dims", ivec2(ctx.get_width(), ctx.get_height()))) {
109 std::cerr << "ERROR in a_buffer::enable(): uniform ivec2 viewport_dims not found in program." << std::endl;
110 return false;
111 }
112 if (!prog.set_uniform(ctx, "nodes_per_pixel", (int)nodes_per_pixel)) {
113 std::cerr << "ERROR in a_buffer::enable(): uniform int nodes_per_pixel not found in program." << std::endl;
114 return false;
115 }
116 // enable depth texture
117 if (tex_unit == -1)
118 tex_unit = depth_tex_unit;
119 depth_tex.enable(ctx, tex_unit);
120 if (!prog.set_uniform(ctx, "depth_tex", tex_unit)) {
121 std::cerr << "ERROR in a_buffer::enable(): uniform sampler2D depth_tex not found in program." << std::endl;
122 return false;
123 }
124 // Bind buffers for first a-buffer pass
126 head_pointer_buffer.bind(ctx, head_pointers_binding_point);
127 node_buffer.bind(ctx, nodes_binding_point);
128
129 return true;
130 }
131 // return current number of nodes in node buffer
132 void a_buffer::disable(context& ctx, size_t* node_count)
133 {
134 // Unbind texture and buffers
135 depth_tex.disable(ctx);
136
138 head_pointer_buffer.unbind(ctx, head_pointers_binding_point);
139 node_buffer.unbind(ctx, nodes_binding_point);
140
141 // Read back node count if requested
142 if(node_count) {
143 GLuint temp = 0;
144 node_counter_buffer.copy(ctx, 0, &temp, 1);
145 *node_count = temp;
146 }
147 }
149 {
150 ctx.push_blend_state();
151 ctx.enable_blending();
153
154 head_pointer_buffer.bind(ctx, 0);
155 node_buffer.bind(ctx, 1);
156
157 a_buffer_prog.enable(ctx);
158 a_buffer_prog.set_uniform(ctx, "viewport_dims", ivec2(ctx.get_width(), ctx.get_height()));
159 a_buffer_prog.set_uniform(ctx, "nodes_per_pixel", nodes_per_pixel);
160 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
161 a_buffer_prog.disable(ctx);
162
163 head_pointer_buffer.unbind(ctx, 0);
164 node_buffer.unbind(ctx, 1);
165
166 ctx.pop_blend_state();
167 }
168 }
169}
size_t get_width() const
return the resolution in the first dimension, or 1 if not defined
size_t get_height() const
return the resolution in the second dimension, or 1 if not defined
unsigned nodes_per_pixel
to be reserved number of fragment nodes per pixel (changes are applied when in init_frame() function)
Definition a_buffer.h:47
unsigned fragments_per_pixel
to be handled fragments per pixel (changes are applied when in init_frame() function)
Definition a_buffer.h:45
a_buffer(unsigned _fragments_per_pixel=32, unsigned _nodes_per_pixel=64, int _depth_tex_unit=0, int _node_counter_binding_point=0, int _head_pointers_binding_point=0, int _nodes_binding_point=1)
construct and configure
Definition a_buffer.cxx:27
void destruct(context &ctx)
destruct all render objects
Definition a_buffer.cxx:53
bool init(context &ctx)
construct internally used programs
Definition a_buffer.cxx:43
texture depth_tex
Depth texture used to emulate depth buffer.
Definition a_buffer.h:22
bool enable(context &ctx, shader_program &prog, int tex_unit=-1)
Enable writing fragments to a_buffer with provided program.
Definition a_buffer.cxx:100
void disable(context &ctx, size_t *out_node_count=nullptr)
finish writing fragments to a_buffer and return current number of nodes in node buffer
Definition a_buffer.cxx:132
cgv::render::vertex_buffer node_counter_buffer
Buffers used to store per pixel frament lists.
Definition a_buffer.h:24
int node_counter_binding_point
Buffer binding point indices.
Definition a_buffer.h:30
void init_frame(context &ctx)
ensure that a_buffer size corresponds to context size
Definition a_buffer.cxx:62
int depth_tex_unit
Default texture unit used for depth texture.
Definition a_buffer.h:28
void finish_frame(context &ctx)
per fragment sort nodes and blend over current framebuffer
Definition a_buffer.cxx:148
base class for all drawables, which is independent of the used rendering API.
Definition context.h:670
void set_blend_func_back_to_front()
set the default blend function for back to front blending (source = BF_SRC_ALPHA, destination = BF_ON...
Definition context.cxx:1858
void push_blend_state()
push a copy of the current blend state onto the stack saved attributes: blend enablement,...
Definition context.cxx:1822
void pop_blend_state()
pop the top of the current culling state from the stack
Definition context.cxx:1826
virtual unsigned int get_width() const =0
return the width of the window
virtual unsigned int get_height() const =0
return the height of the window
virtual void enable_blending()
enable blending
Definition context.cxx:1862
virtual bool is_created() const
return whether component has been created
Definition context.cxx:2152
Stores preprocessor options used for conditionally compiling shader programs.
Definition shader_code.h:73
a shader program combines several shader code fragments to a complete definition of the shading pipel...
bool enable(context &ctx)
enable the shader program
bool disable(context &ctx)
disable shader program and restore fixed functionality
bool set_uniform(const context &ctx, const std::string &name, const T &value, bool generate_error=false)
Set the value of a uniform by name, where the type can be any of int, unsigned, float,...
void destruct(const context &ctx)
destruct shader program
bool build_program(const context &ctx, const std::string &file_name, bool show_error=false)
successively calls create, attach_program and link.
bool build_files(const context &ctx, const std::string &base_name, bool show_error=false)
successively calls create, attach_files and link.
void set_mag_filter(TextureFilter _mag_filter)
set the magnification filter
Definition texture.cxx:143
bool create(const context &ctx, TextureType _tt=TT_UNDEF, unsigned width=-1, unsigned height=-1, unsigned depth=-1)
create the texture of dimension and resolution specified in the data format base class.
Definition texture.cxx:213
bool replace_from_buffer(const context &ctx, int x, int y, int x_buffer, int y_buffer, int width, int height, int level=-1)
replace a block within a 2d texture from the current read buffer.
Definition texture.cxx:672
bool disable(const context &ctx)
disable texture and restore state from before last enable call
Definition texture.cxx:774
bool enable(const context &ctx, int tex_unit=-1)
enable this texture in the given texture unit, -1 corresponds to the current unit.
Definition texture.cxx:763
bool destruct(const context &ctx)
destruct the texture and free texture memory and handle
Definition texture.cxx:748
void set_min_filter(TextureFilter _min_filter, float _anisotropy=2.0f)
set the minification filters, if minification is set to TF_ANISOTROP, the second floating point param...
Definition texture.cxx:125
void bind(context &ctx, VertexBufferType type=VBT_UNDEF) const
Bind buffer to appropriate target.
bool create_or_resize(const context &ctx, size_t size_in_bytes)
Convenience wrapper to either create() or resize() the buffer.
void unbind(context &ctx, VertexBufferType type=VBT_UNDEF) const
Unbind buffer from the appropriate target.
bool copy(const context &ctx, size_t src_offset_in_bytes, size_t size_in_bytes, vertex_buffer &dst, size_t dst_offset_in_bytes) const
Copy bytes between different vertex_buffer instances.
void destruct(const context &ctx)
destruct the render buffer
bool replace(const context &ctx, size_t buffer_offset_in_bytes, const T *array_ptr, size_t nr_elements)
replace part (starting at byte offset buffer_offset_in_bytes) or whole vertex buffer content from nr_...
the cgv namespace
Definition print.h:11
cgv::math::fvec< int32_t, 2 > ivec2
declare type of 2d 32 bit integer vectors
Definition fvec.h:659