d3d9: Add traces for when shader support is missing in the shader test.
[wine] / dlls / d3d9 / tests / shader.c
1 /*
2  * Copyright (C) 2005 Henri Verbeet
3  *
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.
8  *
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.
13  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #define COBJMACROS
20 #include <d3d9.h>
21 #include "wine/test.h"
22
23 static HMODULE d3d9_handle = 0;
24
25 static IDirect3DDevice9 *init_d3d9(void)
26 {
27     IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
28     IDirect3D9 *d3d9_ptr = 0;
29     IDirect3DDevice9 *device_ptr = 0;
30     D3DPRESENT_PARAMETERS present_parameters;
31     HRESULT hres;
32
33     d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
34     ok(d3d9_create != NULL, "Failed to get address of Direct3DCreate9\n");
35     if (!d3d9_create) return NULL;
36     
37     d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
38     ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
39     if (!d3d9_ptr) return NULL;
40
41     ZeroMemory(&present_parameters, sizeof(present_parameters));
42     present_parameters.Windowed = TRUE;
43     present_parameters.hDeviceWindow = GetDesktopWindow();
44     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
45
46     hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
47     ok(hres == D3D_OK, "IDirect3D_CreateDevice returned: 0x%lx\n", hres);
48
49     return device_ptr;
50 }
51
52 static int get_refcount(IUnknown *object)
53 {
54     IUnknown_AddRef(object);
55     return IUnknown_Release(object);
56 }
57
58 static void test_get_set_vertex_shader(IDirect3DDevice9 *device_ptr)
59 {
60
61     static DWORD simple_vs[] = {0xFFFE0101,             /* vs_1_1               */
62         0x0000001F, 0x80000000, 0x900F0000,             /* dcl_position0 v0     */
63         0x00000009, 0xC0010000, 0x90E40000, 0xA0E40000, /* dp4 oPos.x, v0, c0   */
64         0x00000009, 0xC0020000, 0x90E40000, 0xA0E40001, /* dp4 oPos.y, v0, c1   */
65         0x00000009, 0xC0040000, 0x90E40000, 0xA0E40002, /* dp4 oPos.z, v0, c2   */
66         0x00000009, 0xC0080000, 0x90E40000, 0xA0E40003, /* dp4 oPos.w, v0, c3   */
67         0x0000FFFF};                                    /* END                  */
68
69     IDirect3DVertexShader9 *shader_ptr = 0;
70     IDirect3DVertexShader9 *current_shader_ptr = 0;
71     HRESULT hret = 0;
72     int shader_refcount = 0;
73     int i = 0;
74     
75     hret = IDirect3DDevice9_CreateVertexShader(device_ptr, simple_vs, &shader_ptr);
76     ok(hret == D3D_OK && shader_ptr != NULL, "CreateVertexShader returned: hret 0x%lx, shader_ptr %p. "
77         "Expected hret 0x%lx, shader_ptr != %p. Aborting.\n", hret, shader_ptr, D3D_OK, NULL);
78     if (hret != D3D_OK || shader_ptr == NULL) return;
79
80     /* SetVertexShader should not touch the shader's refcount. */
81     i = get_refcount((IUnknown *)shader_ptr);
82     hret = IDirect3DDevice9_SetVertexShader(device_ptr, shader_ptr);
83     shader_refcount = get_refcount((IUnknown *)shader_ptr);
84     ok(hret == D3D_OK && shader_refcount == i, "SetVertexShader returned: hret 0x%lx, refcount %d. "
85         "Expected hret 0x%lx, refcount %d.\n", hret, shader_refcount, D3D_OK, i);
86
87     /* GetVertexShader should increase the shader's refcount by one. */
88     i = shader_refcount+1;
89     hret = IDirect3DDevice9_GetVertexShader(device_ptr, &current_shader_ptr);
90     shader_refcount = get_refcount((IUnknown *)shader_ptr);
91     ok(hret == D3D_OK && shader_refcount == i && current_shader_ptr == shader_ptr, 
92         "GetVertexShader returned: hret 0x%lx, current_shader_ptr %p refcount %d. "
93         "Expected hret 0x%lx, current_shader_ptr %p, refcount %d.\n", hret, current_shader_ptr, shader_refcount, D3D_OK, shader_ptr, i);
94 }
95
96 static void test_get_set_pixel_shader(IDirect3DDevice9 *device_ptr)
97 {
98     static DWORD simple_ps[] = {0xFFFF0101,                                     /* ps_1_1                       */
99         0x00000051, 0xA00F0001, 0x3F800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0  */
100         0x00000042, 0xB00F0000,                                                 /* tex t0                       */
101         0x00000008, 0x800F0000, 0xA0E40001, 0xA0E40000,                         /* dp3 r0, c1, c0               */
102         0x00000005, 0x800F0000, 0x90E40000, 0x80E40000,                         /* mul r0, v0, r0               */
103         0x00000005, 0x800F0000, 0xB0E40000, 0x80E40000,                         /* mul r0, t0, r0               */
104         0x0000FFFF};                                                            /* END                          */
105
106     IDirect3DPixelShader9 *shader_ptr = 0;
107     IDirect3DPixelShader9 *current_shader_ptr = 0;
108     HRESULT hret = 0;
109     int shader_refcount = 0;
110     int i = 0;
111
112     hret = IDirect3DDevice9_CreatePixelShader(device_ptr, simple_ps, &shader_ptr);
113     ok(hret == D3D_OK && shader_ptr != NULL, "CreatePixelShader returned: hret 0x%lx, shader_ptr %p. "
114         "Expected hret 0x%lx, shader_ptr != %p. Aborting.\n", hret, shader_ptr, D3D_OK, NULL);
115     if (hret != D3D_OK || shader_ptr == NULL) return;
116
117     /* SetPixelsShader should not touch the shader's refcount. */
118     i = get_refcount((IUnknown *)shader_ptr);
119     hret = IDirect3DDevice9_SetPixelShader(device_ptr, shader_ptr);
120     shader_refcount = get_refcount((IUnknown *)shader_ptr);
121     ok(hret == D3D_OK && shader_refcount == i, "SetPixelShader returned: hret 0x%lx, refcount %d. "
122         "Expected hret 0x%lx, refcount %d.\n", hret, shader_refcount, D3D_OK, i);
123
124     /* GetPixelShader should increase the shader's refcount by one. */
125     i = shader_refcount+1;
126     hret = IDirect3DDevice9_GetPixelShader(device_ptr, &current_shader_ptr);
127     shader_refcount = get_refcount((IUnknown *)shader_ptr);
128     ok(hret == D3D_OK && shader_refcount == i && current_shader_ptr == shader_ptr, 
129         "GetPixelShader returned: hret 0x%lx, current_shader_ptr %p refcount %d. "
130         "Expected hret 0x%lx, current_shader_ptr %p, refcount %d.\n", hret, current_shader_ptr, shader_refcount, D3D_OK, shader_ptr, i);
131 }
132
133 START_TEST(shader)
134 {
135     D3DCAPS9 caps;
136     IDirect3DDevice9 *device_ptr;
137
138     d3d9_handle = LoadLibraryA("d3d9.dll");
139     if (!d3d9_handle)
140     {
141         trace("Could not load d3d9.dll, skipping tests\n");
142         return;
143     }
144
145     device_ptr = init_d3d9();
146     if (!device_ptr) return;
147
148     IDirect3DDevice9_GetDeviceCaps(device_ptr, &caps);
149
150     if (caps.VertexShaderVersion & 0xffff)
151     {
152         test_get_set_vertex_shader(device_ptr);
153     }
154     else trace("No vertex shader support, skipping test\n");
155
156     if (caps.PixelShaderVersion & 0xffff)
157     {
158         test_get_set_pixel_shader(device_ptr);
159     }
160     else trace("No pixel shader support, skipping test\n");
161 }