d3dx9/tests: Fix a shader test failure.
[wine] / dlls / d3dx9_36 / tests / shader.c
1 /*
2  * Copyright 2008 Luis Busquets
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 #include "wine/test.h"
20 #include "d3dx9.h"
21
22 static const DWORD simple_vs[] = {
23     0xfffe0101,                                                             /* vs_1_1                       */
24     0x0000001f, 0x80000000, 0x900f0000,                                     /* dcl_position0 v0             */
25     0x00000009, 0xc0010000, 0x90e40000, 0xa0e40000,                         /* dp4 oPos.x, v0, c0           */
26     0x00000009, 0xc0020000, 0x90e40000, 0xa0e40001,                         /* dp4 oPos.y, v0, c1           */
27     0x00000009, 0xc0040000, 0x90e40000, 0xa0e40002,                         /* dp4 oPos.z, v0, c2           */
28     0x00000009, 0xc0080000, 0x90e40000, 0xa0e40003,                         /* dp4 oPos.w, v0, c3           */
29     0x0000ffff};                                                            /* END                          */
30
31 static const DWORD simple_ps[] = {
32     0xffff0101,                                                             /* ps_1_1                       */
33     0x00000051, 0xa00f0001, 0x3f800000, 0x00000000, 0x00000000, 0x00000000, /* def c1 = 1.0, 0.0, 0.0, 0.0  */
34     0x00000042, 0xb00f0000,                                                 /* tex t0                       */
35     0x00000008, 0x800f0000, 0xa0e40001, 0xa0e40000,                         /* dp3 r0, c1, c0               */
36     0x00000005, 0x800f0000, 0x90e40000, 0x80e40000,                         /* mul r0, v0, r0               */
37     0x00000005, 0x800f0000, 0xb0e40000, 0x80e40000,                         /* mul r0, t0, r0               */
38     0x0000ffff};                                                            /* END                          */
39
40 #define FCC_TEXT MAKEFOURCC('T','E','X','T')
41 #define FCC_CTAB MAKEFOURCC('C','T','A','B')
42
43 static const DWORD shader_with_ctab[] = {
44     0xfffe0300,                                                             /* vs_3_0                       */
45     0x0002fffe, FCC_TEXT,   0x00000000,                                     /* TEXT comment                 */
46     0x0008fffe, FCC_CTAB,   0x0000001c, 0x00000010, 0xfffe0300, 0x00000000, /* CTAB comment                 */
47                 0x00000000, 0x00000000, 0x00000000,
48     0x0004fffe, FCC_TEXT,   0x00000000, 0x00000000, 0x00000000,             /* TEXT comment                 */
49     0x0000ffff};                                                            /* END                          */
50
51 static const DWORD shader_with_invalid_ctab[] = {
52     0xfffe0300,                                                             /* vs_3_0                       */
53     0x0005fffe, FCC_CTAB,                                                   /* CTAB comment                 */
54                 0x0000001c, 0x000000a9, 0xfffe0300,
55                 0x00000000, 0x00000000,
56     0x0000ffff};                                                            /* END                          */
57
58 static void test_get_shader_size(void)
59 {
60     UINT shader_size, expected;
61
62     shader_size = D3DXGetShaderSize(simple_vs);
63     expected = sizeof(simple_vs);
64     ok(shader_size == expected, "Got shader size %u, expected %u\n", shader_size, expected);
65
66     shader_size = D3DXGetShaderSize(simple_ps);
67     expected = sizeof(simple_ps);
68     ok(shader_size == expected, "Got shader size %u, expected %u\n", shader_size, expected);
69
70     shader_size = D3DXGetShaderSize(NULL);
71     ok(shader_size == 0, "Got shader size %u, expected 0\n", shader_size);
72 }
73
74 static void test_get_shader_version(void)
75 {
76     DWORD shader_version;
77
78     shader_version = D3DXGetShaderVersion(simple_vs);
79     ok(shader_version == D3DVS_VERSION(1, 1), "Got shader version 0x%08x, expected 0x%08x\n",
80             shader_version, D3DVS_VERSION(1, 1));
81
82     shader_version = D3DXGetShaderVersion(simple_ps);
83     ok(shader_version == D3DPS_VERSION(1, 1), "Got shader version 0x%08x, expected 0x%08x\n",
84             shader_version, D3DPS_VERSION(1, 1));
85
86     shader_version = D3DXGetShaderVersion(NULL);
87     ok(shader_version == 0, "Got shader version 0x%08x, expected 0\n", shader_version);
88 }
89
90 static void test_find_shader_comment(void)
91 {
92     HRESULT hr;
93     LPCVOID data;
94     UINT size;
95
96     hr = D3DXFindShaderComment(NULL, MAKEFOURCC('C','T','A','B'), &data, &size);
97     ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
98
99     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('C','T','A','B'), NULL, &size);
100     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
101
102     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('C','T','A','B'), &data, NULL);
103     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
104
105     hr = D3DXFindShaderComment(shader_with_ctab, 0, &data, &size);
106     ok(hr == S_FALSE, "Got result %x, expected 1 (S_FALSE)\n", hr);
107
108     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('X','X','X','X'), &data, &size);
109     ok(hr == S_FALSE, "Got result %x, expected 1 (S_FALSE)\n", hr);
110
111     hr = D3DXFindShaderComment(shader_with_ctab, MAKEFOURCC('C','T','A','B'), &data, &size);
112     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
113     ok(data == (LPCVOID)(shader_with_ctab + 6), "Got result %p, expected %p\n", data, shader_with_ctab + 6);
114     ok(size == 28, "Got result %d, expected 28\n", size);
115 }
116
117 static void test_get_shader_constant_table_ex(void)
118 {
119     LPD3DXCONSTANTTABLE constant_table = NULL;
120     HRESULT hr;
121     LPVOID data;
122     DWORD size;
123     D3DXCONSTANTTABLE_DESC desc;
124
125     hr = D3DXGetShaderConstantTableEx(NULL, 0, &constant_table);
126     ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
127
128     /* No CTAB data */
129     hr = D3DXGetShaderConstantTableEx(simple_ps, 0, &constant_table);
130     ok(hr == D3DXERR_INVALIDDATA, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DXERR_INVALIDDATA);
131
132     /* With invalid CTAB data */
133     hr = D3DXGetShaderConstantTableEx(shader_with_invalid_ctab, 0, &constant_table);
134     ok(hr == D3DXERR_INVALIDDATA || broken(hr == D3D_OK), /* winxp 64-bit, w2k3 64-bit */
135        "Got result %x, expected %x (D3DXERR_INVALIDDATA)\n", hr, D3DXERR_INVALIDDATA);
136     if (constant_table) ID3DXConstantTable_Release(constant_table);
137
138     hr = D3DXGetShaderConstantTableEx(shader_with_ctab, 0, &constant_table);
139     ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
140
141     if (constant_table)
142     {
143         size = ID3DXConstantTable_GetBufferSize(constant_table);
144         ok(size == 28, "Got result %x, expected 28\n", size);
145
146         data = ID3DXConstantTable_GetBufferPointer(constant_table);
147         ok(!memcmp(data, shader_with_ctab + 6, size), "Retrieved wrong CTAB data\n");
148
149         hr = ID3DXConstantTable_GetDesc(constant_table, NULL);
150         ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
151
152         hr = ID3DXConstantTable_GetDesc(constant_table, &desc);
153         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
154         ok(desc.Creator == (LPCSTR)data + 0x10, "Got result %p, expected %p\n", desc.Creator, (LPCSTR)data + 0x10);
155         ok(desc.Version == D3DVS_VERSION(3, 0), "Got result %x, expected %x\n", desc.Version, D3DVS_VERSION(3, 0));
156         ok(desc.Constants == 0, "Got result %x, expected 0\n", desc.Constants);
157
158         ID3DXConstantTable_Release(constant_table);
159     }
160 }
161
162 START_TEST(shader)
163 {
164     test_get_shader_size();
165     test_get_shader_version();
166     test_find_shader_comment();
167     test_get_shader_constant_table_ex();
168 }