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