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