Tab->Spaces :(

This commit is contained in:
ocornut 2014-11-30 15:53:47 +00:00
parent 197b2763fc
commit a5a84a9b69
4 changed files with 291 additions and 291 deletions

View File

@ -37,13 +37,13 @@ static ID3D11BlendState* g_blendState = NULL;
struct CUSTOMVERTEX struct CUSTOMVERTEX
{ {
float pos[2]; float pos[2];
float uv[2]; float uv[2];
unsigned int col; unsigned int col;
}; };
struct VERTEX_CONSTANT_BUFFER struct VERTEX_CONSTANT_BUFFER
{ {
float mvp[4][4]; float mvp[4][4];
}; };
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure) // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
@ -59,80 +59,80 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
return; return;
// Copy and convert all vertices into a single contiguous buffer // Copy and convert all vertices into a single contiguous buffer
D3D11_MAPPED_SUBRESOURCE mappedResource; D3D11_MAPPED_SUBRESOURCE mappedResource;
if (g_pd3dDeviceImmediateContext->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource) != S_OK) if (g_pd3dDeviceImmediateContext->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource) != S_OK)
return; return;
CUSTOMVERTEX* vtx_dst = (CUSTOMVERTEX*)mappedResource.pData; CUSTOMVERTEX* vtx_dst = (CUSTOMVERTEX*)mappedResource.pData;
for (int n = 0; n < cmd_lists_count; n++) for (int n = 0; n < cmd_lists_count; n++)
{ {
const ImDrawList* cmd_list = cmd_lists[n]; const ImDrawList* cmd_list = cmd_lists[n];
const ImDrawVert* vtx_src = &cmd_list->vtx_buffer[0]; const ImDrawVert* vtx_src = &cmd_list->vtx_buffer[0];
for (size_t i = 0; i < cmd_list->vtx_buffer.size(); i++) for (size_t i = 0; i < cmd_list->vtx_buffer.size(); i++)
{ {
vtx_dst->pos[0] = vtx_src->pos.x; vtx_dst->pos[0] = vtx_src->pos.x;
vtx_dst->pos[1] = vtx_src->pos.y; vtx_dst->pos[1] = vtx_src->pos.y;
vtx_dst->uv[0] = vtx_src->uv.x; vtx_dst->uv[0] = vtx_src->uv.x;
vtx_dst->uv[1] = vtx_src->uv.y; vtx_dst->uv[1] = vtx_src->uv.y;
vtx_dst->col = vtx_src->col; vtx_dst->col = vtx_src->col;
vtx_dst++; vtx_dst++;
vtx_src++; vtx_src++;
} }
} }
g_pd3dDeviceImmediateContext->Unmap(g_pVB, 0); g_pd3dDeviceImmediateContext->Unmap(g_pVB, 0);
// Setup orthographic projection matrix into our constant buffer // Setup orthographic projection matrix into our constant buffer
{ {
D3D11_MAPPED_SUBRESOURCE mappedResource; D3D11_MAPPED_SUBRESOURCE mappedResource;
if (g_pd3dDeviceImmediateContext->Map(g_pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource) != S_OK) if (g_pd3dDeviceImmediateContext->Map(g_pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource) != S_OK)
return; return;
VERTEX_CONSTANT_BUFFER* pConstantBuffer = (VERTEX_CONSTANT_BUFFER*)mappedResource.pData; VERTEX_CONSTANT_BUFFER* pConstantBuffer = (VERTEX_CONSTANT_BUFFER*)mappedResource.pData;
const float L = 0.5f; const float L = 0.5f;
const float R = ImGui::GetIO().DisplaySize.x + 0.5f; const float R = ImGui::GetIO().DisplaySize.x + 0.5f;
const float B = ImGui::GetIO().DisplaySize.y + 0.5f; const float B = ImGui::GetIO().DisplaySize.y + 0.5f;
const float T = 0.5f; const float T = 0.5f;
const float mvp[4][4] = const float mvp[4][4] =
{ {
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f}, { 2.0f/(R-L), 0.0f, 0.0f, 0.0f},
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f,}, { 0.0f, 2.0f/(T-B), 0.0f, 0.0f,},
{ 0.0f, 0.0f, 0.5f, 0.0f }, // -1.0f { 0.0f, 0.0f, 0.5f, 0.0f }, // -1.0f
{ (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f }, // 0.0f { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f }, // 0.0f
}; };
memcpy(&pConstantBuffer->mvp, mvp, sizeof(mvp)); memcpy(&pConstantBuffer->mvp, mvp, sizeof(mvp));
g_pd3dDeviceImmediateContext->Unmap(g_pVertexConstantBuffer, 0); g_pd3dDeviceImmediateContext->Unmap(g_pVertexConstantBuffer, 0);
} }
// Setup viewport // Setup viewport
{ {
D3D11_VIEWPORT vp; D3D11_VIEWPORT vp;
memset(&vp, 0, sizeof(D3D11_VIEWPORT)); memset(&vp, 0, sizeof(D3D11_VIEWPORT));
vp.Width = ImGui::GetIO().DisplaySize.x; vp.Width = ImGui::GetIO().DisplaySize.x;
vp.Height = ImGui::GetIO().DisplaySize.y; vp.Height = ImGui::GetIO().DisplaySize.y;
vp.MinDepth = 0.0f; vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f; vp.MaxDepth = 1.0f;
vp.TopLeftX = 0; vp.TopLeftX = 0;
vp.TopLeftY = 0; vp.TopLeftY = 0;
g_pd3dDeviceImmediateContext->RSSetViewports(1, &vp); g_pd3dDeviceImmediateContext->RSSetViewports(1, &vp);
} }
// Bind shader and vertex buffers // Bind shader and vertex buffers
g_pd3dDeviceImmediateContext->IASetInputLayout(g_pInputLayout); g_pd3dDeviceImmediateContext->IASetInputLayout(g_pInputLayout);
unsigned int stride = sizeof(CUSTOMVERTEX); unsigned int stride = sizeof(CUSTOMVERTEX);
unsigned int offset = 0; unsigned int offset = 0;
g_pd3dDeviceImmediateContext->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset); g_pd3dDeviceImmediateContext->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);
g_pd3dDeviceImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); g_pd3dDeviceImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
g_pd3dDeviceImmediateContext->VSSetShader(g_pVertexShader, NULL, 0); g_pd3dDeviceImmediateContext->VSSetShader(g_pVertexShader, NULL, 0);
g_pd3dDeviceImmediateContext->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer); g_pd3dDeviceImmediateContext->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
g_pd3dDeviceImmediateContext->PSSetShader(g_pPixelShader, NULL, 0); g_pd3dDeviceImmediateContext->PSSetShader(g_pPixelShader, NULL, 0);
g_pd3dDeviceImmediateContext->PSSetShaderResources(0, 1, &g_pFontTextureView); g_pd3dDeviceImmediateContext->PSSetShaderResources(0, 1, &g_pFontTextureView);
g_pd3dDeviceImmediateContext->PSSetSamplers(0, 1, &g_pFontSampler); g_pd3dDeviceImmediateContext->PSSetSamplers(0, 1, &g_pFontSampler);
// Setup render state // Setup render state
const float blendFactor[4] = { 0.f, 0.f, 0.f, 0.f }; const float blendFactor[4] = { 0.f, 0.f, 0.f, 0.f };
const UINT sampleMask = 0xffffffff; const UINT sampleMask = 0xffffffff;
g_pd3dDeviceImmediateContext->OMSetBlendState(g_blendState, blendFactor, sampleMask); g_pd3dDeviceImmediateContext->OMSetBlendState(g_blendState, blendFactor, sampleMask);
// Render command lists // Render command lists
int vtx_offset = 0; int vtx_offset = 0;
@ -140,171 +140,171 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
{ {
// Render command list // Render command list
const ImDrawList* cmd_list = cmd_lists[n]; const ImDrawList* cmd_list = cmd_lists[n];
for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
{ {
const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
const D3D11_RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w }; const D3D11_RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w };
g_pd3dDeviceImmediateContext->RSSetScissorRects(1, &r); g_pd3dDeviceImmediateContext->RSSetScissorRects(1, &r);
g_pd3dDeviceImmediateContext->Draw(pcmd->vtx_count, vtx_offset); g_pd3dDeviceImmediateContext->Draw(pcmd->vtx_count, vtx_offset);
vtx_offset += pcmd->vtx_count; vtx_offset += pcmd->vtx_count;
} }
} }
// Restore modified state // Restore modified state
g_pd3dDeviceImmediateContext->IASetInputLayout(NULL); g_pd3dDeviceImmediateContext->IASetInputLayout(NULL);
g_pd3dDeviceImmediateContext->PSSetShader(NULL, NULL, 0); g_pd3dDeviceImmediateContext->PSSetShader(NULL, NULL, 0);
g_pd3dDeviceImmediateContext->VSSetShader(NULL, NULL, 0); g_pd3dDeviceImmediateContext->VSSetShader(NULL, NULL, 0);
} }
HRESULT InitD3D(HWND hWnd) HRESULT InitD3D(HWND hWnd)
{ {
IDXGIFactory1* pFactory = NULL; IDXGIFactory1* pFactory = NULL;
CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory); CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory);
DXGI_SWAP_CHAIN_DESC sd; DXGI_SWAP_CHAIN_DESC sd;
// Setup the swap chain // Setup the swap chain
{ {
// Setup swap chain // Setup swap chain
ZeroMemory(&sd, sizeof(sd)); ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 2; sd.BufferCount = 2;
sd.BufferDesc.Width = (UINT)ImGui::GetIO().DisplaySize.x; sd.BufferDesc.Width = (UINT)ImGui::GetIO().DisplaySize.x;
sd.BufferDesc.Height = (UINT)ImGui::GetIO().DisplaySize.y; sd.BufferDesc.Height = (UINT)ImGui::GetIO().DisplaySize.y;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60; sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1; sd.BufferDesc.RefreshRate.Denominator = 1;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd; sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 1; sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0; sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE; sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
} }
UINT createDeviceFlags = 0; UINT createDeviceFlags = 0;
#ifdef _DEBUG #ifdef _DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif #endif
D3D_FEATURE_LEVEL featureLevel; D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[1] = { D3D_FEATURE_LEVEL_11_0, }; const D3D_FEATURE_LEVEL featureLevelArray[1] = { D3D_FEATURE_LEVEL_11_0, };
if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 1, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceImmediateContext) != S_OK) if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 1, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceImmediateContext) != S_OK)
return E_FAIL; return E_FAIL;
// Setup rasterizer // Setup rasterizer
{ {
D3D11_RASTERIZER_DESC RSDesc; D3D11_RASTERIZER_DESC RSDesc;
memset(&RSDesc, 0, sizeof(D3D11_RASTERIZER_DESC)); memset(&RSDesc, 0, sizeof(D3D11_RASTERIZER_DESC));
RSDesc.FillMode = D3D11_FILL_SOLID; RSDesc.FillMode = D3D11_FILL_SOLID;
RSDesc.CullMode = D3D11_CULL_NONE; RSDesc.CullMode = D3D11_CULL_NONE;
RSDesc.FrontCounterClockwise = FALSE; RSDesc.FrontCounterClockwise = FALSE;
RSDesc.DepthBias = 0; RSDesc.DepthBias = 0;
RSDesc.SlopeScaledDepthBias = 0.0f; RSDesc.SlopeScaledDepthBias = 0.0f;
RSDesc.DepthBiasClamp = 0; RSDesc.DepthBiasClamp = 0;
RSDesc.DepthClipEnable = TRUE; RSDesc.DepthClipEnable = TRUE;
RSDesc.ScissorEnable = TRUE; RSDesc.ScissorEnable = TRUE;
RSDesc.AntialiasedLineEnable = FALSE; RSDesc.AntialiasedLineEnable = FALSE;
if (sd.SampleDesc.Count > 1) if (sd.SampleDesc.Count > 1)
RSDesc.MultisampleEnable = TRUE; RSDesc.MultisampleEnable = TRUE;
else else
RSDesc.MultisampleEnable = FALSE; RSDesc.MultisampleEnable = FALSE;
ID3D11RasterizerState* g_pRState = NULL; ID3D11RasterizerState* g_pRState = NULL;
g_pd3dDevice->CreateRasterizerState(&RSDesc, &g_pRState); g_pd3dDevice->CreateRasterizerState(&RSDesc, &g_pRState);
g_pd3dDeviceImmediateContext->RSSetState(g_pRState); g_pd3dDeviceImmediateContext->RSSetState(g_pRState);
} }
// Create the render target // Create the render target
{ {
ID3D11Texture2D* g_pBackBuffer; ID3D11Texture2D* g_pBackBuffer;
D3D11_RENDER_TARGET_VIEW_DESC render_target_view_desc; D3D11_RENDER_TARGET_VIEW_DESC render_target_view_desc;
ZeroMemory(&render_target_view_desc, sizeof(render_target_view_desc)); ZeroMemory(&render_target_view_desc, sizeof(render_target_view_desc));
render_target_view_desc.Format = sd.BufferDesc.Format; render_target_view_desc.Format = sd.BufferDesc.Format;
render_target_view_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; render_target_view_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&g_pBackBuffer); g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&g_pBackBuffer);
g_pd3dDevice->CreateRenderTargetView(g_pBackBuffer, &render_target_view_desc, &g_mainRenderTargetView); g_pd3dDevice->CreateRenderTargetView(g_pBackBuffer, &render_target_view_desc, &g_mainRenderTargetView);
g_pd3dDeviceImmediateContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL); g_pd3dDeviceImmediateContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
} }
// Create the vertex shader // Create the vertex shader
{ {
ID3D10Blob * pErrorBlob; ID3D10Blob * pErrorBlob;
D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_5_0", 0, 0, &g_pVertexShaderBlob, &pErrorBlob); D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_5_0", 0, 0, &g_pVertexShaderBlob, &pErrorBlob);
if (g_pVertexShaderBlob == NULL) if (g_pVertexShaderBlob == NULL)
{ {
const char* pError = (const char*)pErrorBlob->GetBufferPointer(); const char* pError = (const char*)pErrorBlob->GetBufferPointer();
pErrorBlob->Release(); pErrorBlob->Release();
return E_FAIL; return E_FAIL;
} }
if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), NULL, &g_pVertexShader) != S_OK) if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), NULL, &g_pVertexShader) != S_OK)
return E_FAIL; return E_FAIL;
if (pErrorBlob) if (pErrorBlob)
pErrorBlob->Release(); pErrorBlob->Release();
// Create the input layout // Create the input layout
D3D11_INPUT_ELEMENT_DESC localLayout[] = { D3D11_INPUT_ELEMENT_DESC localLayout[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, (size_t)(&((CUSTOMVERTEX*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "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 }, { "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 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((CUSTOMVERTEX*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
}; };
if (g_pd3dDevice->CreateInputLayout(localLayout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK) if (g_pd3dDevice->CreateInputLayout(localLayout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK)
return E_FAIL; return E_FAIL;
// Create the constant buffer // Create the constant buffer
{ {
D3D11_BUFFER_DESC cbDesc; D3D11_BUFFER_DESC cbDesc;
cbDesc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER); cbDesc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
cbDesc.Usage = D3D11_USAGE_DYNAMIC; cbDesc.Usage = D3D11_USAGE_DYNAMIC;
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbDesc.MiscFlags = 0; cbDesc.MiscFlags = 0;
g_pd3dDevice->CreateBuffer(&cbDesc, NULL, &g_pVertexConstantBuffer); g_pd3dDevice->CreateBuffer(&cbDesc, NULL, &g_pVertexConstantBuffer);
} }
} }
// Create the pixel shader // Create the pixel shader
{ {
ID3D10Blob * pErrorBlob; ID3D10Blob * pErrorBlob;
D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &g_pPixelShaderBlob, &pErrorBlob); D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &g_pPixelShaderBlob, &pErrorBlob);
if (g_pPixelShaderBlob == NULL) if (g_pPixelShaderBlob == NULL)
{ {
const char* pError = (const char*)pErrorBlob->GetBufferPointer(); const char* pError = (const char*)pErrorBlob->GetBufferPointer();
pErrorBlob->Release(); pErrorBlob->Release();
return E_FAIL; return E_FAIL;
} }
if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), NULL, &g_pPixelShader) != S_OK) if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), NULL, &g_pPixelShader) != S_OK)
return E_FAIL; return E_FAIL;
if (pErrorBlob) if (pErrorBlob)
pErrorBlob->Release(); pErrorBlob->Release();
} }
// Create the blending setup // Create the blending setup
{ {
D3D11_BLEND_DESC desc; D3D11_BLEND_DESC desc;
ZeroMemory(&desc, sizeof(desc)); ZeroMemory(&desc, sizeof(desc));
desc.AlphaToCoverageEnable = false; desc.AlphaToCoverageEnable = false;
desc.RenderTarget[0].BlendEnable = true; desc.RenderTarget[0].BlendEnable = true;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
g_pd3dDevice->CreateBlendState(&desc, &g_blendState); g_pd3dDevice->CreateBlendState(&desc, &g_blendState);
} }
return S_OK; return S_OK;
} }
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
@ -325,7 +325,7 @@ LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
io.MouseDown[1] = false; io.MouseDown[1] = false;
return true; return true;
case WM_MOUSEWHEEL: case WM_MOUSEWHEEL:
io.MouseWheel = GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f; io.MouseWheel = GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
return true; return true;
case WM_MOUSEMOVE: case WM_MOUSEMOVE:
// Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.) // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
@ -385,79 +385,79 @@ void InitImGui()
io.KeyMap[ImGuiKey_Z] = 'Z'; io.KeyMap[ImGuiKey_Z] = 'Z';
io.RenderDrawListsFn = ImImpl_RenderDrawLists; io.RenderDrawListsFn = ImImpl_RenderDrawLists;
io.ImeSetInputScreenPosFn = ImImpl_ImeSetInputScreenPosFn; io.ImeSetInputScreenPosFn = ImImpl_ImeSetInputScreenPosFn;
// Create the vertex buffer // Create the vertex buffer
{ {
D3D11_BUFFER_DESC bufferDesc; D3D11_BUFFER_DESC bufferDesc;
memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC)); memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
bufferDesc.Usage = D3D11_USAGE_DYNAMIC; bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
bufferDesc.ByteWidth = 10000 * sizeof(CUSTOMVERTEX); bufferDesc.ByteWidth = 10000 * sizeof(CUSTOMVERTEX);
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bufferDesc.MiscFlags = 0; bufferDesc.MiscFlags = 0;
if (g_pd3dDevice->CreateBuffer(&bufferDesc, NULL, &g_pVB) < 0) if (g_pd3dDevice->CreateBuffer(&bufferDesc, NULL, &g_pVB) < 0)
{ {
IM_ASSERT(0); IM_ASSERT(0);
return; return;
} }
} }
// Load font texture // Load font texture
// Default font (embedded in code) // Default font (embedded in code)
const void* png_data; const void* png_data;
unsigned int png_size; unsigned int png_size;
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
int tex_x, tex_y, tex_comp; int tex_x, tex_y, tex_comp;
void* tex_data = stbi_load_from_memory((const unsigned char*)png_data, (int)png_size, &tex_x, &tex_y, &tex_comp, 0); void* tex_data = stbi_load_from_memory((const unsigned char*)png_data, (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
IM_ASSERT(tex_data != NULL); IM_ASSERT(tex_data != NULL);
{ {
D3D11_TEXTURE2D_DESC desc; D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc)); ZeroMemory(&desc, sizeof(desc));
desc.Width = tex_x; desc.Width = tex_x;
desc.Height = tex_y; desc.Height = tex_y;
desc.MipLevels = 1; desc.MipLevels = 1;
desc.ArraySize = 1; desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1; desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT; desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0; desc.CPUAccessFlags = 0;
ID3D11Texture2D *pTexture = NULL; ID3D11Texture2D *pTexture = NULL;
D3D11_SUBRESOURCE_DATA subResource; D3D11_SUBRESOURCE_DATA subResource;
subResource.pSysMem = tex_data; subResource.pSysMem = tex_data;
subResource.SysMemPitch = tex_x * 4; subResource.SysMemPitch = tex_x * 4;
subResource.SysMemSlicePitch = 0; subResource.SysMemSlicePitch = 0;
g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture); g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
// create texture view // create texture view
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
ZeroMemory(&srvDesc, sizeof(srvDesc)); ZeroMemory(&srvDesc, sizeof(srvDesc));
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = desc.MipLevels; srvDesc.Texture2D.MipLevels = desc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0; srvDesc.Texture2D.MostDetailedMip = 0;
g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView); g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView);
} }
// create texture sampler // create texture sampler
{ {
D3D11_SAMPLER_DESC desc; D3D11_SAMPLER_DESC desc;
ZeroMemory(&desc, sizeof(desc)); ZeroMemory(&desc, sizeof(desc));
desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
desc.MipLODBias = 0.f; desc.MipLODBias = 0.f;
desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
desc.MinLOD = 0.f; desc.MinLOD = 0.f;
desc.MaxLOD = 0.f; desc.MaxLOD = 0.f;
g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler); g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler);
} }
} }
INT64 ticks_per_second = 0; INT64 ticks_per_second = 0;
@ -570,10 +570,10 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
} }
// Rendering // Rendering
float clearColor[4] = { 204 / 255.f, 153 / 255.f, 153 / 255.f }; float clearColor[4] = { 204 / 255.f, 153 / 255.f, 153 / 255.f };
g_pd3dDeviceImmediateContext->ClearRenderTargetView(g_mainRenderTargetView, clearColor); g_pd3dDeviceImmediateContext->ClearRenderTargetView(g_mainRenderTargetView, clearColor);
ImGui::Render(); ImGui::Render();
g_pSwapChain->Present(0, 0); g_pSwapChain->Present(0, 0);
} }
ImGui::Shutdown(); ImGui::Shutdown();
@ -585,43 +585,43 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
static const char* vertexShader = "\ static const char* vertexShader = "\
cbuffer vertexBuffer : register(c0) \ cbuffer vertexBuffer : register(c0) \
{\ {\
float4x4 ProjectionMatrix; \ float4x4 ProjectionMatrix; \
};\ };\
struct VS_INPUT\ struct VS_INPUT\
{\ {\
float2 pos : POSITION;\ float2 pos : POSITION;\
float4 col : COLOR0;\ float4 col : COLOR0;\
float2 uv : TEXCOORD0;\ float2 uv : TEXCOORD0;\
};\ };\
\ \
struct PS_INPUT\ struct PS_INPUT\
{\ {\
float4 pos : SV_POSITION;\ float4 pos : SV_POSITION;\
float4 col : COLOR0;\ float4 col : COLOR0;\
float2 uv : TEXCOORD0;\ float2 uv : TEXCOORD0;\
};\ };\
\ \
PS_INPUT main(VS_INPUT input)\ PS_INPUT main(VS_INPUT input)\
{\ {\
PS_INPUT output;\ PS_INPUT output;\
output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\ output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
output.col = input.col;\ output.col = input.col;\
output.uv = input.uv;\ output.uv = input.uv;\
return output;\ return output;\
}"; }";
static const char* pixelShader = "\ static const char* pixelShader = "\
struct PS_INPUT\ struct PS_INPUT\
{\ {\
float4 pos : SV_POSITION;\ float4 pos : SV_POSITION;\
float4 col : COLOR0;\ float4 col : COLOR0;\
float2 uv : TEXCOORD0;\ float2 uv : TEXCOORD0;\
};\ };\
sampler sampler0;\ sampler sampler0;\
Texture2D texture0;\ Texture2D texture0;\
\ \
float4 main(PS_INPUT input) : SV_Target\ float4 main(PS_INPUT input) : SV_Target\
{\ {\
float4 out_col = texture0.Sample(sampler0, input.uv);\ float4 out_col = texture0.Sample(sampler0, input.uv);\
return input.col * out_col;\ return input.col * out_col;\
}"; }";

View File

@ -18,7 +18,7 @@ struct CUSTOMVERTEX
{ {
D3DXVECTOR3 pos; D3DXVECTOR3 pos;
D3DCOLOR col; D3DCOLOR col;
D3DXVECTOR2 uv; D3DXVECTOR2 uv;
}; };
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
@ -48,8 +48,8 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
vtx_dst->pos.y = vtx_src->pos.y; vtx_dst->pos.y = vtx_src->pos.y;
vtx_dst->pos.z = 0.0f; vtx_dst->pos.z = 0.0f;
vtx_dst->col = (vtx_src->col & 0xFF00FF00) | ((vtx_src->col & 0xFF0000)>>16) | ((vtx_src->col & 0xFF) << 16); // RGBA --> ARGB for DirectX9 vtx_dst->col = (vtx_src->col & 0xFF00FF00) | ((vtx_src->col & 0xFF0000)>>16) | ((vtx_src->col & 0xFF) << 16); // RGBA --> ARGB for DirectX9
vtx_dst->uv.x = vtx_src->uv.x; vtx_dst->uv.x = vtx_src->uv.x;
vtx_dst->uv.y = vtx_src->uv.y; vtx_dst->uv.y = vtx_src->uv.y;
vtx_dst++; vtx_dst++;
vtx_src++; vtx_src++;
} }
@ -93,9 +93,9 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
{ {
// Render command list // Render command list
const ImDrawList* cmd_list = cmd_lists[n]; const ImDrawList* cmd_list = cmd_lists[n];
for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
{ {
const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
const RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w }; const RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w };
g_pd3dDevice->SetScissorRect(&r); g_pd3dDevice->SetScissorRect(&r);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vtx_offset, pcmd->vtx_count/3); g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vtx_offset, pcmd->vtx_count/3);

View File

@ -69,17 +69,17 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + offsetof(ImDrawVert, col))); glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + offsetof(ImDrawVert, col)));
int vtx_offset = 0; int vtx_offset = 0;
for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
{ {
const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y)); glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count); glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
vtx_offset += pcmd->vtx_count; vtx_offset += pcmd->vtx_count;
} }
} }
// Restore modified state // Restore modified state
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);

View File

@ -4331,7 +4331,7 @@ void ImGuiTextEditCallbackData::InsertChars(int pos, const char* new_text, const
if (new_text_len + text_len + 1 >= BufSize) if (new_text_len + text_len + 1 >= BufSize)
return; return;
size_t upos = (size_t)pos; size_t upos = (size_t)pos;
if (text_len != upos) if (text_len != upos)
memmove(Buf + upos + new_text_len, Buf + upos, text_len - upos); memmove(Buf + upos + new_text_len, Buf + upos, text_len - upos);
memcpy(Buf + upos, new_text, new_text_len * sizeof(char)); memcpy(Buf + upos, new_text, new_text_len * sizeof(char));
@ -5798,7 +5798,7 @@ bool ImFont::LoadFromMemory(const void* data, size_t data_size)
IM_ASSERT(Kerning == NULL && KerningCount == 0); IM_ASSERT(Kerning == NULL && KerningCount == 0);
Kerning = (FntKerning*)p; Kerning = (FntKerning*)p;
KerningCount = block_size / sizeof(FntKerning); KerningCount = block_size / sizeof(FntKerning);
break; break;
default: default:
break; break;
} }