]> git.cworth.org Git - apitrace-tests/blob - apps/d3d11_1/tri.cpp
Update examples to use DXGI.
[apitrace-tests] / apps / d3d11_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 <d3d11_1.h>
36
37 #include "tri_vs_4_0.h"
38 #include "tri_ps_4_0.h"
39
40
41 static IDXGISwapChain* g_pSwapChain = NULL;
42 static ID3D11Device * g_pDevice = NULL;
43 static ID3D11DeviceContext * g_pDeviceContext = NULL;
44 static ID3D11Device1 * g_pDevice1 = NULL;
45 static ID3D11DeviceContext1 * g_pDeviceContext1 = NULL;
46
47
48 int main(int argc, char *argv[]){
49     HRESULT hr;
50
51     HINSTANCE hInstance = GetModuleHandle(NULL);
52
53     WNDCLASSEX wc = {
54         sizeof(WNDCLASSEX),
55         CS_CLASSDC,
56         DefWindowProc,
57         0,
58         0,
59         hInstance,
60         NULL,
61         NULL,
62         NULL,
63         NULL,
64         "SimpleDX10",
65         NULL
66     };
67     RegisterClassEx(&wc);
68
69     const int WindowWidth = 250;
70     const int WindowHeight = 250;
71     BOOL Windowed = TRUE;
72
73     DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
74
75     RECT rect = {0, 0, WindowWidth, WindowHeight};
76     AdjustWindowRect(&rect, dwStyle, FALSE);
77
78     HWND hWnd = CreateWindow(wc.lpszClassName,
79                              "Simple example using DirectX10",
80                              dwStyle,
81                              CW_USEDEFAULT, CW_USEDEFAULT,
82                              rect.right - rect.left,
83                              rect.bottom - rect.top,
84                              NULL,
85                              NULL,
86                              hInstance,
87                              NULL);
88     if (!hWnd) {
89         return 1;
90     }
91
92     ShowWindow(hWnd, SW_SHOW);
93
94     UINT Flags = 0;
95     if (LoadLibraryA("d3d11sdklayers")) {
96         Flags |= D3D11_CREATE_DEVICE_DEBUG;
97     }
98
99     DXGI_SWAP_CHAIN_DESC SwapChainDesc;
100     ZeroMemory(&SwapChainDesc, sizeof SwapChainDesc);
101     SwapChainDesc.BufferDesc.Width = WindowWidth;
102     SwapChainDesc.BufferDesc.Height = WindowHeight;
103     SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;;
104     SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
105     SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
106     SwapChainDesc.SampleDesc.Quality = 0;
107     SwapChainDesc.SampleDesc.Count = 1;
108     SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
109     SwapChainDesc.BufferCount = 2;
110     SwapChainDesc.OutputWindow = hWnd;
111     SwapChainDesc.Windowed = true;
112
113     static const D3D_FEATURE_LEVEL FeatureLevels[] = {
114         D3D_FEATURE_LEVEL_11_1,
115         D3D_FEATURE_LEVEL_11_0,
116         D3D_FEATURE_LEVEL_10_1,
117         D3D_FEATURE_LEVEL_10_0
118     };
119
120     hr = D3D11CreateDeviceAndSwapChain(NULL, /* pAdapter */
121                                        D3D_DRIVER_TYPE_HARDWARE,
122                                        NULL, /* Software */
123                                        Flags,
124                                        FeatureLevels,
125                                        sizeof FeatureLevels / sizeof FeatureLevels[0],
126                                        D3D11_SDK_VERSION,
127                                        &SwapChainDesc,
128                                        &g_pSwapChain,
129                                        &g_pDevice,
130                                        NULL, /* pFeatureLevel */
131                                        &g_pDeviceContext); /* ppImmediateContext */
132     if (FAILED(hr)) {
133         return 1;
134     }
135
136     hr = g_pDevice->QueryInterface(IID_ID3D11Device1, (void **)&g_pDevice1);
137     if (FAILED(hr)) {
138         return 1;
139     }
140
141     hr = g_pDeviceContext->QueryInterface(IID_ID3D11DeviceContext1, (void **)&g_pDeviceContext1);
142     if (FAILED(hr)) {
143         return 1;
144     }
145
146     ID3D11RenderTargetView *pRenderTargetView = NULL;
147     ID3D11Texture2D* pBackBuffer;
148     hr = g_pSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void **)&pBackBuffer);
149     if (FAILED(hr)) {
150         return 1;
151     }
152     D3D11_RENDER_TARGET_VIEW_DESC RenderTargetViewDesc;
153     ZeroMemory(&RenderTargetViewDesc, sizeof RenderTargetViewDesc);
154     RenderTargetViewDesc.Format = SwapChainDesc.BufferDesc.Format;
155     RenderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
156     RenderTargetViewDesc.Texture2D.MipSlice = 0;
157     hr = g_pDevice1->CreateRenderTargetView(pBackBuffer, &RenderTargetViewDesc, &pRenderTargetView);
158     if (FAILED(hr)) {
159         return 1;
160     }
161     pBackBuffer->Release();
162
163     g_pDeviceContext1->OMSetRenderTargets(1, &pRenderTargetView, NULL);
164
165     const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
166     g_pDeviceContext1->ClearRenderTargetView(pRenderTargetView, clearColor);
167
168     ID3D11VertexShader * pVertexShader;
169     hr = g_pDevice1->CreateVertexShader(g_VS, sizeof g_VS, NULL, &pVertexShader);
170     if (FAILED(hr)) {
171         return 1;
172     }
173
174     struct Vertex {
175         float position[4];
176         float color[4];
177     };
178
179     static const D3D11_INPUT_ELEMENT_DESC InputElementDescs[] = {
180         { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, position), D3D11_INPUT_PER_VERTEX_DATA, 0 },
181         { "COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, color),    D3D11_INPUT_PER_VERTEX_DATA, 0 }
182     };
183
184     ID3D11InputLayout *pVertexLayout = NULL;
185     hr = g_pDevice1->CreateInputLayout(InputElementDescs,
186                                       2,
187                                       g_VS,
188                                       sizeof g_VS,
189                                       &pVertexLayout);
190     if (FAILED(hr)) {
191         return 1;
192     }
193
194     g_pDeviceContext1->IASetInputLayout(pVertexLayout);
195
196     ID3D11PixelShader * pPixelShader;
197     hr = g_pDevice1->CreatePixelShader(g_PS, sizeof g_PS, NULL, &pPixelShader);
198     if (FAILED(hr)) {
199         return 1;
200     }
201
202     g_pDeviceContext1->VSSetShader(pVertexShader, NULL, 0);
203     g_pDeviceContext1->PSSetShader(pPixelShader, NULL, 0);
204
205     static const Vertex vertices[] = {
206         { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
207         { {  0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
208         { {  0.0f,  0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
209     };
210
211     D3D11_BUFFER_DESC BufferDesc;
212     ZeroMemory(&BufferDesc, sizeof BufferDesc);
213     BufferDesc.Usage = D3D11_USAGE_DYNAMIC;
214     BufferDesc.ByteWidth = sizeof vertices;
215     BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
216     BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
217     BufferDesc.MiscFlags = 0;
218
219     D3D11_SUBRESOURCE_DATA BufferData;
220     BufferData.pSysMem = vertices;
221     BufferData.SysMemPitch = 0;
222     BufferData.SysMemSlicePitch = 0;
223
224     ID3D11Buffer *pVertexBuffer;
225     hr = g_pDevice1->CreateBuffer(&BufferDesc, &BufferData, &pVertexBuffer);
226     if (FAILED(hr)) {
227         return 1;
228     }
229
230     UINT Stride = sizeof(Vertex);
231     UINT Offset = 0;
232     g_pDeviceContext1->IASetVertexBuffers(0, 1, &pVertexBuffer, &Stride, &Offset);
233     
234     D3D11_VIEWPORT ViewPort;
235     ViewPort.TopLeftX = 0;
236     ViewPort.TopLeftY = 0;
237     ViewPort.Width = WindowWidth;
238     ViewPort.Height = WindowHeight;
239     ViewPort.MinDepth = 0.0f;
240     ViewPort.MaxDepth = 1.0f;
241     g_pDeviceContext1->RSSetViewports(1, &ViewPort);
242     
243     D3D11_RASTERIZER_DESC RasterizerDesc;
244     ZeroMemory(&RasterizerDesc, sizeof RasterizerDesc);
245     RasterizerDesc.CullMode = D3D11_CULL_NONE;
246     RasterizerDesc.FillMode = D3D11_FILL_SOLID;
247     RasterizerDesc.FrontCounterClockwise = true;
248     RasterizerDesc.DepthClipEnable = true;
249     ID3D11RasterizerState* pRasterizerState = NULL;
250     hr = g_pDevice1->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
251     if (FAILED(hr)) {
252         return 1;
253     }
254     g_pDeviceContext1->RSSetState(pRasterizerState);
255
256     g_pDeviceContext1->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
257     g_pDeviceContext1->Draw(3, 0);
258
259     g_pSwapChain->Present(0, 0);
260
261
262     ID3D11Buffer *pNullBuffer = NULL;
263     UINT NullStride = 0;
264     UINT NullOffset = 0;
265     g_pDeviceContext1->IASetVertexBuffers(0, 1, &pNullBuffer, &NullStride, &NullOffset);
266     pVertexBuffer->Release();
267
268     g_pDeviceContext1->OMSetRenderTargets(0, NULL, NULL);
269     pRenderTargetView->Release();
270
271     g_pDeviceContext1->IASetInputLayout(NULL);
272     pVertexLayout->Release();
273
274     g_pDeviceContext1->VSSetShader(NULL, NULL, 0);
275     pVertexShader->Release();
276
277     g_pDeviceContext1->PSSetShader(NULL, NULL, 0);
278     pPixelShader->Release();
279
280     g_pDeviceContext1->RSSetState(NULL);
281     pRasterizerState->Release();
282
283     g_pSwapChain->Release();
284     g_pSwapChain = NULL;
285
286     g_pDeviceContext1->Release();
287     g_pDeviceContext1 = NULL;
288
289     g_pDevice1->Release();
290     g_pDevice1 = NULL;
291
292     g_pDeviceContext->Release();
293     g_pDeviceContext = NULL;
294
295     g_pDevice->Release();
296     g_pDevice = NULL;
297
298     DestroyWindow(hWnd);
299
300     return 0;
301 }
302