wined3d: Implement more GLSL instructions and a little cleanup.
[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     ok(hres == D3D_OK, "IDirect3D_CreateDevice returned: 0x%lx\n", hres);
59
60     return device_ptr;
61 }
62
63 static int get_refcount(IUnknown *object)
64 {
65     IUnknown_AddRef(object);
66     return IUnknown_Release(object);
67 }
68
69 static void test_get_set_vertex_shader(IDirect3DDevice9 *device_ptr)
70 {
71
72     static DWORD simple_vs[] = {0xFFFE0101,             /* vs_1_1               */
73         0x0000001F, 0x80000000, 0x900F0000,             /* dcl_position0 v0     */
74         0x00000009, 0xC0010000, 0x90E40000, 0xA0E40000, /* dp4 oPos.x, v0, c0   */
75         0x00000009, 0xC0020000, 0x90E40000, 0xA0E40001, /* dp4 oPos.y, v0, c1   */
76         0x00000009, 0xC0040000, 0x90E40000, 0xA0E40002, /* dp4 oPos.z, v0, c2   */
77         0x00000009, 0xC0080000, 0x90E40000, 0xA0E40003, /* dp4 oPos.w, v0, c3   */
78         0x0000FFFF};                                    /* END                  */
79
80     IDirect3DVertexShader9 *shader_ptr = 0;
81     IDirect3DVertexShader9 *current_shader_ptr = 0;
82     HRESULT hret = 0;
83     int shader_refcount = 0;
84     int i = 0;
85     
86     hret = IDirect3DDevice9_CreateVertexShader(device_ptr, simple_vs, &shader_ptr);
87     ok(hret == D3D_OK && shader_ptr != NULL, "CreateVertexShader returned: hret 0x%lx, shader_ptr %p. "
88         "Expected hret 0x%lx, shader_ptr != %p. Aborting.\n", hret, shader_ptr, D3D_OK, NULL);
89     if (hret != D3D_OK || shader_ptr == NULL) return;
90
91     /* SetVertexShader should not touch the shader's refcount. */
92     i = get_refcount((IUnknown *)shader_ptr);
93     hret = IDirect3DDevice9_SetVertexShader(device_ptr, shader_ptr);
94     shader_refcount = get_refcount((IUnknown *)shader_ptr);
95     ok(hret == D3D_OK && shader_refcount == i, "SetVertexShader returned: hret 0x%lx, refcount %d. "
96         "Expected hret 0x%lx, refcount %d.\n", hret, shader_refcount, D3D_OK, i);
97
98     /* GetVertexShader should increase the shader's refcount by one. */
99     i = shader_refcount+1;
100     hret = IDirect3DDevice9_GetVertexShader(device_ptr, &current_shader_ptr);
101     shader_refcount = get_refcount((IUnknown *)shader_ptr);
102     ok(hret == D3D_OK && shader_refcount == i && current_shader_ptr == shader_ptr, 
103         "GetVertexShader returned: hret 0x%lx, current_shader_ptr %p refcount %d. "
104         "Expected hret 0x%lx, current_shader_ptr %p, refcount %d.\n", hret, current_shader_ptr, shader_refcount, D3D_OK, shader_ptr, i);
105 }
106
107 static void test_get_set_vertex_shader_constants(IDirect3DDevice9 *device_ptr)
108 {
109     HRESULT hret;
110     int int_in[4] = {0xdead0000, 0xdead0001, 0xdead0002, 0xdead0003};
111     int int_out[4] = {0};
112     float float_in[4] = {0.14f, 0.15f, 0.92f, 0.65f};
113     float float_out[4] = {0};
114     BOOL bool_in[4] = {TRUE, FALSE, FALSE, TRUE};
115     BOOL bool_out[4] = {0};
116
117     hret = IDirect3DDevice9_SetVertexShaderConstantI(device_ptr, 0, int_in, 1);
118     ok(hret == D3D_OK, "SetVertexShaderConstantI returned %#lx.\n", hret);
119     hret = IDirect3DDevice9_SetVertexShaderConstantF(device_ptr, 0, float_in, 1);
120     ok(hret == D3D_OK, "SetVertexShaderConstantF returned %#lx.\n", hret);
121     hret = IDirect3DDevice9_SetVertexShaderConstantB(device_ptr, 0, bool_in, 4);
122     ok(hret == D3D_OK, "SetVertexShaderConstantB returned %#lx.\n", hret);
123
124     hret = IDirect3DDevice9_GetVertexShaderConstantI(device_ptr, 0, int_out, 1);
125     ok(hret == D3D_OK, "GetVertexShaderConstantI returned %#lx.\n", hret);
126     hret = IDirect3DDevice9_GetVertexShaderConstantF(device_ptr, 0, float_out, 1);
127     ok(hret == D3D_OK, "GetVertexShaderConstantF returned %#lx.\n", hret);
128     hret = IDirect3DDevice9_GetVertexShaderConstantB(device_ptr, 0, bool_out, 4);
129     ok(hret == D3D_OK, "GetVertexShaderConstantB returned %#lx.\n", hret);
130
131     /* The constant arrays aren't shared between I/F/B, so they shouldn't
132      * overwrite eachother's values */
133     ok(!memcmp(int_in, int_out, sizeof(int_in)),
134             "Int constant data doesn't match (%#x, %#x, %#x, %#x).\n",
135             int_out[0], int_out[1], int_out[2], int_out[3]);
136     ok(!memcmp(float_in, float_out, sizeof(float_in)),
137             "Float constant data doesn't match (%f, %f, %f, %f).\n",
138             float_out[0], float_out[1], float_out[2], float_out[3]);
139     ok(!memcmp(bool_in, bool_out, sizeof(bool_in)),
140             "Bool constant data doesn't match (%#x, %#x, %#x, %#x).\n",
141             bool_out[0], bool_out[1], bool_out[2], bool_out[3]);
142 }
143
144 static void test_get_set_pixel_shader(IDirect3DDevice9 *device_ptr)
145 {
146     static DWORD simple_ps[] = {0xFFFF0101,                                     /* ps_1_1                       */
147         0x00000051, 0xA00F0001, 0x3F800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0  */
148         0x00000042, 0xB00F0000,                                                 /* tex t0                       */
149         0x00000008, 0x800F0000, 0xA0E40001, 0xA0E40000,                         /* dp3 r0, c1, c0               */
150         0x00000005, 0x800F0000, 0x90E40000, 0x80E40000,                         /* mul r0, v0, r0               */
151         0x00000005, 0x800F0000, 0xB0E40000, 0x80E40000,                         /* mul r0, t0, r0               */
152         0x0000FFFF};                                                            /* END                          */
153
154     IDirect3DPixelShader9 *shader_ptr = 0;
155     IDirect3DPixelShader9 *current_shader_ptr = 0;
156     HRESULT hret = 0;
157     int shader_refcount = 0;
158     int i = 0;
159
160     hret = IDirect3DDevice9_CreatePixelShader(device_ptr, simple_ps, &shader_ptr);
161     ok(hret == D3D_OK && shader_ptr != NULL, "CreatePixelShader returned: hret 0x%lx, shader_ptr %p. "
162         "Expected hret 0x%lx, shader_ptr != %p. Aborting.\n", hret, shader_ptr, D3D_OK, NULL);
163     if (hret != D3D_OK || shader_ptr == NULL) return;
164
165     /* SetPixelsShader should not touch the shader's refcount. */
166     i = get_refcount((IUnknown *)shader_ptr);
167     hret = IDirect3DDevice9_SetPixelShader(device_ptr, shader_ptr);
168     shader_refcount = get_refcount((IUnknown *)shader_ptr);
169     ok(hret == D3D_OK && shader_refcount == i, "SetPixelShader returned: hret 0x%lx, refcount %d. "
170         "Expected hret 0x%lx, refcount %d.\n", hret, shader_refcount, D3D_OK, i);
171
172     /* GetPixelShader should increase the shader's refcount by one. */
173     i = shader_refcount+1;
174     hret = IDirect3DDevice9_GetPixelShader(device_ptr, &current_shader_ptr);
175     shader_refcount = get_refcount((IUnknown *)shader_ptr);
176     ok(hret == D3D_OK && shader_refcount == i && current_shader_ptr == shader_ptr, 
177         "GetPixelShader returned: hret 0x%lx, current_shader_ptr %p refcount %d. "
178         "Expected hret 0x%lx, current_shader_ptr %p, refcount %d.\n", hret, current_shader_ptr, shader_refcount, D3D_OK, shader_ptr, i);
179 }
180
181 START_TEST(shader)
182 {
183     D3DCAPS9 caps;
184     IDirect3DDevice9 *device_ptr;
185
186     d3d9_handle = LoadLibraryA("d3d9.dll");
187     if (!d3d9_handle)
188     {
189         trace("Could not load d3d9.dll, skipping tests\n");
190         return;
191     }
192
193     device_ptr = init_d3d9();
194     if (!device_ptr) return;
195
196     IDirect3DDevice9_GetDeviceCaps(device_ptr, &caps);
197
198     if (caps.VertexShaderVersion & 0xffff)
199     {
200         test_get_set_vertex_shader(device_ptr);
201         test_get_set_vertex_shader_constants(device_ptr);
202     }
203     else trace("No vertex shader support, skipping test\n");
204
205     if (caps.PixelShaderVersion & 0xffff)
206     {
207         test_get_set_pixel_shader(device_ptr);
208     }
209     else trace("No pixel shader support, skipping test\n");
210 }