Failed to create render target view DirectX 11 - c++

This is my code. The final if is true so i get the messageBox that tells my the createRenderTargetView is failed. I'm pretty new to this and i searched online a lot, but found nothing, do you have any tips? Thanks.
This is the probelmatic part
//feature levels
D3D_FEATURE_LEVEL featureLevels[6]{
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,
};
//swap chain
ZeroMemory(&m_swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
m_swapChainDesc.BufferDesc.Height = 600;
m_swapChainDesc.BufferDesc.Width = 800;
m_swapChainDesc.Windowed = true;
m_swapChainDesc.BufferCount = 1;
m_swapChainDesc.OutputWindow = hwnd;
m_swapChainDesc.Flags = 0;
m_swapChainDesc.SampleDesc.Count = 1;
m_swapChainDesc.SampleDesc.Quality = 0;
m_swapChainDesc.BufferUsage = DXGI_USAGE_DISCARD_ON_PRESENT;
m_swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
m_swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
m_swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
UINT createDeviceFlags = 0;
m_swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
m_swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
m_swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
#if defined(DEBUG) || defined
(_DEBUG)createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif*/
//creazione device e swapchain
/*hr=D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, 0, 0, 0, ARRAYSIZE(featureLevels), D3D11_SDK_VERSION,
&m_swapChainDesc, &m_swapChain, &m_device, featureLevels, &m_deviceContext );
*/
D3D_FEATURE_LEVEL featureLevel;
hr = D3D11CreateDevice(0, D3D_DRIVER_TYPE_HARDWARE, 0, createDeviceFlags, featureLevels, 6, D3D11_SDK_VERSION, &m_device, &featureLevel, &m_deviceContext);
if (FAILED(hr))
MessageBox(0, L"D3D11CreateDevice Failed.", 0, 0);
IDXGIDevice *dxgiDevice = 0;
hr=m_device->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
if (FAILED(hr))
MessageBox(0, L"Fallita queryInterface", 0, 0);
IDXGIAdapter *dxgiAdapter = 0;
hr=dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&dxgiAdapter);
if (FAILED(hr))
MessageBox(0, L"Fallito getParent", 0, 0);
IDXGIFactory *dxgiFactory = 0;
hr=dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxgiFactory);
if (FAILED(hr))
MessageBox(0, L"Fallito getParent", 0, 0);
IDXGISwapChain *m_swapChain;
hr=dxgiFactory->CreateSwapChain(m_device, &m_swapChainDesc, &m_swapChain);
if (FAILED(hr))
MessageBox(0, L"Failed crating the swap chain", 0, 0);
dxgiAdapter->Release();
dxgiDevice->Release();
dxgiFactory->Release();
ID3D11RenderTargetView *m_renderTargetView;
ID3D11Texture2D *m_backBuffer;
hr=m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&m_backBuffer));
if (FAILED(hr))
MessageBox(0, L"Failed get buffer", 0, 0);
hr=m_device->CreateRenderTargetView(m_backBuffer, NULL, &m_renderTargetView);
if (FAILED(hr))
MessageBox(0, L"Failed to create rendertargetview", 0, 0);

Related

How to draw partially transparent bitmap inside a fully transparent window?

I have a fully transparent window:
wClass.cbClsExtra = NULL;
wClass.cbSize = sizeof(WNDCLASSEX);
wClass.cbWndExtra = NULL;
wClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 0));
wClass.hCursor = LoadCursor(0, IDC_ARROW);
wClass.hIcon = LoadIcon(0, IDI_APPLICATION);
wClass.hIconSm = LoadIcon(0, IDI_APPLICATION);
wClass.hInstance = GetModuleHandleA(0);
wClass.lpfnWndProc = &Overlay::WinProc;
wClass.lpszClassName = overlay_window_name.c_str();
wClass.lpszMenuName = overlay_window_name.c_str();
wClass.style = CS_VREDRAW | CS_HREDRAW;
cid = RegisterClassEx(&wClass);
DWORD ex_style = 0;
DWORD style = 0;
ex_style = WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED;
style = WS_POPUP;
m_overlay_window = CreateWindowEx(ex_style, overlay_window_name.c_str(), overlay_window_name.c_str(), style, 0, 0, 0, 0, 0, 0, 0, this);
SetLayeredWindowAttributes(m_overlay_window, 0, 0, LWA_ALPHA);
SetLayeredWindowAttributes(m_overlay_window, 0, RGB(0, 0, 0), LWA_COLORKEY);
I am creating swap chain:
DXGI_SWAP_CHAIN_DESC desc{};
desc.Windowed = TRUE;
desc.BufferCount = 2;
desc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
desc.SampleDesc.Count = 1; //multisampling setting
desc.SampleDesc.Quality = 0; //vendor-specific flag
desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
desc.OutputWindow = hwnd;
D3D_FEATURE_LEVEL levels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_11_1
};
D3D_FEATURE_LEVEL feature_level;
UINT device_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
HRESULT hr = D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE::D3D_DRIVER_TYPE_HARDWARE,
nullptr,
device_flags,
levels,
ARRAYSIZE(levels),
D3D11_SDK_VERSION,
&desc,
&m_swap_chain,
&m_device,
&feature_level,
&m_context
);
and then I want to draw half-transparent bitmap(through which I can see content of other windows):
m_render_target->DrawBitmap(m_bitmap, rect, 0.5, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR);
but this bitmap always not transparent. Only thing is going for him he becomes darker. May be something wrong with window or swap chain Im creating?

(C++/DirectX11) Something wrong in ID3D11DeviceContext::Draw() (or in my head)

Using DirectX SDK which was included to Windows8 SDK.
Compile is success, but program stops in one place. Debugging showed error in this function:
//ID3D11DeviceContext* deviceContext
//IDXGISwapChain* swapChain
//const float BackGround[4] = {0.0f, 0.0f, 0.0f, 0.0f};
void DrawScene(){
deviceContext->ClearRenderTargetView(renderTargetView, BackGroung);
deviceContext->Draw(3, 0); //<-this
swapChain->Present(0, 0);
}
While commenting this function everything goes OK. I think that I have some problems in this functions:
bool InitScene(){
hr = D3DCompileFromFile(L"Effects.fx", 0, 0, "VS", "vs_4_0", NULL, NULL, &vsBuffer, NULL);
hr = D3DCompileFromFile(L"Effects.fx", 0, 0, "PS", "ps_4_0", NULL, NULL, &psBuffer, NULL);
hr = device->CreateVertexShader(vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), NULL, &VS);
hr = device->CreatePixelShader(psBuffer->GetBufferPointer(), psBuffer->GetBufferSize(), NULL, &PS);
Vertex v[] = {
Vertex(0.0f, 0.5f, 0.5f),
Vertex(0.5f, -0.5f, 0.5f),
Vertex(-0.5f, -0.5f, 0.5f),
};
D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(&vertexBufferDesc));
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(Vertex) * 3;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA vertexBufferData;
ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
vertexBufferData.pSysMem = v;
hr = device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &triangleVertexBuffer);
UINT stride = sizeof(Vertex);
UINT offset = 0;
deviceContext->IASetVertexBuffers(0, 0, &triangleVertexBuffer, &stride, &offset);
hr = device->CreateInputLayout(layout, numElements, vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), &vertLayout);
deviceContext->IASetInputLayout(vertLayout);
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Height = Height;
viewport.Width = Width;
deviceContext->RSSetViewports(1, &viewport);
return true;
}
Or here:
bool Init3D(){
DXGI_MODE_DESC bufferDesc;
ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
bufferDesc.Width = Width;
bufferDesc.Height = Height;
bufferDesc.RefreshRate.Numerator = 60;
bufferDesc.RefreshRate.Denominator = 1;
bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
swapChainDesc.BufferDesc = bufferDesc;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = hWnd;
swapChainDesc.Windowed = TRUE;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
hr = D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&swapChainDesc,
&swapChain,
&device,
NULL,
&deviceContext);
ID3D11Texture2D *backBuffer;
hr = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);
hr = device->CreateRenderTargetView(backBuffer, NULL, &renderTargetView);
backBuffer->Release();
deviceContext->OMSetRenderTargets(1, &renderTargetView, NULL);
return true;
}
I'm perfect lamer and can't understand what's going wrong. Help me please.

RenderTargetView <-> Texture

I found this tutorial, i follow it and i create the class DirectX
class DirectX
{
private:
D3D_DRIVER_TYPE DriverType;
D3D_FEATURE_LEVEL FeatureLevel;
public:
int InitDirectX(HWND &hwnd, WinDesc &wd)
{
HRESULT result;
RECT dimensions;
GetClientRect( hwnd, &dimensions );
int width=dimensions.right-dimensions.left;
int height=dimensions.bottom-dimensions.top;
D3D_DRIVER_TYPE driverTypes[]=
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_SOFTWARE
};
unsigned int totalDriverTypes=ARRAYSIZE(driverTypes);
D3D_FEATURE_LEVEL featureLevels[]=
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
unsigned int totalFeatureLevels = ARRAYSIZE( featureLevels );
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc,sizeof(swapChainDesc));
swapChainDesc.BufferCount=1;
swapChainDesc.BufferDesc.Width=width;
swapChainDesc.BufferDesc.Height=height;
swapChainDesc.BufferDesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator=60;
swapChainDesc.BufferDesc.RefreshRate.Denominator=1;
swapChainDesc.BufferUsage=DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow=hwnd;
swapChainDesc.Windowed=1;
swapChainDesc.SampleDesc.Count=1;
swapChainDesc.SampleDesc.Quality=0;
for(int driver=0;driver<totalDriverTypes;++driver)
{
result = D3D11CreateDeviceAndSwapChain( 0, driverTypes[driver], 0,
0, featureLevels, totalFeatureLevels,
D3D11_SDK_VERSION, &swapChainDesc, &SwapChain,
&Device, &FeatureLevel, &Context );
if( SUCCEEDED( result ) )
{
D3D_DRIVER_TYPE driverType_ = driverTypes[driver];
break;
}
}
}
D3D11_TEXTURE2D_DESC textureDesc;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
ZeroMemory(&textureDesc, sizeof(textureDesc));
textureDesc.Width = width;
textureDesc.Height = height;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
result=Device->CreateTexture2D(&textureDesc, NULL, &backBuffer);
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
renderTargetViewDesc.Texture2D.MipSlice = 0;
result=Device->CreateRenderTargetView(backBuffer, &renderTargetViewDesc, &RenderTargetView);
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;
result=Device->CreateShaderResourceView(backBuffer, &shaderResourceViewDesc, &shaderResourceView);
float color[4]={1,1,0,0};
Context->ClearRenderTargetView(RenderTargetView,color);
Context->OMSetRenderTargets(1,&RenderTargetView,0);
}
};
When i run it it get no error put when i call SwapChain->Present(0,0) nothing is displayed althought the renderTargetView should to be yellow.
Why?? I cannot found the error.
PS if i create a renderTargetView with this code:
ID3D11Texture2D* backBufferTexture;
result = SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ),
( LPVOID* )&backBufferTexture );
result = Device->CreateRenderTargetView( backBufferTexture, 0,
&RenderTargetView );
all works;
and in my code i check all the "result" values;
2°
this is code of the sprite i want render:
Init()//init vertex buffer, index buffer, texture, matrix....
the render functioon is this:
C->OMSetRenderTargets(1,&RT1,0);
C->Update(...)// matrix to sent vertex shader, set texture, vertex, buffer....
C->Draw(6,0)//draw the sprite;
It should render a sprite(ex. a ball) on RT1 then :
C->OMSetRenderTargets(1,&RT,0);
RT1->Update() //like the texture;
RT1->Draw(6,0)
...
Swapchain->present(1,0);
The RT1 is displayed(ex. if i have clear it with blue there is a blue shape on the screen i can translate, scale and rotate) but not the ball(i have render on RT1);
but if i render the ball without set RT1 as renderTargetView the ball is correct displayed

Texture fail to render - DirectX 9

I have this code in DirectX 9, in which I need to get the DC from a COM interface and draw it. I do get the DC and it contains the image, but I get a black screen. Any ideas why?
LPDIRECT3DSURFACE9 pRenderSurface = NULL, pRenderSurfaceTMP = NULL;
m_pRenderTexture->GetSurfaceLevel(0, &pRenderSurface);
if (pRenderSurface == NULL)
return FALSE;
m_pD3DDevice->CreateOffscreenPlainSurface(m_nWidth, m_nHeight,
D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &pRenderSurfaceTMP, 0);
m_pD3DDevice->GetRenderTargetData(pRenderSurface,pRenderSurfaceTMP);
HDC hDC = NULL;
hr = pRenderSurfaceTMP->GetDC(&hDC);
if (FAILED(hr))
return FALSE;
if (m_pViewObject != NULL)
{
// RECT is relative to the windowless container rect
RECTL rcRect = {0, 0, m_nWidth, m_nHeight};
// Draw onto the DC!
hr = m_pViewObject->Draw(DVASPECT_CONTENT, 1,
NULL, NULL, NULL, hDC, &rcRect, NULL, NULL,
0);
}
pRenderSurface->ReleaseDC(hDC);
pRenderSurface->Release();
// Draw the surface
m_pD3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(Vertex) );
m_pD3DDevice->SetTexture( 0, m_pRenderTexture );
hr = m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
m_hbrBackground = NULL;
pRenderSurfaceTMP->Release();
Thanks in advance
Make sure that m_pRenderTexture is created with D3DUSAGE_RENDERTARGET. The size and format of both pRenderSurface and pRenderSurfaceTMP should match. For more info, see when GetRenderTargetData fails: http://msdn.microsoft.com/en-us/library/windows/desktop/bb174405(v=vs.85).aspx
Try this:
m_pD3DDevice->CreateTexture(m_nWidth, m_nHeight, 1, 0, D3DFMT_X8R8G8B8,
D3DPOOL_MANAGED, &m_pRenderTexture, NULL);
// ...
LPDIRECT3DSURFACE9 pRenderSurface = NULL;
if (FAILED(m_pRenderTexture->GetSurfaceLevel(0, &pRenderSurface))) return FALSE;
HDC hDC = NULL;
if (FAILED(pRenderSurface->GetDC(&hDC))) return FALSE;
if (m_pViewObject != NULL)
{
RECTL rcRect = {0, 0, m_nWidth, m_nHeight};
m_pViewObject->Draw(DVASPECT_CONTENT, 1,
NULL, NULL, NULL, hDC, &rcRect, NULL, NULL, 0);
}
pRenderSurface->ReleaseDC(hDC);
pRenderSurface->Release();
m_pD3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(Vertex) );
m_pD3DDevice->SetTexture( 0, m_pRenderTexture );
m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
or this:
m_pD3DDevice->CreateTexture(m_nWidth, m_nHeight, 1, 0, D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT, &m_pRenderTexture, NULL);
// ...
LPDIRECT3DSURFACE9 pRenderSurfaceTMP = NULL;
m_pD3DDevice->CreateOffscreenPlainSurface(m_nWidth, m_nHeight,
D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &pRenderSurfaceTMP, 0);
HDC hDC = NULL;
if (FAILED(pRenderSurfaceTMP->GetDC(&hDC))) return FALSE;
if (m_pViewObject != NULL)
{
RECTL rcRect = {0, 0, m_nWidth, m_nHeight};
m_pViewObject->Draw(DVASPECT_CONTENT, 1,
NULL, NULL, NULL, hDC, &rcRect, NULL, NULL, 0);
}
pRenderSurfaceTMP->ReleaseDC(hDC);
LPDIRECT3DSURFACE9 pRenderSurface = NULL;
if (FAILED(m_pRenderTexture->GetSurfaceLevel(0, &pRenderSurface)))
return FALSE;
if (FAILED(m_pD3DDevice->UpdateSurface(pRenderSurfaceTMP, NULL,
pRenderSurface, NULL))) return FALSE;
pRenderSurface->Release();
m_pD3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(Vertex) );
m_pD3DDevice->SetTexture( 0, m_pRenderTexture );
m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

Simple DirectX 11 program runtime error

I'm new to DirectX 11, and I have managed to get the Drawing Triangle chapter in my book.
I have written this code, but I get a runtime error:
"Unhandled exception at 0x00cd14e6 in DirectX 11.exe: 0xc0000005: Access violation reading location 0x00000000."
My compiler show that the error is at the InitPipeline function, but I can't find the error there.
What have I done wrong?
Code:
#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <D3DX11.h>
#include <D3DX10.h>
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"d3dx11.lib")
#pragma comment(lib,"d3dx10.lib")
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
IDXGISwapChain *swapchain;
ID3D11Device *dev;
ID3D11DeviceContext *devcon;
ID3D11InputLayout *pInputLayout;
ID3D11Buffer *pVBuffer;
ID3D11RenderTargetView *backbuffer;
ID3D11VertexShader *pVS;
ID3D11PixelShader *pPS;
// Type
struct VERTEX
{
FLOAT X,Y,Z;
D3DXCOLOR Color;
};
//DirectX
void InitPipeline()
{
// load and compile the two shaders
ID3D10Blob *VS, *PS;
D3DX11CompileFromFile("shaders.hlsl", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile("shaders.hlsl", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0);
//debug
if(!dev)
{
MessageBox(NULL, "DEV = NULL", "ERROR", NULL);
}
if(!devcon)
{
MessageBox(NULL, "DEVCON = NULL", "ERROR", NULL);
}
if(!VS)
{
MessageBox(NULL, "VS = NULL", "ERROR", NULL);
}
if(!PS)
{
MessageBox(NULL, "PS = NULL", "ERROR", NULL);
}
// encapsulate both shaders into shader objects
dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);
// set the shader objects
devcon->VSSetShader(pVS, 0, 0);
devcon->PSSetShader(pPS, 0, 0);
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,0,D3D11_INPUT_PER_VERTEX_DATA,0},
{"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pInputLayout);
devcon->IASetInputLayout(pInputLayout);
}
void InitGraphics()
{
VERTEX OurVertices[] =
{
{1,0,0,D3DXCOLOR(1,0,0,1)},
{0,-1,0,D3DXCOLOR(1,0,0,1)},
{0,0,1,D3DXCOLOR(1,0,0,1)},
};
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(D3D11_BUFFER_DESC));
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.ByteWidth = sizeof(VERTEX)*3;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bd.Usage = D3D11_USAGE_DYNAMIC;
dev->CreateBuffer(&bd, NULL, &pVBuffer);
D3D11_MAPPED_SUBRESOURCE ms;
devcon->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);
memcpy(ms.pData, OurVertices, sizeof(OurVertices));
devcon->Unmap(pVBuffer, NULL);
}
void RenderFrame()
{
devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR(0,0,1,1));
UINT stride = sizeof(VERTEX);
UINT offset = 0;
devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);
devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
devcon->Draw(3,0);
swapchain->Present(0,0);
}
void InitD3D(HWND hWnd)
{
// create a struct to hold information about the swap chain
DXGI_SWAP_CHAIN_DESC scd;
// clear out the struct for use
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
// fill the swap chain description struct
scd.BufferCount = 1; // one back buffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
scd.BufferDesc.Width = SCREEN_WIDTH; // set the back buffer width
scd.BufferDesc.Height = SCREEN_HEIGHT; // set the back buffer height
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
scd.OutputWindow = hWnd; // the window to be used
scd.SampleDesc.Count = 4; // how many multisamples
scd.Windowed = TRUE; // windowed/full-screen mode
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // allow full-screen switching
// create a device, device context and swap chain using the information in the scd struct
if(FAILED(D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapchain,
&dev,
NULL,
&devcon)))
{
MessageBox(NULL, "D3D11CreateDeviceAndSwapChain Failed", "ERROR", NULL);
}
// get the address of the back buffer
ID3D11Texture2D *pBackBuffer;
swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
// use the back buffer address to create the render target
dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
pBackBuffer->Release();
// set the render target as the back buffer
devcon->OMSetRenderTargets(1, &backbuffer, NULL);
// Set the viewport
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = SCREEN_WIDTH;
viewport.Height = SCREEN_HEIGHT;
devcon->RSSetViewports(1, &viewport);
InitPipeline();
InitGraphics();
}
void CleanD3D()
{
swapchain->SetFullscreenState(FALSE, NULL);
dev->Release();
devcon->Release();
swapchain->Release();
pInputLayout->Release();
pVBuffer->Release();
backbuffer->Release();
pVS->Release();
pPS->Release();
}
// Window
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
//wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = (HICON)LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "DirectXWindow";
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
"DirectXWindow",
"DirectX 11 (June 2010",
WS_OVERLAPPEDWINDOW,
0,0,
SCREEN_WIDTH, SCREEN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
InitD3D(hWnd);
UpdateWindow(hWnd);
MSG msg = {0};
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
RenderFrame();
}
}
CleanD3D();
return msg.wParam;
}
That error basically means that you are using a null pointer somewhere. Check all your pointers. My guess is that some function somewhere is failing to set your pointers (or you have the parameters in the wrong order).
You are using a NULL pointer. You define 8 global D3D interface pointers and you are using dev and devcon within InitPipeline they need to be allocated and initialized before you can call any methods on them.
Try setting the scd.SampleDesc.Count = 1
This value tells Direct3D how much detail should be put into anti-aliasing, the higher the number the better. Direct3D 11 video cards are guaranteed to support up to 4 here, but the minimum is 1.
Perhaps your video card doesn't support Direct3D 11?