]> git.cworth.org Git - apitrace-tests/blob - apps/d3d10_1/tri.cpp
674eaacc453f1fd0293f2f8e23718ae31e7b4548
[apitrace-tests] / apps / d3d10_1 / 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 <stdio.h>
28 #include <stddef.h>
29
30 #include <initguid.h>
31 #include <windows.h>
32
33 #include "compat.h"
34
35 #include <d3d10_1.h>
36
37 #include "tri_vs_4_0.h"
38 #include "tri_ps_4_0.h"
39
40
41 static IDXGIFactory1 *g_pFactory = NULL;
42 static IDXGIAdapter *g_pAdapter = NULL;
43 static IDXGISwapChain* g_pSwapChain = NULL;
44 static ID3D10Device1 * g_pDevice = NULL;
45
46
47 int
48 main(int argc, char *argv[])
49 {
50     HRESULT hr;
51
52     HINSTANCE hInstance = GetModuleHandle(NULL);
53
54     WNDCLASSEX wc = {
55         sizeof(WNDCLASSEX),
56         CS_CLASSDC,
57         DefWindowProc,
58         0,
59         0,
60         hInstance,
61         NULL,
62         NULL,
63         NULL,
64         NULL,
65         "SimpleDX10",
66         NULL
67     };
68     RegisterClassEx(&wc);
69
70     const int WindowWidth = 250;
71     const int WindowHeight = 250;
72     BOOL Windowed = TRUE;
73
74     DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
75
76     RECT rect = {0, 0, WindowWidth, WindowHeight};
77     AdjustWindowRect(&rect, dwStyle, FALSE);
78
79     HWND hWnd = CreateWindow(wc.lpszClassName,
80                              "Simple example using DirectX10",
81                              dwStyle,
82                              CW_USEDEFAULT, CW_USEDEFAULT,
83                              rect.right - rect.left,
84                              rect.bottom - rect.top,
85                              NULL,
86                              NULL,
87                              hInstance,
88                              NULL);
89     if (!hWnd) {
90         return 1;
91     }
92
93     ShowWindow(hWnd, SW_SHOW);
94
95     UINT Flags = 0;
96     if (LoadLibraryA("d3d10sdklayers")) {
97         Flags |= D3D10_CREATE_DEVICE_DEBUG;
98     }
99
100     hr = CreateDXGIFactory1(IID_IDXGIFactory1, (void**)(&g_pFactory) );
101     if (FAILED(hr)) {
102         return 1;
103     }
104
105     hr = g_pFactory->EnumAdapters(0, &g_pAdapter);
106     if (FAILED(hr)) {
107         return 1;
108     }
109
110     hr = D3D10CreateDevice1(g_pAdapter,
111                             D3D10_DRIVER_TYPE_HARDWARE,
112                             NULL,
113                             Flags,
114                             D3D10_FEATURE_LEVEL_10_0,
115                             D3D10_1_SDK_VERSION,
116                             &g_pDevice);
117     if (FAILED(hr)) {
118         return 1;
119     }
120
121     DXGI_SWAP_CHAIN_DESC SwapChainDesc;
122     ZeroMemory(&SwapChainDesc, sizeof SwapChainDesc);
123     SwapChainDesc.BufferDesc.Width = WindowWidth;
124     SwapChainDesc.BufferDesc.Height = WindowHeight;
125     SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;;
126     SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
127     SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
128     SwapChainDesc.SampleDesc.Quality = 0;
129     SwapChainDesc.SampleDesc.Count = 1;
130     SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
131     SwapChainDesc.BufferCount = 2;
132     SwapChainDesc.OutputWindow = hWnd;
133     SwapChainDesc.Windowed = true;
134     SwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
135
136     hr = g_pFactory->CreateSwapChain(g_pDevice, &SwapChainDesc, &g_pSwapChain);
137     if (FAILED(hr)) {
138         return 1;
139     }
140
141     ID3D10RenderTargetView *pRenderTargetView = NULL;
142     ID3D10Texture2D* pBackBuffer;
143     hr = g_pSwapChain->GetBuffer(0, IID_ID3D10Texture2D, (void **)&pBackBuffer);
144     if (FAILED(hr)) {
145         return 1;
146     }
147     D3D10_RENDER_TARGET_VIEW_DESC RenderTargetViewDesc;
148     ZeroMemory(&RenderTargetViewDesc, sizeof RenderTargetViewDesc);
149     RenderTargetViewDesc.Format = SwapChainDesc.BufferDesc.Format;
150     RenderTargetViewDesc.ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2D;
151     RenderTargetViewDesc.Texture2D.MipSlice = 0;
152     hr = g_pDevice->CreateRenderTargetView(pBackBuffer, &RenderTargetViewDesc, &pRenderTargetView);
153     if (FAILED(hr)) {
154         return 1;
155     }
156     pBackBuffer->Release();
157
158     g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
159
160     const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
161     g_pDevice->ClearRenderTargetView(pRenderTargetView, clearColor);
162
163     ID3D10VertexShader * pVertexShader;
164     hr = g_pDevice->CreateVertexShader(g_VS, sizeof g_VS, &pVertexShader);
165     if (FAILED(hr)) {
166         return 1;
167     }
168
169     struct Vertex {
170         float position[4];
171         float color[4];
172     };
173
174     static const D3D10_INPUT_ELEMENT_DESC InputElementDescs[] = {
175         { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, position), D3D10_INPUT_PER_VERTEX_DATA, 0 },
176         { "COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, color),    D3D10_INPUT_PER_VERTEX_DATA, 0 }
177     };
178
179     ID3D10InputLayout *pVertexLayout = NULL;
180     hr = g_pDevice->CreateInputLayout(InputElementDescs,
181                                       2,
182                                       g_VS,
183                                       sizeof g_VS,
184                                       &pVertexLayout);
185     if (FAILED(hr)) {
186         return 1;
187     }
188
189     g_pDevice->IASetInputLayout(pVertexLayout);
190
191     ID3D10PixelShader * pPixelShader;
192     hr = g_pDevice->CreatePixelShader(g_PS, sizeof g_PS, &pPixelShader);
193     if (FAILED(hr)) {
194         return 1;
195     }
196
197     g_pDevice->VSSetShader(pVertexShader);
198     g_pDevice->PSSetShader(pPixelShader);
199
200     static const Vertex vertices[] = {
201         { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
202         { {  0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
203         { {  0.0f,  0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
204     };
205
206     D3D10_BUFFER_DESC BufferDesc;
207     ZeroMemory(&BufferDesc, sizeof BufferDesc);
208     BufferDesc.Usage = D3D10_USAGE_DYNAMIC;
209     BufferDesc.ByteWidth = sizeof vertices;
210     BufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
211     BufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
212     BufferDesc.MiscFlags = 0;
213
214     ID3D10Buffer *pVertexBuffer;
215     hr = g_pDevice->CreateBuffer(&BufferDesc, NULL, &pVertexBuffer);
216     if (FAILED(hr)) {
217         return 1;
218     }
219
220     void *pMap = NULL;
221     pVertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pMap);
222     memcpy(pMap, vertices, sizeof vertices);
223     pVertexBuffer->Unmap();
224
225     UINT Stride = sizeof(Vertex);
226     UINT Offset = 0;
227     g_pDevice->IASetVertexBuffers(0, 1, &pVertexBuffer, &Stride, &Offset);
228     
229     D3D10_VIEWPORT ViewPort;
230     ViewPort.TopLeftX = 0;
231     ViewPort.TopLeftY = 0;
232     ViewPort.Width = WindowWidth;
233     ViewPort.Height = WindowHeight;
234     ViewPort.MinDepth = 0.0f;
235     ViewPort.MaxDepth = 1.0f;
236     g_pDevice->RSSetViewports(1, &ViewPort);
237     
238     D3D10_RASTERIZER_DESC RasterizerDesc;
239     ZeroMemory(&RasterizerDesc, sizeof RasterizerDesc);
240     RasterizerDesc.CullMode = D3D10_CULL_NONE;
241     RasterizerDesc.FillMode = D3D10_FILL_SOLID;
242     RasterizerDesc.FrontCounterClockwise = true;
243     RasterizerDesc.DepthClipEnable = true;
244     ID3D10RasterizerState* pRasterizerState = NULL;
245     hr = g_pDevice->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
246     if (FAILED(hr)) {
247         return 1;
248     }
249     g_pDevice->RSSetState(pRasterizerState);
250
251     g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
252     g_pDevice->Draw(3, 0);
253
254     g_pSwapChain->Present(0, 0);
255
256
257     ID3D10Buffer *pNullBuffer = NULL;
258     UINT NullStride = 0;
259     UINT NullOffset = 0;
260     g_pDevice->IASetVertexBuffers(0, 1, &pNullBuffer, &NullStride, &NullOffset);
261     pVertexBuffer->Release();
262
263     g_pDevice->OMSetRenderTargets(0, NULL, NULL);
264     pRenderTargetView->Release();
265
266     g_pDevice->IASetInputLayout(NULL);
267     pVertexLayout->Release();
268
269     g_pDevice->VSSetShader(NULL);
270     pVertexShader->Release();
271
272     g_pDevice->PSSetShader(NULL);
273     pPixelShader->Release();
274
275     g_pDevice->RSSetState(NULL);
276     pRasterizerState->Release();
277
278     g_pSwapChain->Release();
279     g_pSwapChain = NULL;
280
281     g_pDevice->Release();
282     g_pDevice = NULL;
283
284     DestroyWindow(hWnd);
285
286     return 0;
287 }
288