From 37f1715bfa3122e7c8cc7cc03f9dd9959b86faaa Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 9 Apr 2015 21:14:52 +0100 Subject: [PATCH 1/3] Examples: DirectX11: Removed unnecessary vertices conversion and CUSTOMVERTEX types. --- .../directx11_example/imgui_impl_dx11.cpp | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/examples/directx11_example/imgui_impl_dx11.cpp b/examples/directx11_example/imgui_impl_dx11.cpp index ba8d3d8b..4b5513ad 100644 --- a/examples/directx11_example/imgui_impl_dx11.cpp +++ b/examples/directx11_example/imgui_impl_dx11.cpp @@ -29,13 +29,6 @@ static ID3D11ShaderResourceView*g_pFontTextureView = NULL; static ID3D11BlendState* g_blendState = NULL; static int VERTEX_BUFFER_SIZE = 30000; // TODO: Make vertex buffer smaller and grow dynamically as needed. -struct CUSTOMVERTEX -{ - float pos[2]; - float uv[2]; - unsigned int col; -}; - struct VERTEX_CONSTANT_BUFFER { float mvp[4][4]; @@ -50,21 +43,13 @@ static void ImGui_ImplDX11_RenderDrawLists(ImDrawList** const cmd_lists, int cmd D3D11_MAPPED_SUBRESOURCE mappedResource; if (g_pd3dDeviceContext->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource) != S_OK) return; - CUSTOMVERTEX* vtx_dst = (CUSTOMVERTEX*)mappedResource.pData; + ImDrawVert* vtx_dst = (ImDrawVert*)mappedResource.pData; for (int n = 0; n < cmd_lists_count; n++) { const ImDrawList* cmd_list = cmd_lists[n]; const ImDrawVert* vtx_src = &cmd_list->vtx_buffer[0]; - for (size_t i = 0; i < cmd_list->vtx_buffer.size(); i++) - { - vtx_dst->pos[0] = vtx_src->pos.x; - vtx_dst->pos[1] = vtx_src->pos.y; - vtx_dst->uv[0] = vtx_src->uv.x; - vtx_dst->uv[1] = vtx_src->uv.y; - vtx_dst->col = vtx_src->col; - vtx_dst++; - vtx_src++; - } + memcpy(vtx_dst, vtx_src, cmd_list->vtx_buffer.size() * sizeof(ImDrawVert)); + vtx_dst += cmd_list->vtx_buffer.size(); } g_pd3dDeviceContext->Unmap(g_pVB, 0); @@ -104,7 +89,7 @@ static void ImGui_ImplDX11_RenderDrawLists(ImDrawList** const cmd_lists, int cmd } // Bind shader and vertex buffers - unsigned int stride = sizeof(CUSTOMVERTEX); + unsigned int stride = sizeof(ImDrawVert); unsigned int offset = 0; g_pd3dDeviceContext->IASetInputLayout(g_pInputLayout); g_pd3dDeviceContext->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset); @@ -293,9 +278,9 @@ bool ImGui_ImplDX11_CreateDeviceObjects() // Create the input layout D3D11_INPUT_ELEMENT_DESC localLayout[] = { - { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, (size_t)(&((CUSTOMVERTEX*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 }, - { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((CUSTOMVERTEX*)0)->col), D3D11_INPUT_PER_VERTEX_DATA, 0 }, - { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((CUSTOMVERTEX*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; if (g_pd3dDevice->CreateInputLayout(localLayout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK) @@ -359,7 +344,7 @@ bool ImGui_ImplDX11_CreateDeviceObjects() D3D11_BUFFER_DESC bufferDesc; memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC)); bufferDesc.Usage = D3D11_USAGE_DYNAMIC; - bufferDesc.ByteWidth = VERTEX_BUFFER_SIZE * sizeof(CUSTOMVERTEX); + bufferDesc.ByteWidth = VERTEX_BUFFER_SIZE * sizeof(ImDrawVert); bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; bufferDesc.MiscFlags = 0; From 90766141b3316fbd87c1e8d167b30b7735a1991d Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 9 Apr 2015 21:18:43 +0100 Subject: [PATCH 2/3] Examples: OpenGL3: Tweaks. --- examples/opengl3_example/imgui_impl_glfw_gl3.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/opengl3_example/imgui_impl_glfw_gl3.cpp b/examples/opengl3_example/imgui_impl_glfw_gl3.cpp index 1cd2faf0..b7f225ea 100644 --- a/examples/opengl3_example/imgui_impl_glfw_gl3.cpp +++ b/examples/opengl3_example/imgui_impl_glfw_gl3.cpp @@ -23,7 +23,7 @@ static GLuint g_FontTexture = 0; static int g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0; static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; static int g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0; -static size_t g_VboMaxSize = 20000; +static size_t g_VboSize = 0; static unsigned int g_VboHandle = 0, g_VaoHandle = 0; // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure) @@ -62,11 +62,10 @@ static void ImGui_ImplGlfwGL3_RenderDrawLists(ImDrawList** const cmd_lists, int for (int n = 0; n < cmd_lists_count; n++) total_vtx_count += cmd_lists[n]->vtx_buffer.size(); glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); - size_t neededBufferSize = total_vtx_count * sizeof(ImDrawVert); - if (neededBufferSize > g_VboMaxSize) + size_t needed_vtx_size = total_vtx_count * sizeof(ImDrawVert); + if (g_VboSize < needed_vtx_size) { - g_VboMaxSize = neededBufferSize + 5000; // Grow buffer - glBufferData(GL_ARRAY_BUFFER, g_VboMaxSize, NULL, GL_STREAM_DRAW); + glBufferData(GL_ARRAY_BUFFER, g_VboSize, NULL, GL_STREAM_DRAW); } // Copy and convert all vertices into a single contiguous buffer From 4f51b77937b395d61a84d94837e5c195fc053c66 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 9 Apr 2015 21:19:21 +0100 Subject: [PATCH 3/3] Examples: OpenGL3: Tweaks (argh, github ui) --- examples/opengl3_example/imgui_impl_glfw_gl3.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/opengl3_example/imgui_impl_glfw_gl3.cpp b/examples/opengl3_example/imgui_impl_glfw_gl3.cpp index b7f225ea..3bba00d6 100644 --- a/examples/opengl3_example/imgui_impl_glfw_gl3.cpp +++ b/examples/opengl3_example/imgui_impl_glfw_gl3.cpp @@ -65,6 +65,7 @@ static void ImGui_ImplGlfwGL3_RenderDrawLists(ImDrawList** const cmd_lists, int size_t needed_vtx_size = total_vtx_count * sizeof(ImDrawVert); if (g_VboSize < needed_vtx_size) { + g_VboSize = needed_vtx_size + 5000 * sizeof(ImDrawVert); // Grow buffer glBufferData(GL_ARRAY_BUFFER, g_VboSize, NULL, GL_STREAM_DRAW); } @@ -216,8 +217,6 @@ bool ImGui_ImplGlfwGL3_CreateDeviceObjects() g_AttribLocationColor = glGetAttribLocation(g_ShaderHandle, "Color"); glGenBuffers(1, &g_VboHandle); - glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); - glBufferData(GL_ARRAY_BUFFER, g_VboMaxSize, NULL, GL_DYNAMIC_DRAW); glGenVertexArrays(1, &g_VaoHandle); glBindVertexArray(g_VaoHandle);