2 * Copyright (C) 2005 Henri Verbeet
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/test.h"
23 static HMODULE d3d9_handle = 0;
25 static HWND create_window(void)
28 wc.lpfnWndProc = DefWindowProc;
29 wc.lpszClassName = "d3d9_test_wc";
32 return CreateWindow("d3d9_test_wc", "d3d9_test",
33 0, 0, 0, 0, 0, 0, 0, 0, 0);
36 static IDirect3DDevice9 *init_d3d9(void)
38 IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
39 IDirect3D9 *d3d9_ptr = 0;
40 IDirect3DDevice9 *device_ptr = 0;
41 D3DPRESENT_PARAMETERS present_parameters;
44 d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
45 ok(d3d9_create != NULL, "Failed to get address of Direct3DCreate9\n");
46 if (!d3d9_create) return NULL;
48 d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
51 skip("could not create D3D9\n");
55 ZeroMemory(&present_parameters, sizeof(present_parameters));
56 present_parameters.Windowed = TRUE;
57 present_parameters.hDeviceWindow = create_window();
58 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
60 hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
64 skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hres);
71 static int get_refcount(IUnknown *object)
73 IUnknown_AddRef(object);
74 return IUnknown_Release(object);
77 static void test_get_set_vertex_shader(IDirect3DDevice9 *device_ptr)
80 static DWORD simple_vs[] = {0xFFFE0101, /* vs_1_1 */
81 0x0000001F, 0x80000000, 0x900F0000, /* dcl_position0 v0 */
82 0x00000009, 0xC0010000, 0x90E40000, 0xA0E40000, /* dp4 oPos.x, v0, c0 */
83 0x00000009, 0xC0020000, 0x90E40000, 0xA0E40001, /* dp4 oPos.y, v0, c1 */
84 0x00000009, 0xC0040000, 0x90E40000, 0xA0E40002, /* dp4 oPos.z, v0, c2 */
85 0x00000009, 0xC0080000, 0x90E40000, 0xA0E40003, /* dp4 oPos.w, v0, c3 */
86 0x0000FFFF}; /* END */
88 IDirect3DVertexShader9 *shader_ptr = 0;
89 IDirect3DVertexShader9 *current_shader_ptr = 0;
91 int shader_refcount = 0;
94 hret = IDirect3DDevice9_CreateVertexShader(device_ptr, NULL, &shader_ptr);
95 ok(hret == D3DERR_INVALIDCALL, "CreateVertexShader returned %#x, expected D3DERR_INVALIDCALL (%#x).\n",
96 hret, D3DERR_INVALIDCALL);
98 hret = IDirect3DDevice9_CreateVertexShader(device_ptr, simple_vs, &shader_ptr);
99 ok(hret == D3D_OK && shader_ptr != NULL, "CreateVertexShader returned: hret 0x%x, shader_ptr %p. "
100 "Expected hret 0x%x, shader_ptr != %p. Aborting.\n", hret, shader_ptr, D3D_OK, NULL);
101 if (hret != D3D_OK || shader_ptr == NULL) return;
103 /* SetVertexShader should not touch the shader's refcount. */
104 i = get_refcount((IUnknown *)shader_ptr);
105 hret = IDirect3DDevice9_SetVertexShader(device_ptr, shader_ptr);
106 shader_refcount = get_refcount((IUnknown *)shader_ptr);
107 ok(hret == D3D_OK && shader_refcount == i, "SetVertexShader returned: hret 0x%x, refcount %d. "
108 "Expected hret 0x%x, refcount %d.\n", hret, shader_refcount, D3D_OK, i);
110 /* GetVertexShader should increase the shader's refcount by one. */
111 i = shader_refcount+1;
112 hret = IDirect3DDevice9_GetVertexShader(device_ptr, ¤t_shader_ptr);
113 shader_refcount = get_refcount((IUnknown *)shader_ptr);
114 ok(hret == D3D_OK && shader_refcount == i && current_shader_ptr == shader_ptr,
115 "GetVertexShader returned: hret 0x%x, current_shader_ptr %p refcount %d. "
116 "Expected hret 0x%x, current_shader_ptr %p, refcount %d.\n", hret, current_shader_ptr, shader_refcount, D3D_OK, shader_ptr, i);
119 static void test_vertex_shader_constant(IDirect3DDevice9 *device_ptr, DWORD consts)
121 float c[4] = { 0.0, 0.0, 0.0, 0.0 };
122 float d[16] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
125 /* A simple check that the stuff works at all */
126 hr = IDirect3DDevice9_SetVertexShaderConstantF(device_ptr, 0, c, 1);
127 ok(hr == D3D_OK, "IDirect3DDevice9_SetVertexShaderConstantF returned 0x%08x\n", hr);
129 /* Test corner cases: Write to const MAX - 1, MAX, MAX + 1, and writing 4 consts from
132 hr = IDirect3DDevice9_SetVertexShaderConstantF(device_ptr, consts - 1, c, 1);
133 ok(hr == D3D_OK, "IDirect3DDevice9_SetVertexShaderConstantF returned 0x%08x\n", hr);
134 hr = IDirect3DDevice9_SetVertexShaderConstantF(device_ptr, consts + 0, c, 1);
135 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_SetVertexShaderConstantF returned 0x%08x\n", hr);
136 hr = IDirect3DDevice9_SetVertexShaderConstantF(device_ptr, consts + 1, c, 1);
137 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_SetVertexShaderConstantF returned 0x%08x\n", hr);
138 hr = IDirect3DDevice9_SetVertexShaderConstantF(device_ptr, consts - 1, d, 4);
139 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_SetVertexShaderConstantF returned 0x%08x\n", hr);
142 hr = IDirect3DDevice9_SetVertexShaderConstantF(device_ptr, -1, c, 1);
143 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_SetVertexShaderConstantF returned 0x%08x\n", hr);
146 static void test_get_set_pixel_shader(IDirect3DDevice9 *device_ptr)
148 static DWORD simple_ps[] = {0xFFFF0101, /* ps_1_1 */
149 0x00000051, 0xA00F0001, 0x3F800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0 */
150 0x00000042, 0xB00F0000, /* tex t0 */
151 0x00000008, 0x800F0000, 0xA0E40001, 0xA0E40000, /* dp3 r0, c1, c0 */
152 0x00000005, 0x800F0000, 0x90E40000, 0x80E40000, /* mul r0, v0, r0 */
153 0x00000005, 0x800F0000, 0xB0E40000, 0x80E40000, /* mul r0, t0, r0 */
154 0x0000FFFF}; /* END */
156 IDirect3DPixelShader9 *shader_ptr = 0;
157 IDirect3DPixelShader9 *current_shader_ptr = 0;
159 int shader_refcount = 0;
162 hret = IDirect3DDevice9_CreatePixelShader(device_ptr, NULL, &shader_ptr);
163 ok(hret == D3DERR_INVALIDCALL, "CreatePixelShader returned %#x, expected D3DERR_INVALIDCALL (%#x).\n",
164 hret, D3DERR_INVALIDCALL);
166 hret = IDirect3DDevice9_CreatePixelShader(device_ptr, simple_ps, &shader_ptr);
167 ok(hret == D3D_OK && shader_ptr != NULL, "CreatePixelShader returned: hret 0x%x, shader_ptr %p. "
168 "Expected hret 0x%x, shader_ptr != %p. Aborting.\n", hret, shader_ptr, D3D_OK, NULL);
169 if (hret != D3D_OK || shader_ptr == NULL) return;
171 /* SetPixelsShader should not touch the shader's refcount. */
172 i = get_refcount((IUnknown *)shader_ptr);
173 hret = IDirect3DDevice9_SetPixelShader(device_ptr, shader_ptr);
174 shader_refcount = get_refcount((IUnknown *)shader_ptr);
175 ok(hret == D3D_OK && shader_refcount == i, "SetPixelShader returned: hret 0x%x, refcount %d. "
176 "Expected hret 0x%x, refcount %d.\n", hret, shader_refcount, D3D_OK, i);
178 /* GetPixelShader should increase the shader's refcount by one. */
179 i = shader_refcount+1;
180 hret = IDirect3DDevice9_GetPixelShader(device_ptr, ¤t_shader_ptr);
181 shader_refcount = get_refcount((IUnknown *)shader_ptr);
182 ok(hret == D3D_OK && shader_refcount == i && current_shader_ptr == shader_ptr,
183 "GetPixelShader returned: hret 0x%x, current_shader_ptr %p refcount %d. "
184 "Expected hret 0x%x, current_shader_ptr %p, refcount %d.\n", hret, current_shader_ptr, shader_refcount, D3D_OK, shader_ptr, i);
187 static void test_pixel_shader_constant(IDirect3DDevice9 *device_ptr)
189 float c[4] = { 0.0, 0.0, 0.0, 0.0 };
190 float d[16] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
194 /* A simple check that the stuff works at all */
195 hr = IDirect3DDevice9_SetPixelShaderConstantF(device_ptr, 0, c, 1);
196 ok(hr == D3D_OK, "IDirect3DDevice9_SetVertexShaderConstantF returned 0x%08x\n", hr);
198 /* Is there really no max pixel shader constant value??? Test how far I can go */
199 while(SUCCEEDED(IDirect3DDevice9_SetPixelShaderConstantF(device_ptr, consts++, c, 1)));
201 trace("SetPixelShaderConstantF was able to set %d shader constants\n", consts);
203 /* Test corner cases: writing 4 consts from MAX - 1, everything else is pointless
204 * given the way the constant limit was found out
206 hr = IDirect3DDevice9_SetPixelShaderConstantF(device_ptr, consts - 1, d, 4);
207 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_SetPixelShaderConstantF returned 0x%08x\n", hr);
210 hr = IDirect3DDevice9_SetPixelShaderConstantF(device_ptr, -1, c, 1);
211 ok(hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_SetPixelShaderConstantF returned 0x%08x\n", hr);
217 IDirect3DDevice9 *device_ptr;
219 d3d9_handle = LoadLibraryA("d3d9.dll");
222 skip("Could not load d3d9.dll\n");
226 device_ptr = init_d3d9();
227 if (!device_ptr) return;
229 IDirect3DDevice9_GetDeviceCaps(device_ptr, &caps);
231 if (caps.VertexShaderVersion & 0xffff)
233 test_get_set_vertex_shader(device_ptr);
234 test_vertex_shader_constant(device_ptr, caps.MaxVertexShaderConst);
236 else skip("No vertex shader support\n");
238 if (caps.PixelShaderVersion & 0xffff)
240 test_get_set_pixel_shader(device_ptr);
241 /* No max pixel shader constant value??? */
242 test_pixel_shader_constant(device_ptr);
244 else skip("No pixel shader support\n");