Skip to content

Commit

Permalink
buffer pointer offset support
Browse files Browse the repository at this point in the history
  • Loading branch information
wkjarosz committed Dec 26, 2023
1 parent 65a29b2 commit 225cb1e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
7 changes: 7 additions & 0 deletions include/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ class Shader
*/
void set_buffer_divisor(const std::string &name, size_t divisor);

/**
Set the pointer offset for call to glVertexAttribPointer. Useful in instance drawing to start drawing instances
from a certain index in an attribute buffer.
*/
void set_buffer_pointer_offset(const std::string &name, size_t offset);

// /**
// * \brief Associate a texture with a named shader parameter
// *
Expand Down Expand Up @@ -240,6 +246,7 @@ class Shader
size_t shape[3]{0, 0, 0};
size_t size = 0;
size_t instance_divisor = 0;
size_t pointer_offset = 0;
bool dirty = false;

std::string to_string() const;
Expand Down
3 changes: 2 additions & 1 deletion src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,8 @@ void SampleViewer::draw_2D_points_and_grid(const float4x4 &mvp, int2 dims, int p
m_2d_point_shader->set_uniform("color", m_point_color);
int2 range = get_draw_range();

m_2d_point_shader->set_buffer("center", m_2d_points, m_subset_count * plot_index + range.x, range.y);
m_2d_point_shader->set_buffer_pointer_offset("center",
size_t(m_subset_count * plot_index + range.x) * sizeof(float3));
m_2d_point_shader->set_buffer_divisor("center", 1); // one center per quad/instance

if (range.y > 0)
Expand Down
17 changes: 15 additions & 2 deletions src/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,22 @@ void Shader::set_buffer_divisor(const std::string &name, size_t divisor)
{
auto it = m_buffers.find(name);
if (it == m_buffers.end())
throw std::runtime_error("Shader::set_buffer(): could not find argument named \"" + name + "\"");
throw std::runtime_error("Shader::set_buffer_divisor(): could not find argument named \"" + name + "\"");

Buffer &buf = m_buffers[name];
buf.instance_divisor = divisor;
buf.dirty = true;
}

void Shader::set_buffer_pointer_offset(const std::string &name, size_t offset)
{
auto it = m_buffers.find(name);
if (it == m_buffers.end())
throw std::runtime_error("Shader::set_buffer_pointer_offset(): could not find argument named \"" + name + "\"");

Buffer &buf = m_buffers[name];
buf.pointer_offset = offset;
buf.dirty = true;
}

// void Shader::set_texture(const std::string &name, Texture *texture)
Expand Down Expand Up @@ -461,7 +473,8 @@ void Shader::begin()
"\" has an invalid shapeension (expected ndim=2, got " +
std::to_string(buf.ndim) + ")");

CHK(glVertexAttribPointer(buf.index, (GLint)buf.shape[1], gl_type, GL_FALSE, 0, nullptr));
CHK(glVertexAttribPointer(buf.index, (GLint)buf.shape[1], gl_type, GL_FALSE, 0,
(const void *)buf.pointer_offset));
CHK(glVertexAttribDivisor(buf.index, (GLuint)buf.instance_divisor));
break;

Expand Down

0 comments on commit 225cb1e

Please sign in to comment.