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