diff --git a/doc/tutorial/01_triangle.cpp b/doc/tutorial/01_triangle.cpp index 6d879a7d..4d1f07af 100644 --- a/doc/tutorial/01_triangle.cpp +++ b/doc/tutorial/01_triangle.cpp @@ -53,8 +53,8 @@ public: WorldEntity::tick_draw(seconds, scene); m_shader->Bind(); - m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->Bind(); + m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 3); m_vdecl->Unbind(); } diff --git a/doc/tutorial/02_cube.cpp b/doc/tutorial/02_cube.cpp index 39e2822c..ee15f18c 100644 --- a/doc/tutorial/02_cube.cpp +++ b/doc/tutorial/02_cube.cpp @@ -141,8 +141,8 @@ public: scene.get_renderer()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); m_shader->Bind(); - m_vdecl->SetStream(m_vbo, m_coord, m_color); m_vdecl->Bind(); + m_vdecl->SetStream(m_vbo, m_coord, m_color); m_shader->SetUniform(m_mvp, m_matrix); m_lines_ibo->Bind(); diff --git a/doc/tutorial/03_noise.cpp b/doc/tutorial/03_noise.cpp index 780d5ce7..fb9e9ec2 100644 --- a/doc/tutorial/03_noise.cpp +++ b/doc/tutorial/03_noise.cpp @@ -60,8 +60,8 @@ public: m_shader->Bind(); m_shader->SetUniform(m_time_uni, m_time); - m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->Bind(); + m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); m_vdecl->Unbind(); } diff --git a/doc/tutorial/04_texture.cpp b/doc/tutorial/04_texture.cpp index 274ee112..0c820ef6 100644 --- a/doc/tutorial/04_texture.cpp +++ b/doc/tutorial/04_texture.cpp @@ -90,8 +90,8 @@ public: m_shader->Bind(); m_shader->SetUniform(m_texture_uni, m_texture->GetTextureUniform(), 0); - m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->Bind(); + m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); m_vdecl->Unbind(); } diff --git a/doc/tutorial/07_input.cpp b/doc/tutorial/07_input.cpp index fa0f7e98..d4f85c36 100644 --- a/doc/tutorial/07_input.cpp +++ b/doc/tutorial/07_input.cpp @@ -165,8 +165,8 @@ public: scene.get_renderer()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); m_shader->Bind(); - m_vdecl->SetStream(m_vbo, m_coord, m_color); m_vdecl->Bind(); + m_vdecl->SetStream(m_vbo, m_coord, m_color); m_shader->SetUniform(m_mvp, m_matrix); m_lines_ibo->Bind(); diff --git a/doc/tutorial/08_fbo.cpp b/doc/tutorial/08_fbo.cpp index 7e27b597..ab1de571 100644 --- a/doc/tutorial/08_fbo.cpp +++ b/doc/tutorial/08_fbo.cpp @@ -98,8 +98,8 @@ public: m_shader->SetUniform(m_uni_flag, 0.f); m_shader->SetUniform(m_uni_point, m_hotspot); m_shader->SetUniform(m_uni_color, m_color); - m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->Bind(); + m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); m_vdecl->Unbind(); m_shader->Unbind(); @@ -108,8 +108,8 @@ public: m_shader->Bind(); m_shader->SetUniform(m_uni_flag, 1.f); m_shader->SetUniform(m_uni_texture, m_fbo->GetTextureUniform(), 0); - m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->Bind(); + m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); m_vdecl->Unbind(); m_shader->Unbind(); diff --git a/doc/tutorial/12_voronoi.cpp b/doc/tutorial/12_voronoi.cpp index f78c3cad..d76d65bd 100644 --- a/doc/tutorial/12_voronoi.cpp +++ b/doc/tutorial/12_voronoi.cpp @@ -263,8 +263,8 @@ public: m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], voronoi_points[j].m1); //"u_source_point" m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], vec2(512.f, 512.f)); //"u_screen_res" - m_vdecl->SetStream(m_vbo, m_fbos[f].m4.last()); m_vdecl->Bind(); + m_vdecl->SetStream(m_vbo, m_fbos[f].m4.last()); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); m_vdecl->Unbind(); m_fbos[f].m2->Unbind(); @@ -329,8 +329,8 @@ public: shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], vec2(512.f, 512.f)); //"u_screen_res" } - m_vdecl->SetStream(m_vbo, m_fbos[m_cur_fbo].m4.last()); m_vdecl->Bind(); + m_vdecl->SetStream(m_vbo, m_fbos[m_cur_fbo].m4.last()); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); m_vdecl->Unbind(); m_fbos[m_cur_fbo].m2->Unbind(); @@ -353,8 +353,8 @@ public: //SCREEN DRAW m_screen_shader->Bind(); m_screen_shader->SetUniform(m_screen_texture, m_fbos[m_cur_fbo].m1->GetTextureUniform(), 0); - m_vdecl->SetStream(m_vbo, m_screen_coord); m_vdecl->Bind(); + m_vdecl->SetStream(m_vbo, m_screen_coord); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); m_vdecl->Unbind(); m_screen_shader->Unbind(); diff --git a/src/gpu/framebuffer.cpp b/src/gpu/framebuffer.cpp index ee1c4943..ae343e28 100644 --- a/src/gpu/framebuffer.cpp +++ b/src/gpu/framebuffer.cpp @@ -151,7 +151,7 @@ uint32_t FramebufferFormat::GetFormatOrder() case RGBA_8: case RGBA_8_I: case RGBA_8_UI: case RGBA_8_F: case RGBA_16: case RGBA_16_I: case RGBA_16_UI: case RGBA_16_F: case RGBA_32: case RGBA_32_I: case RGBA_32_UI: case RGBA_32_F: -# if defined GL_BGRA && !defined __APPLE__ +# if defined GL_BGRA && !defined __APPLE__ && !defined __NX__ return (m_invert_rgb)?(GL_BGRA):(GL_RGBA); # else return GL_RGBA; @@ -173,7 +173,10 @@ Framebuffer::Framebuffer(ivec2 size, FramebufferFormat fbo_format) { m_data->m_size = size; m_data->m_bound = false; -#if defined GL_ES_VERSION_2_0 +#if defined GL_VERSION_4_2 + GLenum internal_format = fbo_format.GetFormat(); + GLenum depth = GL_DEPTH_COMPONENT16; /* for WebGL */ +#elif defined GL_ES_VERSION_2_0 /* In OpenGL ES, internal format and format must match. */ GLenum internal_format = fbo_format.GetFormat(); GLenum format = fbo_format.GetFormat(); @@ -205,8 +208,12 @@ Framebuffer::Framebuffer(ivec2 size, FramebufferFormat fbo_format) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapmode); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering); +#if defined GL_VERSION_4_2 + glTexStorage2D(GL_TEXTURE_2D, 1, internal_format, size.x, size.y); +#else glTexImage2D(GL_TEXTURE_2D, 0, internal_format, size.x, size.y, 0, format, GL_UNSIGNED_BYTE, nullptr); +#endif #if defined GL_VERSION_1_1 || defined GL_ES_VERSION_2_0 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, diff --git a/src/gpu/shader.cpp b/src/gpu/shader.cpp index 2dca99ca..745974e3 100644 --- a/src/gpu/shader.cpp +++ b/src/gpu/shader.cpp @@ -342,7 +342,7 @@ Shader::Shader(std::string const &name, uint64_t flags = (uint64_t)(uint16_t)usage.ToScalar() << 16; flags |= (uint64_t)(uint16_t)index; // TODO: this is here just in case. Remove this once everything has been correctly tested -#if _DEBUG +#if LOL_BUILD_DEBUG if (has_key(data->attrib_locations, flags)) { msg::error("error while parsing attribute semantics in %s\n", diff --git a/src/gpu/vertexbuffer.cpp b/src/gpu/vertexbuffer.cpp index aeef3d5b..8f45ca11 100644 --- a/src/gpu/vertexbuffer.cpp +++ b/src/gpu/vertexbuffer.cpp @@ -69,15 +69,19 @@ VertexDeclaration::VertexDeclaration(VertexStreamBase const &s1, if (&s10 != &VertexStreamBase::Empty) AddStream(s10); if (&s11 != &VertexStreamBase::Empty) AddStream(s11); if (&s12 != &VertexStreamBase::Empty) AddStream(s12); + + glGenVertexArrays(1, &m_vao); } VertexDeclaration::~VertexDeclaration() { + glDeleteVertexArrays(1, &m_vao); } void VertexDeclaration::Bind() { /* FIXME: Nothing to do? */ + glBindVertexArray(m_vao); } void VertexDeclaration::DrawElements(MeshPrimitive type, int skip, int count) @@ -148,6 +152,7 @@ void VertexDeclaration::Unbind() } } glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); } void VertexDeclaration::SetStream(std::shared_ptr vb, @@ -176,15 +181,13 @@ void VertexDeclaration::SetStream(std::shared_ptr vb, ShaderAttrib return; glBindBuffer(GL_ARRAY_BUFFER, vb->m_data->m_vbo); + for (int n = 0; n < 12 && attribs[n].m_flags != (uint64_t)0 - 1; n++) { VertexUsage usage = VertexUsage((attribs[n].m_flags >> 16) & 0xffff); uint32_t index = attribs[n].m_flags & 0xffff; uint32_t reg = attribs[n].m_flags >> 32; - if (reg != 0xffffffffu) - glEnableVertexAttribArray((GLint)reg); - /* We need to parse the whole vertex declaration to retrieve * the information. It sucks. */ @@ -245,9 +248,9 @@ void VertexDeclaration::SetStream(std::shared_ptr vb, ShaderAttrib if (type_index < 0 || type_index >= (int)(sizeof(tlut) / sizeof(*tlut))) type_index = 0; - if (reg != 0xffffffffu) { + glEnableVertexAttribArray((GLint)reg); if (tlut[type_index].type == GL_FLOAT || tlut[type_index].type == GL_DOUBLE || tlut[type_index].type == GL_BYTE diff --git a/src/lol/gpu/vertexbuffer.h b/src/lol/gpu/vertexbuffer.h index 38dd34cd..4372d079 100644 --- a/src/lol/gpu/vertexbuffer.h +++ b/src/lol/gpu/vertexbuffer.h @@ -245,6 +245,7 @@ private: int reg; } m_streams[12 + 1]; + unsigned int m_vao; int m_count; }; diff --git a/src/mesh/mesh.cpp b/src/mesh/mesh.cpp index 6953a6b9..b356a56d 100644 --- a/src/mesh/mesh.cpp +++ b/src/mesh/mesh.cpp @@ -109,6 +109,8 @@ void SubMesh::Render() { size_t vertex_count = 0; + m_vdecl->Bind(); + for (int i = 0; i < m_vbos.count(); ++i) { ShaderAttrib attribs[12]; @@ -143,7 +145,6 @@ void SubMesh::Render() } m_ibo->Bind(); - m_vdecl->Bind(); m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, (int)(m_ibo->GetSize() / sizeof(uint16_t))); m_vdecl->Unbind(); m_ibo->Unbind(); diff --git a/src/scene.cpp b/src/scene.cpp index b116f40d..f6fb5ae6 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -517,8 +517,8 @@ void Scene::post_render(float) m_pp.m_shader[1]->SetUniform(m_pp.m_buffer_uni[1][0], m_renderbuffer[0]->GetTextureUniform(), 0); m_pp.m_shader[1]->SetUniform(m_pp.m_buffer_uni[1][1], m_renderbuffer[1]->GetTextureUniform(), 1); m_pp.m_shader[1]->SetUniform(m_pp.m_buffer_uni[1][2], m_renderbuffer[2]->GetTextureUniform(), 2); - m_pp.m_vdecl->SetStream(m_pp.m_vbo, m_pp.m_coord[1]); m_pp.m_vdecl->Bind(); + m_pp.m_vdecl->SetStream(m_pp.m_vbo, m_pp.m_coord[1]); m_pp.m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); m_pp.m_vdecl->Unbind(); m_pp.m_shader[1]->Unbind(); @@ -538,8 +538,8 @@ void Scene::post_render(float) /* Blit final image to screen */ m_pp.m_shader[0]->SetUniform(m_pp.m_buffer_uni[0][0], m_renderbuffer[3]->GetTextureUniform(), 3); - m_pp.m_vdecl->SetStream(m_pp.m_vbo, m_pp.m_coord[0]); m_pp.m_vdecl->Bind(); + m_pp.m_vdecl->SetStream(m_pp.m_vbo, m_pp.m_coord[0]); m_pp.m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6); m_pp.m_vdecl->Unbind(); m_pp.m_shader[0]->Unbind();