]> git.cworth.org Git - apitrace-tests/blob - apps/d3d10/tri.cpp
Prototype of d3d10 sample app.
[apitrace-tests] / apps / d3d10 / tri.cpp
1 /**************************************************************************
2  *
3  * Copyright 2012 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <initguid.h>
28
29 #include <windows.h>
30
31 #include "compat.h"
32
33 #include <d3d10.h>
34 #include <d3d10shader.h>
35
36
37 static IDXGISwapChain* g_pSwapChain = NULL;
38 static ID3D10Device * g_pDevice = NULL;
39
40
41 int WINAPI
42 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
43 {
44     HRESULT hr;
45
46     WNDCLASSEX wc = {
47         sizeof(WNDCLASSEX),
48         CS_CLASSDC,
49         DefWindowProc,
50         0,
51         0,
52         hInstance,
53         NULL,
54         NULL,
55         NULL,
56         NULL,
57         "SimpleDX10",
58         NULL
59     };
60     RegisterClassEx(&wc);
61
62     const int WindowWidth = 250;
63     const int WindowHeight = 250;
64     BOOL Windowed = TRUE;
65
66     DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
67
68     RECT rect = {0, 0, WindowWidth, WindowHeight};
69     AdjustWindowRect(&rect, dwStyle, FALSE);
70
71     HWND hWnd = CreateWindow(wc.lpszClassName,
72                              "Simple example using DirectX10",
73                              dwStyle,
74                              CW_USEDEFAULT, CW_USEDEFAULT,
75                              rect.right - rect.left,
76                              rect.bottom - rect.top,
77                              NULL,
78                              NULL,
79                              hInstance,
80                              NULL);
81     if (!hWnd) {
82         return 1;
83     }
84
85     ShowWindow(hWnd, SW_SHOW);
86
87     DXGI_SWAP_CHAIN_DESC SwapChainDesc;
88     ZeroMemory(&SwapChainDesc, sizeof SwapChainDesc);
89     SwapChainDesc.BufferDesc.Width = WindowWidth;
90     SwapChainDesc.BufferDesc.Height = WindowHeight;
91     SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;;
92     SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
93     SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
94     SwapChainDesc.SampleDesc.Quality = 0;
95     SwapChainDesc.SampleDesc.Count = 1;
96     SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
97     SwapChainDesc.BufferCount = 2;
98     SwapChainDesc.OutputWindow = hWnd;
99     SwapChainDesc.Windowed = true;
100
101     hr = D3D10CreateDeviceAndSwapChain(NULL,
102                                        D3D10_DRIVER_TYPE_HARDWARE,
103                                        NULL,
104                                        0,
105                                        D3D10_SDK_VERSION,
106                                        &SwapChainDesc,
107                                        &g_pSwapChain,
108                                        &g_pDevice);
109     if (FAILED(hr)) {
110         return 1;
111     }
112
113     ID3D10RenderTargetView *pRenderTargetView = NULL;
114     ID3D10Texture2D* pBackBuffer;
115     hr = g_pSwapChain->GetBuffer(0, IID_ID3D10Texture2D, (void **)&pBackBuffer);
116     if (FAILED(hr)) {
117         return 1;
118     }
119     hr = g_pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
120     if (FAILED(hr)) {
121         return 1;
122     }
123     pBackBuffer->Release();
124
125     g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
126
127     const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
128     g_pDevice->ClearRenderTargetView(pRenderTargetView, clearColor);
129
130     const char *szShader = 
131         "struct VS_OUTPUT {\n"
132         "    float4 Pos : SV_POSITION;\n"
133         "    float4 Color : COLOR0;\n"
134         "};\n"
135         "\n"
136         "VS_OUTPUT VertexShader(float4 Pos : POSITION, float4 Color : COLOR) {\n"
137         "    VS_OUTPUT Out;\n"
138         "    Out.Pos = Pos;\n"
139         "    Out.Color = Color;\n"
140         "    return Out;\n"
141         "}\n"
142         "\n"
143         "float4 PixelShader(VS_OUTPUT In) : SV_Target {\n"
144         "    return In.Color;\n"
145         "}\n"
146     ;
147
148     ID3D10Blob *pVertexShaderBlob;
149     hr = D3D10CompileShader(szShader, strlen(szShader), NULL, NULL, NULL, "VertexShader", "vs_4_0", 0, &pVertexShaderBlob, NULL);
150
151     ID3D10VertexShader * pVertexShader;
152     hr = g_pDevice->CreateVertexShader((DWORD*)pVertexShaderBlob->GetBufferPointer(), pVertexShaderBlob->GetBufferSize(), &pVertexShader);
153
154     struct Vertex {
155         float x, y, z;
156         float r, g, b, a;
157     };
158
159     D3D10_INPUT_ELEMENT_DESC InputElementDescs[] = {
160         { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
161         { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, D3D10_INPUT_PER_VERTEX_DATA, 0 }
162     };
163
164     ID3D10InputLayout *pVertexLayout = NULL;
165     hr = g_pDevice->CreateInputLayout(InputElementDescs,
166                                       2,
167                                       pVertexShaderBlob->GetBufferPointer(),
168                                       pVertexShaderBlob->GetBufferSize(),
169                                       &pVertexLayout);
170     pVertexShaderBlob->Release();
171
172     g_pDevice->IASetInputLayout(pVertexLayout);
173
174
175     ID3D10Blob *pPixelShaderBlob;
176     hr = D3D10CompileShader(szShader, strlen(szShader), NULL, NULL, NULL, "PixelShader", "ps_4_0", 0, &pPixelShaderBlob, NULL);
177
178     ID3D10PixelShader * pPixelShader;
179     hr = g_pDevice->CreatePixelShader((DWORD*)pPixelShaderBlob->GetBufferPointer(), pPixelShaderBlob->GetBufferSize(), &pPixelShader);
180     pPixelShaderBlob->Release();
181
182
183     g_pDevice->VSSetShader(pVertexShader);
184     g_pDevice->GSSetShader(NULL);
185     g_pDevice->PSSetShader(pPixelShader);
186
187     static const Vertex vertices[] = {
188         { -0.9f, -0.9f, 0.5f,  0.8f, 0.0f, 0.0f, 0.1f },
189         {  0.9f, -0.9f, 0.5f,  0.0f, 0.9f, 0.0f, 0.1f },
190         {  0.0f,  0.9f, 0.5f,  0.0f, 0.0f, 0.7f, 0.1f },
191     };
192
193     D3D10_BUFFER_DESC BufferDesc;
194     ZeroMemory(&BufferDesc, sizeof BufferDesc);
195     BufferDesc.Usage = D3D10_USAGE_DYNAMIC;
196     BufferDesc.ByteWidth = sizeof vertices;
197     BufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
198     BufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
199     BufferDesc.MiscFlags = 0;
200
201     ID3D10Buffer *pVertexBuffer;
202     hr = g_pDevice->CreateBuffer(&BufferDesc, NULL, &pVertexBuffer);
203
204     void *pMap = NULL;
205     pVertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pMap);
206     memcpy(pMap, vertices, sizeof vertices);
207     pVertexBuffer->Unmap();
208
209     UINT Stride = sizeof(Vertex);
210     UINT Offset = 0;
211     g_pDevice->IASetVertexBuffers(0, 1, &pVertexBuffer, &Stride, &Offset);
212     
213     D3D10_VIEWPORT ViewPort;
214     ViewPort.TopLeftX = 0;
215     ViewPort.TopLeftY = 0;
216     ViewPort.Width = WindowWidth;
217     ViewPort.Height = WindowHeight;
218     ViewPort.MinDepth = 0.0f;
219     ViewPort.MaxDepth = 1.0f;
220     g_pDevice->RSSetViewports(1, &ViewPort);
221     
222     D3D10_RASTERIZER_DESC RasterizerDesc;
223     ZeroMemory(&RasterizerDesc, sizeof RasterizerDesc);
224     RasterizerDesc.CullMode = D3D10_CULL_NONE;
225     RasterizerDesc.FillMode = D3D10_FILL_SOLID;
226     RasterizerDesc.FrontCounterClockwise = true;
227     ID3D10RasterizerState* pRasterizerState;
228     g_pDevice->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
229     g_pDevice->RSSetState(pRasterizerState);
230
231     g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
232     g_pDevice->Draw(3, 0);
233
234     g_pSwapChain->Present(0, 0);
235
236     g_pSwapChain->Release();
237     g_pSwapChain = NULL;
238
239     g_pDevice->Release();
240     g_pDevice = NULL;
241
242     DestroyWindow(hWnd);
243
244     return 0;
245 }
246