]> git.cworth.org Git - apitrace-tests/blob - apps/d3d10/tri.cpp
737d27d589a4d37c08d3d58a8831a1318cabcba3
[apitrace-tests] / apps / d3d10 / 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 <d3d10.h>
36
37 #include "tri_vs.h"
38 #include "tri_ps.h"
39
40
41 static IDXGISwapChain* g_pSwapChain = NULL;
42 static ID3D10Device * 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     DXGI_SWAP_CHAIN_DESC SwapChainDesc;
92     ZeroMemory(&SwapChainDesc, sizeof SwapChainDesc);
93     SwapChainDesc.BufferDesc.Width = WindowWidth;
94     SwapChainDesc.BufferDesc.Height = WindowHeight;
95     SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;;
96     SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
97     SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
98     SwapChainDesc.SampleDesc.Quality = 0;
99     SwapChainDesc.SampleDesc.Count = 1;
100     SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
101     SwapChainDesc.BufferCount = 2;
102     SwapChainDesc.OutputWindow = hWnd;
103     SwapChainDesc.Windowed = true;
104
105     hr = D3D10CreateDeviceAndSwapChain(NULL,
106                                        D3D10_DRIVER_TYPE_HARDWARE,
107                                        NULL,
108                                        D3D10_CREATE_DEVICE_DEBUG,
109                                        D3D10_SDK_VERSION,
110                                        &SwapChainDesc,
111                                        &g_pSwapChain,
112                                        &g_pDevice);
113     if (FAILED(hr)) {
114         return 1;
115     }
116
117     ID3D10RenderTargetView *pRenderTargetView = NULL;
118     ID3D10Texture2D* pBackBuffer;
119     hr = g_pSwapChain->GetBuffer(0, IID_ID3D10Texture2D, (void **)&pBackBuffer);
120     if (FAILED(hr)) {
121         return 1;
122     }
123     hr = g_pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
124     if (FAILED(hr)) {
125         return 1;
126     }
127     pBackBuffer->Release();
128
129     g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
130
131     const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
132     g_pDevice->ClearRenderTargetView(pRenderTargetView, clearColor);
133
134     ID3D10VertexShader * pVertexShader;
135     hr = g_pDevice->CreateVertexShader(g_VS, sizeof g_VS, &pVertexShader);
136     if (FAILED(hr)) {
137         return 1;
138     }
139
140     struct Vertex {
141         float position[4];
142         float color[4];
143     };
144
145     static const D3D10_INPUT_ELEMENT_DESC InputElementDescs[] = {
146         { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, position), D3D10_INPUT_PER_VERTEX_DATA, 0 },
147         { "COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, color),    D3D10_INPUT_PER_VERTEX_DATA, 0 }
148     };
149
150     ID3D10InputLayout *pVertexLayout = NULL;
151     hr = g_pDevice->CreateInputLayout(InputElementDescs,
152                                       2,
153                                       g_VS,
154                                       sizeof g_VS,
155                                       &pVertexLayout);
156     if (FAILED(hr)) {
157         return 1;
158     }
159
160     g_pDevice->IASetInputLayout(pVertexLayout);
161
162     ID3D10PixelShader * pPixelShader;
163     hr = g_pDevice->CreatePixelShader(g_PS, sizeof g_PS, &pPixelShader);
164     if (FAILED(hr)) {
165         return 1;
166     }
167
168     g_pDevice->VSSetShader(pVertexShader);
169     g_pDevice->PSSetShader(pPixelShader);
170
171     static const Vertex vertices[] = {
172         { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
173         { {  0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
174         { {  0.0f,  0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
175     };
176
177     D3D10_BUFFER_DESC BufferDesc;
178     ZeroMemory(&BufferDesc, sizeof BufferDesc);
179     BufferDesc.Usage = D3D10_USAGE_DYNAMIC;
180     BufferDesc.ByteWidth = sizeof vertices;
181     BufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
182     BufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
183     BufferDesc.MiscFlags = 0;
184
185     ID3D10Buffer *pVertexBuffer;
186     hr = g_pDevice->CreateBuffer(&BufferDesc, NULL, &pVertexBuffer);
187     if (FAILED(hr)) {
188         return 1;
189     }
190
191     void *pMap = NULL;
192     pVertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pMap);
193     memcpy(pMap, vertices, sizeof vertices);
194     pVertexBuffer->Unmap();
195
196     UINT Stride = sizeof(Vertex);
197     UINT Offset = 0;
198     g_pDevice->IASetVertexBuffers(0, 1, &pVertexBuffer, &Stride, &Offset);
199     
200     D3D10_VIEWPORT ViewPort;
201     ViewPort.TopLeftX = 0;
202     ViewPort.TopLeftY = 0;
203     ViewPort.Width = WindowWidth;
204     ViewPort.Height = WindowHeight;
205     ViewPort.MinDepth = 0.0f;
206     ViewPort.MaxDepth = 1.0f;
207     g_pDevice->RSSetViewports(1, &ViewPort);
208     
209     D3D10_RASTERIZER_DESC RasterizerDesc;
210     ZeroMemory(&RasterizerDesc, sizeof RasterizerDesc);
211     RasterizerDesc.CullMode = D3D10_CULL_NONE;
212     RasterizerDesc.FillMode = D3D10_FILL_SOLID;
213     RasterizerDesc.FrontCounterClockwise = true;
214     ID3D10RasterizerState* pRasterizerState = NULL;
215     g_pDevice->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
216     g_pDevice->RSSetState(pRasterizerState);
217
218     g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
219     g_pDevice->Draw(3, 0);
220
221     g_pSwapChain->Present(0, 0);
222
223
224     ID3D10Buffer *pNullBuffer = NULL;
225     UINT NullStride = 0;
226     UINT NullOffset = 0;
227     g_pDevice->IASetVertexBuffers(0, 1, &pNullBuffer, &NullStride, &NullOffset);
228     pVertexBuffer->Release();
229
230     g_pDevice->OMSetRenderTargets(0, NULL, NULL);
231     pRenderTargetView->Release();
232
233     g_pDevice->IASetInputLayout(NULL);
234     pVertexLayout->Release();
235
236     g_pDevice->VSSetShader(NULL);
237     pVertexShader->Release();
238
239     g_pDevice->PSSetShader(NULL);
240     pPixelShader->Release();
241
242     g_pDevice->RSSetState(NULL);
243     pRasterizerState->Release();
244
245     g_pSwapChain->Release();
246     g_pSwapChain = NULL;
247
248     g_pDevice->Release();
249     g_pDevice = NULL;
250
251     DestroyWindow(hWnd);
252
253     return 0;
254 }
255