Jump to content
  • Advertisement
Sign in to follow this  
deks

D3D10 basic problem -- source included

This topic is 3853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I just started playing with D3D10, trying to render a simple line. PIX reports that everything is in viewport, but still, nothing renders (depth/stencil tests and blending are disabled). Here's the complete code, if anyone's knows what's going on:
#include "stdafx.h"
#include "d3d10.h"
#include "d3dx10.h"
#include "testdx10.h"

#define MAX_LOADSTRING 100

const char shader[] = 
{
"struct VS_IN"
"{"
    "float2 position : POSITION;"
"};"
""
"struct VS_OUT"
"{"
    "float4 position : POSITION;"
"};"
""
"VS_OUT Vertex_Shader(const VS_IN v)"
"{"
    "VS_OUT o = (VS_OUT)0;"
    "o.position = float4(v.position, 0, 1);"
    "return o;"
"}"
""
"float4 Pixel_Shader(const VS_OUT v) : SV_Target"
"{"
    "return float4(1,1,1,1);"
"}"
};

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int, HWND&);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_TESTDX10, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
    HWND WindowHandle;
	if (!InitInstance (hInstance, nCmdShow, WindowHandle))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTDX10));

    // Dx10 tests
    int Width;
    int Height;
    IDXGISwapChain* SwapChain;
    ID3D10Texture2D* BackBuffer;
    ID3D10RenderTargetView* RenderTargetView;

    RECT ClientRect;
    GetClientRect((HWND)WindowHandle, &ClientRect);
    Width = (ClientRect.right - ClientRect.left);
    Height = (ClientRect.bottom - ClientRect.top);

    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)WindowHandle;
    SwapChainDesc.SampleDesc.Count = 1;
    SwapChainDesc.SampleDesc.Quality = 0;
    SwapChainDesc.Windowed = TRUE;

    ID3D10Device* GD3DDevice = NULL;
    D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, 0, D3D10_SDK_VERSION, &SwapChainDesc, &SwapChain, &GD3DDevice);

    SwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&BackBuffer);
    GD3DDevice->CreateRenderTargetView(BackBuffer, 0, &RenderTargetView);
    BackBuffer->Release();
    
    GD3DDevice->OMSetRenderTargets(1, &RenderTargetView, NULL);

    struct cvertex
    {
        D3DXVECTOR2 position;
    };

    static ID3D10VertexShader* VertexShader = NULL;
    static ID3D10PixelShader* PixelShader = NULL;
    static ID3D10InputLayout* VertexLayout = NULL;
    static ID3D10Buffer* IndexBuffer = NULL;
    static ID3D10BlendState*       g_pBlendStateNoBlend = NULL;
    static ID3D10RasterizerState*	g_pRasterizerStateNoCull = NULL;
    static ID3D10Buffer* VertexBuffer = NULL;
    static ID3D10DepthStencilState* DepthStencilState = NULL;

    UINT CompileShaderFlags = 0;

    ID3D10Blob* Shader = NULL;
    ID3D10Blob* Errors = NULL;
    HRESULT Result = D3D10CompileShader(
        shader,
        strlen(shader),
        "",
        NULL,
        NULL,
        "Vertex_Shader",
        "vs_4_0",
        CompileShaderFlags,
        &Shader,
        &Errors
    );

    GD3DDevice->CreateVertexShader((void*)Shader->GetBufferPointer(), Shader->GetBufferSize(), &VertexShader);

    // Create our vertex input layout
    const D3D10_INPUT_ELEMENT_DESC layout[] =
    {
        { "POSITION",  0, DXGI_FORMAT_R32G32_FLOAT, 0, 0,  D3D10_INPUT_PER_VERTEX_DATA, 0 },
    };
    GD3DDevice->CreateInputLayout(layout, 1, Shader->GetBufferPointer(), Shader->GetBufferSize(), &VertexLayout);

    Shader->Release();

    Result = D3D10CompileShader(
        shader,
        strlen(shader),
        "",
        NULL,
        NULL,
        "Pixel_Shader",
        "ps_4_0",
        0,
        &Shader,
        &Errors
    );

    GD3DDevice->CreatePixelShader((void*)Shader->GetBufferPointer(), Shader->GetBufferSize(), &PixelShader);
    Shader->Release();

    // Create index buffer
    D3D10_BUFFER_DESC ibDesc;
    ibDesc.ByteWidth = 2 * sizeof(WORD);
    ibDesc.Usage = D3D10_USAGE_DEFAULT;
    ibDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;
    ibDesc.CPUAccessFlags = 0;
    ibDesc.MiscFlags = 0;

    WORD IndexData[2] = { 0, 1 };
    D3D10_SUBRESOURCE_DATA ibInitData;
    ZeroMemory( &ibInitData, sizeof( D3D10_SUBRESOURCE_DATA ) );
    ibInitData.pSysMem = IndexData;
    GD3DDevice->CreateBuffer( &ibDesc, &ibInitData, &IndexBuffer );

    // Create a blend state to disable alpha blending
    D3D10_BLEND_DESC BlendState;
    ZeroMemory(&BlendState, sizeof(D3D10_BLEND_DESC));
    BlendState.BlendEnable[0] = FALSE;
    BlendState.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;
    GD3DDevice->CreateBlendState(&BlendState, &g_pBlendStateNoBlend);

    // Create a rasterizer state to disable culling
    D3D10_RASTERIZER_DESC RSDesc;
    RSDesc.FillMode = D3D10_FILL_SOLID;
    RSDesc.CullMode = D3D10_CULL_NONE;
    RSDesc.FrontCounterClockwise = TRUE;
    RSDesc.DepthBias = 0;
    RSDesc.DepthBiasClamp = 0;
    RSDesc.SlopeScaledDepthBias = 0;
    RSDesc.DepthClipEnable = FALSE;
    RSDesc.ScissorEnable = FALSE;
    RSDesc.MultisampleEnable = FALSE;
    RSDesc.AntialiasedLineEnable = FALSE;
    GD3DDevice->CreateRasterizerState(&RSDesc, &g_pRasterizerStateNoCull);

    // Create dynamic vertex buffer
    D3D10_BUFFER_DESC vbDesc;
    vbDesc.ByteWidth = 4 * sizeof(cvertex);
    vbDesc.Usage = D3D10_USAGE_DEFAULT;
    vbDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    vbDesc.CPUAccessFlags = 0;
    vbDesc.MiscFlags = 0;

    float X0 = 0.0f;
    float Y0 = 0.0f;
    float X1 = 1.0f;
    float Y1 = 1.0f;

    cvertex vertices[2] = { D3DXVECTOR2(X0, Y0), D3DXVECTOR2(X1, Y1) };
    D3D10_SUBRESOURCE_DATA vbInitData;
    ZeroMemory( &vbInitData, sizeof( D3D10_SUBRESOURCE_DATA ) );
    vbInitData.pSysMem = vertices;
    GD3DDevice->CreateBuffer( &vbDesc, &vbInitData, &VertexBuffer );

    D3D10_DEPTH_STENCIL_DESC DepthStencilDesc;
    ZeroMemory( &DepthStencilDesc, sizeof( D3D10_DEPTH_STENCIL_DESC ) );
    DepthStencilDesc.DepthFunc = D3D10_COMPARISON_ALWAYS;
    GD3DDevice->CreateDepthStencilState(&DepthStencilDesc, &DepthStencilState);

    GD3DDevice->OMSetDepthStencilState(DepthStencilState, 0);

    // Main message loop
    msg.message = 0;
	while(msg.message != WM_QUIT)
	{
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
		    if(!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		    {
    	        TranslateMessage(&msg);
	            DispatchMessage(&msg);
            }
        }
        else
        {
            float ClearColor[4] = { 0.0f, 0.0f, 1.0f, 1.0f };
            GD3DDevice->ClearRenderTargetView(RenderTargetView, ClearColor);

            GD3DDevice->OMSetRenderTargets(1, &RenderTargetView, NULL);

            // Setup the viewport
            D3D10_VIEWPORT vp;
            vp.Width = Width;
            vp.Height = Height;
            vp.MinDepth = 0.0f;
            vp.MaxDepth = 1.0f;
            vp.TopLeftX = 0;
            vp.TopLeftY = 0;
            GD3DDevice->RSSetViewports( 1, &vp );

            // Set IA parameters
            UINT strides[1] = { sizeof( cvertex ) };
            UINT offsets[1] = {0};
            ID3D10Buffer* pBuffers[1] = { VertexBuffer };

            GD3DDevice->IASetVertexBuffers( 0, 1, pBuffers, strides, offsets );
            GD3DDevice->IASetInputLayout( VertexLayout );
            GD3DDevice->IASetIndexBuffer( IndexBuffer, DXGI_FORMAT_R16_UINT, 0 );
            GD3DDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_LINELIST );

            GD3DDevice->OMSetBlendState(g_pBlendStateNoBlend, 0, 0xffffffff);
            GD3DDevice->RSSetState(g_pRasterizerStateNoCull);
            GD3DDevice->VSSetShader( VertexShader );
            GD3DDevice->GSSetShader( NULL );
            GD3DDevice->PSSetShader( PixelShader ); 
            GD3DDevice->DrawIndexed( 2, 0, 0 );

            SwapChain->Present(0, 0);
        }
	}

	return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTDX10));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_TESTDX10);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, HWND& hWnd)
{
   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

Thanks in advance! JF

Share this post


Link to post
Share on other sites
Advertisement
1) The vertex shader input structure must have got a float4 position, not a float2, even if you don't use x,y,z (and w) system.
1.1) Remember to change the Vertex Layout for a float4. (sizeof(float) = 3)

2) The vertex shader output structure must have got as semantic name SV_POSITION and not simply POSITION, becouse...becouse...i do not know why yet.

3)Mmm, an index buffer can be useful for you only for triangles, not for lines. You should use a simply vertex buffer...in lines (and expecially LINE_LIST) you can't have vertex repetitions.

For now i've seen only this.

Share this post


Link to post
Share on other sites
Quote:
Original post by XVincentX
1) The vertex shader input structure must have got a float4 position, not a float2, even if you don't use x,y,z (and w) system.
1.1) Remember to change the Vertex Layout for a float4. (sizeof(float) = 3)

2) The vertex shader output structure must have got as semantic name SV_POSITION and not simply POSITION, becouse...becouse...i do not know why yet.

According to PIX, I see that the line ends-up correctly in the viewport, what makes me think that everything's fine from the vertex setup, and that the problem is during rasterization. The clear works though, so I'm puzzled.

Anyone got ideas?

Share this post


Link to post
Share on other sites
Quote:
Original post by XVincentX
Have you tried to use my suggestions?

Of course I did, and the results were the same.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!