mirror of
https://github.com/ocornut/imgui.git
synced 2024-12-18 08:51:06 +08:00
Examples: DirectX11: resizing buffers dynamically (#299)
This commit is contained in:
parent
c016f6c171
commit
215747635d
@ -29,8 +29,7 @@ static ID3D11SamplerState* g_pFontSampler = NULL;
|
|||||||
static ID3D11ShaderResourceView*g_pFontTextureView = NULL;
|
static ID3D11ShaderResourceView*g_pFontTextureView = NULL;
|
||||||
static ID3D11RasterizerState* g_pRasterizerState = NULL;
|
static ID3D11RasterizerState* g_pRasterizerState = NULL;
|
||||||
static ID3D11BlendState* g_pBlendState = NULL;
|
static ID3D11BlendState* g_pBlendState = NULL;
|
||||||
static int VERTEX_BUFFER_SIZE = 20000; // TODO: Make buffers smaller and grow dynamically as needed.
|
static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
|
||||||
static int INDEX_BUFFER_SIZE = 40000; // TODO: Make buffers smaller and grow dynamically as needed.
|
|
||||||
|
|
||||||
struct VERTEX_CONSTANT_BUFFER
|
struct VERTEX_CONSTANT_BUFFER
|
||||||
{
|
{
|
||||||
@ -42,6 +41,35 @@ struct VERTEX_CONSTANT_BUFFER
|
|||||||
// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
|
// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
|
||||||
static void ImGui_ImplDX11_RenderDrawLists(ImDrawData* draw_data)
|
static void ImGui_ImplDX11_RenderDrawLists(ImDrawData* draw_data)
|
||||||
{
|
{
|
||||||
|
// Create and grow vertex/index buffers if needed
|
||||||
|
if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
|
||||||
|
{
|
||||||
|
if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
|
||||||
|
g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
|
||||||
|
D3D11_BUFFER_DESC desc;
|
||||||
|
memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
|
||||||
|
desc.Usage = D3D11_USAGE_DYNAMIC;
|
||||||
|
desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert);
|
||||||
|
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||||
|
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||||
|
desc.MiscFlags = 0;
|
||||||
|
if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVB) < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
|
||||||
|
{
|
||||||
|
if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
|
||||||
|
g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
|
||||||
|
D3D11_BUFFER_DESC bufferDesc;
|
||||||
|
memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
|
||||||
|
bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||||
|
bufferDesc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx);
|
||||||
|
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
||||||
|
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||||
|
if (g_pd3dDevice->CreateBuffer(&bufferDesc, NULL, &g_pIB) < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Copy and convert all vertices into a single contiguous buffer
|
// Copy and convert all vertices into a single contiguous buffer
|
||||||
D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
|
D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
|
||||||
if (g_pd3dDeviceContext->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
|
if (g_pd3dDeviceContext->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
|
||||||
@ -253,7 +281,7 @@ bool ImGui_ImplDX11_CreateDeviceObjects()
|
|||||||
{
|
{
|
||||||
if (!g_pd3dDevice)
|
if (!g_pd3dDevice)
|
||||||
return false;
|
return false;
|
||||||
if (g_pVB)
|
if (g_pFontSampler)
|
||||||
ImGui_ImplDX11_InvalidateDeviceObjects();
|
ImGui_ImplDX11_InvalidateDeviceObjects();
|
||||||
|
|
||||||
// Create the vertex shader
|
// Create the vertex shader
|
||||||
@ -366,31 +394,6 @@ bool ImGui_ImplDX11_CreateDeviceObjects()
|
|||||||
g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
|
g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the vertex buffer
|
|
||||||
{
|
|
||||||
D3D11_BUFFER_DESC desc;
|
|
||||||
memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
|
|
||||||
desc.Usage = D3D11_USAGE_DYNAMIC;
|
|
||||||
desc.ByteWidth = VERTEX_BUFFER_SIZE * sizeof(ImDrawVert);
|
|
||||||
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
||||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
||||||
desc.MiscFlags = 0;
|
|
||||||
if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVB) < 0)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the index buffer
|
|
||||||
{
|
|
||||||
D3D11_BUFFER_DESC bufferDesc;
|
|
||||||
memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
|
|
||||||
bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
|
||||||
bufferDesc.ByteWidth = INDEX_BUFFER_SIZE * sizeof(ImDrawIdx);
|
|
||||||
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
|
||||||
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
||||||
if (g_pd3dDevice->CreateBuffer(&bufferDesc, NULL, &g_pIB) < 0)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ImGui_ImplDX11_CreateFontsTexture();
|
ImGui_ImplDX11_CreateFontsTexture();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -32,17 +32,17 @@ struct CUSTOMVERTEX
|
|||||||
// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
|
// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
|
||||||
static void ImGui_ImplDX9_RenderDrawLists(ImDrawData* draw_data)
|
static void ImGui_ImplDX9_RenderDrawLists(ImDrawData* draw_data)
|
||||||
{
|
{
|
||||||
// Grow buffers if needed
|
// Create and grow buffers if needed
|
||||||
if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
|
if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
|
||||||
{
|
{
|
||||||
if (g_pVB) g_pVB->Release();
|
if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
|
||||||
g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
|
g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
|
||||||
if (g_pd3dDevice->CreateVertexBuffer(g_VertexBufferSize * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0)
|
if (g_pd3dDevice->CreateVertexBuffer(g_VertexBufferSize * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
|
if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
|
||||||
{
|
{
|
||||||
if (g_pIB) g_pIB->Release();
|
if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
|
||||||
g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
|
g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
|
||||||
if (g_pd3dDevice->CreateIndexBuffer(g_IndexBufferSize * sizeof(ImDrawIdx), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIB, NULL) < 0)
|
if (g_pd3dDevice->CreateIndexBuffer(g_IndexBufferSize * sizeof(ImDrawIdx), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIB, NULL) < 0)
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user