瀏覽代碼

gpu: add helper functions to upload buffer data.

legacy
Sam Hocevar 5 年之前
父節點
當前提交
f6f61ceb23
共有 17 個文件被更改,包括 61 次插入92 次删除
  1. +1
    -3
      doc/tutorial/01_triangle.cpp
  2. +5
    -11
      doc/tutorial/02_cube.cpp
  3. +1
    -3
      doc/tutorial/03_noise.cpp
  4. +1
    -3
      doc/tutorial/04_texture.cpp
  5. +5
    -11
      doc/tutorial/07_input.cpp
  6. +1
    -3
      doc/tutorial/08_fbo.cpp
  7. +2
    -8
      doc/tutorial/11_fractal.cpp
  8. +1
    -3
      doc/tutorial/12_voronoi.cpp
  9. +6
    -10
      src/easymesh/easymeshrender.cpp
  10. +3
    -3
      src/gpu/indexbuffer.cpp
  11. +3
    -3
      src/gpu/vertexbuffer.cpp
  12. +2
    -7
      src/gradient.cpp
  13. +10
    -3
      src/lol/gpu/indexbuffer.h
  14. +10
    -3
      src/lol/gpu/vertexbuffer.h
  15. +2
    -2
      src/mesh/mesh.cpp
  16. +6
    -10
      src/scene.cpp
  17. +2
    -6
      src/ui/gui.cpp

+ 1
- 3
doc/tutorial/01_triangle.cpp 查看文件

@@ -41,9 +41,7 @@ public:
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = std::make_shared<VertexBuffer>(vertices.bytes());
void *data = m_vbo->Lock(0, 0);
memcpy(data, vertices.data(), vertices.bytes());
m_vbo->Unlock();
m_vbo->set_data(vertices.data(), vertices.bytes());

return true;
}


+ 5
- 11
doc/tutorial/02_cube.cpp 查看文件

@@ -84,19 +84,13 @@ public:
VertexUsage::Color));

m_vbo = std::make_shared<VertexBuffer>(mesh.bytes());
void *data = m_vbo->Lock(0, 0);
memcpy(data, mesh.data(), mesh.bytes());
m_vbo->Unlock();
m_vbo->set_data(mesh.data(), mesh.bytes());

m_lines_ibo = std::make_shared<IndexBuffer>(lines_indices.bytes());
data = m_lines_ibo->Lock(0, 0);
memcpy(data, lines_indices.data(), lines_indices.bytes());
m_lines_ibo->Unlock();
m_lines_ibo->set_data(lines_indices.data(), lines_indices.bytes());

m_faces_ibo = std::make_shared<IndexBuffer>(faces_indices.bytes());
data = m_faces_ibo->Lock(0, 0);
memcpy(data, faces_indices.data(), faces_indices.bytes());
m_faces_ibo->Unlock();
m_faces_ibo->set_data(faces_indices.data(), faces_indices.bytes());

return true;
}
@@ -146,12 +140,12 @@ public:

m_shader->SetUniform(m_mvp, m_matrix);
m_lines_ibo->Bind();
m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, m_lines_ibo->GetSize() / sizeof(uint16_t));
m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, m_lines_ibo->size() / sizeof(uint16_t));
m_lines_ibo->Unbind();

m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
m_faces_ibo->Bind();
m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, m_faces_ibo->GetSize() / sizeof(uint16_t));
m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, m_faces_ibo->size() / sizeof(uint16_t));
m_faces_ibo->Unbind();

m_vdecl->Unbind();


+ 1
- 3
doc/tutorial/03_noise.cpp 查看文件

@@ -45,9 +45,7 @@ public:
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = std::make_shared<VertexBuffer>(vertices.bytes());
void *data = m_vbo->Lock(0, 0);
memcpy(data, vertices.data(), vertices.bytes());
m_vbo->Unlock();
m_vbo->set_data(vertices.data(), vertices.bytes());

return true;
}


+ 1
- 3
doc/tutorial/04_texture.cpp 查看文件

@@ -73,9 +73,7 @@ public:
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = std::make_shared<VertexBuffer>(vertices.bytes());
void *data = m_vbo->Lock(0, 0);
memcpy(data, vertices.data(), vertices.bytes());
m_vbo->Unlock();
m_vbo->set_data(vertices.data(), vertices.bytes());

return true;
}


+ 5
- 11
doc/tutorial/07_input.cpp 查看文件

@@ -141,19 +141,13 @@ public:
VertexUsage::Color));

m_vbo = std::make_shared<VertexBuffer>(mesh.bytes());
void *data = m_vbo->Lock(0, 0);
memcpy(data, mesh.data(), mesh.bytes());
m_vbo->Unlock();
m_vbo->set_data(mesh.data(), mesh.bytes());

m_lines_ibo = std::make_shared<IndexBuffer>(lines_indices.bytes());
data = m_lines_ibo->Lock(0, 0);
memcpy(data, lines_indices.data(), lines_indices.bytes());
m_lines_ibo->Unlock();
m_lines_ibo->set_data(lines_indices.data(), lines_indices.bytes());

m_faces_ibo = std::make_shared<IndexBuffer>(faces_indices.bytes());
data = m_faces_ibo->Lock(0, 0);
memcpy(data, faces_indices.data(), faces_indices.bytes());
m_faces_ibo->Unlock();
m_faces_ibo->set_data(faces_indices.data(), faces_indices.bytes());

return WorldEntity::init_draw();
}
@@ -170,12 +164,12 @@ public:

m_shader->SetUniform(m_mvp, m_matrix);
m_lines_ibo->Bind();
m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, m_lines_ibo->GetSize() / sizeof(uint16_t));
m_vdecl->DrawIndexedElements(MeshPrimitive::Lines, m_lines_ibo->size() / sizeof(uint16_t));
m_lines_ibo->Unbind();

m_shader->SetUniform(m_mvp, m_matrix * mat4::scale(0.5f));
m_faces_ibo->Bind();
m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, m_faces_ibo->GetSize() / sizeof(uint16_t));
m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, m_faces_ibo->size() / sizeof(uint16_t));
m_faces_ibo->Unbind();

m_vdecl->Unbind();


+ 1
- 3
doc/tutorial/08_fbo.cpp 查看文件

@@ -64,9 +64,7 @@ public:
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = std::make_shared<VertexBuffer>(vertices.bytes());
void *data = m_vbo->Lock(0, 0);
memcpy(data, vertices.data(), vertices.bytes());
m_vbo->Unlock();
m_vbo->set_data(vertices.data(), vertices.bytes());

m_fbo = std::make_shared<Framebuffer>(Video::GetSize());
m_fbo->Bind();


+ 2
- 8
doc/tutorial/11_fractal.cpp 查看文件

@@ -460,15 +460,9 @@ public:
VertexStream<vec2>(VertexUsage::Position),
VertexStream<vec2>(VertexUsage::TexCoord));
m_vbo = std::make_shared<VertexBuffer>(sizeof(vertices));
m_vbo->set_data(vertices, sizeof(vertices));
m_tbo = std::make_shared<VertexBuffer>(sizeof(texcoords));

void *data = m_vbo->Lock(0, 0);
memcpy(data, vertices, sizeof(vertices));
m_vbo->Unlock();

data = m_tbo->Lock(0, 0);
memcpy(data, texcoords, sizeof(texcoords));
m_tbo->Unlock();
m_tbo->set_data(texcoords, sizeof(texcoords));

return true;
}


+ 1
- 3
doc/tutorial/12_voronoi.cpp 查看文件

@@ -87,9 +87,7 @@ public:
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
void *vertices = m_vbo->Lock(0, 0);
memcpy(vertices, &m_vertices[0], m_vertices.bytes());
m_vbo->Unlock();
m_vbo->set_data(m_vertices.data(), m_vertices.bytes());

m_screen_shader = Shader::Create(LOLFX_RESOURCE_NAME(12_texture_to_screen));
m_screen_coord = m_screen_shader->GetAttribLocation(VertexUsage::Position, 0);


+ 6
- 10
src/easymesh/easymeshrender.cpp 查看文件

@@ -33,10 +33,10 @@ void EasyMesh::MeshConvert()

/* Push index buffer to GPU */
auto ibo = std::make_shared<IndexBuffer>(m_indices.count() * sizeof(uint16_t));
uint16_t *indices = (uint16_t *)ibo->Lock(0, 0);
uint16_t *indices = (uint16_t *)ibo->lock(0, 0);
for (int i = 0; i < m_indices.count(); ++i)
indices[i] = m_indices[i];
ibo->Unlock();
ibo->unlock();

/* Push vertex buffer to GPU */
struct Vertex
@@ -53,7 +53,7 @@ void EasyMesh::MeshConvert()
VertexUsage::TexCoord));

auto vbo = std::make_shared<VertexBuffer>(m_vert.count() * sizeof(Vertex));
Vertex *vert = (Vertex *)vbo->Lock(0, 0);
Vertex *vert = (Vertex *)vbo->lock(0, 0);
for (int i = 0; i < m_vert.count(); ++i)
{
vert[i].pos = m_vert[i].m_coord,
@@ -61,7 +61,7 @@ void EasyMesh::MeshConvert()
vert[i].color = (u8vec4)(m_vert[i].m_color * 255.f);
vert[i].texcoord = m_vert[i].m_texcoord;
}
vbo->Unlock();
vbo->unlock();

/* Reference our new data in our submesh */
m_submeshes.push_back(std::make_shared<SubMesh>(shader, vdecl));
@@ -271,9 +271,7 @@ void GpuEasyMeshData::AddGpuData(std::shared_ptr<GpuShaderData> gpudata, std::sh
}

m_ibo = std::make_shared<IndexBuffer>(indexlist.bytes());
void *indices = m_ibo->Lock(0, 0);
memcpy(indices, &indexlist[0], indexlist.bytes());
m_ibo->Unlock();
m_ibo->set_data(indexlist.data(), indexlist.bytes());

m_indexcount = indexlist.count();
}
@@ -307,9 +305,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, std::shared_ptr<EasyMesh>
vbo_bytes = vertexlist.bytes(); \
m_vertexcount = vertexlist.count(); \
new_vbo = std::make_shared<VertexBuffer>(vbo_bytes); \
void *mesh = new_vbo->Lock(0, 0); \
memcpy(mesh, vbo_data, vbo_bytes); \
new_vbo->Unlock();
new_vbo->set_data(vbo_data, vbo_bytes);

//Keep a count of the flags
uint16_t saveflags = vflags;


+ 3
- 3
src/gpu/indexbuffer.cpp 查看文件

@@ -59,12 +59,12 @@ IndexBuffer::~IndexBuffer()
delete m_data;
}

size_t IndexBuffer::GetSize()
size_t IndexBuffer::size()
{
return m_data->m_size;
}

void *IndexBuffer::Lock(size_t offset, size_t size)
void *IndexBuffer::lock(size_t offset, size_t size)
{
if (!m_data->m_size)
return nullptr;
@@ -73,7 +73,7 @@ void *IndexBuffer::Lock(size_t offset, size_t size)
return m_data->m_memory + offset;
}

void IndexBuffer::Unlock()
void IndexBuffer::unlock()
{
if (!m_data->m_size)
return;


+ 3
- 3
src/gpu/vertexbuffer.cpp 查看文件

@@ -364,12 +364,12 @@ VertexBuffer::~VertexBuffer()
delete m_data;
}

size_t VertexBuffer::GetSize()
size_t VertexBuffer::size()
{
return m_data->m_size;
}

void *VertexBuffer::Lock(size_t offset, size_t size)
void *VertexBuffer::lock(size_t offset, size_t size)
{
if (!m_data->m_size)
return nullptr;
@@ -379,7 +379,7 @@ void *VertexBuffer::Lock(size_t offset, size_t size)
return m_data->m_memory + offset;
}

void VertexBuffer::Unlock()
void VertexBuffer::unlock()
{
if (!m_data->m_size)
return;


+ 2
- 7
src/gradient.cpp 查看文件

@@ -100,13 +100,8 @@ void Gradient::tick_draw(float seconds, Scene &scene)
data->shader->Bind();
data->m_vdecl->Bind();

void *tmp = data->m_vbo->Lock(0, 0);
memcpy(tmp, vertex, sizeof(vertex));
data->m_vbo->Unlock();

tmp = data->m_cbo->Lock(0, 0);
memcpy(tmp, color, sizeof(color));
data->m_cbo->Unlock();
data->m_vbo->set_data(vertex, sizeof(vertex));
data->m_cbo->set_data(color, sizeof(color));

/* Bind vertex and color buffers */
data->m_vdecl->SetStream(data->m_vbo, attr_pos);


+ 10
- 3
src/lol/gpu/indexbuffer.h 查看文件

@@ -28,10 +28,17 @@ public:
IndexBuffer(size_t size);
~IndexBuffer();

size_t GetSize();
size_t size();

void *Lock(size_t offset, size_t size);
void Unlock();
void set_data(void const *data, size_t size)
{
auto ptr = lock(0, size);
::memcpy(ptr, data, size);
unlock();
}

void *lock(size_t offset, size_t size);
void unlock();

void Bind();
void Unbind();


+ 10
- 3
src/lol/gpu/vertexbuffer.h 查看文件

@@ -31,10 +31,17 @@ public:
VertexBuffer(size_t size);
~VertexBuffer();

size_t GetSize();
size_t size();

void *Lock(size_t offset, size_t size);
void Unlock();
void set_data(void const *data, size_t size)
{
auto ptr = lock(0, size);
::memcpy(ptr, data, size);
unlock();
}

void *lock(size_t offset, size_t size);
void unlock();

private:
class VertexBufferData *m_data;


+ 2
- 2
src/mesh/mesh.cpp 查看文件

@@ -130,7 +130,7 @@ void SubMesh::Render()
attribs[j] = m_shader->GetAttribLocation(usage, usages[usage_index]++);
}

vertex_count += m_vbos[i]->GetSize() / m_vdecl->GetStream(i).GetSize();
vertex_count += m_vbos[i]->size() / m_vdecl->GetStream(i).GetSize();

m_vdecl->SetStream(m_vbos[i], attribs);
}
@@ -145,7 +145,7 @@ void SubMesh::Render()
}

m_ibo->Bind();
m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, (int)(m_ibo->GetSize() / sizeof(uint16_t)));
m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, (int)(m_ibo->size() / sizeof(uint16_t)));
m_vdecl->Unbind();
m_ibo->Unbind();
}


+ 6
- 10
src/scene.cpp 查看文件

@@ -139,9 +139,7 @@ Scene::Scene(ivec2 size)
vec2(-1.0, -1.0), vec2( 1.0, 1.0), vec2(-1.0, 1.0), };

m_pp.m_vbo = std::make_shared<VertexBuffer>(quad.bytes());
void *vertices = m_pp.m_vbo->Lock(0, 0);
memcpy(vertices, quad.data(), quad.bytes());
m_pp.m_vbo->Unlock();
m_pp.m_vbo->set_data(quad.data(), quad.bytes());

/* Create a default orthographic camera, in case the user doesn’t. */
m_default_cam = new Camera();
@@ -637,9 +635,9 @@ void Scene::render_tiles() // XXX: rename to Blit()

/* Create a vertex array object */
auto vb1 = std::make_shared<VertexBuffer>(6 * (n - i) * sizeof(vec3));
vec3 *vertex = (vec3 *)vb1->Lock(0, 0);
vec3 *vertex = (vec3 *)vb1->lock(0, 0);
auto vb2 = std::make_shared<VertexBuffer>(6 * (n - i) * sizeof(vec2));
vec2 *texture = (vec2 *)vb2->Lock(0, 0);
vec2 *texture = (vec2 *)vb2->lock(0, 0);

m_tile_api.m_bufs.push(vb1);
m_tile_api.m_bufs.push(vb2);
@@ -650,8 +648,8 @@ void Scene::render_tiles() // XXX: rename to Blit()
vertex + 6 * (j - i), texture + 6 * (j - i));
}

vb1->Unlock();
vb2->Unlock();
vb1->unlock();
vb2->unlock();

/* Bind texture */
if (tiles[i].m_tileset->GetPalette())
@@ -736,9 +734,7 @@ void Scene::render_lines(float seconds)
}
}
auto vb = std::make_shared<VertexBuffer>(buff.bytes());
float *vertex = (float *)vb->Lock(0, 0);
memcpy(vertex, buff.data(), buff.bytes());
vb->Unlock();
vb->set_data(buff.data(), buff.bytes());

m_line_api.m_shader->Bind();



+ 2
- 6
src/ui/gui.cpp 查看文件

@@ -353,14 +353,10 @@ void gui::render_draw_lists(ImDrawData* draw_data)
};

auto vbo = std::make_shared<VertexBuffer>(command_list.VtxBuffer.Size * sizeof(ImDrawVert));
ImDrawVert *vert = (ImDrawVert *)vbo->Lock(0, 0);
memcpy(vert, command_list.VtxBuffer.Data, command_list.VtxBuffer.Size * sizeof(ImDrawVert));
vbo->Unlock();
vbo->set_data(command_list.VtxBuffer.Data, command_list.VtxBuffer.Size * sizeof(ImDrawVert));

auto ibo = std::make_shared<IndexBuffer>(command_list.IdxBuffer.Size * sizeof(ImDrawIdx));
ImDrawIdx *indices = (ImDrawIdx *)ibo->Lock(0, 0);
memcpy(indices, command_list.IdxBuffer.Data, command_list.IdxBuffer.Size * sizeof(ImDrawIdx));
ibo->Unlock();
ibo->set_data(command_list.IdxBuffer.Data, command_list.IdxBuffer.Size * sizeof(ImDrawIdx));

m_vdecl->Bind();
ibo->Bind();


Loading…
取消
儲存