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