]> git.cworth.org Git - apitrace-tests/blob - apps/d3d11/tri.cpp
d3d*: Don't set the BufferDesc dimmension.
[apitrace-tests] / apps / d3d11 / 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.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
45
46 int
47 main(int argc, char *argv[])
48 {
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.Format = DXGI_FORMAT_R8G8B8A8_UNORM;;
102     SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
103     SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
104     SwapChainDesc.SampleDesc.Quality = 0;
105     SwapChainDesc.SampleDesc.Count = 1;
106     SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
107     SwapChainDesc.BufferCount = 2;
108     SwapChainDesc.OutputWindow = hWnd;
109     SwapChainDesc.Windowed = true;
110     SwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
111
112     static const D3D_FEATURE_LEVEL FeatureLevels[] = {
113         D3D_FEATURE_LEVEL_11_0,
114         D3D_FEATURE_LEVEL_10_1,
115         D3D_FEATURE_LEVEL_10_0
116     };
117
118     hr = D3D11CreateDeviceAndSwapChain(NULL, /* pAdapter */
119                                        D3D_DRIVER_TYPE_HARDWARE,
120                                        NULL, /* Software */
121                                        Flags,
122                                        FeatureLevels,
123                                        sizeof FeatureLevels / sizeof FeatureLevels[0],
124                                        D3D11_SDK_VERSION,
125                                        &SwapChainDesc,
126                                        &g_pSwapChain,
127                                        &g_pDevice,
128                                        NULL, /* pFeatureLevel */
129                                        &g_pDeviceContext); /* ppImmediateContext */
130     if (FAILED(hr)) {
131         return 1;
132     }
133
134     ID3D11RenderTargetView *pRenderTargetView = NULL;
135     ID3D11Texture2D* pBackBuffer;
136     hr = g_pSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void **)&pBackBuffer);
137     if (FAILED(hr)) {
138         return 1;
139     }
140     D3D11_RENDER_TARGET_VIEW_DESC RenderTargetViewDesc;
141     ZeroMemory(&RenderTargetViewDesc, sizeof RenderTargetViewDesc);
142     RenderTargetViewDesc.Format = SwapChainDesc.BufferDesc.Format;
143     RenderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
144     RenderTargetViewDesc.Texture2D.MipSlice = 0;
145     hr = g_pDevice->CreateRenderTargetView(pBackBuffer, &RenderTargetViewDesc, &pRenderTargetView);
146     if (FAILED(hr)) {
147         return 1;
148     }
149     pBackBuffer->Release();
150
151     g_pDeviceContext->OMSetRenderTargets(1, &pRenderTargetView, NULL);
152
153     const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
154     g_pDeviceContext->ClearRenderTargetView(pRenderTargetView, clearColor);
155
156     ID3D11VertexShader * pVertexShader;
157     hr = g_pDevice->CreateVertexShader(g_VS, sizeof g_VS, NULL, &pVertexShader);
158     if (FAILED(hr)) {
159         return 1;
160     }
161
162     struct Vertex {
163         float position[4];
164         float color[4];
165     };
166
167     static const D3D11_INPUT_ELEMENT_DESC InputElementDescs[] = {
168         { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, position), D3D11_INPUT_PER_VERTEX_DATA, 0 },
169         { "COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, color),    D3D11_INPUT_PER_VERTEX_DATA, 0 }
170     };
171
172     ID3D11InputLayout *pVertexLayout = NULL;
173     hr = g_pDevice->CreateInputLayout(InputElementDescs,
174                                       2,
175                                       g_VS,
176                                       sizeof g_VS,
177                                       &pVertexLayout);
178     if (FAILED(hr)) {
179         return 1;
180     }
181
182     g_pDeviceContext->IASetInputLayout(pVertexLayout);
183
184     ID3D11PixelShader * pPixelShader;
185     hr = g_pDevice->CreatePixelShader(g_PS, sizeof g_PS, NULL, &pPixelShader);
186     if (FAILED(hr)) {
187         return 1;
188     }
189
190     g_pDeviceContext->VSSetShader(pVertexShader, NULL, 0);
191     g_pDeviceContext->PSSetShader(pPixelShader, NULL, 0);
192
193     static const Vertex vertices[] = {
194         { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
195         { {  0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
196         { {  0.0f,  0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
197     };
198
199     D3D11_BUFFER_DESC BufferDesc;
200     ZeroMemory(&BufferDesc, sizeof BufferDesc);
201     BufferDesc.Usage = D3D11_USAGE_DYNAMIC;
202     BufferDesc.ByteWidth = sizeof vertices;
203     BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
204     BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
205     BufferDesc.MiscFlags = 0;
206
207     D3D11_SUBRESOURCE_DATA BufferData;
208     BufferData.pSysMem = vertices;
209     BufferData.SysMemPitch = 0;
210     BufferData.SysMemSlicePitch = 0;
211
212     ID3D11Buffer *pVertexBuffer;
213     hr = g_pDevice->CreateBuffer(&BufferDesc, &BufferData, &pVertexBuffer);
214     if (FAILED(hr)) {
215         return 1;
216     }
217
218     UINT Stride = sizeof(Vertex);
219     UINT Offset = 0;
220     g_pDeviceContext->IASetVertexBuffers(0, 1, &pVertexBuffer, &Stride, &Offset);
221     
222     D3D11_VIEWPORT ViewPort;
223     ViewPort.TopLeftX = 0;
224     ViewPort.TopLeftY = 0;
225     ViewPort.Width = WindowWidth;
226     ViewPort.Height = WindowHeight;
227     ViewPort.MinDepth = 0.0f;
228     ViewPort.MaxDepth = 1.0f;
229     g_pDeviceContext->RSSetViewports(1, &ViewPort);
230     
231     D3D11_RASTERIZER_DESC RasterizerDesc;
232     ZeroMemory(&RasterizerDesc, sizeof RasterizerDesc);
233     RasterizerDesc.CullMode = D3D11_CULL_NONE;
234     RasterizerDesc.FillMode = D3D11_FILL_SOLID;
235     RasterizerDesc.FrontCounterClockwise = true;
236     RasterizerDesc.DepthClipEnable = true;
237     ID3D11RasterizerState* pRasterizerState = NULL;
238     hr = g_pDevice->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
239     if (FAILED(hr)) {
240         return 1;
241     }
242     g_pDeviceContext->RSSetState(pRasterizerState);
243
244     g_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
245     g_pDeviceContext->Draw(3, 0);
246
247     g_pSwapChain->Present(0, 0);
248
249
250     ID3D11Buffer *pNullBuffer = NULL;
251     UINT NullStride = 0;
252     UINT NullOffset = 0;
253     g_pDeviceContext->IASetVertexBuffers(0, 1, &pNullBuffer, &NullStride, &NullOffset);
254     pVertexBuffer->Release();
255
256     g_pDeviceContext->OMSetRenderTargets(0, NULL, NULL);
257     pRenderTargetView->Release();
258
259     g_pDeviceContext->IASetInputLayout(NULL);
260     pVertexLayout->Release();
261
262     g_pDeviceContext->VSSetShader(NULL, NULL, 0);
263     pVertexShader->Release();
264
265     g_pDeviceContext->PSSetShader(NULL, NULL, 0);
266     pPixelShader->Release();
267
268     g_pDeviceContext->RSSetState(NULL);
269     pRasterizerState->Release();
270
271     g_pSwapChain->Release();
272     g_pSwapChain = NULL;
273
274     g_pDeviceContext->Release();
275     g_pDeviceContext = NULL;
276
277     g_pDevice->Release();
278     g_pDevice = NULL;
279
280     DestroyWindow(hWnd);
281
282     return 0;
283 }
284