correct resizing of D2D over D3D - c++

I have problem with resizing D2D render target over D3D11. I make function to resize swap chain buffer.
It works great, but when I activate D2D, resizing became broken.
When I make window smaller, all meshes disappear from window - only text displayed.
When I make bigger - meshes rendered, but proportions not correct, despite that I recreate projection Matrix.
My question is - how to correctly resize D2D render target and text after window size changed?
Here is my code for creating D3D11Device, swapchain1 and D2D.
bool D3D11::InitializeD3D11(HWND hwnd, SettingsContainer* settingsContainer)
{
this->hwnd = hwnd;
HRESULT result;
IDXGIFactory2* factory;
IDXGIAdapter* adapter;
IDXGIOutput* adapterOutput;
D3D_DRIVER_TYPE driverType = D3D_DRIVER_TYPE_UNKNOWN;
unsigned int numModes, numerator, denominator;
DXGI_MODE_DESC* displayModeList;
DXGI_ADAPTER_DESC adapterDesc;
DXGI_OUTPUT_DESC displayDesc;
DXGI_SWAP_CHAIN_DESC1 swapChainDescription;
D3D_FEATURE_LEVEL featureLevel;
ID3D11Texture2D* backBufferPtr;
D3D11_TEXTURE2D_DESC depthBufferDescription;
D3D11_DEPTH_STENCIL_DESC depthStencilDescription;
D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDescription;
D3D11_RASTERIZER_DESC rasterizerDescription;
D3D11_VIEWPORT viewPort;
D3D11_DEPTH_STENCIL_DESC depthStencilDescriptionDisabled;
D3D11_BLEND_DESC blendStateDescrition;
AdapterInfo* adapterInfo;
DisplayInfo* displayInfo;
adapterInfo = NULL;
//Создаём фабрику графических интерфейсов DX
result = CreateDXGIFactory1(__uuidof(IDXGIFactory2), (void**)&factory);
if (FAILED(result))
{
return false;
}
/*IDXGIDevice2 * pDXGIDevice;
result = d3d11_Device->QueryInterface(__uuidof(IDXGIDevice2), (void **)&pDXGIDevice);*/
/*IDXGIAdapter * pDXGIAdapter;
result = pDXGIDevice->GetParent(__uuidof(IDXGIAdapter), (void **)&pDXGIAdapter);*/
/*IDXGIFactory2 * pIDXGIFactory;
pDXGIAdapter->GetParent(__uuidof(IDXGIFactory2), (void **)&pIDXGIFactory);*/
//pIDXGIFactory->EnumAdapters1(0,&adapter);
//adapter->EnumOutputs(0, &adapterOutput);
//Достаём все графические адаптеры
for (UINT i = 0; factory->EnumAdapters(i,&adapter)!= DXGI_ERROR_NOT_FOUND;++i)
{
adapterInfo = new AdapterInfo(adapter);
adapter->GetDesc(&adapterDesc);
adapterInfo->Name = adapterDesc.Description;
adapterInfo->InternalMemory = (float)adapterDesc.DedicatedVideoMemory/1024/1024;
for (UINT j = 0; adapter->EnumOutputs(j, &adapterOutput) != DXGI_ERROR_NOT_FOUND;++j)
{
displayInfo = new DisplayInfo;
adapterOutput->GetDesc(&displayDesc);
displayInfo->MonitorHWND = displayDesc.Monitor;
DWORD cPhysicalMonitors;
LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(displayInfo->MonitorHWND, &cPhysicalMonitors);
ZeroMemory(&pPhysicalMonitors, sizeof(pPhysicalMonitors));
pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
cPhysicalMonitors* sizeof(PHYSICAL_MONITOR));
GetPhysicalMonitorsFromHMONITOR(displayInfo->MonitorHWND, cPhysicalMonitors, pPhysicalMonitors);
displayInfo->Name = pPhysicalMonitors->szPhysicalMonitorDescription;
//Получаем количество режимов которые удовлетворяют формату монитора DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM
adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, NULL );
displayModeList = new DXGI_MODE_DESC[numModes];
if (!displayModeList)
{
return false;
}
//Заполняем структуру display mode list
result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, displayModeList);
//Проходимся по всем режимам дисплея и находим тот, который совпадает с высотой и шириной экрана
//Когда совпадение найдено сохраняем нумератор и деноминатор частоты обновления для монитора
std::ofstream input_file("file.txt");
for(int k=0; k<numModes; k++)
{
if (input_file.is_open())
{
input_file << displayModeList[k].Width<<" "<<displayModeList[k].Height<<" "<<displayModeList[k].RefreshRate.Numerator<<" "<<displayModeList[k].RefreshRate.Denominator
<<" "<<displayModeList[k].ScanlineOrdering<<" "<<displayModeList[k].Format<<" "<<displayModeList[k].Scaling<<std::endl;
}
if (displayModeList[k].Width >= 1024)
{
if ((displayModeList[k].RefreshRate.Numerator/displayModeList[k].RefreshRate.Denominator)>59)
{
if (displayModeList[k].Scaling == DXGI_MODE_SCALING_CENTERED)
{
DisplayMode displayMode;
displayMode.Width = displayModeList[k].Width;
displayMode.Height = displayModeList[k].Height;
displayMode.Numerator = displayModeList[k].RefreshRate.Numerator;
displayMode.Denominator = displayModeList[k].RefreshRate.Denominator;
displayInfo->DisplayModes.push_back(displayMode);
/*numerator = displayModeList[k].RefreshRate.Numerator;
denominator = displayModeList[k].RefreshRate.Denominator;*/
}
}
}
}
//Удаляем display mode list
delete[] displayModeList;
displayModeList = NULL;
adapterInfo->displayList.push_back(displayInfo);
input_file.close ();
}
//добавляем в вектор всю собранную информацию по видеокарте
adapterList.push_back(adapterInfo);
}
//Удаляем адаптер и адаптер output
//adapter->Release();
adapter = 0;
//adapterOutput->Release();
adapterOutput = 0;
//Удаляем фабрику
/*factory->Release();
factory = 0;*/
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
UINT numFeatureLevels = ARRAYSIZE(featureLevels);
factory->EnumAdapters(0,&adapter);
//Создаём устройство Direct3D и контекст устройства Direct3D
/*result = D3D11CreateDevice(NULL, driverType,NULL,D3D11_CREATE_DEVICE_DEBUG|D3D11_CREATE_DEVICE_BGRA_SUPPORT, featureLevels,numFeatureLevels,
D3D11_SDK_VERSION, &d3d11_Device, &featureLevel, &d3d11_DeviceContext );*/
result = D3D11CreateDevice(adapterList[0]->adapter, driverType, NULL, D3D11_CREATE_DEVICE_DEBUG|D3D11_CREATE_DEVICE_BGRA_SUPPORT, featureLevels,numFeatureLevels,
D3D11_SDK_VERSION, &d3d11_Device, &featureLevel, &d3d11_DeviceContext );
if (FAILED(result))
{
return false;
}
UINT qualityLevels;
d3d11_Device->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM, 4, &qualityLevels);
//Инициализируем структуру swap chain description
ZeroMemory(&swapChainDescription, sizeof(swapChainDescription));
//Устанавливаем количество буферов
swapChainDescription.BufferCount = 1;
//устанавливаем ширину и высоту заднего буфера
swapChainDescription.Width = settingsContainer->renderSettingsDX11->GetScreenWidth();
swapChainDescription.Height = settingsContainer->renderSettingsDX11->GetScreenHeight();
//устанавливаем формат пикселей
swapChainDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDescription.SampleDesc.Count = 1;
swapChainDescription.SampleDesc.Quality = 0;
//Устанавливаем использование заднего буфера
swapChainDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDescription.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDescription.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
DXGI_SWAP_CHAIN_FULLSCREEN_DESC fullscreen;
fullscreen.RefreshRate.Numerator = 0;
fullscreen.RefreshRate.Denominator = 1;
fullscreen.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
fullscreen.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE;
//Устанавливаем в полноэкранный режим и оконный
if (settingsContainer->renderSettingsDX11->IsFullScreenEnabled())
{
fullscreen.Windowed = FALSE;
}
else
{
fullscreen.Windowed = TRUE;
}
//Создаём swap chain
result = factory->CreateSwapChainForHwnd(d3d11_Device, hwnd, &swapChainDescription, &fullscreen, NULL, &swapChain);
if(FAILED(result))
return false;
//Получаем указатель на задний буфер
result = swapChain->GetBuffer(0,__uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr);
if (FAILED(result))
return false;
//Создаём render target view с указателем на задний буфер
result = d3d11_Device->CreateRenderTargetView(backBufferPtr, NULL, &d3d11_RenderTargetView);
if(FAILED(result))
return false;
//Освобождаем указатель на задний буфер, так как он больше не нужен
backBufferPtr->Release();
backBufferPtr = 0;
//Инициализируем описание структуры Depth_Buffer
ZeroMemory(&depthBufferDescription, sizeof(depthBufferDescription));
//Заполняем описание структуры
depthBufferDescription.Width = settingsContainer->renderSettingsDX11->GetScreenWidth();
depthBufferDescription.Height = settingsContainer->renderSettingsDX11->GetScreenHeight();
depthBufferDescription.MipLevels = 1;
depthBufferDescription.ArraySize = 1;
depthBufferDescription.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDescription.SampleDesc.Count = 1;
depthBufferDescription.SampleDesc.Quality = 0;
depthBufferDescription.Usage = D3D11_USAGE_DEFAULT;
depthBufferDescription.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDescription.CPUAccessFlags = 0;
depthBufferDescription.MiscFlags = 0;
//Создаём текстуру для буфера глубины используя заполненную структуру
result = d3d11_Device->CreateTexture2D(&depthBufferDescription, NULL, &d3d11_DepthStencilBuffer);
if (FAILED(result))
return false;
//Инициализируем описание stencil state
ZeroMemory(&depthStencilDescription, sizeof(depthStencilDescription));
//Вводим описание структуры stencil_state
depthStencilDescription.DepthEnable = true;
depthStencilDescription.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDescription.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilDescription.StencilEnable = true;
depthStencilDescription.StencilReadMask = 0xFF;
depthStencilDescription.StencilWriteMask = 0xFF;
//Шаблонные операции если пиксель впереди
depthStencilDescription.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDescription.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDescription.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDescription.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
//Швблонные операции, если пиксель на обратной стороне
depthStencilDescription.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDescription.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDescription.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDescription.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
//Создаём Depth_Stencil_State
result = d3d11_Device->CreateDepthStencilState(&depthStencilDescription, &d3d11_DepthStencilState);
if (FAILED(result))
{
return false;
}
//Задаём состояние Depth Stencil State
d3d11_DeviceContext->OMSetDepthStencilState(d3d11_DepthStencilState,1);
//Инициализируем структуру Depth Stencil View
ZeroMemory (&depthStencilViewDescription, sizeof(depthStencilViewDescription));
//Заполняем описание структуры Depth_Stencil_View
depthStencilViewDescription.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDescription.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDescription.Texture2D.MipSlice = 0;
//Задаём описание структуры Depth_Stencil_View
result = d3d11_Device->CreateDepthStencilView(d3d11_DepthStencilBuffer, &depthStencilViewDescription, &d3d11_DepthStencilView);
if (FAILED(result))
return false;
//Привязываем render target view и depth stencil buffer к output pipeline
d3d11_DeviceContext->OMSetRenderTargets(1, &d3d11_RenderTargetView, d3d11_DepthStencilView);
//Заполняем структуру Raster_Description, которая будет определять как и какие полигоны будут нарисованы
rasterizerDescription.AntialiasedLineEnable = false;
rasterizerDescription.CullMode = D3D11_CULL_BACK;
rasterizerDescription.DepthBias = 0;
rasterizerDescription.DepthBiasClamp = 0.0f;
rasterizerDescription.DepthClipEnable = true;
rasterizerDescription.FillMode = D3D11_FILL_SOLID;
rasterizerDescription.FrontCounterClockwise = false;
rasterizerDescription.MultisampleEnable = true;
rasterizerDescription.ScissorEnable = false;
rasterizerDescription.SlopeScaledDepthBias = 0.0f;
//Создаём rasterizerState из описания, которое только что заполнили
result = d3d11_Device->CreateRasterizerState(&rasterizerDescription, &d3d11_RasterizerState);
if(FAILED(result))
{
return false;
}
//Устанавливаем rasterizer state (можно менять состояние с D3D11_FILL_SOLID на D3D11_FILL_WIREFRAME)
d3d11_DeviceContext->RSSetState(d3d11_RasterizerState);
//Устанавливаем вьюпорт для рендеринга
viewPort.Width = (float)settingsContainer->renderSettingsDX11->GetScreenWidth();
viewPort.Height = (float)settingsContainer->renderSettingsDX11->GetScreenHeight();
viewPort.MinDepth = 0.0f;
viewPort.MaxDepth = 1.0f;
viewPort.TopLeftX = 0.0f;
viewPort.TopLeftY = 0.0f;
//Создаём ViewPort
d3d11_DeviceContext->RSSetViewports(1,&viewPort);
return true;
}
void D3D11::ResizeSwapChain()
{
HRESULT result;
if (swapChain)
{
RECT rc;
GetClientRect(hwnd, &rc);
UINT width = rc.right - rc.left;
UINT height = rc.bottom - rc.top;
d3d11_DeviceContext->OMSetRenderTargets(0,0,0);
d3d11_RenderTargetView->Release();
swapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr);
d3d11_Device->CreateRenderTargetView(backBufferPtr, NULL, &d3d11_RenderTargetView);
//Инициализируем описание структуры Depth_Buffer
ZeroMemory(&depthBufferDescription, sizeof(depthBufferDescription));
//Заполняем описание структуры
depthBufferDescription.Width = width;
depthBufferDescription.Height = height;
depthBufferDescription.MipLevels = 1;
depthBufferDescription.ArraySize = 1;
depthBufferDescription.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDescription.SampleDesc.Count = 1;
depthBufferDescription.SampleDesc.Quality = 0;
depthBufferDescription.Usage = D3D11_USAGE_DEFAULT;
depthBufferDescription.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDescription.CPUAccessFlags = 0;
depthBufferDescription.MiscFlags = 0;
d3d11_DepthStencilBuffer->Release();
d3d11_DepthStencilBuffer = NULL;
//Создаём текстуру для буфера глубины используя заполненную структуру
result = d3d11_Device->CreateTexture2D(&depthBufferDescription, NULL, &d3d11_DepthStencilBuffer);
bool D3D11::InitializeD2D11()
{
options.debugLevel = D2D1_DEBUG_LEVEL_ERROR;
D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, options, &d2dfactory);
swapChain->GetBuffer(0, IID_PPV_ARGS(&dxgiBackbuffer));
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_HARDWARE,
D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED));
d2dfactory->CreateDxgiSurfaceRenderTarget(dxgiBackbuffer, props, &d2dRenderTarget);
dxgiBackbuffer->Release();
dxgiBackbuffer = NULL;
d2dfactory->Release();
d2dfactory = NULL;
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(DWriteFactory), (IUnknown**)(&DWriteFactory));
DWriteFactory->CreateTextFormat(
L"Cambria",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
12,
L"",
&textFormat);
DWriteFactory->CreateTextFormat(
L"Cambria",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
12,
L"",
&textFormat2);
textFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR);
textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
textFormat2->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_FAR);
textFormat2->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
d2dRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), &Brush);
return true;
}
//Задаём состояние Depth Stencil State
d3d11_DeviceContext->OMSetDepthStencilState(d3d11_DepthStencilState, 1);
//Инициализируем структуру Depth Stencil View
ZeroMemory (&depthStencilViewDescription, sizeof(depthStencilViewDescription));
//Заполняем описание структуры Depth_Stencil_View
depthStencilViewDescription.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDescription.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDescription.Texture2D.MipSlice = 0;
d3d11_DepthStencilView->Release();
d3d11_DepthStencilView = NULL;
//Задаём описание структуры Depth_Stencil_View
result = d3d11_Device->CreateDepthStencilView(d3d11_DepthStencilBuffer, &depthStencilViewDescription, &d3d11_DepthStencilView);
//Освобождаем указатель на задний буфер, так как он больше не нужен
backBufferPtr->Release();
backBufferPtr = 0;
d3d11_DeviceContext->OMSetRenderTargets(1, &d3d11_RenderTargetView, d3d11_DepthStencilView);
//d3d11_DeviceContext->OMSetRenderTargets(1, &d3d11_RenderTargetView, NULL);
//Устанавливаем вьюпорт для рендеринга
D3D11_VIEWPORT viewPort;
viewPort.Width = (float)width;
viewPort.Height = (float)height;
viewPort.MinDepth = 0.0f;
viewPort.MaxDepth = 1.0f;
viewPort.TopLeftX = 0.0f;
viewPort.TopLeftY = 0.0f;
//Создаём ViewPort
d3d11_DeviceContext->RSSetViewports(1,&viewPort);
}
}
I resize buffer on WM_SIZE event:
case WM_SIZE:
{
int Width = LOWORD(lparam);
int Height = HIWORD(lparam);
WCHAR* str = NULL;
if (wparam == SIZE_RESTORED)
{
if (system->application)
{
system->application->d3d11->ResizeSwapChain();
system->application->d3d11->ResizeD2DBuffer();
system->application->cameraDX_Free->perspective(system->application->cameraDX_Free->m_fovx,
static_cast<float>(Width) / static_cast<float>(Height),
system->application->cameraDX_Free->m_znear,
system->application->cameraDX_Free->m_zfar);
}
}
}

To correctly resize D2D in my case you need first to delete all d2d resources and after resizing swap chain recreate all resources again.

Related

I can't assign a texture to IMGUI because I can't create directx texture

I'm trying to add some images for using with IMGUI library. Basically, IMGUI can get directx or OpenGL raw texture data to draw. You can see example code from IMGUI for creating a texture ;
// Simple helper function to load an image into a DX11 texture with common settings
bool LoadTextureFromFile(const char* filename, ID3D11ShaderResourceView** out_srv, int* out_width, int* out_height)
{
// Load from disk into a raw RGBA buffer
int image_width = 0;
int image_height = 0;
unsigned char* image_data = stbi_load(filename, &image_width, &image_height, NULL, 4);
if (image_data == NULL)
return false;
// Create texture
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = image_width;
desc.Height = image_height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
ID3D11Texture2D *pTexture = NULL;
D3D11_SUBRESOURCE_DATA subResource;
subResource.pSysMem = image_data;
subResource.SysMemPitch = desc.Width * 4;
subResource.SysMemSlicePitch = 0;
g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
// Create texture view
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
ZeroMemory(&srvDesc, sizeof(srvDesc));
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = desc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0;
g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, out_srv);
pTexture->Release();
*out_width = image_width;
*out_height = image_height;
stbi_image_free(image_data);
return true;
}
Everything is ok except 'g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);' line. Because as you can see g_pd3dDevice is undefined. That's why I put a line for this like : ID3D11Device* g_pd3dDevice = nullptr; but when I run code it hangs.. I guess I must create a 3ddevice before using this part. But I can't. Any suggestions? Thanks
Now I can get some image but it's wrong:
Now my code like this ;
bool LoadTextureFromBufferDX11(unsigned char* address, ID3D11ShaderResourceView** out_srv, int* out_width, int* out_height)
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("ImGui Example"), NULL };
::RegisterClassEx(&wc);
//HWND hwnd = ::CreateWindow(wc.lpszClassName, _T("Dear ImGui DirectX11 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 200, 200, NULL, NULL, wc.hInstance, NULL);
HWND hwnd = GetForegroundWindow();
CreateDeviceD3D(hwnd);
/*
if (!CreateDeviceD3D(hwnd))
{
CleanupDeviceD3D();
::UnregisterClass(wc.lpszClassName, wc.hInstance);
return 1;
}
*/
//ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
// Load from disk into a raw RGBA buffer
int image_width = 0;
int image_height = 0;
const char* filename = "e:\\obama.png";
unsigned char* image_data = stbi_load(filename, &image_width, &image_height, NULL, 4);
//unsigned char* image_data = address;
if (image_data == NULL)
return false;
// Create texture
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = image_width;
desc.Height = image_height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
ID3D11Texture2D *pTexture = NULL;
D3D11_SUBRESOURCE_DATA subResource;
subResource.pSysMem = image_data;
subResource.SysMemPitch = desc.Width * 4;
subResource.SysMemSlicePitch = 0;
g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
// Create texture view
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
ZeroMemory(&srvDesc, sizeof(srvDesc));
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = desc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0;
g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, out_srv);
pTexture->Release();
*out_width = image_width;
*out_height = image_height;
return true;
}
And I trigger this function from host app.
static ID3D11ShaderResourceView* my_texture = NULL;
int my_image_width = 0;
int my_image_height = 0;
fn_export double imgui_showimage(unsigned char* address) {
bool ret = LoadTextureFromBufferDX11(address, &my_texture, &my_image_width, &my_image_height);
//IM_ASSERT(ret);
//ImGui::Image((void*)my_texture, ImVec2(my_image_width, my_image_height));
return 1;
}
And I draw imgu image from host like ;
fn_export double imgui_image(char* address) {
ImGui::Image((void*)my_texture, ImVec2(my_image_width, my_image_height));
return 1;
}

Error passing CoreWindow as function parameter

I am new to UWP and WinRT, I already knew DirectX11 and Win32 and I am quite interested in learning UWP and WinRT.
DXMainResources is a class to use Diret3D and Direct2D, and the most important thing when creating resources is the window (CoreWindow), however, when passing the window as a function parameter, many errors appear such as:
C2065 error 'bEnableDepthStencilBuffer': undeclared identifier
Error C2065 'window': undeclared identifier
Error C2065 'window_': undeclared identifier
Faced with these errors, remove the CoreWindow window parameter and the program compiles, but the window as a parameter is necessary, since I need its height and width to create the swap chain and the texture for the depth stencil view.
Is there a way to pass the window as a parameter?
I'm using DirectX 11.3 interfaces and the WinRT version I'm using is 2.0.201026.4
#pragma once
#include "pch.h"
#include <d3d11_3.h>
#include <d2d1_3.h>
#include <dxgi1_4.h>
#include <dwrite_3.h>
#include <array>
using namespace D2D1;
template<class Interface> void TSafeRelease(Interface** ppInterface)
{
if (*ppInterface)
{
(*ppInterface)->Release();
(*ppInterface) = nullptr;
}
}
class DXMainResources
{
public:
DXMainResources()
{
d3dDev = nullptr;
d3dDevContext = nullptr;
d3dRTView = nullptr;
d3dDSView = nullptr;
d2dDev = nullptr;
d2dDevContext = nullptr;
d2dTargetBitmap = nullptr;
dxgiFactory = nullptr;
dxgiDev = nullptr;
dxgiDefaultAdapter = nullptr;
dxgiSwapChain = nullptr;
dwriteFactory = nullptr;
dwriteTextFmt = nullptr;
featureLevel = static_cast<D3D_FEATURE_LEVEL>(0);
dxgiSwapChainFmt = DXGI_FORMAT_UNKNOWN;
dxgiDepthStencilFmt = DXGI_FORMAT_UNKNOWN;
viewPort = D3D11_VIEWPORT();
}
~DXMainResources()
{
}
void CreateResources(CoreWindow const& window, bool bOnlyDirect2D = false, bool bEnableDepthStencilBuffer = true
, DXGI_FORMAT dxgiSwapChainFmt_ = DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT dxgiDepthStencilFmt_ = DXGI_FORMAT_D24_UNORM_S8_UINT
, DXGI_SWAP_EFFECT swapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL)
{
dxgiSwapChainFmt = dxgiSwapChainFmt_;
dxgiDepthStencilFmt = dxgiDepthStencilFmt_;
CoreWindow window_ = window;
std::array<D3D_FEATURE_LEVEL, 9> featureLevels =
{
D3D_FEATURE_LEVEL_12_1,
D3D_FEATURE_LEVEL_12_0,
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1,
};
UINT deviceFlags = D3D11_CREATE_DEVICE_DEBUG | D3D11_CREATE_DEVICE_BGRA_SUPPORT;
HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, deviceFlags, featureLevels.data(), static_cast<UINT>(std::size(featureLevels))
, D3D11_SDK_VERSION, reinterpret_cast<ID3D11Device**>(&d3dDev), &featureLevel, nullptr);
hr = d3dDev->QueryInterface(__uuidof(IDXGIDevice3), reinterpret_cast<void**>(&dxgiDev));
hr = dxgiDev->GetAdapter(reinterpret_cast<IDXGIAdapter**>(&dxgiDefaultAdapter));
hr = dxgiDefaultAdapter->GetParent(__uuidof(IDXGIFactory4), reinterpret_cast<void**>(&dxgiFactory));
DXGI_SWAP_CHAIN_DESC1 swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC1));
swapChainDesc.BufferCount = 2;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.Format = dxgiSwapChainFmt_;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SwapEffect = swapEffect;
hr = dxgiFactory->CreateSwapChainForCoreWindow(d3dDev, reinterpret_cast<IUnknown*>(window_)
, &swapChainDesc, nullptr, reinterpret_cast<IDXGISwapChain1**>(&dxgiSwapChain));
if (bOnlyDirect2D)
{
IDXGISurface2* dxgiBackBufferSurface = nullptr;
hr = dxgiSwapChain->GetBuffer(0, __uuidof(IDXGISurface2), reinterpret_cast<void**>(&dxgiBackBufferSurface));
D2D1_CREATION_PROPERTIES creationProps = CreationProperties(D2D1_THREADING_MODE_SINGLE_THREADED, D2D1_DEBUG_LEVEL_INFORMATION, D2D1_DEVICE_CONTEXT_OPTIONS_NONE);
D2D1_BITMAP_PROPERTIES1 bmpProps = BitmapProperties1(D2D1_BITMAP_OPTIONS_CANNOT_DRAW | D2D1_BITMAP_OPTIONS_TARGET
, PixelFormat(dxgiSwapChainFmt_, D2D1_ALPHA_MODE_IGNORE));
hr = D2D1CreateDevice(dxgiDev, creationProps, reinterpret_cast<ID2D1Device**>(&d2dDev));
hr = d2dDev->CreateDeviceContext(creationProps.options, &d2dDevContext);
hr = d2dDevContext->CreateBitmapFromDxgiSurface(dxgiBackBufferSurface, bmpProps, &d2dTargetBitmap);
d2dDevContext->SetTarget(d2dTargetBitmap);
TSafeRelease(&dxgiBackBufferSurface);
}
else
{
IDXGISurface2* dxgiBackBufferSurface = nullptr;
hr = dxgiSwapChain->GetBuffer(0, __uuidof(IDXGISurface2), reinterpret_cast<void**>(&dxgiBackBufferSurface));
D2D1_CREATION_PROPERTIES creationProps = CreationProperties(D2D1_THREADING_MODE_SINGLE_THREADED, D2D1_DEBUG_LEVEL_INFORMATION, D2D1_DEVICE_CONTEXT_OPTIONS_NONE);
D2D1_BITMAP_PROPERTIES1 bmpProps = BitmapProperties1(D2D1_BITMAP_OPTIONS_CANNOT_DRAW | D2D1_BITMAP_OPTIONS_TARGET
, PixelFormat(dxgiSwapChainFmt_, D2D1_ALPHA_MODE_IGNORE));
hr = D2D1CreateDevice(dxgiDev, creationProps, reinterpret_cast<ID2D1Device**>(&d2dDev));
hr = d2dDev->CreateDeviceContext(creationProps.options, &d2dDevContext);
hr = d2dDevContext->CreateBitmapFromDxgiSurface(dxgiBackBufferSurface, bmpProps, &d2dTargetBitmap);
d2dDevContext->SetTarget(d2dTargetBitmap);
TSafeRelease(&dxgiBackBufferSurface);
d3dDev->GetImmediateContext3(&d3dDevContext);
ID3D11Texture2D1* d3dBackBufferTex = nullptr;
hr = dxgiSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D1), reinterpret_cast<void**>(&d3dBackBufferTex));
hr = d3dDev->CreateRenderTargetView1(d3dBackBufferTex, nullptr, &d3dRTView);
TSafeRelease(&d3dBackBufferTex);
if (bEnableDepthStencilBuffer)
{
ID3D11Texture2D1* d3dDepthStencilTex = nullptr;
D3D11_TEXTURE2D_DESC1 depthStencilTexDesc;
ZeroMemory(&depthStencilTexDesc, sizeof(D3D11_TEXTURE2D_DESC1));
depthStencilTexDesc.ArraySize = 1;
depthStencilTexDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilTexDesc.Format = dxgiDepthStencilFmt_;
depthStencilTexDesc.Height = window_.Bounds().Height;
depthStencilTexDesc.MipLevels = 1;
depthStencilTexDesc.SampleDesc.Count = 1;
depthStencilTexDesc.Width = window_.Bounds().Width;
hr = d3dDev->CreateTexture2D1(&depthStencilTexDesc, nullptr, &d3dDepthStencilTex);
hr = d3dDev->CreateDepthStencilView(d3dDepthStencilTex, nullptr, &d3dDSView);
d3dDevContext->OMSetRenderTargets(1, reinterpret_cast<ID3D11RenderTargetView**>(&d3dRTView), d3dDSView);
TSafeRelease(&d3dDepthStencilTex);
}
else
{
d3dDevContext->OMSetRenderTargets(1, reinterpret_cast<ID3D11RenderTargetView**>(&d3dRTView), nullptr);
}
viewPort.Height = window_.Bounds().Height;
viewPort.MaxDepth = 1.0f;
viewPort.Width = window_.Bounds().Width;
d3dDevContext->RSSetViewports(1, &viewPort);
}
hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory3), reinterpret_cast<IUnknown**>(&dwriteFactory));
hr = dwriteFactory->CreateTextFormat(L"Segoe", nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 11.0f
, L"en-US", reinterpret_cast<IDWriteTextFormat**>(&dwriteTextFmt));
}
ID3D11Device3* GetD3D11Device() const { return d3dDev; }
ID3D11DeviceContext3* GetD3D11DeviceContext() const { return d3dDevContext; }
ID3D11RenderTargetView1* GetD3D11RenderTargetView() const { return d3dRTView; }
ID3D11DepthStencilView* GetD3D11DepthStencilView() const { return d3dDSView; }
ID2D1Device2* GetD2DDevice() const { return d2dDev; }
ID2D1DeviceContext2* GetD2DDeviceContext() const { return d2dDevContext; }
ID2D1Bitmap1* GetD2DTargetBitmap() const { return d2dTargetBitmap; }
IDXGIFactory4* GetDXGIFactory() const { return dxgiFactory; }
IDXGIDevice3* GetDXGIDevice() const { return dxgiDev; }
IDXGIAdapter3* GetDXGIDefaultAdapter() const { return dxgiDefaultAdapter; }
IDXGISwapChain3* GetDXGISwapChain() const { return dxgiSwapChain; }
IDWriteFactory* GetDWriteFactory() const { return dwriteFactory; }
IDWriteTextFormat3* GetDWriteTextFormat() const { return dwriteTextFmt; }
D3D_FEATURE_LEVEL GetD3DDeviceFeatureLevel() { return featureLevel; }
DXGI_FORMAT GetDXGISwapChainFormat() { return dxgiSwapChainFmt; }
DXGI_FORMAT GetDXGIDepthStencilFormat() { return dxgiDepthStencilFmt; }
D3D11_VIEWPORT GetD3D11ViewPort() { return viewPort; }
private:
ID3D11Device3* d3dDev;
ID3D11DeviceContext3* d3dDevContext;
ID3D11RenderTargetView1* d3dRTView;
ID3D11DepthStencilView* d3dDSView;
ID2D1Device2* d2dDev;
ID2D1DeviceContext2* d2dDevContext;
ID2D1Bitmap1* d2dTargetBitmap;
IDXGIFactory4* dxgiFactory;
IDXGIDevice3* dxgiDev;
IDXGIAdapter3* dxgiDefaultAdapter;
IDXGISwapChain3* dxgiSwapChain;
IDWriteFactory* dwriteFactory;
IDWriteTextFormat3* dwriteTextFmt;
D3D_FEATURE_LEVEL featureLevel;
DXGI_FORMAT dxgiSwapChainFmt;
DXGI_FORMAT dxgiDepthStencilFmt;
D3D11_VIEWPORT viewPort;
};
Refer to the Important in the document, please add the following #include statement and using statement, then try to compile your project.
#include <winrt/Windows.UI.Core.h>
using namespace winrt::Windows::UI::Core;

Direct X: How to display an image loaded in a texture?

I'm new to DirectX and have been reading tons of tutorials and samples, however I'm unable to find any documentation on how to directly display an image that is loaded into a Texture2D on the screen. Almost all tutorials I've seen deal with 3D graphics, shaders, etc. However, I just want to display the contents of the texture.
Here's what I have so far:
DeviceResources.cpp:
#include "DeviceResources.h"
#include "Renderer.h"
DeviceResources::DeviceResources()
{
}
HRESULT DeviceResources::CreateDeviceResources(HWND hwnd)
{
D3D_FEATURE_LEVEL levels[] = {
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_12_0,
D3D_FEATURE_LEVEL_12_1
};
UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
DXGI_SWAP_CHAIN_DESC swap_chain_desc;
ZeroMemory(&swap_chain_desc, sizeof(DXGI_SWAP_CHAIN_DESC));
swap_chain_desc.Windowed = TRUE;
swap_chain_desc.BufferCount = 2;
swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R10G10B10A2_UNORM;
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swap_chain_desc.SampleDesc.Count = 1;
swap_chain_desc.SampleDesc.Quality = 0;
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
swap_chain_desc.OutputWindow = hwnd;
Microsoft::WRL::ComPtr<ID3D11Device> device;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> context;
Microsoft::WRL::ComPtr<IDXGISwapChain> swapChain;
D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
flags,
levels,
ARRAYSIZE(levels),
D3D11_SDK_VERSION,
&swap_chain_desc,
swapChain.GetAddressOf(),
device.GetAddressOf(),
&m_feature_level,
context.GetAddressOf()
);
device.As(&m_device);
context.As(&m_context);
swapChain.As(&m_swapChain);
cv::directx::ocl::initializeContextFromD3D11Device(m_device.Get());
auto hdr = Renderer::HDRMetadata();
m_swapChain->SetHDRMetaData(DXGI_HDR_METADATA_TYPE_HDR10, sizeof(DXGI_HDR_METADATA_HDR10), &hdr);
m_swapChain->SetColorSpace1(DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020);
m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), static_cast<void**>(& m_backBuffer));
m_backBuffer->GetDesc(&m_bbDesc);
ZeroMemory(&m_viewport, sizeof(D3D11_VIEWPORT));
m_viewport.Height = static_cast<float>(m_bbDesc.Height);
m_viewport.Width = static_cast<float>(m_bbDesc.Width);
m_viewport.MinDepth = 0;
m_viewport.MaxDepth = 1;
m_context->RSSetViewports(1, &m_viewport);
m_device->CreateRenderTargetView(m_backBuffer.Get(), nullptr, m_renderTargetView.GetAddressOf());
return S_OK;
}
HRESULT DeviceResources::ConfigureBackBuffer()
{
m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), static_cast<void**>(& m_backBuffer));
m_device->CreateRenderTargetView(m_backBuffer.Get(), nullptr, m_renderTargetView.GetAddressOf());
m_backBuffer->GetDesc(&m_bbDesc);
ZeroMemory(&m_viewport, sizeof(D3D11_VIEWPORT));
m_viewport.Height = static_cast<float>(m_bbDesc.Height);
m_viewport.Width = static_cast<float>(m_bbDesc.Width);
m_viewport.MinDepth = 0;
m_viewport.MaxDepth = 1;
m_context->RSSetViewports(1, &m_viewport);
return S_OK;
}
HRESULT DeviceResources::ReleaseBackBuffer()
{
m_renderTargetView.Reset();
m_backBuffer.Reset();
m_context->Flush();
return S_OK;
}
HRESULT DeviceResources::SetFullscreen(bool fullscreen)
{
m_swapChain->SetFullscreenState(fullscreen, nullptr);
ReleaseBackBuffer();
m_swapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0);
ConfigureBackBuffer();
return S_OK;
}
void DeviceResources::Present()
{
m_swapChain->Present(1, 0);
}
DeviceResources::~DeviceResources()
= default;
Renderer.cpp:
#include "Renderer.h"
#include <utility>
#include <comdef.h>
#include <vector>
Renderer::Renderer(std::shared_ptr<DeviceResources> resources) : m_resources(std::move(resources)), m_frameCount(0)
{
m_frameCount = 0;
}
HRESULT Renderer::CreateDeviceDependentResources()
{
return S_OK;
}
HRESULT Renderer::CreateWindowSizeDependentResources()
{
return S_OK;
}
void Renderer::Update()
{
//
}
void Renderer::Render()
{
cv::Mat mat = cv::imread("C:/Users/Richard/Downloads/orig_cave_L.ppm", cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);
cv::Mat as4channelMat(mat.size(), CV_MAKE_TYPE(mat.depth(), 4));
int conversion[] = { 0, 0, 1, 1, 2, 2, -1, 3 };
cv::mixChannels(&mat, 1, &as4channelMat, 1, conversion, 4);
D3D11_TEXTURE2D_DESC desc;
desc.Width = 3840;
desc.Height = 2160;
desc.MipLevels = desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R16G16B16A16_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0;
ID3D11Texture2D* tex = nullptr;
auto hr = m_resources->GetDevice()->CreateTexture2D(&desc, nullptr, &tex);
if FAILED(hr)
{
_com_error err(hr);
auto errMsg = err.ErrorMessage();
}
try {
cv::directx::convertToD3D11Texture2D(as4channelMat, tex);
} catch (cv::Exception& e)
{
std::cerr << "ERROR: " << e.msg << std::endl;
throw e;
}
auto hr3 = m_resources->m_device->CreateShaderResourceView(tex.Get(), nullptr, m_texture.GetAddressOf());
if FAILED(hr3)
{
_com_error err(hr3);
auto errMsg = err.ErrorMessage();
}
std::unique_ptr<DirectX::SpriteBatch> m_spriteBatch;
DirectX::SimpleMath::Vector2 m_screenPos, m_origin;
m_spriteBatch = std::make_unique<DirectX::SpriteBatch>(m_resources->m_context.Get());
CD3D11_TEXTURE2D_DESC catDesc;
tex->GetDesc(&catDesc);
m_origin.x = float(catDesc.Width / 2);
m_origin.y = float(catDesc.Height / 2);
m_screenPos.x = m_resources->m_bbDesc.Width / 2.f;
m_screenPos.y = m_resources->m_bbDesc.Height / 2.f;
m_spriteBatch->Begin();
m_spriteBatch->Draw(
m_texture.Get(),
m_screenPos,
nullptr,
DirectX::Colors::White,
0.0f,
m_origin
);
m_spriteBatch->End();
}
DXGI_HDR_METADATA_HDR10 Renderer::HDRMetadata()
{
//
}
Renderer::~Renderer()
{
}
From my understand, I have to somehow create a "Quad", apply the texture to it, and then display the quad itself. However,I am unsure how to do any of this and can't find any resources to help.
Edit: Given the recommendations, I have tried using DirectXTK, specifically SpriteBatch. I followed the relevant instructions in the documentation, however Draw doesn't seem to do / display anything. (In Renderer.cpp)

copy color and depthstencil buffer for later use

I'm new to directX and my task is to copy the current depthstencil and color buffer into a texture. Later this textures will be back copied into the color/depthstencil buffer to render on the old scene without rendering the hole scene twice.
This code generates the rendertarget:
bool CGraphicsDriverDX11::CreateRenderTargetTexture(UINT nWidth, UINT nHeight, DXGI_FORMAT Format,
ID3D11Texture2D** ppRenderTargetTexture, ID3D11RenderTargetView** ppRenderTargetView,
ID3D11ShaderResourceView** ppRenderTargetSRV, bool bMultiSample)
{
D3D11_TEXTURE2D_DESC TextureDesc;
ZeroMemory(&TextureDesc, sizeof(TextureDesc));
TextureDesc.Width = nWidth;
TextureDesc.Height = nHeight;
TextureDesc.MipLevels = 1;
TextureDesc.ArraySize = 1;
TextureDesc.Format = Format;
if (bMultiSample)
{
TextureDesc.SampleDesc.Count = m_nMultiSampleCount;
TextureDesc.SampleDesc.Quality = m_nMultiSampleQuality;
}
else
{
TextureDesc.SampleDesc.Count = 1;
TextureDesc.SampleDesc.Quality = 0;
}
TextureDesc.Usage = D3D11_USAGE_DEFAULT;
TextureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
TextureDesc.CPUAccessFlags = 0;
TextureDesc.MiscFlags = 0;
HRESULT hr = m_pD3D11Device->CreateTexture2D(&TextureDesc, nullptr, ppRenderTargetTexture);
if (FAILED(hr))
{
DebugAssertOnce(UNABLE_TO_CREATE_RENDER_TARGET_TEXTURE);
return false;
}
hr = m_pD3D11Device->CreateRenderTargetView(*ppRenderTargetTexture, nullptr, ppRenderTargetView);
if (FAILED(hr))
{
DebugAssertOnce(UNABLE_TO_CREATE_RENDER_TARGET_VIEW);
return false;
}
if (ppRenderTargetSRV)
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
ZeroMemory(&SRVDesc, sizeof(SRVDesc));
SRVDesc.Format = TextureDesc.Format;
SRVDesc.Texture2D.MipLevels = TextureDesc.MipLevels;
SRVDesc.Texture2D.MostDetailedMip = 0;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
hr = m_pD3D11Device->CreateShaderResourceView(*ppRenderTargetTexture, &SRVDesc, ppRenderTargetSRV);
if (FAILED(hr))
{
DebugAssertOnce(UNABLE_TO_CREATE_SHADER_RESOURCE_VIEW);
return false;
}
}
return true;
}
This code generates the depthbuffer
bool CGraphicsDriverDX11::CreateDepthTexture(UINT nWidth, UINT nHeight, DXGI_FORMAT Format,
ID3D11Texture2D** ppDepthStencilTexture, ID3D11DepthStencilView** ppDepthStencilView,
ID3D11ShaderResourceView** ppDepthStencilSRV, bool bMultiSample)
{
D3D11_TEXTURE2D_DESC TextureDesc;
ZeroMemory(&TextureDesc, sizeof(TextureDesc));
TextureDesc.Width = nWidth;
TextureDesc.Height = nHeight;
TextureDesc.MipLevels = 1;
TextureDesc.ArraySize = 1;
TextureDesc.Format = Format;
if (bMultiSample)
{
TextureDesc.SampleDesc.Count = m_nMultiSampleCount;
TextureDesc.SampleDesc.Quality = m_nMultiSampleQuality;
}
else
{
TextureDesc.SampleDesc.Count = 1;
TextureDesc.SampleDesc.Quality = 0;
}
TextureDesc.Usage = D3D11_USAGE_DEFAULT;
TextureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
TextureDesc.CPUAccessFlags = 0;
TextureDesc.MiscFlags = 0;
HRESULT hr = m_pD3D11Device->CreateTexture2D(&TextureDesc, nullptr, ppDepthStencilTexture);
if (FAILED(hr))
{
DebugAssertOnce(UNABLE_TO_CREATE_DEPTHBUFFER_TEXTURE);
return false;
}
m_pD3D11Device->CreateDepthStencilView(*ppDepthStencilTexture, nullptr, ppDepthStencilView);
if (FAILED(hr))
{
DebugAssertOnce(UNABLE_TO_CREATE_DEPTHBUFFER_VIEW);
return false;
}
if (ppDepthStencilSRV)
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
ZeroMemory(&SRVDesc, sizeof(SRVDesc));
SRVDesc.Format = TextureDesc.Format;
SRVDesc.Texture2D.MipLevels = TextureDesc.MipLevels;
SRVDesc.Texture2D.MostDetailedMip = 0;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
hr = m_pD3D11Device->CreateShaderResourceView(*ppDepthStencilTexture, &SRVDesc, ppDepthStencilSRV);
if (FAILED(hr))
{
DebugAssertOnce(UNABLE_TO_CREATE_SHADER_RESOURCE_VIEW);
return false;
}
}
return true;
}
now I try to make a copy of it:
ResolveSubresource(GetZBufferCopyTexture(), 0, GetDepthStencilBufferTexture(), 0, DXGI_FORMAT_D24_UNORM_S8_UINT);
ResolveSubresource(GetColorCopyTexture(), 0, GetBackBuffer(), 0, DXGI_FORMAT_R8G8B8A8_UNORM);
and also try to copy the copy back to the rendertarget/depthstencil
ResolveSubresource(GetDepthStencilBufferTexture(), 0, GetZBufferCopyTexture(), 0, DXGI_FORMAT_D24_UNORM_S8_UINT);
ResolveSubresource(GetBackBuffer(), 0, GetColorCopyTexture(), 0, DXGI_FORMAT_R8G8B8A8_UNORM);
but this does not work correctly. I see no changes. Maybe my understanding how directx11 works is completely wrong.
I did this with OpenGL, there I only had to copy the FramebufferObject with the blitframebuffer command and it worked very well. It was the same project, so I'm sure that I call these commands in the right order. But directx11 is completely new to me
EDIT:
I also changed the command "ResolveSubresource" to "CopyResource" but also no changes
I found the mistake:
I used the wrong textures...
Now it works very fine, BTW I use the "CopyResource" command, because the "ResolveSubresource" only copys a multisampled resource into a non-multisampled resource

InstanceBuffer not working in DX 11_0

So I have this problem I have been stuck on for a few weeks now where the instance buffer is not working in my DX 11_0 application, the vertex buffer and index buffers are working just fin but for some reason nothing is getting passed in to the instance buffer even though the instance buffer was created with S_OK and throws no error.
Here is the definition and creation of the instance buffer
instanceDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
instanceDesc.ByteWidth = sizeof(InstanceVertex2) * MAX_INSTANCES;
instanceDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
instanceDesc.MiscFlags = 0;
instanceDesc.StructureByteStride = 0;
instanceDesc.Usage = D3D11_USAGE_DYNAMIC;
instanceData.pSysMem = new InstanceVertex2[MAX_INSTANCES];
instanceData.SysMemPitch = 0;
instanceData.SysMemSlicePitch = 0;
//create the instance buffer
result = device->CreateBuffer(&instanceDesc, &instanceData, &m_instanceBuffer);
if (FAILED(result))
{
return false;
}
Here is the polygon layout
//vertex position, by vertex
polygonLayout[0].AlignedByteOffset = 0;
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
polygonLayout[0].InputSlot = 0;
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[0].InstanceDataStepRate = 0;
polygonLayout[0].SemanticIndex = 0;
polygonLayout[0].SemanticName = "POSITION";
//uv coords, by vertex
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
polygonLayout[1].InputSlot = 0;
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[1].InstanceDataStepRate = 0;
polygonLayout[1].SemanticIndex = 0;
polygonLayout[1].SemanticName = "TEXCOORD";
//texture ID, by instance
polygonLayout[2].AlignedByteOffset = 0;
polygonLayout[2].Format = DXGI_FORMAT_R32_SINT;
polygonLayout[2].InputSlot = 1;
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
polygonLayout[2].InstanceDataStepRate = 1;
polygonLayout[2].SemanticIndex = 0;
polygonLayout[2].SemanticName = "TEXTUREID";
//color, by instance
polygonLayout[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[3].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
polygonLayout[3].InputSlot = 1;
polygonLayout[3].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
polygonLayout[3].InstanceDataStepRate = 1;
polygonLayout[3].SemanticIndex = 0;
polygonLayout[3].SemanticName = "COLOR";
//UVAdd , by instance
polygonLayout[4].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[4].Format = DXGI_FORMAT_R32G32_FLOAT;
polygonLayout[4].InputSlot = 1;
polygonLayout[4].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
polygonLayout[4].InstanceDataStepRate = 1;
polygonLayout[4].SemanticIndex = 0;
polygonLayout[4].SemanticName = "UVADD";
//UVMultiply, by instance
polygonLayout[5].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[5].Format = DXGI_FORMAT_R32G32_FLOAT;
polygonLayout[5].InputSlot = 1;
polygonLayout[5].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
polygonLayout[5].InstanceDataStepRate = 1;
polygonLayout[5].SemanticIndex = 0;
polygonLayout[5].SemanticName = "UVMULTIPLY";
//matrix row 1
polygonLayout[6].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[6].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
polygonLayout[6].InputSlot = 1;
polygonLayout[6].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
polygonLayout[6].InstanceDataStepRate = 1;
polygonLayout[6].SemanticIndex = 0;
polygonLayout[6].SemanticName = "MATRIX";
//matrix row 2
polygonLayout[7].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[7].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
polygonLayout[7].InputSlot = 1;
polygonLayout[7].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
polygonLayout[7].InstanceDataStepRate = 1;
polygonLayout[7].SemanticIndex = 1;
polygonLayout[7].SemanticName = "MATRIX";
//matrix row 3
polygonLayout[8].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[8].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
polygonLayout[8].InputSlot = 1;
polygonLayout[8].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
polygonLayout[8].InstanceDataStepRate = 1;
polygonLayout[8].SemanticIndex = 2;
polygonLayout[8].SemanticName = "MATRIX";
//matrix row 4
polygonLayout[9].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[9].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
polygonLayout[9].InputSlot = 1;
polygonLayout[9].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
polygonLayout[9].InstanceDataStepRate = 1;
polygonLayout[9].SemanticIndex = 3;
polygonLayout[9].SemanticName = "MATRIX";
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
//create the input layout
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout);
if (FAILED(result))
{
MessageBox(hwnd, TEXT("Failed to create the input layout"), TEXT("Error initializaing shader"), MB_OK);
return false;
}
Here is me actually updating the instance buffer(it is dynamic)
result = deviceContext->Map(m_instanceBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &data);
if (FAILED(result))
{
return false;
}
instancesPtr = (InstanceVertex2*)data.pData;
memcpy(instancesPtr, (void*)instances, sizeof(&m_vertices[0]));
//now un map
deviceContext->Unmap(m_instanceBuffer, 0);
And finally this is the code where I put the buffers into the device context
//set the buffers
buffers[0] = m_vertexBuffer;
buffers[1] = m_instanceBuffer;
//set the strides
strides[0] = sizeof(InstanceVertex1);
strides[1] = sizeof(InstanceVertex2);
//set the offsets
offsets[0] = 0;
offsets[1] = 0;
//set the vertex buffers
deviceContext->IASetVertexBuffers(0, 2, buffers, strides, offsets);
//set the index buffers
deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
This does seem like a lot of code to look through but I don't know exactly what is going wrong, I have got instancing and dynamic vertex buffers to work in the past without problems and for some reason I can not get it to work when I combine them.I have nvidea nsight so I can look in the vertex shader and buffers directly, and from what I am looking at I can tell that nothing is getting passed into the instance buffer. I would appreciate any help or pointers anyone is willing to give me so that I can fix my problem.
I figured it out, it turned out the problem lay not in the initialization of the instance buffer but in the way I was updating it, I will include the answer to it so that others might be able to get help with a similar problem.
I changed :
result = deviceContext->Map(m_instanceBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &data);
if (FAILED(result))
{
return false;
}
instancesPtr = (InstanceVertex2*)data.pData;
memcpy(instancesPtr, (void*)instances, sizeof(&m_vertices[0]));
//now un map
deviceContext->Unmap(m_instanceBuffer, 0);
To :
result = deviceContext->Map(m_instanceBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &data);
if (FAILED(result))
{
return false;
}
instancesPtr = (InstanceVertex2*)data.pData;
for (int i = 0;i < m_vertices.size();i++)
{
instancesPtr[i].color = m_vertices[i].color;
instancesPtr[i].matrixInstance = m_vertices[i].matrixInstance;
instancesPtr[i].textureID = m_vertices[i].textureID;
instancesPtr[i].UVAdd = m_vertices[i].UVAdd;
instancesPtr[i].UVMultiply = m_vertices[i].UVMultiply;
}
//memcpy(instancesPtr, (void*)instances, sizeof(&m_vertices[0]));
//now un map
deviceContext->Unmap(m_instanceBuffer, 0);
I hope this helps someone else with there problem someday, it certainly took me long enough.