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