Skip to content

Commit 225cb1e

Browse files
committed
buffer pointer offset support
1 parent 65a29b2 commit 225cb1e

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

include/shader.h

+7
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ class Shader
150150
*/
151151
void set_buffer_divisor(const std::string &name, size_t divisor);
152152

153+
/**
154+
Set the pointer offset for call to glVertexAttribPointer. Useful in instance drawing to start drawing instances
155+
from a certain index in an attribute buffer.
156+
*/
157+
void set_buffer_pointer_offset(const std::string &name, size_t offset);
158+
153159
// /**
154160
// * \brief Associate a texture with a named shader parameter
155161
// *
@@ -240,6 +246,7 @@ class Shader
240246
size_t shape[3]{0, 0, 0};
241247
size_t size = 0;
242248
size_t instance_divisor = 0;
249+
size_t pointer_offset = 0;
243250
bool dirty = false;
244251

245252
std::string to_string() const;

src/app.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,8 @@ void SampleViewer::draw_2D_points_and_grid(const float4x4 &mvp, int2 dims, int p
12481248
m_2d_point_shader->set_uniform("color", m_point_color);
12491249
int2 range = get_draw_range();
12501250

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

12541255
if (range.y > 0)

src/shader.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,22 @@ void Shader::set_buffer_divisor(const std::string &name, size_t divisor)
384384
{
385385
auto it = m_buffers.find(name);
386386
if (it == m_buffers.end())
387-
throw std::runtime_error("Shader::set_buffer(): could not find argument named \"" + name + "\"");
387+
throw std::runtime_error("Shader::set_buffer_divisor(): could not find argument named \"" + name + "\"");
388388

389389
Buffer &buf = m_buffers[name];
390390
buf.instance_divisor = divisor;
391+
buf.dirty = true;
392+
}
393+
394+
void Shader::set_buffer_pointer_offset(const std::string &name, size_t offset)
395+
{
396+
auto it = m_buffers.find(name);
397+
if (it == m_buffers.end())
398+
throw std::runtime_error("Shader::set_buffer_pointer_offset(): could not find argument named \"" + name + "\"");
399+
400+
Buffer &buf = m_buffers[name];
401+
buf.pointer_offset = offset;
402+
buf.dirty = true;
391403
}
392404

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

464-
CHK(glVertexAttribPointer(buf.index, (GLint)buf.shape[1], gl_type, GL_FALSE, 0, nullptr));
476+
CHK(glVertexAttribPointer(buf.index, (GLint)buf.shape[1], gl_type, GL_FALSE, 0,
477+
(const void *)buf.pointer_offset));
465478
CHK(glVertexAttribDivisor(buf.index, (GLuint)buf.instance_divisor));
466479
break;
467480

0 commit comments

Comments
 (0)