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