render implement thread, wrap GL commands into tasks

This commit is contained in:
2019-07-06 22:25:07 +02:00
parent db27334ce5
commit 0012e2ce9b
18 changed files with 1252 additions and 904 deletions

View File

@@ -6,6 +6,7 @@
void BrushMesh::draw(const std::vector<StrokeSample>& samples, const glm::mat4& proj)
{
assert(App::I.is_render_thread());
std::vector<instance_t> attributes;
attributes.reserve(samples.size());
for (const auto& s : samples)
@@ -69,64 +70,74 @@ void BrushMesh::draw(const std::vector<StrokeSample>& samples, const glm::mat4&
}
bool BrushMesh::create()
{
static GLushort idx[6]{ 0, 1, 2, 0, 2, 3 };
static vertex_t vertices[4]{
{ { -.5f, -.5f, 0, 1 }, { 0, 0 } }, // A B----C
{ { -.5f, .5f, 0, 1 }, { 0, 1 } }, // B --\ | |
{ { .5f, .5f, 0, 1 }, { 1, 1 } }, // C --/ | |
{ { .5f, -.5f, 0, 1 }, { 1, 0 } }, // D A----D
};
glGenBuffers(3, buffers);
if (!(buffers[0] && buffers[1] && buffers[2]))
return false;
bool ret = true;
App::I.render_task([&]
{
static GLushort idx[6]{ 0, 1, 2, 0, 2, 3 };
static vertex_t vertices[4]{
{ { -.5f, -.5f, 0, 1 }, { 0, 0 } }, // A B----C
{ { -.5f, .5f, 0, 1 }, { 0, 1 } }, // B --\ | |
{ { .5f, .5f, 0, 1 }, { 1, 1 } }, // C --/ | |
{ { .5f, -.5f, 0, 1 }, { 1, 0 } }, // D A----D
};
glGenBuffers(3, buffers);
if (!(buffers[0] && buffers[1] && buffers[2]))
{
ret = false;
return;
}
static instance_t inst{ glm::mat4(1), .1f };
glBindBuffer(GL_ARRAY_BUFFER, buffers[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(instance_t), &inst, GL_STATIC_DRAW);
static instance_t inst{ glm::mat4(1), .1f };
glBindBuffer(GL_ARRAY_BUFFER, buffers[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(instance_t), &inst, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idx), idx, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idx), idx, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
auto shader = ShaderManager::get(kShader::BrushStroke);
auto shader = ShaderManager::get(kShader::BrushStroke);
loc_flow = shader->GetAttribLocation("a_flow");
loc_mvp = shader->GetAttribLocation("a_mvp");
loc_flow = shader->GetAttribLocation("a_flow");
loc_mvp = shader->GetAttribLocation("a_mvp");
#if USE_VBO
glGenVertexArrays(1, &vao);
if (!vao)
return false;
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, pos));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
glBindBuffer(GL_ARRAY_BUFFER, buffers[2]);
// Loop over each column of the matrix...
for (int i = 0; i < 4; i++)
{
// Set up the vertex attribute
glVertexAttribPointer(loc_mvp + i, 4, GL_FLOAT, GL_FALSE, sizeof(instance_t),
(GLvoid*)(offsetof(instance_t, mvp) + sizeof(glm::vec4) * i));
// Enable it
glEnableVertexAttribArray(loc_mvp + i);
// Make it instanced
glVertexAttribDivisor(loc_mvp + i, 1);
}
glEnableVertexAttribArray(loc_flow);
glVertexAttribPointer(loc_flow, 1, GL_FLOAT, GL_FALSE, sizeof(instance_t),
(GLvoid*)offsetof(instance_t, flow));
glVertexAttribDivisor(loc_flow, 1);
glBindVertexArray(0);
glGenVertexArrays(1, &vao);
if (!vao)
{
ret = false;
return;
}
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, pos));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
glBindBuffer(GL_ARRAY_BUFFER, buffers[2]);
// Loop over each column of the matrix...
for (int i = 0; i < 4; i++)
{
// Set up the vertex attribute
glVertexAttribPointer(loc_mvp + i, 4, GL_FLOAT, GL_FALSE, sizeof(instance_t),
(GLvoid*)(offsetof(instance_t, mvp) + sizeof(glm::vec4) * i));
// Enable it
glEnableVertexAttribArray(loc_mvp + i);
// Make it instanced
glVertexAttribDivisor(loc_mvp + i, 1);
}
glEnableVertexAttribArray(loc_flow);
glVertexAttribPointer(loc_flow, 1, GL_FLOAT, GL_FALSE, sizeof(instance_t),
(GLvoid*)offsetof(instance_t, flow));
glVertexAttribDivisor(loc_flow, 1);
glBindVertexArray(0);
#endif
});
return true;
return ret;
}
StrokeSample Stroke::randomize_sample(const glm::vec3& pos, float pressure, float dir_angle)
{