Release 1.5.29.
[wine] / dlls / d3dcompiler_43 / tests / hlsl.c
1 /*
2  * Copyright (C) 2010 Travis Athougies
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 #define COBJMACROS
19 #include "wine/test.h"
20 #include "d3dx9.h"
21 #include "d3dcompiler.h"
22
23 #include <math.h>
24
25 struct vertex
26 {
27     float x, y, z;
28     float tx, ty;
29 };
30
31 /* Tells compute_shader_probe* which pixels should be what colors */
32 struct hlsl_probe_info
33 {
34     unsigned int x, y;
35     /* The expected values in this region */
36     D3DXCOLOR c;
37     /* The max error for any value */
38     float epsilon;
39     /* An error message to print if this test fails */
40     const char *message;
41 };
42
43 static HWND create_window(void)
44 {
45     WNDCLASS wc = {0};
46     wc.lpfnWndProc = DefWindowProc;
47     wc.lpszClassName = "d3d9_test_wc";
48     RegisterClass(&wc);
49
50     return CreateWindow("d3d9_test_wc", "d3d9_test",
51             0, 0, 0, 0, 0, 0, 0, 0, 0);
52 }
53
54 static IDirect3DDevice9 *init_d3d9(IDirect3DVertexDeclaration9 **vdeclaration,
55         IDirect3DVertexBuffer9 **quad_geometry, IDirect3DVertexShader9 **vshader_passthru)
56 {
57     static const struct vertex quad_vertices[4] =
58     {
59         {-1.0f, -1.0f, 0.0f, 0.0f, 1.0f},
60         {-1.0f,  1.0f, 0.0f, 0.0f, 0.0f},
61         { 1.0f, -1.0f, 0.0f, 1.0f, 1.0f},
62         { 1.0f,  1.0f, 0.0f, 1.0f, 0.0f}
63     };
64
65     static const D3DVERTEXELEMENT9 vdeclelements[] =
66     {
67         {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
68         {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
69         D3DDECL_END()
70     };
71
72     static const char *vshader_passthru_hlsl =
73         "float4 vshader(float4 pos: POSITION, inout float2 texcoord: TEXCOORD0): POSITION\n"
74         "{\n"
75         "   return pos;\n"
76         "}";
77
78     IDirect3D9 *d3d9_ptr;
79     IDirect3DDevice9 *device_ptr = NULL;
80     D3DPRESENT_PARAMETERS present_parameters;
81
82     void *temp_geometry_vertices;
83
84     ID3D10Blob *compiled = NULL;
85     ID3D10Blob *errors = NULL;
86
87     HRESULT hr;
88
89     d3d9_ptr = Direct3DCreate9(D3D_SDK_VERSION);
90     if (!d3d9_ptr)
91     {
92         skip("could not create D3D9\n");
93         return NULL;
94     }
95
96     hr = IDirect3D9_CheckDeviceFormat(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8,
97             0, D3DRTYPE_SURFACE, D3DFMT_A32B32G32R32F);
98     if (FAILED(hr))
99     {
100         skip("A32B32G32R32F format not available on this device\n");
101         return NULL;
102     }
103
104     ZeroMemory(&present_parameters, sizeof(present_parameters));
105     present_parameters.Windowed = TRUE;
106     present_parameters.hDeviceWindow = create_window();
107     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
108
109     hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL,
110             D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
111     if (FAILED(hr))
112     {
113         skip("could not create Direct3D9 device\n");
114         return NULL;
115     }
116
117     /* Create the quad geometry */
118     hr = IDirect3DDevice9_CreateVertexBuffer(device_ptr, 4 * sizeof(struct vertex),
119             D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, quad_geometry, NULL);
120     ok(SUCCEEDED(hr),
121             "Could not create vertex buffer, IDirect3DDevice9_CreateVertexBuffer returned: %08x\n", hr);
122
123     hr = IDirect3DVertexBuffer9_Lock(*quad_geometry, 0, sizeof(quad_vertices), &temp_geometry_vertices, 0);
124     ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Lock returned: %08x\n", hr);
125     memcpy(temp_geometry_vertices, quad_vertices, sizeof(quad_vertices));
126     IDirect3DVertexBuffer9_Unlock(*quad_geometry);
127
128     hr = IDirect3DDevice9_CreateVertexDeclaration(device_ptr, vdeclelements, vdeclaration);
129     ok(SUCCEEDED(hr), "Could not create vertex declaration: "
130             "IDirect3DDevice9_CreateVertexDeclaration returned: %08x\n", hr);
131
132     hr = IDirect3DDevice9_SetVertexDeclaration(device_ptr, *vdeclaration);
133     ok(hr == D3D_OK, "IDirect3DDevice9_SetVertexDeclaration returned: %08x\n", hr);
134
135     /* Create a simple vertex shader to just pass through the values */
136     hr = D3DCompile(vshader_passthru_hlsl, strlen(vshader_passthru_hlsl), NULL,
137             NULL, NULL, "vshader", "vs_1_1", 0, 0, &compiled, &errors);
138     if (FAILED(hr))
139     {
140         skip("not compiling vertex shader due to lacking wine HLSL support!\n");
141         if (errors)
142             ID3D10Blob_Release(errors);
143         return NULL;
144     }
145
146     hr = IDirect3DDevice9_CreateVertexShader(device_ptr, ID3D10Blob_GetBufferPointer(compiled),
147             vshader_passthru);
148     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexShader returned: %08x\n", hr);
149     ID3D10Blob_Release(compiled);
150
151     return device_ptr;
152 }
153
154 /* Convenience functions */
155 static void set_float4_d3d9(IDirect3DDevice9 *device, ID3DXConstantTable *constants, const char *name,
156         float x, float y, float z, float w)
157 {
158     D3DXVECTOR4 vector;
159     vector.x = x;
160     vector.y = y;
161     vector.z = z;
162     vector.w = w;
163     ID3DXConstantTable_SetVector(constants, device, name, &vector);
164 }
165
166 /* Compile our pixel shader and get back the compiled version and a constant table */
167 static IDirect3DPixelShader9 *compile_pixel_shader9(IDirect3DDevice9 *device, const char *shader,
168         const char *profile, ID3DXConstantTable **constants)
169 {
170     ID3D10Blob *compiled = NULL;
171     ID3D10Blob *errors = NULL;
172     IDirect3DPixelShader9 *pshader;
173     HRESULT hr;
174
175     hr = D3DCompile(shader, strlen(shader), NULL, NULL,
176             NULL, "test", profile, /* test is the name of the entry point of our shader */
177             0, 0, &compiled, &errors);
178     ok(hr == D3D_OK, "Pixel shader %s compilation failed: %s\n", shader,
179             errors ? (char *)ID3D10Blob_GetBufferPointer(errors) : "");
180     if (FAILED(hr)) return NULL;
181
182     hr = D3DXGetShaderConstantTable(ID3D10Blob_GetBufferPointer(compiled), constants);
183     ok(hr == D3D_OK, "Could not get constant table from compiled pixel shader\n");
184
185     hr = IDirect3DDevice9_CreatePixelShader(device, ID3D10Blob_GetBufferPointer(compiled), &pshader);
186     ok(SUCCEEDED(hr), "IDirect3DDevice9_CreatePixelShader returned: %08x\n", hr);
187     ID3D10Blob_Release(compiled);
188     return pshader;
189 }
190
191 /* Draw a full screen quad */
192 static void draw_quad_with_shader9(IDirect3DDevice9 *device, IDirect3DVertexBuffer9 *quad_geometry)
193 {
194     HRESULT hr;
195     D3DXMATRIX projection_matrix;
196
197     D3DXMatrixOrthoLH(&projection_matrix, 2.0f, 2.0f, 0.0f, 1.0f);
198     IDirect3DDevice9_SetTransform(device, D3DTS_PROJECTION, &projection_matrix);
199
200     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
201     ok(hr == D3D_OK, "IDirect3DDevice9_Clear returned: %08x\n", hr);
202
203     hr = IDirect3DDevice9_BeginScene(device);
204     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene returned: %08x\n", hr);
205
206     hr = IDirect3DDevice9_SetStreamSource(device, 0, quad_geometry, 0, sizeof(struct vertex));
207     ok(hr == D3D_OK, "IDirect3DDevice9_SetStreamSource returned: %08x\n", hr);
208     hr = IDirect3DDevice9_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, 0, 2);
209     ok(hr == D3D_OK, "IDirect3DDevice9_DrawPrimitive returned: %08x\n", hr);
210
211     hr = IDirect3DDevice9_EndScene(device);
212     ok(hr == D3D_OK, "IDirect3DDevice9_EndScene returned: %08x\n", hr);
213 }
214
215 static void setup_device9(IDirect3DDevice9 *device, IDirect3DSurface9 **render_target,
216         IDirect3DSurface9 **readback, D3DFORMAT format, unsigned int width, unsigned int height,
217         IDirect3DVertexShader9 *vshader, IDirect3DPixelShader9 *pshader)
218 {
219     HRESULT hr;
220     hr = IDirect3DDevice9_CreateRenderTarget(device, width, height, format,
221             D3DMULTISAMPLE_NONE, 0, FALSE, render_target, NULL);
222     ok(hr == D3D_OK, "IDirect3DDevice9_CreateRenderTarget returned: %08x\n", hr);
223
224     /* The Direct3D 9 docs state that we cannot lock a render target surface,
225        instead we must copy the render target onto this surface to lock it */
226     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, width, height, format,
227             D3DPOOL_SYSTEMMEM, readback, NULL);
228     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned: %08x\n", hr);
229
230     hr = IDirect3DDevice9_SetRenderTarget(device, 0, *render_target);
231     ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget returned: %08x\n", hr);
232
233     hr = IDirect3DDevice9_SetVertexShader(device, vshader);
234     ok(hr == D3D_OK, "IDirect3DDevice9_SetVertexShader returned: %08x\n", hr);
235     hr = IDirect3DDevice9_SetPixelShader(device, pshader);
236     ok(hr == D3D_OK, "IDirect3DDevice9_SetPixelShader returned: %08x\n", hr);
237 }
238
239 static int colors_match(D3DXCOLOR a, D3DXCOLOR b, float epsilon)
240 {
241   return (fabs(a.r - b.r) < epsilon && fabs(a.g - b.g) < epsilon && fabs(a.b - b.b) < epsilon &&
242           fabs(a.a - b.a) < epsilon);
243 }
244
245 /* Compute a shader on a width by height buffer and probes certain locations
246    to see if they are as expected. */
247 static void compute_shader_probe9(IDirect3DDevice9 *device, IDirect3DVertexShader9 *vshader,
248         IDirect3DPixelShader9 *pshader, IDirect3DVertexBuffer9 *quad_geometry,
249         const struct hlsl_probe_info *probes, unsigned int count,
250         unsigned int width, unsigned int height, unsigned int line_number)
251 {
252     IDirect3DSurface9 *render_target;
253     IDirect3DSurface9 *readback;
254
255     HRESULT hr;
256     D3DLOCKED_RECT lr;
257     D3DXCOLOR *pbits_data;
258     unsigned int i;
259
260     setup_device9(device, &render_target, &readback, D3DFMT_A32B32G32R32F,
261             width, height, vshader, pshader);
262
263     /* Draw the quad with the shader and read back the data */
264     draw_quad_with_shader9(device, quad_geometry);
265     IDirect3DDevice9_GetRenderTargetData(device, render_target, readback);
266     hr = IDirect3DSurface9_LockRect(readback, &lr, NULL, D3DLOCK_READONLY);
267     ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned: %08x\n", hr);
268     pbits_data = lr.pBits;
269
270     /* Now go through the probes and check each one */
271     for (i = 0; i < count; i++, probes++) {
272         int index = probes->x + (probes->y * lr.Pitch / sizeof(D3DXCOLOR));
273         ok(colors_match(probes->c, pbits_data[index], probes->epsilon),
274                 "Line %d: At (%d, %d): %s: Expected (%.04f,%.04f,%.04f, %.04f), got "
275                 "(%.04f,%.04f,%.04f,%.04f)\n", line_number, probes->x, probes->y, probes->message,
276                 probes->c.r, probes->c.g, probes->c.b, probes->c.a, pbits_data[index].r,
277                 pbits_data[index].g, pbits_data[index].b, pbits_data[index].a);
278     }
279
280     hr = IDirect3DSurface9_UnlockRect(readback);
281     ok(hr == D3D_OK, "IDirect3DSurface9_UnlockRect returned: %08x\n", hr);
282
283     /* We now present the scene. This is mostly for debugging purposes, since GetRenderTargetData
284        also waits for drawing commands to complete. The reason this call is here and not in a
285        draw function is because the contents of the render target surface are invalidated after
286        this call. */
287     hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
288     ok(hr == D3D_OK, "IDirect3DDevice9_Present returned: %08x\n", hr);
289
290     IDirect3DSurface9_Release(render_target);
291     IDirect3DSurface9_Release(readback);
292 }
293
294 /* Now the actual test functions */
295 static void test_swizzle(IDirect3DDevice9 *device, IDirect3DVertexBuffer9 *quad_geometry,
296         IDirect3DVertexShader9 *vshader_passthru)
297 {
298     static const struct hlsl_probe_info probes[] =
299     {
300        {0, 0, {0.0101f, 0.0303f, 0.0202f, 0.0404f}, 0.0001f, "swizzle_test failed"}
301     };
302
303     static const char *swizzle_test_shader =
304         "uniform float4 color;\n"
305         "float4 test(): COLOR\n"
306         "{\n"
307         "    float4 ret = color;\n"
308         "    ret.gb = ret.ra;\n"
309         "    ret.ra = float2(0.0101, 0.0404);\n"
310         "    return ret;\n"
311         "}";
312
313     ID3DXConstantTable *constants;
314     IDirect3DPixelShader9 *pshader;
315
316     pshader = compile_pixel_shader9(device, swizzle_test_shader, "ps_2_0", &constants);
317     if (pshader != NULL)
318     {
319         set_float4_d3d9(device, constants, "color", 0.0303f, 0.0f, 0.0f, 0.0202f);
320
321         compute_shader_probe9(device, vshader_passthru, pshader, quad_geometry,
322                 probes, sizeof(probes) / sizeof(*probes), 1, 1, __LINE__);
323
324         ID3DXConstantTable_Release(constants);
325         IDirect3DPixelShader9_Release(pshader);
326     }
327 }
328
329 static void test_math(IDirect3DDevice9 *device, IDirect3DVertexBuffer9 *quad_geometry,
330         IDirect3DVertexShader9 *vshader_passthru)
331 {
332     /* Tests order of operations */
333     static const float u = 2.5f, v = 0.3f, w = 0.2f, x = 0.7f, y = 0.1f, z = 1.5f;
334
335     static const struct hlsl_probe_info probes[] =
336     {
337         {0, 0, {-12.4300f, 9.8333f, 1.6000f, 34.9999f}, 0.0001f,
338                 "order of operations test failed"}
339     };
340
341     static const char *order_of_operations_shader =
342         "float4 test(uniform float u, uniform float v, uniform float w, uniform float x,\n"
343         "            uniform float y, uniform float z): COLOR\n"
344         "{\n"
345         "    return float4(x * y - z / w + --u / -v,\n"
346         "            z * x / y + w / -v,\n"
347         "            u + v - w,\n"
348         "            x / y / w);\n"
349         "}";
350
351     ID3DXConstantTable *constants;
352     IDirect3DPixelShader9 *pshader;
353
354     pshader = compile_pixel_shader9(device, order_of_operations_shader, "ps_2_0", &constants);
355     if (pshader != NULL)
356     {
357         ID3DXConstantTable_SetFloat(constants, device, "$u", u);
358         ID3DXConstantTable_SetFloat(constants, device, "$v", v);
359         ID3DXConstantTable_SetFloat(constants, device, "$w", w);
360         ID3DXConstantTable_SetFloat(constants, device, "$x", x);
361         ID3DXConstantTable_SetFloat(constants, device, "$y", y);
362         ID3DXConstantTable_SetFloat(constants, device, "$z", z);
363
364         compute_shader_probe9(device, vshader_passthru, pshader, quad_geometry,
365                 probes, sizeof(probes) / sizeof(*probes), 1, 1, __LINE__);
366
367         ID3DXConstantTable_Release(constants);
368         IDirect3DPixelShader9_Release(pshader);
369     }
370 }
371
372 static void test_conditionals(IDirect3DDevice9 *device, IDirect3DVertexBuffer9 *quad_geometry,
373         IDirect3DVertexShader9 *vshader_passthru)
374 {
375     static const struct hlsl_probe_info if_greater_probes[] =
376     {
377         { 0, 0, {0.9f, 0.8f, 0.7f, 0.6f}, 0.0001f, "if greater test failed"},
378         { 5, 0, {0.9f, 0.8f, 0.7f, 0.6f}, 0.0001f, "if greater test failed"},
379         {10, 0, {0.9f, 0.8f, 0.7f, 0.6f}, 0.0001f, "if greater test failed"},
380         {15, 0, {0.9f, 0.8f, 0.7f, 0.6f}, 0.0001f, "if greater test failed"},
381         {25, 0, {0.1f, 0.2f, 0.3f, 0.4f}, 0.0001f, "if greater test failed"},
382         {30, 0, {0.1f, 0.2f, 0.3f, 0.4f}, 0.0001f, "if greater test failed"}
383     };
384
385     static const char *if_greater_shader =
386         "float4 test(float2 pos: TEXCOORD0): COLOR\n"
387         "{\n"
388         "    if((pos.x * 32.0) > 20.0)\n"
389         "        return float4(0.1, 0.2, 0.3, 0.4);\n"
390         "    else\n"
391         "        return float4(0.9, 0.8, 0.7, 0.6);\n"
392         "}";
393
394     static const struct hlsl_probe_info ternary_operator_probes[] =
395     {
396         {0, 0, {0.50f, 0.25f, 0.50f, 0.75f}, 0.00001f, "ternary operator test failed"},
397         {1, 0, {0.50f, 0.25f, 0.50f, 0.75f}, 0.00001f, "ternary operator test failed"},
398         {2, 0, {0.50f, 0.25f, 0.50f, 0.75f}, 0.00001f, "ternary operator test failed"},
399         {3, 0, {0.50f, 0.25f, 0.50f, 0.75f}, 0.00001f, "ternary operator test failed"},
400         {4, 0, {0.60f, 0.80f, 0.10f, 0.20f}, 0.00001f, "ternary operator test failed"},
401         {5, 0, {0.60f, 0.80f, 0.10f, 0.20f}, 0.00001f, "ternary operator test failed"},
402         {6, 0, {0.60f, 0.80f, 0.10f, 0.20f}, 0.00001f, "ternary operator test failed"},
403         {7, 0, {0.60f, 0.80f, 0.10f, 0.20f}, 0.00001f, "ternary operator test failed"}
404     };
405
406     static const char *ternary_operator_shader =
407         "float4 test(float2 pos: TEXCOORD0): COLOR\n"
408         "{\n"
409         "    return (pos.x < 0.5?float4(0.5, 0.25, 0.5, 0.75):float4(0.6, 0.8, 0.1, 0.2));\n"
410         "}";
411
412     ID3DXConstantTable *constants;
413     IDirect3DPixelShader9 *pshader;
414
415     pshader = compile_pixel_shader9(device, if_greater_shader, "ps_2_0", &constants);
416     if (pshader != NULL)
417     {
418         compute_shader_probe9(device, vshader_passthru, pshader, quad_geometry, if_greater_probes,
419                 sizeof(if_greater_probes) / sizeof(*if_greater_probes), 32, 1, __LINE__);
420
421         ID3DXConstantTable_Release(constants);
422         IDirect3DPixelShader9_Release(pshader);
423     }
424
425     pshader = compile_pixel_shader9(device, ternary_operator_shader, "ps_2_0", &constants);
426     if (pshader != NULL)
427     {
428         compute_shader_probe9(device, vshader_passthru, pshader, quad_geometry, ternary_operator_probes,
429                 sizeof(ternary_operator_probes) / sizeof(*ternary_operator_probes), 8, 1, __LINE__);
430
431         ID3DXConstantTable_Release(constants);
432         IDirect3DPixelShader9_Release(pshader);
433     }
434 }
435
436 static void test_float_vectors(IDirect3DDevice9 *device, IDirect3DVertexBuffer9 *quad_geometry,
437         IDirect3DVertexShader9 *vshader_passthru)
438 {
439     static const struct hlsl_probe_info vec4_indexing_test1_probes[] =
440     {
441         {0, 0, {0.020f, 0.245f, 0.351f, 1.000f}, 0.0001f, "vec4 indexing test 1 failed"}
442     };
443
444     static const char *vec4_indexing_test1_shader =
445         "float4 test(): COLOR\n"
446         "{\n"
447         "    float4 color;\n"
448         "    color[0] = 0.020;\n"
449         "    color[1] = 0.245;\n"
450         "    color[2] = 0.351;\n"
451         "    color[3] = 1.0;\n"
452         "    return color;\n"
453         "}";
454
455     static const struct hlsl_probe_info vec4_indexing_test2_probes[] =
456     {
457         {0, 0, {0.5f, 0.3f, 0.8f, 0.2f}, 0.0001f, "vec4 indexing test 2 failed"}
458     };
459
460     /* We have this uniform i here so the compiler can't optimize */
461     static const char *vec4_indexing_test2_shader =
462         "uniform int i;\n"
463         "float4 test(): COLOR\n"
464         "{\n"
465         "    float4 color = float4(0.5, 0.4, 0.3, 0.2);\n"
466         "    color.g = color[i];\n"
467         "    color.b = 0.8;\n"
468         "    return color;\n"
469         "}";
470
471     ID3DXConstantTable *constants;
472     IDirect3DPixelShader9 *pshader;
473
474     pshader = compile_pixel_shader9(device, vec4_indexing_test1_shader, "ps_2_0", &constants);
475     if (pshader != NULL)
476     {
477         compute_shader_probe9(device, vshader_passthru, pshader, quad_geometry, vec4_indexing_test1_probes,
478                 sizeof(vec4_indexing_test1_probes) / sizeof(*vec4_indexing_test1_probes), 1, 1, __LINE__);
479
480         ID3DXConstantTable_Release(constants);
481         IDirect3DPixelShader9_Release(pshader);
482     }
483
484     pshader = compile_pixel_shader9(device, vec4_indexing_test2_shader, "ps_2_0", &constants);
485     if (pshader != NULL)
486     {
487         ID3DXConstantTable_SetInt(constants, device, "i", 2);
488
489         compute_shader_probe9(device, vshader_passthru, pshader, quad_geometry, vec4_indexing_test2_probes,
490                 sizeof(vec4_indexing_test2_probes) / sizeof(*vec4_indexing_test2_probes), 32, 1, __LINE__);
491
492         ID3DXConstantTable_Release(constants);
493         IDirect3DPixelShader9_Release(pshader);
494     }
495 }
496
497 static void test_trig(IDirect3DDevice9 *device, IDirect3DVertexBuffer9 *quad_geometry,
498         IDirect3DVertexShader9 *vshader_passthru)
499 {
500     static const struct hlsl_probe_info sincos_probes[] =
501     {
502         {0, 0, {0.5000f, 1.0000f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
503         {1, 0, {0.5975f, 0.9904f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
504         {2, 0, {0.6913f, 0.9620f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
505         {3, 0, {0.7778f, 0.9160f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
506         {4, 0, {0.8536f, 0.8536f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
507         {5, 0, {0.9157f, 0.7778f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
508         {6, 0, {0.9620f, 0.6913f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
509         {7, 0, {0.9904f, 0.5975f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
510         {8, 0, {1.0000f, 0.5000f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
511         {9, 0, {0.9904f, 0.4025f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
512         {10, 0, {0.9619f, 0.3087f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
513         {11, 0, {0.9157f, 0.2222f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
514         {12, 0, {0.8536f, 0.1464f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
515         {13, 0, {0.7778f, 0.0843f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
516         {14, 0, {0.6913f, 0.0381f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
517         {15, 0, {0.5975f, 0.0096f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
518         {16, 0, {0.5000f, 0.0000f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
519         {17, 0, {0.4025f, 0.0096f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
520         {18, 0, {0.3087f, 0.0381f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
521         {19, 0, {0.2222f, 0.0843f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
522         {20, 0, {0.1464f, 0.1464f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
523         {21, 0, {0.0843f, 0.2222f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
524         {22, 0, {0.0381f, 0.3087f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
525         {23, 0, {0.0096f, 0.4025f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
526         {24, 0, {0.0000f, 0.5000f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
527         {25, 0, {0.0096f, 0.5975f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
528         {26, 0, {0.0381f, 0.6913f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
529         {27, 0, {0.0843f, 0.7778f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
530         {28, 0, {0.1464f, 0.8536f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
531         {29, 0, {0.2222f, 0.9157f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
532         {30, 0, {0.3087f, 0.9619f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
533         {31, 0, {0.4025f, 0.9904f, 0.0f, 0.0f}, 0.001f, "sin/cos test failed"},
534     };
535
536     static const char *sincos_shader =
537         "float4 test(float x: TEXCOORD0): COLOR\n"
538         "{\n"
539         "    const float pi2 = 6.2831853;\n"
540         "    float calcd_sin = (sin(x * pi2) + 1)/2;\n"
541         "    float calcd_cos = (cos(x * pi2) + 1)/2;\n"
542         "    return float4(calcd_sin, calcd_cos, 0, 0);\n"
543         "}";
544
545     ID3DXConstantTable *constants;
546     IDirect3DPixelShader9 *pshader;
547
548     pshader = compile_pixel_shader9(device, sincos_shader, "ps_2_0", &constants);
549     if (pshader != NULL)
550     {
551         compute_shader_probe9(device, vshader_passthru, pshader, quad_geometry, sincos_probes,
552                 sizeof(sincos_probes) / sizeof(*sincos_probes), 32, 1, __LINE__);
553
554         ID3DXConstantTable_Release(constants);
555         IDirect3DPixelShader9_Release(pshader);
556     }
557 }
558
559 static void test_fail(IDirect3DDevice9 *device, IDirect3DVertexBuffer9 *qquad_geometry,
560         IDirect3DVertexShader9 *vshader_passthru)
561 {
562     static const char *undefined_variable_shader =
563         "float4 test(float2 pos: TEXCOORD0) : COLOR\n"
564         "{\n"
565         "   return y;\n"
566         "}";
567
568     static const char *invalid_swizzle_shader =
569         "float4 test(float2 pos: TEXCOORD0) : COLOR\n"
570         "{\n"
571         "  float4 x = float4(0, 0, 0, 0);\n"
572         "  x.xzzx = float4(1, 2, 3, 4);\n"
573         "  return x;\n"
574         "}";
575
576     static const char *invalid_conversion_shader =
577         "float4 test(float2 pos: TEXCOORD0) : COLOR\n"
578         "{\n"
579         "  float4 x = pos;\n"
580         "  return x;\n"
581         "}";
582
583     static const char *invalid_syntax_shader =
584         "float4 test(float2 pos, TEXCOORD0) ; COLOR\n"
585         "{\n"
586         "  pos = float4 x;\n"
587         "  mul(float4(5, 4, 3, 2), mvp) = x;\n"
588         "  return float4;\n"
589         "}";
590
591     static const char *invalid_identifiers_shader =
592         "float4 563r(float2 45s: TEXCOORD0) : COLOR\n"
593         "{\n"
594         "  float2 x = 45s;\n"
595         "  return float4(x.x, x.y, 0, 0);\n"
596         "}";
597
598     ID3D10Blob *compiled = NULL, *errors = NULL;
599     HRESULT hr;
600
601     hr = D3DCompile(undefined_variable_shader, strlen(undefined_variable_shader), NULL, NULL, NULL,
602             "test", "ps_2_0", 0, 0, &compiled, &errors);
603     ok(hr != D3D_OK, "Pixel shader compilation succeeded on shader with undefined variable\n");
604     ok(errors != NULL, "No errors returned for a shader with undefined variables\n");
605     ok(compiled == NULL, "A shader blob was returned for a shader with undefined variables\n");
606
607     ID3D10Blob_Release(errors);
608     errors = NULL;
609
610     hr = D3DCompile(invalid_swizzle_shader, strlen(invalid_swizzle_shader), NULL, NULL, NULL,
611             "test","ps_2_0", 0, 0, &compiled, &errors);
612     ok(hr != D3D_OK, "Pixel shader compilation succeeded on shader with an invalid swizzle mask\n");
613     ok(errors != NULL, "No errors returned for a shader with an invalid swizzle mask\n");
614     ok(compiled == NULL, "A shader blob was returned for a shader with an invalid swizzle mask\n");
615
616     ID3D10Blob_Release(errors);
617     errors = NULL;
618
619     hr = D3DCompile(invalid_conversion_shader, strlen(invalid_conversion_shader), NULL, NULL, NULL,
620              "test", "ps_2_0", 0, 0, &compiled, &errors);
621     ok(hr != D3D_OK, "Pixel shader compilation succeeded on shader with an invalid type "
622             "conversion\n");
623     ok(errors != NULL, "No errors returned for a shader with invalid type conversions\n");
624     ok(compiled == NULL, "A shader blob was returned for a shader with invalid type conversions\n");
625
626     ID3D10Blob_Release(errors);
627     errors = NULL;
628
629     hr = D3DCompile(invalid_syntax_shader, strlen(invalid_syntax_shader), NULL, NULL, NULL, "test",
630             "ps_2_0", 0, 0, &compiled, &errors);
631     ok(hr != D3D_OK, "Pixel shader compilation succeeded on shader with blatantly invalid "
632             "syntax\n");
633     ok(errors != NULL, "No errors returned for a shader with invalid syntax\n");
634     ok(compiled == NULL, "A shader blob was returned for a shader with invalid syntax\n");
635
636     ID3D10Blob_Release(errors);
637     errors = NULL;
638
639     hr = D3DCompile(invalid_identifiers_shader, strlen(invalid_identifiers_shader), NULL, NULL,
640             NULL, "test", "ps_2_0", 0, 0, &compiled, &errors);
641     ok(hr != D3D_OK, "Pixel shader compilation successful on a shader with invalid variable and "
642             "function names\n");
643     ok(errors != NULL, "No errors returned for a shader with invalid variable and function "
644             "names\n");
645     ok(compiled == NULL, "A shader blob was returned for a shader with invalid variable and "
646             "function names\n");
647
648     ID3D10Blob_Release(errors);
649 }
650
651 START_TEST(hlsl)
652 {
653     D3DCAPS9 caps;
654     ULONG refcount;
655     IDirect3DDevice9 *device;
656     IDirect3DVertexDeclaration9 *vdeclaration;
657     IDirect3DVertexBuffer9 *quad_geometry;
658     IDirect3DVertexShader9 *vshader_passthru;
659
660     device = init_d3d9(&vdeclaration, &quad_geometry, &vshader_passthru);
661     if (!device) return;
662
663     /* Make sure we support pixel shaders, before trying to compile them! */
664     /* Direct3D 9 (Shader model 1-3 tests) */
665     IDirect3DDevice9_GetDeviceCaps(device, &caps);
666     if (caps.PixelShaderVersion >= D3DPS_VERSION(2, 0))
667     {
668         todo_wine
669         {
670             test_swizzle(device, quad_geometry, vshader_passthru);
671             test_math(device, quad_geometry, vshader_passthru);
672             test_conditionals(device, quad_geometry, vshader_passthru);
673             test_float_vectors(device, quad_geometry, vshader_passthru);
674             test_trig(device, quad_geometry, vshader_passthru);
675             test_fail(device, quad_geometry, vshader_passthru);
676         }
677     } else skip("no pixel shader support\n");
678
679     /* Reference counting sanity checks */
680     if (vshader_passthru)
681     {
682         refcount = IDirect3DVertexShader9_Release(vshader_passthru);
683         ok(!refcount, "Pass-through vertex shader has %u references left\n", refcount);
684     }
685
686     refcount = IDirect3DVertexBuffer9_Release(quad_geometry);
687     ok(!refcount, "Vertex buffer has %u references left\n", refcount);
688
689     refcount = IDirect3DVertexDeclaration9_Release(vdeclaration);
690     ok(!refcount, "Vertex declaration has %u references left\n", refcount);
691
692     refcount = IDirect3DDevice9_Release(device);
693     ok(!refcount, "Device has %u references left\n", refcount);
694 }