diff --git a/src/DivaGL/Glitter/glitter.cpp b/src/DivaGL/Glitter/glitter.cpp index 8914149..8353ca4 100644 --- a/src/DivaGL/Glitter/glitter.cpp +++ b/src/DivaGL/Glitter/glitter.cpp @@ -30,14 +30,14 @@ namespace Glitter { *angle = (float_t)M_PI - *angle; } - void CreateBuffer(int32_t max_count, bool is_quad, GLuint& vao, GLuint& vbo, GLuint& ebo) { + void CreateBuffer(int32_t max_count, bool is_quad, GLuint& vao, GLArrayBuffer& vbo, GLElementArrayBuffer& ebo) { glGenVertexArrays(1, &vao); gl_state_bind_vertex_array(vao, true); - glGenBuffers(1, &vbo); - gl_state_bind_array_buffer(vbo, true); + static const GLsizei buffer_size = sizeof(Buffer); - glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(sizeof(Buffer) * max_count), 0, GL_DYNAMIC_DRAW); + vbo.Create(buffer_size * (size_t)max_count); + vbo.Bind(); if (is_quad) { size_t count = (size_t)max_count / 4 * 5; @@ -51,15 +51,12 @@ namespace Glitter { ebo_data[i + 4] = 0xFFFFFFFF; } - glGenBuffers(1, &ebo); - gl_state_bind_element_array_buffer(ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int32_t) * (count - 1), ebo_data, GL_STATIC_DRAW); + ebo.Create(sizeof(uint16_t) * count, ebo_data); + ebo.Bind(); _operator_delete(ebo_data); } else - ebo = 0; - - static const GLsizei buffer_size = sizeof(Buffer); + ebo = {}; glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, buffer_size, @@ -77,16 +74,9 @@ namespace Glitter { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } - void DeleteBuffer(GLuint& vao, GLuint& vbo, GLuint& ebo) { - if (ebo) { - glDeleteBuffers(1, &ebo); - ebo = 0; - } - - if (vbo) { - glDeleteBuffers(1, &vbo); - vbo = 0; - } + void DeleteBuffer(GLuint& vao, GLArrayBuffer& vbo, GLElementArrayBuffer& ebo) { + ebo.Destroy(); + vbo.Destroy(); if (vao) { glDeleteVertexArrays(1, &vao); diff --git a/src/DivaGL/Glitter/glitter.hpp b/src/DivaGL/Glitter/glitter.hpp index f1616be..9d9e7fb 100644 --- a/src/DivaGL/Glitter/glitter.hpp +++ b/src/DivaGL/Glitter/glitter.hpp @@ -8,6 +8,8 @@ #include "../../KKdLib/mat.hpp" #include "../../KKdLib/vec.hpp" #include "../gl.hpp" +#include "../gl_array_buffer.hpp" +#include "../gl_element_array_buffer.hpp" #include "../types.hpp" #include "../texture.hpp" @@ -219,11 +221,11 @@ namespace Glitter { void* particle_inst; // Glitter::ParticleInst DispType disp_type; FogType fog_type; - GLuint vbo; + GLArrayBuffer vbo; float_t emission; bool use_own_buffer; GLuint vao; - GLuint ebo; + GLElementArrayBuffer ebo; size_t disp; mat4 mat_draw; DrawListData* draw_list; @@ -313,8 +315,9 @@ namespace Glitter { extern void axis_angle_from_vectors(vec3* axis, float_t* angle, const vec3* vec0, const vec3* vec1); - extern void CreateBuffer(int32_t max_count, bool is_quad, GLuint& vao, GLuint& vbo, GLuint& ebo); - extern void DeleteBuffer(GLuint& vao, GLuint& vbo, GLuint& ebo); + extern void CreateBuffer(int32_t max_count, bool is_quad, + GLuint& vao, GLArrayBuffer& vbo, GLElementArrayBuffer& ebo); + extern void DeleteBuffer(GLuint& vao, GLArrayBuffer& vbo, GLElementArrayBuffer& ebo); extern void Patch(); diff --git a/src/DivaGL/Glitter/particle.cpp b/src/DivaGL/Glitter/particle.cpp index 7f93421..fd96c22 100644 --- a/src/DivaGL/Glitter/particle.cpp +++ b/src/DivaGL/Glitter/particle.cpp @@ -8,26 +8,26 @@ namespace Glitter { void Particle::CreateBuffer(size_t data) { ParticleType type = (ParticleType) * (int32_t*)(data + 0x40 + 0x00 + 0x08); - GLuint& vbo = ((GLuint*)(data + 0x40 + 0x160))[0]; + GLArrayBuffer& vbo = *(GLArrayBuffer*)(data + 0x40 + 0x160); int32_t max_count = *(int32_t*)(data + 0x40 + 0x164); - GLuint& vao = ((GLuint*)(data + 0x40 + 0x170))[0]; - GLuint& ebo = ((GLuint*)(data + 0x40 + 0x170))[1]; + GLuint& vao = *(GLuint*)(data + 0x40 + 0x170); + GLElementArrayBuffer& ebo = *(GLElementArrayBuffer*)(data + 0x40 + 0x174); Glitter::CreateBuffer(max_count, type == PARTICLE_QUAD, vao, vbo, ebo); - DrawListData*& draw_list = *(DrawListData**)(data + 0x40 + 0x170 + sizeof(GLuint) * 2); + DrawListData*& draw_list = *(DrawListData**)(data + 0x40 + 0x178); draw_list = new DrawListData; } void Particle::DeleteBuffer(size_t data) { - DrawListData*& draw_list = *(DrawListData**)(data + 0x40 + 0x170 + sizeof(GLuint) * 2); + DrawListData*& draw_list = *(DrawListData**)(data + 0x40 + 0x178); if (draw_list) { delete draw_list; draw_list = 0; } - GLuint& vbo = ((GLuint*)(data + 0x40 + 0x160))[0]; - GLuint& vao = ((GLuint*)(data + 0x40 + 0x170))[0]; - GLuint& ebo = ((GLuint*)(data + 0x40 + 0x170))[1]; + GLArrayBuffer& vbo = *(GLArrayBuffer*)(data + 0x40 + 0x160); + GLuint& vao = *(GLuint*)(data + 0x40 + 0x170); + GLElementArrayBuffer& ebo = *(GLElementArrayBuffer*)(data + 0x40 + 0x174); Glitter::DeleteBuffer(vao, vbo, ebo); } } diff --git a/src/DivaGL/Glitter/render_group.cpp b/src/DivaGL/Glitter/render_group.cpp index 77ae9d0..ea97ce8 100644 --- a/src/DivaGL/Glitter/render_group.cpp +++ b/src/DivaGL/Glitter/render_group.cpp @@ -61,19 +61,15 @@ namespace Glitter { if (!rend_group->buffer) rend_group->max_count = 0; - ParticleType type = rend_group->type; - GLuint& vbo = rend_group->vbo; - int32_t max_count = rend_group->max_count; - GLuint& vao = rend_group->vao; - GLuint& ebo = rend_group->ebo; - Glitter::CreateBuffer(max_count, type == PARTICLE_QUAD, vao, vbo, ebo); + Glitter::CreateBuffer(rend_group->max_count, rend_group->type == PARTICLE_QUAD, + rend_group->vao, rend_group->vbo, rend_group->ebo); rend_group->draw_list = new DrawListData; } } else { size_t particle = *(size_t*)((size_t)rend_group->particle_inst + 0x98 + 0x178); - rend_group->vao = ((GLuint*)(particle + 0x40 + 0x170))[0]; - rend_group->ebo = ((GLuint*)(particle + 0x40 + 0x170))[1]; + rend_group->vao = *((GLuint*)(particle + 0x40 + 0x170)); + rend_group->ebo = *((GLElementArrayBuffer*)(particle + 0x40 + 0x174)); rend_group->draw_list = *(DrawListData**)(particle + 0x40 + 0x170 + sizeof(GLuint) * 2); } @@ -89,14 +85,11 @@ namespace Glitter { rend_group->draw_list = 0; } - GLuint& vbo = rend_group->vbo; - GLuint& vao = rend_group->vao; - GLuint& ebo = rend_group->ebo; - Glitter::DeleteBuffer(vao, vbo, ebo); + Glitter::DeleteBuffer(rend_group->vao, rend_group->vbo, rend_group->ebo); } else { rend_group->vbo = 0; - rend_group->ebo = 0; + rend_group->ebo = {}; rend_group->draw_list = 0; } } diff --git a/src/DivaGL/Glitter/render_scene.cpp b/src/DivaGL/Glitter/render_scene.cpp index a3c7df1..6d03e8b 100644 --- a/src/DivaGL/Glitter/render_scene.cpp +++ b/src/DivaGL/Glitter/render_scene.cpp @@ -58,7 +58,7 @@ namespace Glitter { } void RenderScene::CalcDispLine(RenderGroup* rend_group) { - if (!rend_group->elements || !rend_group->vbo || rend_group->ctrl < 1) + if (!rend_group->elements || rend_group->vbo.IsNull() || rend_group->ctrl < 1) return; size_t count = 0; @@ -96,19 +96,9 @@ namespace Glitter { else rend_group->mat_draw = mat4_identity; - Buffer* buf; - if (GL_VERSION_4_5) - buf = (Buffer*)glMapNamedBuffer(rend_group->vbo, GL_WRITE_ONLY); - else { - gl_state_bind_array_buffer(rend_group->vbo); - buf = (Buffer*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); - } - - if (!buf) { - if (!GL_VERSION_4_5) - gl_state_bind_array_buffer(0); + Buffer* buf = (Buffer*)rend_group->vbo.MapMemory(); + if (!buf) return; - } elem = rend_group->elements; size_t disp = 0; @@ -151,16 +141,11 @@ namespace Glitter { } rend_group->disp = disp; - if (GL_VERSION_4_5) - glUnmapNamedBuffer(rend_group->vbo); - else { - glUnmapBuffer(GL_ARRAY_BUFFER); - gl_state_bind_array_buffer(0); - } + rend_group->vbo.UnmapMemory(); } void RenderScene::CalcDispLocus(RenderGroup* rend_group) { - if (!rend_group->elements || !rend_group->vbo || rend_group->ctrl < 1) + if (!rend_group->elements || rend_group->vbo.IsNull() || rend_group->ctrl < 1) return; size_t count = 0; @@ -229,19 +214,9 @@ namespace Glitter { mat3_invert(&cam_inv_view_mat3, &cam_inv_view_mat3); mat3_transform_vector(&cam_inv_view_mat3, &x_vec, &x_vec); - Buffer* buf; - if (GL_VERSION_4_5) - buf = (Buffer*)glMapNamedBuffer(rend_group->vbo, GL_WRITE_ONLY); - else { - gl_state_bind_array_buffer(rend_group->vbo); - buf = (Buffer*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); - } - - if (!buf) { - if (!GL_VERSION_4_5) - gl_state_bind_array_buffer(0); + Buffer* buf = (Buffer*)rend_group->vbo.MapMemory(); + if (!buf) return; - } elem = rend_group->elements; vec2 split_uv = rend_group->split_uv; @@ -326,16 +301,11 @@ namespace Glitter { } rend_group->disp = disp; - if (GL_VERSION_4_5) - glUnmapNamedBuffer(rend_group->vbo); - else { - glUnmapBuffer(GL_ARRAY_BUFFER); - gl_state_bind_array_buffer(0); - } + rend_group->vbo.UnmapMemory(); } void RenderScene::CalcDispQuad(RenderGroup* rend_group) { - if (!rend_group->elements || !rend_group->vbo || rend_group->ctrl < 1) + if (!rend_group->elements || rend_group->vbo.IsNull() || rend_group->ctrl < 1) return; mat4 cam_view; @@ -445,19 +415,9 @@ namespace Glitter { vec3 scale; mat4_get_scale(model_mat, &scale); - Buffer* buf; - if (GL_VERSION_4_5) - buf = (Buffer*)glMapNamedBuffer(rend_group->vbo, GL_WRITE_ONLY); - else { - gl_state_bind_array_buffer(rend_group->vbo); - buf = (Buffer*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); - } - - if (!buf) { - if (!GL_VERSION_4_5) - gl_state_bind_array_buffer(0); + Buffer* buf = (Buffer*)rend_group->vbo.MapMemory(); + if (!buf) return; - } bool use_scale = rend_group->flags & PARTICLE_SCALE ? true : false; RenderElement* elem = rend_group->elements; @@ -539,12 +499,7 @@ namespace Glitter { } rend_group->disp = disp; - if (GL_VERSION_4_5) - glUnmapNamedBuffer(rend_group->vbo); - else { - glUnmapBuffer(GL_ARRAY_BUFFER); - gl_state_bind_array_buffer(0); - } + rend_group->vbo.UnmapMemory(); } void RenderScene::CalcDispQuadNormal( @@ -615,19 +570,9 @@ namespace Glitter { mat4_transform_vector(&inv_model_mat, &x_vec, &x_vec); mat4_transform_vector(&inv_model_mat, &y_vec, &y_vec); - Buffer* buf; - if (GL_VERSION_4_5) - buf = (Buffer*)glMapNamedBuffer(rend_group->vbo, GL_WRITE_ONLY); - else { - gl_state_bind_array_buffer(rend_group->vbo); - buf = (Buffer*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); - } - - if (!buf) { - if (!GL_VERSION_4_5) - gl_state_bind_array_buffer(0); + Buffer* buf = (Buffer*)rend_group->vbo.MapMemory(); + if (!buf) return; - } RenderElement* elem = rend_group->elements; vec2 split_uv = rend_group->split_uv; @@ -781,12 +726,7 @@ namespace Glitter { } rend_group->disp = disp; - if (GL_VERSION_4_5) - glUnmapNamedBuffer(rend_group->vbo); - else { - glUnmapBuffer(GL_ARRAY_BUFFER); - gl_state_bind_array_buffer(0); - } + rend_group->vbo.UnmapMemory(); } void RenderScene::Disp(RenderScene* rend_sc, RenderGroup* rend_group) { diff --git a/src/DivaGL/gl_array_buffer.hpp b/src/DivaGL/gl_array_buffer.hpp index 9e0a57b..cfa6714 100644 --- a/src/DivaGL/gl_array_buffer.hpp +++ b/src/DivaGL/gl_array_buffer.hpp @@ -17,6 +17,10 @@ public: } + inline GLArrayBuffer(GLuint buffer) : buffer(buffer) { + + } + inline ~GLArrayBuffer() { } diff --git a/src/DivaGL/rob/rob.cpp b/src/DivaGL/rob/rob.cpp index 778f905..3b2633d 100644 --- a/src/DivaGL/rob/rob.cpp +++ b/src/DivaGL/rob/rob.cpp @@ -81,36 +81,17 @@ HOOK(void, FASTCALL, rob_chara_age_age_object__disp, 0x0000000140541D50, rob_age_age_obj->obj_vert_buf.cycle_index(); - GLuint buffer = rob_age_age_obj->obj_vert_buf.get_buffer(); - size_t vtx_data; - if (GL_VERSION_4_5) { - vtx_data = (size_t)glMapNamedBuffer(buffer, GL_WRITE_ONLY); - if (!vtx_data) { - glUnmapNamedBuffer(buffer); - return; - } - } - else { - gl_state_bind_array_buffer(buffer); - vtx_data = (size_t)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); - if (!vtx_data) { - glUnmapBuffer(GL_ARRAY_BUFFER); - gl_state_bind_array_buffer(0); - return; - } - } + GLArrayBuffer buffer = rob_age_age_obj->obj_vert_buf.get_buffer(); + size_t vtx_data = (size_t)buffer.MapMemory(); + if (!vtx_data) + return; size_t vertex_array_size = rob_age_age_obj->vertex_array_size; for (int32_t i = 0; i < disp_count; i++) memmove((void*)(vtx_data + vertex_array_size * i), (void*)((size_t)rob_age_age_obj->vertex_data + vertex_array_size * v44[i].second), vertex_array_size); - if (GL_VERSION_4_5) - glUnmapNamedBuffer(buffer); - else { - glUnmapBuffer(GL_ARRAY_BUFFER); - gl_state_bind_array_buffer(0); - } + buffer.UnmapMemory(); rob_age_age_obj->mesh.num_vertex = disp_count * rob_age_age_obj->num_vertex; rob_age_age_obj->sub_mesh.num_index = disp_count * rob_age_age_obj->num_index;