advapi32: Remove a useless macro.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 HWND create_window(void)
26 {
27     WNDCLASS wc = {0};
28     wc.lpfnWndProc = &DefWindowProc;
29     wc.lpszClassName = "d3d9_test_wc";
30     RegisterClass(&wc);
31
32     return CreateWindow("d3d9_test_wc", "d3d9_test",
33             0, 0, 0, 0, 0, 0, 0, 0, 0);
34 }
35
36 static IDirect3DDevice9 *init_d3d9(void)
37 {
38     IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
39     IDirect3D9 *d3d9_ptr = 0;
40     IDirect3DDevice9 *device_ptr = 0;
41     D3DPRESENT_PARAMETERS present_parameters;
42     HRESULT hres;
43
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;
47     
48     d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
49     ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
50     if (!d3d9_ptr) return NULL;
51
52     ZeroMemory(&present_parameters, sizeof(present_parameters));
53     present_parameters.Windowed = TRUE;
54     present_parameters.hDeviceWindow = create_window();
55     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
56
57     hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
58
59     if(FAILED(hres))
60     {
61         trace("could not create device, IDirect3D9_CreateDevice returned %#x\n", hres);
62         return NULL;
63     }
64
65     return device_ptr;
66 }
67
68 static int get_refcount(IUnknown *object)
69 {
70     IUnknown_AddRef(object);
71     return IUnknown_Release(object);
72 }
73
74 static void test_get_set_vertex_shader(IDirect3DDevice9 *device_ptr)
75 {
76
77     static DWORD simple_vs[] = {0xFFFE0101,             /* vs_1_1               */
78         0x0000001F, 0x80000000, 0x900F0000,             /* dcl_position0 v0     */
79         0x00000009, 0xC0010000, 0x90E40000, 0xA0E40000, /* dp4 oPos.x, v0, c0   */
80         0x00000009, 0xC0020000, 0x90E40000, 0xA0E40001, /* dp4 oPos.y, v0, c1   */
81         0x00000009, 0xC0040000, 0x90E40000, 0xA0E40002, /* dp4 oPos.z, v0, c2   */
82         0x00000009, 0xC0080000, 0x90E40000, 0xA0E40003, /* dp4 oPos.w, v0, c3   */
83         0x0000FFFF};                                    /* END                  */
84
85     IDirect3DVertexShader9 *shader_ptr = 0;
86     IDirect3DVertexShader9 *current_shader_ptr = 0;
87     HRESULT hret = 0;
88     int shader_refcount = 0;
89     int i = 0;
90     
91     hret = IDirect3DDevice9_CreateVertexShader(device_ptr, simple_vs, &shader_ptr);
92     ok(hret == D3D_OK && shader_ptr != NULL, "CreateVertexShader returned: hret 0x%x, shader_ptr %p. "
93         "Expected hret 0x%x, shader_ptr != %p. Aborting.\n", hret, shader_ptr, D3D_OK, NULL);
94     if (hret != D3D_OK || shader_ptr == NULL) return;
95
96     /* SetVertexShader should not touch the shader's refcount. */
97     i = get_refcount((IUnknown *)shader_ptr);
98     hret = IDirect3DDevice9_SetVertexShader(device_ptr, shader_ptr);
99     shader_refcount = get_refcount((IUnknown *)shader_ptr);
100     ok(hret == D3D_OK && shader_refcount == i, "SetVertexShader returned: hret 0x%x, refcount %d. "
101         "Expected hret 0x%x, refcount %d.\n", hret, shader_refcount, D3D_OK, i);
102
103     /* GetVertexShader should increase the shader's refcount by one. */
104     i = shader_refcount+1;
105     hret = IDirect3DDevice9_GetVertexShader(device_ptr, &current_shader_ptr);
106     shader_refcount = get_refcount((IUnknown *)shader_ptr);
107     ok(hret == D3D_OK && shader_refcount == i && current_shader_ptr == shader_ptr, 
108         "GetVertexShader returned: hret 0x%x, current_shader_ptr %p refcount %d. "
109         "Expected hret 0x%x, current_shader_ptr %p, refcount %d.\n", hret, current_shader_ptr, shader_refcount, D3D_OK, shader_ptr, i);
110 }
111
112 static void test_get_set_pixel_shader(IDirect3DDevice9 *device_ptr)
113 {
114     static DWORD simple_ps[] = {0xFFFF0101,                                     /* ps_1_1                       */
115         0x00000051, 0xA00F0001, 0x3F800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0  */
116         0x00000042, 0xB00F0000,                                                 /* tex t0                       */
117         0x00000008, 0x800F0000, 0xA0E40001, 0xA0E40000,                         /* dp3 r0, c1, c0               */
118         0x00000005, 0x800F0000, 0x90E40000, 0x80E40000,                         /* mul r0, v0, r0               */
119         0x00000005, 0x800F0000, 0xB0E40000, 0x80E40000,                         /* mul r0, t0, r0               */
120         0x0000FFFF};                                                            /* END                          */
121
122     IDirect3DPixelShader9 *shader_ptr = 0;
123     IDirect3DPixelShader9 *current_shader_ptr = 0;
124     HRESULT hret = 0;
125     int shader_refcount = 0;
126     int i = 0;
127
128     hret = IDirect3DDevice9_CreatePixelShader(device_ptr, simple_ps, &shader_ptr);
129     ok(hret == D3D_OK && shader_ptr != NULL, "CreatePixelShader returned: hret 0x%x, shader_ptr %p. "
130         "Expected hret 0x%x, shader_ptr != %p. Aborting.\n", hret, shader_ptr, D3D_OK, NULL);
131     if (hret != D3D_OK || shader_ptr == NULL) return;
132
133     /* SetPixelsShader should not touch the shader's refcount. */
134     i = get_refcount((IUnknown *)shader_ptr);
135     hret = IDirect3DDevice9_SetPixelShader(device_ptr, shader_ptr);
136     shader_refcount = get_refcount((IUnknown *)shader_ptr);
137     ok(hret == D3D_OK && shader_refcount == i, "SetPixelShader returned: hret 0x%x, refcount %d. "
138         "Expected hret 0x%x, refcount %d.\n", hret, shader_refcount, D3D_OK, i);
139
140     /* GetPixelShader should increase the shader's refcount by one. */
141     i = shader_refcount+1;
142     hret = IDirect3DDevice9_GetPixelShader(device_ptr, &current_shader_ptr);
143     shader_refcount = get_refcount((IUnknown *)shader_ptr);
144     ok(hret == D3D_OK && shader_refcount == i && current_shader_ptr == shader_ptr, 
145         "GetPixelShader returned: hret 0x%x, current_shader_ptr %p refcount %d. "
146         "Expected hret 0x%x, current_shader_ptr %p, refcount %d.\n", hret, current_shader_ptr, shader_refcount, D3D_OK, shader_ptr, i);
147 }
148
149 START_TEST(shader)
150 {
151     D3DCAPS9 caps;
152     IDirect3DDevice9 *device_ptr;
153
154     d3d9_handle = LoadLibraryA("d3d9.dll");
155     if (!d3d9_handle)
156     {
157         trace("Could not load d3d9.dll, skipping tests\n");
158         return;
159     }
160
161     device_ptr = init_d3d9();
162     if (!device_ptr) return;
163
164     IDirect3DDevice9_GetDeviceCaps(device_ptr, &caps);
165
166     if (caps.VertexShaderVersion & 0xffff)
167     {
168         test_get_set_vertex_shader(device_ptr);
169     }
170     else trace("No vertex shader support, skipping test\n");
171
172     if (caps.PixelShaderVersion & 0xffff)
173     {
174         test_get_set_pixel_shader(device_ptr);
175     }
176     else trace("No pixel shader support, skipping test\n");
177 }