d3dx9_36/tests: Added HLSL test suite. (try 3)

Travis Athougies iammisc at gmail.com
Tue Aug 3 14:07:35 CDT 2010


Changes since try 2:
- passes winetestbot
- checks specificially for pixel shader versions needed
- fixed issues found by Matteo
- uses a more relaxed floating point equality check (should fix test on David Adam's machine)

---
 dlls/d3dx9_36/tests/Makefile.in |    1 +
 dlls/d3dx9_36/tests/hlsl.c      |  666 +++++++++++++++++++++++++++++++++++++++
 include/d3dx9shader.h           |   11 +
 3 files changed, 678 insertions(+), 0 deletions(-)
 create mode 100644 dlls/d3dx9_36/tests/hlsl.c

diff --git a/dlls/d3dx9_36/tests/Makefile.in b/dlls/d3dx9_36/tests/Makefile.in
index 4a7aa28..63c27af 100644
--- a/dlls/d3dx9_36/tests/Makefile.in
+++ b/dlls/d3dx9_36/tests/Makefile.in
@@ -9,6 +9,7 @@ C_SRCS = \
 	asm.c \
 	core.c \
 	effect.c \
+	hlsl.c \
 	line.c \
 	math.c \
 	mesh.c \
diff --git a/dlls/d3dx9_36/tests/hlsl.c b/dlls/d3dx9_36/tests/hlsl.c
new file mode 100644
index 0000000..1df8861
--- /dev/null
+++ b/dlls/d3dx9_36/tests/hlsl.c
@@ -0,0 +1,666 @@
+/*
+ * Copyright (C) 2010 - Travis Athougies
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+#define CINTERFACE
+#define COBJMACROS
+#include "wine/test.h"
+
+#include <d3d9.h>
+#include <d3dx9.h>
+#include <assert.h>
+#include <stdio.h>
+
+struct vertex
+{
+    float x, y, z;
+    float tx, ty;
+};
+
+const struct vertex quad_vertices[4] = {
+    {-1, -1, 0, 0, 1},
+    {-1, 1, 0, 0, 0},
+    {1, -1, 0, 1, 1},
+    {1, 1, 0, 1, 0}
+};
+
+const D3DVERTEXELEMENT9 vdeclelements[] = {
+    {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
+    {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
+    D3DDECL_END()
+};
+
+/* Simple vertex shader to just pass through everything */
+const char *vshader_passthru_hlsl =
+    "float4 vshader(float4 pos: POSITION, inout float2 texcoord: TEXCOORD0): POSITION \
+    {                                                                   \
+        return pos;                                                     \
+    }                                                                   \
+    ";
+
+static IDirect3DVertexBuffer9 *quad_geometry;
+static IDirect3DVertexShader9 *vshader_passthru;
+static IDirect3DVertexDeclaration9 *vdeclaration;
+static IDirect3DDevice9 *device;
+
+/* Generic functions to create windows, initialize Direct3D, etc. */
+static HWND create_window(void)
+{
+    WNDCLASS wc = {0};
+    wc.lpfnWndProc = DefWindowProc;
+    wc.lpszClassName = "d3d9_test_wc";
+    RegisterClass(&wc);
+
+    return CreateWindow("d3d9_test_wc", "d3d9_test",
+                        0, 0, 0, 0, 0, 0, 0, 0, 0);
+}
+
+static IDirect3DDevice9 *init_d3d9(void)
+{
+    IDirect3D9 *d3d9_ptr = 0;
+    IDirect3DDevice9 *device_ptr = 0;
+    D3DPRESENT_PARAMETERS present_parameters;
+
+    void *temp_geometry_vertices;
+
+    ID3DXBuffer *compiled = NULL;
+    ID3DXBuffer *errors = NULL;
+
+    HRESULT hres;
+
+    d3d9_ptr = Direct3DCreate9(D3D_SDK_VERSION);
+    if (!d3d9_ptr)
+    {
+        skip("could not create D3D9\n");
+        return NULL;
+    }
+
+    ZeroMemory(&present_parameters, sizeof(present_parameters));
+    present_parameters.Windowed = TRUE;
+    present_parameters.hDeviceWindow = create_window();
+    present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
+
+    hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
+    if(FAILED(hres)) {
+        skip("could not create Direct3D9 device\n");
+        return NULL;
+    }
+
+    /* Create the quad geometry */
+    hres = IDirect3DDevice9_CreateVertexBuffer(device_ptr, 4 * sizeof(struct vertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &quad_geometry, NULL);
+    ok(SUCCEEDED(hres), "Could not create vertex buffer, IDirect3DDevice9_CreateVertexBuffer returned: %08x\n", hres);
+
+    hres = IDirect3DVertexBuffer9_Lock(quad_geometry, 0, sizeof(quad_vertices), (void**)&temp_geometry_vertices, 0);
+    ok(SUCCEEDED(hres), "IDirect3DVertexBuffer9_Lock returned: %08x\n", hres);
+    memcpy(temp_geometry_vertices, quad_vertices, sizeof(quad_vertices));
+    IDirect3DVertexBuffer9_Unlock(quad_geometry);
+
+    hres = IDirect3DDevice9_CreateVertexDeclaration(device_ptr, vdeclelements, &vdeclaration);
+    ok(SUCCEEDED(hres), "Could not create vertex declaration: IDirect3DDevice9_CreateVertexDeclaration returned: %08x\n", hres);
+
+    /* Create a simple vertex shader to just pass through the values */
+    hres = D3DXCompileShader(vshader_passthru_hlsl, strlen(vshader_passthru_hlsl),
+                             NULL, NULL, "vshader", "vs_1_1", 0,
+                             &compiled, &errors, 0);
+    if(FAILED(hres))
+    {
+        vshader_passthru = NULL;
+        skip("not compiling vertex shader due to lacking wine HLSL support!\n");
+        if(errors)
+            IUnknown_Release(errors);
+        return device_ptr;
+    }
+
+    hres = IDirect3DDevice9_CreateVertexShader(device_ptr, (DWORD*) ID3DXBuffer_GetBufferPointer(compiled), &vshader_passthru);
+    ok(SUCCEEDED(hres), "IDirect3DDevice9_CreateVertexShader returned: %08x\n", hres);
+    IUnknown_Release(compiled);
+
+    return device_ptr;
+}
+
+/* Convenience functions */
+static void set_float4(ID3DXConstantTable *constants, const char *name, float x, float y, float z, float w)
+{
+    D3DXVECTOR4 vector;
+    vector.x = x;
+    vector.y = y;
+    vector.z = z;
+    vector.w = w;
+    ID3DXConstantTable_SetVector(constants, device, name, &vector);
+}
+
+/* Compare floating point arrays keeping a count of how many
+   erroneous values we have found */
+static int cmp_results_linear(int samples, float epsilon, float *expected, float *actual)
+{
+    int result = 0;
+    int i = 0;
+    for(i = 0;i < samples;i++)
+        if(abs(expected[i] - actual[i]) > epsilon)
+            result ++;
+    return result;
+}
+
+/* Compile our pixel shader and get back the compiled version and a constant
+   table */
+static IDirect3DPixelShader9* compile_pixel_shader(const char *shader, const char *profile, ID3DXConstantTable **constants)
+{
+    ID3DXBuffer *compiled = NULL;
+    ID3DXBuffer *errors = NULL;
+    IDirect3DPixelShader9 *pshader;
+    HRESULT hr;
+
+    hr = D3DXCompileShader(shader, strlen(shader),
+                           NULL, NULL, "test", profile,
+                           0, &compiled, &errors, constants);
+    ok (hr == D3D_OK, "Pixel shader %s compilation failed: %s\n", shader, (char*) (errors?ID3DXBuffer_GetBufferPointer(errors):""));
+    if(FAILED(hr)) return NULL;
+
+    hr = IDirect3DDevice9_CreatePixelShader(device, (DWORD*) ID3DXBuffer_GetBufferPointer(compiled), &pshader);
+    ok(SUCCEEDED(hr), "IDirect3DDevice9_CreatePixelShader returned: %08x\n", hr);
+    IUnknown_Release(compiled);
+    return pshader;
+}
+
+/* Draw a full screen quad */
+static void draw_quad_with_shader(void)
+{
+    HRESULT hr;
+    D3DXMATRIX projection_matrix;
+
+    D3DXMatrixOrthoLH(&projection_matrix, 2.0, 2.0, 0, 1.0);
+    IDirect3DDevice9_SetTransform(device, D3DTS_PROJECTION, &projection_matrix);
+
+    hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
+    ok(hr == D3D_OK, "IDirect3DDevice9_Clear returned: %08x\n", hr);
+
+    hr = IDirect3DDevice9_BeginScene(device);
+    ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene returned: %08x\n", hr);
+
+    hr = IDirect3DDevice9_SetVertexDeclaration(device, vdeclaration);
+    ok(hr == D3D_OK, "IDirect3DDevice9_SetVertexDeclaration returned: %08x\n", hr);
+    IDirect3DDevice9_SetStreamSource(device, 0, quad_geometry, 0, sizeof(struct vertex));
+    ok(hr == D3D_OK, "IDirect3DDevice9_SetStreamSource returned: %08x\n", hr);
+    IDirect3DDevice9_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, 0, 2);
+    ok(hr == D3D_OK, "IDirect3DDevice9_DrawPrimitive returned: %08x\n", hr);
+
+    hr = IDirect3DDevice9_EndScene(device);
+    ok(hr == D3D_OK, "IDirect3DDevice9_EndScene returned: %08x\n", hr);
+
+    IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
+
+    return;
+}
+
+static void setup_device(IDirect3DSurface9 **render_target, IDirect3DSurface9 **readback, D3DFORMAT format, int width, int height, IDirect3DVertexShader9 *vshader, IDirect3DPixelShader9 *pshader)
+{
+    HRESULT hr;
+    hr = IDirect3DDevice9_CreateRenderTarget(device, width, height, format,
+                                             D3DMULTISAMPLE_NONE, 0, FALSE, render_target, NULL);
+    ok(hr == D3D_OK, "IDirect3DDevice9_CreateRenderTarget returned: %08x\n", hr);
+
+    /* The Direct3D 9 docs state that we cannot lock a render target surface,
+       instead we must copy the render target onto this surface to lock it */
+    hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, width, height, format,
+                                                      D3DPOOL_SYSTEMMEM, readback, NULL);
+    ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned: %08x\n", hr);
+
+    hr = IDirect3DDevice9_SetRenderTarget(device, 0, *render_target);
+    ok(hr == D3D_OK, "IDirect3DDevice9_SetRenderTarget returned: %08x\n", hr);
+
+    hr = IDirect3DDevice9_SetVertexShader(device, vshader);
+    ok(hr == D3D_OK, "IDirect3DDevice9_SetVertexShader returned: %08x\n", hr);
+    hr = IDirect3DDevice9_SetPixelShader(device, pshader);
+    ok(hr == D3D_OK, "IDirect3DDevice9_SetPixelShader returned: %08x\n", hr);
+}
+
+/* For all compute_shader_* calls, this framework assumes that the data types are 4 bytes
+   long. The two formats used here are D3DFMT_R32F and D3DFMT_A8R8G8B8 */
+
+/* Repeatedly call a shader with linearly varying values and collect the results */
+static void* compute_shader_linear(IDirect3DPixelShader9 *pshader, ID3DXConstantTable *constants, D3DFORMAT format, int samples)
+{
+    IDirect3DSurface9 *render_target;
+    IDirect3DSurface9 *readback;
+    int i;
+
+    HRESULT hr;
+    void *ret = NULL;
+    D3DLOCKED_RECT lr;
+
+    setup_device(&render_target, &readback, format, samples, 1, vshader_passthru, pshader);
+
+    ret = HeapAlloc(GetProcessHeap(), 0, 4 * samples);
+    for(i = 0;i < 32;i++) {
+        ID3DXConstantTable_SetFloat(constants, device, "$x", ((float) i)/((float) samples));
+        draw_quad_with_shader();
+
+        IDirect3DDevice9_GetRenderTargetData(device, render_target, readback);
+
+        hr = IDirect3DSurface9_LockRect(readback, &lr, NULL, D3DLOCK_READONLY);
+        ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned: %08x\n", hr);
+        memcpy((void*) (((DWORD) ret) + (4 * i)), lr.pBits, 4);
+        hr = IDirect3DSurface9_UnlockRect(readback);
+        ok(hr == D3D_OK, "IDirect3DSurface9_UnlockRect returned: %08x\n", hr);
+    }
+
+    /* release everything */
+    IUnknown_Release(render_target);
+    IUnknown_Release(readback);
+
+    return ret;
+}
+
+/* Compute a shader on a fullscreen quad and get the results back */
+static void* compute_shader_fullscreen(IDirect3DPixelShader9 *pshader, D3DFORMAT format, int width, int height)
+{
+    IDirect3DSurface9 *render_target;
+    IDirect3DSurface9 *readback;
+
+    HRESULT hr;
+    void *ret = NULL;
+    D3DLOCKED_RECT lr;
+
+    int y;
+
+    setup_device(&render_target, &readback, format, width, height, vshader_passthru, pshader);
+
+    /* Draw the quad with the shader and read back the data */
+    draw_quad_with_shader();
+    IDirect3DDevice9_GetRenderTargetData(device, render_target, readback);
+    hr = IDirect3DSurface9_LockRect(readback, &lr, NULL, D3DLOCK_READONLY);
+    ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned: %08x\n", hr);
+    /* We're dealing with types of 4 bytes here: float and DWORD. The height is in height,
+       but the width of the actual returned surface is in lr.Pitch, since the texture might
+       be padded.*/
+    ret = HeapAlloc(GetProcessHeap(), 0,  4 * width * height);
+    for(y = 0;y < height;y++)
+        memcpy((void*) (((DWORD) ret) + (y * width)), (void*) (((DWORD) lr.pBits) + (y * lr.Pitch)), width * 4);
+    hr = IDirect3DSurface9_UnlockRect(readback);
+    ok(hr == D3D_OK, "IDirect3DSurface9_UnlockRect returned: %08x\n", hr);
+
+    /* release everything */
+    IUnknown_Release(render_target);
+    IUnknown_Release(readback);
+
+    return ret;
+}
+
+/* Now the actual test functions */
+
+static void test_trig(void)
+{
+    static float sin_expected[] = {
+        0.500000f, 0.597545f, 0.691341f, 0.777785f,
+        0.853553f, 0.915734f, 0.961939f, 0.990392f,
+        1.000000f, 0.990392f, 0.961939f, 0.915734f,
+        0.853553f, 0.777785f, 0.691341f, 0.597545f,
+        0.500000f, 0.402454f, 0.308658f, 0.222214f,
+        0.146446f, 0.084265f, 0.038060f, 0.009607f,
+        0.000000f, 0.009607f, 0.038060f, 0.084265f,
+        0.146446f, 0.222214f, 0.308658f, 0.402454f,
+    };
+
+    static float cos_expected[] = {
+        1.000000f, 0.990392f, 0.961939f, 0.915734f,
+        0.853553f, 0.777785f, 0.691341f, 0.597545f,
+        0.500000f, 0.402454f, 0.308658f, 0.222214f,
+        0.146446f, 0.084265f, 0.038060f, 0.009607f,
+        0.000000f, 0.009607f, 0.038060f, 0.084265f,
+        0.146446f, 0.222214f, 0.308658f, 0.402454f,
+        0.500000f, 0.597545f, 0.691341f, 0.777785f,
+        0.853553f, 0.915734f, 0.961939f, 0.990392f,
+    };
+
+    static const char *sin_shader =
+      "float4 test(uniform float x): COLOR\n"
+      "{\n"
+      "    const float pi2 = 6.2831853;;\n"
+      "    float calcd = (sin(x * pi2) + 1)/2;"
+      "    return calcd;\n"
+      "}\n";
+
+    static const char *cos_shader =
+        "float4 test(uniform float x): COLOR\n"
+        "{\n"
+        "    const float pi2 = 6.2831853;;\n"
+        "    float calcd = (cos(x * pi2) + 1)/2;"
+        "    return calcd;\n"
+        "}\n";
+
+    ID3DXConstantTable* constants;
+    IDirect3DPixelShader9* pshader;
+    void* data;
+    int errors = 0;
+
+    pshader = compile_pixel_shader(sin_shader, "ps_2_0", &constants);
+    if(pshader != NULL)
+    {
+
+        data = compute_shader_linear(pshader, constants, D3DFMT_R32F, 32);
+
+        errors = cmp_results_linear(32, 0.00001, sin_expected, data);
+        ok(errors == 0, "sin: Got %d unexpected values\n", errors);
+
+        HeapFree(GetProcessHeap(), 0, data);
+
+        IUnknown_Release(constants);
+        IUnknown_Release(pshader);
+    }
+
+    pshader = compile_pixel_shader(cos_shader, "ps_2_0", &constants);
+    if(pshader != NULL)
+    {
+        data = compute_shader_linear(pshader, constants, D3DFMT_R32F, 32);
+
+        errors = cmp_results_linear(32, 0.00001, cos_expected, data);
+        ok(errors == 0, "cos: Got %d unexpected values\n", errors);
+
+        HeapFree(GetProcessHeap(), 0, data);
+
+        IUnknown_Release(constants);
+        IUnknown_Release(pshader);
+    }
+}
+
+static void test_math(void)
+{
+    static const char *order_of_operations_shader =
+        "float4 test(uniform float u, uniform float v, uniform float w, uniform float x, uniform float y, uniform float z): COLOR\n"
+        "{\n"
+        "    return float4(x * y - z / w + --u / -v, 0, 0, 0);\n"
+        "}\n";
+
+    static const float u = 2.5, v = 0.3, w = 0.2, x = 0.7, y = 0.1, z = 1.0;
+    static const float expected_value = (x * y) - (z / w) + ( (u - 1) / (-v));
+
+    ID3DXConstantTable *constants;
+    IDirect3DPixelShader9 *pshader;
+    void *data;
+    float actual_value;
+
+    pshader = compile_pixel_shader(order_of_operations_shader, "ps_2_0", &constants);
+    if(pshader != NULL)
+    {
+        ID3DXConstantTable_SetFloat(constants, device, "$u", u);
+        ID3DXConstantTable_SetFloat(constants, device, "$v", v);
+        ID3DXConstantTable_SetFloat(constants, device, "$w", w);
+        ID3DXConstantTable_SetFloat(constants, device, "$x", x);
+        ID3DXConstantTable_SetFloat(constants, device, "$y", y);
+        ID3DXConstantTable_SetFloat(constants, device, "$z", z);
+
+        data = compute_shader_fullscreen(pshader, D3DFMT_R32F, 1, 1);
+        actual_value = *(float*) data;
+
+        ok(abs(actual_value - expected_value) < 0.00001, "order_of_operations_test: Got %f expected %f\n", actual_value, expected_value);
+
+        IUnknown_Release(constants);
+        IUnknown_Release(pshader);
+    }
+}
+
+static void test_swizzle(void)
+{
+    static const char *swizzle_test_shader =
+        "uniform float4 color;\n" /* Once again this is to prevent compiler optimization. */
+        "float4 test(): COLOR\n"
+        "{\n"
+        "    float4 ret = color;\n"
+        "    ret.gb = ret.ra;\n"
+        "    ret.ra = float2(0, 0);\n"
+        "    return ret;\n"
+        "}\n";
+
+    ID3DXConstantTable *constants;
+    IDirect3DPixelShader9 *pshader;
+    DWORD *data;
+
+    pshader = compile_pixel_shader(swizzle_test_shader, "ps_2_0", &constants);
+    if(pshader != NULL)
+    {
+        set_float4(constants, "color", 1, 0, 0, 1);
+
+        data = (DWORD*) compute_shader_fullscreen(pshader,  D3DFMT_A8R8G8B8, 1, 1);
+
+        ok(data[0] == D3DCOLOR_ARGB(0, 0, 255, 255), "swizzle_test: Got color %08x (should be 0x0000FFFF)\n", data[0]);
+
+        IUnknown_Release(constants);
+        IUnknown_Release(pshader);
+    }
+}
+
+static void test_conditionals(void)
+{
+    static const char *if_greater_shader =
+        "float4 test(float2 pos: TEXCOORD0): COLOR\n"
+        "{\n"
+        "    if((pos.x * 32.0) > 20.0)\n"
+        "        return float4(0, 0, 1, 0);\n"
+        "    else\n"
+        "        return float4(0, 1, 0, 0);\n"
+        "}\n";
+
+    static const char *ternary_operator_shader =
+        "float4 test(float2 pos: TEXCOORD0): COLOR\n"
+        "{\n"
+        "    return (pos.x < 0.5?float4(1, 0, 1, 0):float4(0, 1, 0, 1));\n"
+        "}\n";
+
+    ID3DXConstantTable *constants;
+    IDirect3DPixelShader9 *pshader;
+    DWORD *data;
+
+    pshader = compile_pixel_shader(if_greater_shader, "ps_2_0", &constants);
+    if(pshader != NULL)
+    {
+        data = (DWORD*) compute_shader_fullscreen(pshader, D3DFMT_A8R8G8B8, 32, 1);
+
+        ok(data[15] == D3DCOLOR_ARGB(0, 0, 255, 0), "if_greater_test: Got color %08x (should be 0x0000ff00)\n", data[15]);
+        ok(data[25] == D3DCOLOR_ARGB(0, 0, 0, 255), "if_greater_test: Got color %08x (should be 0x000000ff)\n", data[25]);
+
+        IUnknown_Release(constants);
+        IUnknown_Release(pshader);
+    }
+
+    pshader = compile_pixel_shader(ternary_operator_shader, "ps_2_0", &constants);
+    if(pshader != NULL)
+    {
+        data = (DWORD*) compute_shader_fullscreen(pshader, D3DFMT_A8R8G8B8, 8, 1);
+        ok(data[0] == D3DCOLOR_ARGB(0, 255, 0, 255), "ternary_operator_test: Got color %08x (should be 0x00ff00ff)\n", data[0]);
+        ok(data[1] == D3DCOLOR_ARGB(0, 255, 0, 255), "ternary_operator_test: Got color %08x (should be 0x00ff00ff)\n", data[1]);
+        ok(data[2] == D3DCOLOR_ARGB(0, 255, 0, 255), "ternary_operator_test: Got color %08x (should be 0x00ff00ff)\n", data[2]);
+        ok(data[3] == D3DCOLOR_ARGB(0, 255, 0, 255), "ternary_operator_test: Got color %08x (should be 0x00ff00ff)\n", data[3]);
+        ok(data[4] == D3DCOLOR_ARGB(255, 0, 255, 0), "ternary_operator_test: Got color %08x (should be 0xff00ff00)\n", data[4]);
+        ok(data[5] == D3DCOLOR_ARGB(255, 0, 255, 0), "ternary_operator_test: Got color %08x (should be 0xff00ff00)\n", data[5]);
+        ok(data[6] == D3DCOLOR_ARGB(255, 0, 255, 0), "ternary_operator_test: Got color %08x (should be 0xff00ff00)\n", data[6]);
+        ok(data[7] == D3DCOLOR_ARGB(255, 0, 255, 0), "ternary_operator_test: Got color %08x (should be 0xff00ff00)\n", data[7]);
+
+        IUnknown_Release(constants);
+        IUnknown_Release(pshader);
+    }
+}
+
+static void test_float(void)
+{
+    static const char *vec4_indexing_test1_shader =
+        "float4 test(): COLOR\n"
+        "{\n"
+        "    float4 color;\n"
+        "    color[0] = 0.0;\n"
+        "    color[1] = 1.0;\n"
+        "    color[2] = 0.0;\n"
+        "    color[3] = 1.0;\n"
+        "    return color;\n"
+        "}\n";
+
+    static const char *vec4_indexing_test2_shader =
+        "uniform int i;\n" /* We have this uniform here so the compiler can't optimize */
+        "float4 test(): COLOR\n"
+        "{\n"
+        "    float4 color = float4(0, 0, 1, 1);\n"
+        "    color.g = color[i];\n"
+        "    color.b = 0;\n"
+        "    return color;\n"
+        "}\n";
+
+    ID3DXConstantTable *constants;
+    IDirect3DPixelShader9 *pshader;
+    DWORD* data;
+
+    pshader = compile_pixel_shader(vec4_indexing_test1_shader, "ps_2_0", &constants);
+    if(pshader != NULL) {
+        data = (DWORD*) compute_shader_fullscreen(pshader, D3DFMT_A8R8G8B8, 32, 1);
+
+        ok(data[10] == D3DCOLOR_ARGB(255, 0, 255, 0), "vec4_indexing_test1: Got color %08x (should be 0xff00ff00)\n", data[10]);
+        ok(data[16] == D3DCOLOR_ARGB(255, 0, 255, 0), "vec4_indexing_test1: Got color %08x (should be 0xff00ff00)\n", data[16]);
+        ok(data[22] == D3DCOLOR_ARGB(255, 0, 255, 0), "vec4_indexing_test1: Got color %08x (should be 0xff00ff00)\n", data[22]);
+
+        IUnknown_Release(constants);
+        IUnknown_Release(pshader);
+    }
+
+    pshader = compile_pixel_shader(vec4_indexing_test2_shader, "ps_2_0", &constants);
+    if(pshader != NULL)
+    {
+        ID3DXConstantTable_SetInt(constants, device, "i", 2);
+
+        data = (DWORD*) compute_shader_fullscreen(pshader, D3DFMT_A8R8G8B8, 32, 1);
+
+        ok(data[10] == D3DCOLOR_ARGB(255, 0, 255, 0), "vec4_indexing_test2: Got color %08x (should be 0xff00ff00)\n", data[10]);
+        ok(data[16] == D3DCOLOR_ARGB(255, 0, 255, 0), "vec4_indexing_test2: Got color %08x (should be 0xff00ff00)\n", data[16]);
+        ok(data[22] == D3DCOLOR_ARGB(255, 0, 255, 0), "vec4_indexing_test2: Got color %08x (should be 0xff00ff00)\n", data[22]);
+
+        IUnknown_Release(constants);
+        IUnknown_Release(pshader);
+    }
+}
+
+static void test_constant_tables(void)
+{
+    static const char *vector_array_staticindex_test_shader =
+        "uniform float4 colors[4];\n"
+        "float4 test(float2 wpos: TEXCOORD0):COLOR\n"
+        "{\n"
+        "    return colors[0] + colors[1] + colors[2] + colors[3];\n"
+        "}\n";
+
+    static const char *integer_constant_test_shader =
+        "uniform int brk;\n"
+        "float4 test(float2 wpos: TEXCOORD0):COLOR\n"
+        "{\n"
+        "    if((wpos.x * 8) > brk)\n"
+        "        return float4(1, 0, 1, 0);\n"
+        "    else\n"
+        "        return float4(1, 1, 1, 1);\n"
+        "}\n";
+
+    static const char *uniform_array_test_shader =
+        "uniform float4 colors[4];\n"
+        "float4 test(float2 wpos: VPOS):COLOR\n"
+        "{\n"
+        "     return colors[wpos.x];\n"
+        "}\n";
+
+    static D3DXVECTOR4 test_colors[] = {
+        {1, 0, 0, 0},
+        {0, 0, 1, 0},
+        {0, 0, 0, 1},
+        {0, 1, 0, 0}
+    };
+
+    ID3DXConstantTable *constants;
+    IDirect3DPixelShader9 *pshader;
+    DWORD* data;
+
+    pshader = compile_pixel_shader(vector_array_staticindex_test_shader, "ps_2_0", &constants);
+    if(pshader != NULL)
+    {
+        ID3DXConstantTable_SetVectorArray(constants, device, "colors", test_colors, 4);
+        data = (DWORD*) compute_shader_fullscreen(pshader, D3DFMT_A8R8G8B8, 1, 1);
+
+        ok(data[0] == D3DCOLOR_ARGB(255, 255, 255, 255), "vector_array_staticindex_test: Got color %08x (should be 0xffffffff)\n", data[0]);
+
+        IUnknown_Release(constants);
+        IUnknown_Release(pshader);
+    }
+
+    pshader = compile_pixel_shader(integer_constant_test_shader, "ps_2_0", &constants);
+    if(pshader != NULL)
+    {
+        ID3DXConstantTable_SetInt(constants, device, "brk", 5);
+        data = (DWORD*) compute_shader_fullscreen(pshader, D3DFMT_A8R8G8B8, 8, 1);
+
+        ok(data[0] == D3DCOLOR_ARGB(255, 255, 255, 255), "integer_constant_test: Got color %08x (should be 0xffffffff)\n", data[0]);
+        ok(data[6] == D3DCOLOR_ARGB(0, 255, 0, 255), "integer_constant_test: Got color %08x (should be 0x00ff00ff\n", data[6]);
+
+        IUnknown_Release(constants);
+        IUnknown_Release(pshader);
+    }
+
+    pshader = compile_pixel_shader(uniform_array_test_shader, "ps_3_0", &constants);
+    if(pshader != NULL) {
+      ID3DXConstantTable_SetVectorArray(constants, device, "colors", test_colors, 4);
+
+      data = (DWORD*) compute_shader_fullscreen(pshader, D3DFMT_A8R8G8B8, 4, 1);
+
+      ok(data[0] == D3DCOLOR_ARGB(0, 255, 0, 0), "uniform_array_test: Got color %08x (should be 0x00ff0000)\n", data[0]);
+      ok(data[1] == D3DCOLOR_ARGB(0, 0, 0, 255), "uniform_array_test: Got color %08x (should be 0x000000ff)\n", data[1]);
+      ok(data[2] == D3DCOLOR_ARGB(255, 0, 0, 0), "uniform_array_test: Got color %08x (should be 0xff000000)\n", data[2]);
+      ok(data[3] == D3DCOLOR_ARGB(0, 0, 255, 0), "uniform_array_test: Got color %08x (should be 0x0000ff00)\n", data[3]);
+
+      IUnknown_Release(constants);
+      IUnknown_Release(pshader);
+    }
+}
+
+/* The heart of our little testing framework */
+START_TEST(hlsl)
+{
+    D3DCAPS9 caps;
+    ULONG refcount;
+
+    device = init_d3d9();
+    if (!device) return;
+
+    /* Make sure we support pixel shaders, before trying to compile them! */
+    IDirect3DDevice9_GetDeviceCaps(device, &caps);
+    if(caps.PixelShaderVersion >= 0xffff0200) {
+        todo_wine {
+            test_constant_tables();
+            test_math();
+            test_conditionals();
+            test_float();
+            test_trig();
+            test_swizzle();
+        }
+    } else skip("no pixel shader support\n");
+
+    /* Reference counting sanity checks */
+    if(vshader_passthru) {
+        refcount = IDirect3DVertexShader9_Release(vshader_passthru);
+        ok(!refcount, "Pass-through vertex shader has %u references left\n", refcount);
+    }
+
+    refcount = IDirect3DVertexBuffer9_Release(quad_geometry);
+    ok(!refcount, "Vertex buffer has %u references left\n", refcount);
+
+    refcount = IDirect3DVertexDeclaration9_Release(vdeclaration);
+    ok(!refcount, "Vertex declaration has %u references left\n", refcount);
+
+    refcount = IDirect3DDevice9_Release(device);
+    ok(!refcount, "Device has %u references left\n", refcount);
+    return;
+}
diff --git a/include/d3dx9shader.h b/include/d3dx9shader.h
index c8380bc..c54d27c 100644
--- a/include/d3dx9shader.h
+++ b/include/d3dx9shader.h
@@ -296,6 +296,17 @@ HRESULT WINAPI D3DXAssembleShader(LPCSTR data,
                                   LPD3DXBUFFER* shader,
                                   LPD3DXBUFFER* error_messages);
 
+HRESULT WINAPI D3DXCompileShader(LPCSTR src_data,
+                                 UINT data_len,
+                                 const D3DXMACRO* defines,
+                                 LPD3DXINCLUDE include,
+                                 LPCSTR function_name,
+                                 LPCSTR profile,
+                                 DWORD flags,
+                                 LPD3DXBUFFER* shader,
+                                 LPD3DXBUFFER* error_messages,
+                                 LPD3DXCONSTANTTABLE* constant_table);
+
 HRESULT WINAPI D3DXGetShaderConstantTableEx(CONST DWORD* byte_code,
                                             DWORD flags,
                                             LPD3DXCONSTANTTABLE* constant_table);
-- 
1.6.4.4




More information about the wine-patches mailing list