Sign in to follow this  
kopilatis

DX11 Help in Directx11(June SDK)

Recommended Posts

kopilatis    100
I started learning directx a couple of days ago and as of today I can't even put a triangle on the screen[img]http://public.gamedev.net/public/style_emoticons/default/angry.gif[/img](with code I wrote that is). Anyway I have this code right here and it looks EXACTLY the same as the 3rd tutorial on the documentation. Problem is, when I run mine the only thing I see is a blue screen and when I run the documentation example it runs fine. Can someone point out the problem here ? It's realy driving me crazy.

P.S. I know it's horribly written and there is no clean up but I'm just trying to learn here.

[code]#include <Windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dcompiler.h>
#include <xnamath.h>
#include <fstream>
#include <D3DX10math.h>

#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dcompiler.lib")

/////////////////////////
//Forward Declarations//
///////////////////////

struct SimpleVertex
{
XMFLOAT3 Pos;
};

void Render();
HRESULT CompileShaderFromFile(WCHAR* fileName, LPCSTR entryPoint, LPCSTR shaderModel, ID3DBlob** blobOut, HWND hwnd);
IDXGISwapChain* g_swapChain;
ID3D11Device* g_device;
ID3D11DeviceContext* g_deviceContext;
ID3D11RenderTargetView* g_renderTargetView;
ID3D11VertexShader* gVertexShader;
ID3D11PixelShader* gPixelShader;
ID3D11InputLayout* gInputLayout;
ID3D11Buffer* gVertexBuffer;
ID3D11Buffer* gIndexBuffer;
ID3D11Buffer* gConstantBuffer;

LRESULT CALLBACK WndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch(umsg)
{
case WM_QUIT:
{
//SendMessage(hwnd, WM_DESTROY, 0, 0);
break;
}
case WM_PAINT:
hdc = BeginPaint( hwnd, &ps );
EndPaint( hwnd, &ps );
break;
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_ESCAPE:
{
SendMessage(hwnd, WM_DESTROY, 0, 0);
break;
}
}
}
default:
return DefWindowProc(hwnd, umsg, wParam, lParam);
}

return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
{
///////////////////////////////////
//Windows Initialization//////////
/////////////////////////////////
HWND hwnd;
HRESULT hr;

WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = L"Engine";
wc.hIconSm = LoadIcon(wc.hInstance, IDI_WINLOGO);

if(!RegisterClassEx(&wc))
{
return false;
}

hwnd = CreateWindowEx(WS_EX_APPWINDOW, L"Engine", L"Engine",
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP | WS_OVERLAPPEDWINDOW,
(GetSystemMetrics(SM_CXSCREEN) - 1200)/2,(GetSystemMetrics(SM_CYSCREEN) - 720)/2, 1280, 720, NULL, NULL, hInstance, NULL);

if(!hwnd)
{
return false;
}

ShowWindow(hwnd, iCmdshow);

////////////////////////////////
//DirectX Init/////////////////
//////////////////////////////

DXGI_SWAP_CHAIN_DESC swapDesc;
D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
ID3D11Texture2D* backBufferPtr;

swapDesc.BufferDesc.Width = 1200;
swapDesc.BufferDesc.Height = 720;
swapDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapDesc.BufferDesc.RefreshRate.Numerator = 60;
swapDesc.BufferDesc.RefreshRate.Denominator = 1;
swapDesc.BufferCount = 1;
swapDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapDesc.OutputWindow = hwnd;
swapDesc.SampleDesc.Count = 1;
swapDesc.SampleDesc.Quality = 0;
swapDesc.Windowed = TRUE;
swapDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
swapDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapDesc.Flags = 0;




if(FAILED(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1,
D3D11_SDK_VERSION, &swapDesc, &g_swapChain, &g_device, NULL, &g_deviceContext)))
{
return false;
}

if(FAILED(g_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr)))
{
return false;
}

if(FAILED(g_device->CreateRenderTargetView(backBufferPtr, NULL, &g_renderTargetView)))
{
return false;
}

backBufferPtr->Release();
backBufferPtr = NULL;

D3D11_VIEWPORT viewPort;
viewPort.Width = (float)1200;
viewPort.Height = (float)720;
viewPort.MinDepth = 0.0f;
viewPort.MaxDepth = 1.0f;
viewPort.TopLeftX = 0;
viewPort.TopLeftY = 0;
g_deviceContext->RSSetViewports(1, &viewPort);
//////////////////////////////////
//Vertex ////////////////////
////////////////////////////////
ID3DBlob* pVSBlob = NULL;

if(FAILED(CompileShaderFromFile(L"VertexShader.vs", "VS", "vs_5_0", &pVSBlob, hwnd)))
{
return false;
}

hr = g_device->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &gVertexShader);
if(FAILED(hr))
return false;

D3D11_INPUT_ELEMENT_DESC layout[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
};

UINT numElements = ARRAYSIZE(layout);

hr = g_device->CreateInputLayout(layout, numElements, pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &gInputLayout);
if(FAILED(hr))
return false;

pVSBlob->Release();

g_deviceContext->IASetInputLayout(gInputLayout);

ID3DBlob* pPSBlob = NULL;

if(FAILED(CompileShaderFromFile(L"PixelShader.ps", "PS", "ps_5_0", &pPSBlob, hwnd)))
{
return false;
}

hr = g_device->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &gPixelShader);
if(FAILED(hr))
return false;


pPSBlob->Release();

SimpleVertex vertices[3];

vertices[0].Pos = XMFLOAT3(0.0f, 0.5f, 0.5f);

vertices[1].Pos = XMFLOAT3(0.5f, -0.5f, 0.5f);

vertices[2].Pos = XMFLOAT3(-0.5f, -0.5f, 0.5f);

D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(SimpleVertex) * 3;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;
bd.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = vertices;
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;
hr = g_device->CreateBuffer(&bd, &InitData, &gVertexBuffer);
if(FAILED(hr))
return false;

UINT stride = sizeof(SimpleVertex);
UINT offset = 0;
g_deviceContext->IASetVertexBuffers(0, 1, &gVertexBuffer, &stride, &offset);
g_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);


/////////////////////////////////
//Message Loop//////////////////
///////////////////////////////

MSG msg;
bool done = false;
ZeroMemory(&msg, sizeof(MSG));

while(!done)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT)
{
done = true;
}
}
else
{
Render();
}
}

return 0;
}

void Render()
{
float Color[4] = {0.0f, 0.125f, 0.6f, 1.0f};
g_deviceContext->ClearRenderTargetView(g_renderTargetView, Color);

g_deviceContext->VSSetShader(gVertexShader, NULL, 0);
g_deviceContext->PSSetShader(gPixelShader, NULL, 0);
g_deviceContext->Draw(3, 0);

g_swapChain->Present(0, 0);

}

HRESULT CompileShaderFromFile(WCHAR* fileName, LPCSTR entryPoint, LPCSTR shaderModel, ID3DBlob** blobOut, HWND hwnd)
{
HRESULT hr = S_OK;

DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;

ID3DBlob* pErrorBlob;
hr = D3DX11CompileFromFile(fileName, NULL, NULL, entryPoint, shaderModel, dwShaderFlags, 0, NULL, blobOut, &pErrorBlob, NULL);
if(FAILED(hr))
{
char* compileErrors;
unsigned long bufferSize, i;
std::ofstream fout;

compileErrors = (char*)pErrorBlob->GetBufferPointer();
bufferSize = pErrorBlob->GetBufferSize();
fout.open("Shader-Errors.txt");

for(i = 0;i < bufferSize; i++)
{
fout << compileErrors[i];
}

fout.close();


MessageBox(hwnd, L"Shader Error, check the .txt", L"Error", MB_OK);
return hr;
}
if(pErrorBlob)
{
pErrorBlob->Release();
}

return S_OK;
}[/code]

Share this post


Link to post
Share on other sites
Aqua Costa    3692
If forgot this line:
[code]
g_deviceContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );
[/code]
So the draw calls will draw the triangle in the render target

You should set [b]D3D11_CREATE_DEVICE_DEBUG[/b] flag in the [b]DXGI_SWAP_CHAIN_DESC[/b] and see if any you get any error/warning. It will help you in the future :wink:

Finally, run your program in PIX, find the draw call line and try see what happens before and after it...

Share this post


Link to post
Share on other sites
kopilatis    100
A million thanks to you, sir. That was indeed the line I forgot :D
As for the PIX, I've never used it before. What kind of information should I gather?

Share this post


Link to post
Share on other sites
kopilatis    100
OK I just ran it in PIX and it crashed. Without PIX though it runs fine. Here is the log:


Frame 000001 ....PRE: Frame(1)
Trigger 'Frame 1' fired
Created PIXRun file at: C:\Users\KOPILA~1\AppData\Local\Temp\PIXF45B.tmp
Frame 000001 ........PRE: D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, 0x0036FC78, 1, 7, 0x0036FC84, 0x00065398, 0x00065388, NULL, 0x0006538C)
Frame 000001 ............PRE: AddObject(D3D11 Device, 0x04AB1CB0, 0x01F74650)
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Device, 0x04AB1CB0, 0x01F74650)
Frame 000001 ............PRE: AddObject(DXGI Factory, 0x04AB1DA0, 0x00848EA8)
Frame 000001 ............POST: <TRUE> AddObject(DXGI Factory, 0x04AB1DA0, 0x00848EA8)
Frame 000001 ............PRE: AddObject(DXGI Adapter, 0x04AB1DF0, 0x008498E8)
Frame 000001 ............POST: <TRUE> AddObject(DXGI Adapter, 0x04AB1DF0, 0x008498E8)
Frame 000001 ............PRE: AddObject(DXGI Device, 0x04AB1E48, 0x01F74428)
Frame 000001 ............POST: <TRUE> AddObject(DXGI Device, 0x04AB1E48, 0x01F74428)
Frame 000001 ............PRE: AddObject(DXGI Swap Chain, 0x04AB1EA8, 0x0084A128)
Frame 000001 ............POST: <TRUE> AddObject(DXGI Swap Chain, 0x04AB1EA8, 0x0084A128)
Frame 000001 ............PRE: AddObject(DXGI Surface, 0x04AB1F40, 0x01F6829C)
Frame 000001 ............POST: <TRUE> AddObject(DXGI Surface, 0x04AB1F40, 0x01F6829C)
Frame 000001 ............PRE: AddObject(D3D11 Texture2D, 0x04AB1FA0, 0x01F682D4)
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Texture2D, 0x04AB1FA0, 0x01F682D4)
Frame 000001 ............PRE: AddObject(D3D11 Device Context, 0x04B28A38, 0x01F7BCF8)
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Device Context, 0x04B28A38, 0x01F7BCF8)
Frame 000001 ........POST: <S_OK> D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, 0x0036FC78, 1, 7, 0x0036FC84, 0x00065398, 0x00065388, NULL, 0x0006538C)
Frame 000001 ........PRE: <this=0x04ab1ea8>IDXGISwapChain::GetBuffer(0, IID_ID3D11Texture2D, 0x0036FC6C)
Frame 000001 ........POST: <S_OK><this=0x04ab1ea8> IDXGISwapChain::GetBuffer(0, IID_ID3D11Texture2D, 0x0036FC6C)
Frame 000001 ........PRE: <this=0x04ab1cb0>ID3D11Device::CreateRenderTargetView(0x04AB1FA0, NULL, 0x00065390)
Frame 000001 ............PRE: AddObject(D3D11 Render Target View, 0x04AB2440, 0x01F9E680)
Frame 000001 ............POST: <TRUE> AddObject(D3D11 Render Target View, 0x04AB2440, 0x01F9E680)
Frame 000001 ........POST: <S_OK><this=0x04ab1cb0> ID3D11Device::CreateRenderTargetView(0x04AB1FA0, NULL, 0x00065390)
Frame 000001 ........PRE: <this=0x04b28a38>ID3D11DeviceContext::OMSetRenderTargets(1, 0x00065390, NULL)
Frame 000001 ........POST: <><this=0x04b28a38> ID3D11DeviceContext::OMSetRenderTargets(1, 0x00065390, NULL)
Frame 000001 ........PRE: <this=0x04ab1fa0>ID3D11Texture2D::Release()
Frame 000001 ........POST: <0><this=0x04ab1fa0> ID3D11Texture2D::Release()
Frame 000001 ........PRE: <this=0x04b28a38>ID3D11DeviceContext::RSSetViewports(1, 0x0036FC4C)
Frame 000001 ........POST: <><this=0x04b28a38> ID3D11DeviceContext::RSSetViewports(1, 0x0036FC4C)
An unhandled exception occurred.
Closing Run File

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By RubenRS
      How do i open an image to use it as Texture2D information without D3DX11CreateShaderResourceViewFromFile? And how it works for different formats like (JPG, PNG, BMP, DDS,  etc.)?
      I have an (512 x 512) image with font letters, also i have the position and texcoord of every letter. The main idea is that i want to obtain the image pixel info, use the position and texcoords to create a new texture with one letter and render it. Or am I wrong in something?
    • By thmfrnk
      Hey,
      I found a very interesting blog post here: https://bartwronski.com/2017/04/13/cull-that-cone/
      However, I didn't really got how to use his "TestConeVsSphere" test in 3D (last piece of code on his post). I have the frustumCorners of a 2D Tile cell in ViewSpace and my 3D Cone Origin and Direction, so where to place the "testSphere"? I thought about to also move the Cone into viewspace and put the sphere to the Center of the Cell with the radius of half-cellsize, however what about depth? A sphere does not have inf depth?
      I am missing anything? Any Ideas?
      Thx, Thomas
    • By Modymek
      hi all
      I want to enable and disable shader in MPCH Media player Classic
      the MPCH have shader option using HLSL shaders
      I want the shader to read each file extension before it plays the file
      so if the video file name is video.GR.Mp4 it will play it in Grayscale shader 
      if it is not and standard file name Video.Mp4 without GR. unique extension so it plays standard without shader or end the shader
      here is the shader I have for grayscale
      // $MinimumShaderProfile: ps_2_0
      sampler s0 : register(s0);
      float4 main(float2 tex : TEXCOORD0) : COLOR {
          float c0 = dot(tex2D(s0, tex), float4(0.299, 0.587, 0.114, 0));
          return c0;
      }
       
      I want to add if or block stantement or bloean to detect file name before it call the shader in order to go to the procedure or disable it or goto end direct without it
       
      any thoughts or help
    • By noodleBowl
      I've gotten to part in my DirectX 11 project where I need to pass the MVP matrices to my vertex shader. And I'm a little lost when it comes to the use of the constant buffer with the vertex shader
      I understand I need to set up the constant buffer just like any other buffer:
      1. Create a buffer description with the D3D11_BIND_CONSTANT_BUFFER flag 2. Map my matrix data into the constant buffer 3. Use VSSetConstantBuffers to actually use the buffer But I get lost at the VertexShader part, how does my vertex shader know to use this constant buffer when we get to the shader side of things
      In the example I'm following I see they have this as their vertex shader, but I don't understand how the shader knows to use the MatrixBuffer cbuffer. They just use the members directly. What if there was multiple cbuffer declarations like the Microsoft documentation says you could have?
      //Inside vertex shader cbuffer MatrixBuffer { matrix worldMatrix; matrix viewMatrix; matrix projectionMatrix; }; struct VertexInputType { float4 position : POSITION; float4 color : COLOR; }; struct PixelInputType { float4 position : SV_POSITION; float4 color : COLOR; }; PixelInputType ColorVertexShader(VertexInputType input) { PixelInputType output; // Change the position vector to be 4 units for proper matrix calculations. input.position.w = 1.0f; // Calculate the position of the vertex against the world, view, and projection matrices. output.position = mul(input.position, worldMatrix); output.position = mul(output.position, viewMatrix); output.position = mul(output.position, projectionMatrix); // Store the input color for the pixel shader to use. output.color = input.color; return output; }  
    • By gomidas
      I am trying to add normal map to my project I have an example of a cube: 
      I have normal in my shader I think. Then I set shader resource view for texture (NOT BUMP)
                  device.ImmediateContext.PixelShader.SetShaderResource(0, textureView);             device.ImmediateContext.Draw(VerticesCount,0); What should I do to set my normal map or how it is done in dx11 generally example c++?
  • Popular Now