]> git.cworth.org Git - apitrace-tests/blob - apps/d3d9/tri_pp.cpp
Add a shader d3d9 tri test.
[apitrace-tests] / apps / d3d9 / tri_pp.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 <stddef.h>
28
29 #include <windows.h>
30
31 #include <d3d9.h>
32
33 #include "tri_vs_2_0.h"
34 #include "tri_ps_2_0.h"
35
36
37 static IDirect3D9 * g_pD3D = NULL;
38 static IDirect3DDevice9 * g_pDevice = NULL;
39 static D3DPRESENT_PARAMETERS g_PresentationParameters;
40
41
42 int
43 main(int argc, char *argv[])
44 {
45     HRESULT hr;
46
47     HINSTANCE hInstance = GetModuleHandle(NULL);
48
49     WNDCLASSEX wc = {
50         sizeof(WNDCLASSEX),
51         CS_CLASSDC,
52         DefWindowProc,
53         0,
54         0,
55         hInstance,
56         NULL,
57         NULL,
58         NULL,
59         NULL,
60         "SimpleDX9",
61         NULL
62     };
63     RegisterClassEx(&wc);
64
65     const int WindowWidth = 250;
66     const int WindowHeight = 250;
67     BOOL Windowed = TRUE;
68
69     DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
70
71     RECT rect = {0, 0, WindowWidth, WindowHeight};
72     AdjustWindowRect(&rect, dwStyle, FALSE);
73
74     HWND hWnd = CreateWindow(wc.lpszClassName,
75                              "Simple example using DirectX9",
76                              dwStyle,
77                              CW_USEDEFAULT, CW_USEDEFAULT,
78                              rect.right - rect.left,
79                              rect.bottom - rect.top,
80                              NULL,
81                              NULL,
82                              hInstance,
83                              NULL);
84     if (!hWnd) {
85         return 1;
86     }
87
88     ShowWindow(hWnd, SW_SHOW);
89
90     g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
91     if (!g_pD3D) {
92         return 1;
93     }
94
95     D3DCAPS9 caps;
96     hr = g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
97     if (FAILED(hr)) {
98        return 1;
99     }
100
101     DWORD dwBehaviorFlags;
102     if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
103         caps.VertexShaderVersion < D3DVS_VERSION(1, 1)) {
104        dwBehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
105     } else {
106        dwBehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
107     }
108
109     ZeroMemory(&g_PresentationParameters, sizeof g_PresentationParameters);
110     g_PresentationParameters.Windowed = Windowed;
111     if (!Windowed) {
112         g_PresentationParameters.BackBufferWidth = WindowWidth;
113         g_PresentationParameters.BackBufferHeight = WindowHeight;
114     }
115     g_PresentationParameters.BackBufferCount = 1;
116     g_PresentationParameters.SwapEffect = D3DSWAPEFFECT_FLIP;
117     if (!Windowed) {
118         g_PresentationParameters.BackBufferFormat = D3DFMT_X8R8G8B8;
119     } else {
120         g_PresentationParameters.BackBufferFormat = D3DFMT_UNKNOWN;
121     }
122     g_PresentationParameters.hDeviceWindow = hWnd;
123
124     g_PresentationParameters.EnableAutoDepthStencil = FALSE;
125     g_PresentationParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
126
127     hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
128                               D3DDEVTYPE_HAL,
129                               hWnd,
130                               dwBehaviorFlags,
131                               &g_PresentationParameters,
132                               &g_pDevice);
133     if (FAILED(hr)) {
134         return 1;
135     }
136
137     D3DCOLOR clearColor = D3DCOLOR_COLORVALUE(0.3f, 0.1f, 0.3f, 1.0f);
138     g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, clearColor, 1.0f, 0);
139
140     struct Vertex {
141         float position[4];
142         float color[4];
143     };
144
145     static const Vertex vertices[] = {
146         { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
147         { {  0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
148         { {  0.0f,  0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
149     };
150
151     static const D3DVERTEXELEMENT9 VertexElements[] = {
152         { 0, offsetof(Vertex, position), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
153         { 0, offsetof(Vertex, color),    D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
154         D3DDECL_END()
155     };
156
157     LPDIRECT3DVERTEXDECLARATION9 pVertexDeclaration = NULL;
158     hr = g_pDevice->CreateVertexDeclaration(VertexElements, &pVertexDeclaration);
159     if (FAILED(hr)) {
160         return 1;
161     }
162     g_pDevice->SetVertexDeclaration(pVertexDeclaration);
163
164     LPDIRECT3DVERTEXSHADER9 pVertexShader = NULL;
165     hr = g_pDevice->CreateVertexShader((CONST DWORD *)g_vs20_VS, &pVertexShader);
166     if (FAILED(hr)) {
167         return 1;
168     }
169     g_pDevice->SetVertexShader(pVertexShader);
170
171     LPDIRECT3DPIXELSHADER9 pPixelShader = NULL;
172     hr = g_pDevice->CreatePixelShader((CONST DWORD *)g_ps20_PS, &pPixelShader);
173     if (FAILED(hr)) {
174         return 1;
175     }
176     g_pDevice->SetPixelShader(pPixelShader);
177
178     g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
179
180     g_pDevice->BeginScene();
181
182     g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, sizeof(Vertex));
183
184     g_pDevice->EndScene();
185
186     g_pDevice->Present(NULL, NULL, NULL, NULL);
187
188     pPixelShader->Release();
189     pPixelShader = NULL;
190
191     pVertexShader->Release();
192     pVertexShader = NULL;
193
194     pVertexDeclaration->Release();
195     pVertexDeclaration = NULL;
196
197     g_pDevice->Release();
198     g_pDevice = NULL;
199
200     g_pD3D->Release();
201     g_pD3D = NULL;
202
203     DestroyWindow(hWnd);
204
205     return 0;
206 }
207