[3/4] d3dx9: Implement D3DXAssembleShader function, really basic shader assembler.

Matteo Bruni matteo.mystral at gmail.com
Mon Dec 28 15:02:38 CST 2009


-------------- next part --------------
From cddac860823f187168a5eb7863e98e4a07bf776c Mon Sep 17 00:00:00 2001
From: Matteo Bruni <matteo.mystral at gmail.com>
Date: Sat, 26 Dec 2009 16:43:59 +0100
Subject: d3dx9: Implement D3DXAssembleShader function, really basic shader assembler.

Currently it only accepts a minimal subset of the syntax (e.g. just an
instruction and two register types supported) and doesn't produce any
real output (i.e. shader bytecode).
---
 dlls/d3dx9_36/Makefile.in        |    7 +
 dlls/d3dx9_36/asmparser.c        |  193 +++++++++++++++++++
 dlls/d3dx9_36/asmshader.l        |  138 ++++++++++++++
 dlls/d3dx9_36/asmshader.y        |  347 ++++++++++++++++++++++++++++++++++
 dlls/d3dx9_36/asmutils.c         |   65 +++++++
 dlls/d3dx9_36/bytecodewriter.c   |  146 +++++++++++++++
 dlls/d3dx9_36/d3dx9_36_main.c    |    3 +
 dlls/d3dx9_36/d3dx9_36_private.h |  299 +++++++++++++++++++++++++++++
 dlls/d3dx9_36/shader.c           |  382 +++++++++++++++++++++++++++++++++++++-
 dlls/d3dx9_36/tests/asm.c        |    6 +-
 10 files changed, 1582 insertions(+), 4 deletions(-)
 create mode 100644 dlls/d3dx9_36/asmparser.c
 create mode 100644 dlls/d3dx9_36/asmshader.l
 create mode 100644 dlls/d3dx9_36/asmshader.y
 create mode 100644 dlls/d3dx9_36/asmutils.c
 create mode 100644 dlls/d3dx9_36/bytecodewriter.c

diff --git a/dlls/d3dx9_36/Makefile.in b/dlls/d3dx9_36/Makefile.in
index 88ac1d7..fd7f5ca 100644
--- a/dlls/d3dx9_36/Makefile.in
+++ b/dlls/d3dx9_36/Makefile.in
@@ -5,8 +5,12 @@ VPATH     = @srcdir@
 MODULE    = d3dx9_36.dll
 IMPORTLIB = d3dx9
 IMPORTS   = d3d9 gdi32 user32 kernel32
+EXTRALIBS = $(LIBWPP) $(LIBPORT)
 
 C_SRCS = \
+	asmparser.c \
+	asmutils.c \
+	bytecodewriter.c \
 	core.c \
 	d3dx9_36_main.c \
 	font.c \
@@ -17,6 +21,9 @@ C_SRCS = \
 	surface.c \
 	util.c
 
+LEX_SRCS = asmshader.l
+BISON_SRCS = asmshader.y
+
 RC_SRCS = version.rc
 
 @MAKE_DLL_RULES@
diff --git a/dlls/d3dx9_36/asmparser.c b/dlls/d3dx9_36/asmparser.c
new file mode 100644
index 0000000..65e4461
--- /dev/null
+++ b/dlls/d3dx9_36/asmparser.c
@@ -0,0 +1,193 @@
+/*
+ * Direct3D asm shader parser
+ *
+ * Copyright 2008 Stefan Dösinger
+ * Copyright 2009 Matteo Bruni
+ *
+ * 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
+ *
+ */
+
+#include "config.h"
+#include "wine/port.h"
+#include "wine/debug.h"
+
+#include "d3dx9_36_private.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(asmshader);
+WINE_DECLARE_DEBUG_CHANNEL(parsed_shader);
+
+
+/****************************************************************
+ * Common(non-version specific) shader parser control code      *
+ ****************************************************************/
+
+static void asmparser_end(struct asm_parser *This) {
+    TRACE("Finalizing shader\n");
+}
+
+static void asmparser_end_fake(struct asm_parser *This) {
+    TRACE("Finalizing shader, fake asm_parser backend\n");
+    set_parse_status(This, PARSE_ERR);
+}
+
+static void asmparser_instr(struct asm_parser *This, DWORD opcode,
+                            DWORD mod, DWORD shift,
+                            BWRITER_COMPARISON_TYPE comp,
+                            const struct shader_reg *dst,
+                            const struct src_regs *srcs, int expectednsrcs) {
+    struct instruction *instr;
+    unsigned int i;
+    BOOL firstreg = TRUE;
+    unsigned int src_count = srcs ? srcs->count : 0;
+
+    if(!This->shader) return;
+
+    TRACE_(parsed_shader)("%s ", debug_print_opcode(opcode));
+    if(dst) {
+        TRACE_(parsed_shader)("%s", debug_print_dstreg(dst, This->shader->type));
+        firstreg = FALSE;
+    }
+    for(i = 0; i < src_count; i++) {
+        if(!firstreg) TRACE_(parsed_shader)(", ");
+        else firstreg = FALSE;
+        TRACE_(parsed_shader)("%s", debug_print_srcreg(&srcs->reg[i],
+                                                       This->shader->type));
+    }
+    TRACE_(parsed_shader)("\n");
+
+    if(src_count != expectednsrcs) {
+        asmparser_message(This, "Line %u: Wrong number of source registers\n", This->line_no);
+        set_parse_status(This, PARSE_ERR);
+        return;
+    }
+
+    instr = alloc_instr(src_count);
+    if(!instr) {
+        ERR("Error allocating memory for the instruction\n");
+        set_parse_status(This, PARSE_ERR);
+        return;
+    }
+
+    instr->opcode = opcode;
+    instr->dstmod = mod;
+    instr->shift = shift;
+    instr->comptype = comp;
+    if(dst) This->funcs->dstreg(This, instr, dst);
+    for(i = 0; i < src_count; i++) {
+        This->funcs->srcreg(This, instr, i, &srcs->reg[i]);
+    }
+
+    if(!add_instruction(This->shader, instr)) {
+        ERR("Out of memory\n");
+        set_parse_status(This, PARSE_ERR);
+    }
+}
+
+static void asmparser_srcreg_vs_3(struct asm_parser *This,
+                                  struct instruction *instr, int num,
+                                  const struct shader_reg *src) {
+    memcpy(&instr->src[num], src, sizeof(*src));
+}
+
+static void asmparser_dstreg_vs_3(struct asm_parser *This,
+                                  struct instruction *instr,
+                                  const struct shader_reg *dst) {
+    memcpy(&instr->dst, dst, sizeof(*dst));
+    instr->has_dst = TRUE;
+}
+
+static void asmparser_predicate_unsupported(struct asm_parser *This,
+                                            const struct shader_reg *predicate) {
+    asmparser_message(This, "Line %u: Predicate not supported in < VS 2.0 or PS 2.x\n", This->line_no);
+    set_parse_status(This, PARSE_ERR);
+}
+
+static void asmparser_coissue_unsupported(struct asm_parser *This) {
+    asmparser_message(This, "Line %u: Coissue is only supported in pixel shaders versions <= 1.4\n", This->line_no);
+    set_parse_status(This, PARSE_ERR);
+}
+
+static struct asmparser_backend parser_vs_3 = {
+    NULL,
+    NULL,
+    NULL,
+
+    asmparser_dstreg_vs_3,
+    asmparser_srcreg_vs_3,
+
+    asmparser_predicate_unsupported,
+    asmparser_coissue_unsupported,
+
+    NULL,
+    NULL,
+    NULL,
+
+    asmparser_end,
+
+    asmparser_instr,
+};
+
+void create_vs30_parser(struct asm_parser *ret) {
+    TRACE_(parsed_shader)("vs_3_0\n");
+
+    ret->shader = asm_alloc(sizeof(*ret->shader));
+    if(!ret->shader) {
+        ERR("Failed to allocate memory for the shader\n");
+        set_parse_status(ret, PARSE_ERR);
+        return;
+    }
+
+    ret->shader->type = ST_VERTEX;
+    ret->shader->version = BWRITERVS_VERSION(3, 0);
+    ret->funcs = &parser_vs_3;
+}
+
+/* FIXME: This backend is used in place of a real parser backend
+   for the shader versions not implemented yet */
+static struct asmparser_backend parser_fake = {
+    NULL,
+    NULL,
+    NULL,
+
+    asmparser_dstreg_vs_3,
+    asmparser_srcreg_vs_3,
+
+    asmparser_predicate_unsupported,
+    asmparser_coissue_unsupported,
+
+    NULL,
+    NULL,
+    NULL,
+
+    asmparser_end_fake,
+
+    asmparser_instr,
+};
+
+void create_fake_parser(struct asm_parser *ret) {
+    TRACE_(parsed_shader)("fake parser\n");
+
+    ret->shader = asm_alloc(sizeof(*ret->shader));
+    if(!ret->shader) {
+        ERR("Failed to allocate memory for the shader\n");
+        set_parse_status(ret, PARSE_ERR);
+        return;
+    }
+
+    ret->shader->type = ST_VERTEX;
+    ret->shader->version = BWRITERVS_VERSION(3, 0);
+    ret->funcs = &parser_fake;
+}
diff --git a/dlls/d3dx9_36/asmshader.l b/dlls/d3dx9_36/asmshader.l
new file mode 100644
index 0000000..c7187a4
--- /dev/null
+++ b/dlls/d3dx9_36/asmshader.l
@@ -0,0 +1,138 @@
+/*
+ * Direct3D shader assembler
+ *
+ * Copyright 2008 Stefan Dösinger
+ * Copyright 2009 Matteo Bruni
+ *
+ * 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
+ */
+
+%{
+#include "config.h"
+#include "wine/port.h"
+#include "wine/debug.h"
+
+#include "d3dx9_36_private.h"
+#include "asmshader.tab.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(asmshader);
+%}
+
+%option reentrant bison-bridge
+%option noyywrap
+%option prefix="asmshader_"
+%option noinput nounput
+
+/* Registers */
+REG_TEMP                r[0-9]+
+/* for relative addressing in the form o[x], v[x] and c[x] */
+REG_CONSTFLOAT          c[0-9]*
+
+PREPROCESSORDIRECTIVE   #[^\n]*\n
+
+/* Comments */
+DOUBLESLASHCOMMENT      "//"[^\n]*
+SEMICOLONCOMMENT        ";"[^\n]*
+
+/* Whitespaces are spaces, tabs and newlines */
+WHITESPACE              [ \t]+
+NEWLINE                 (\n)|(\r\n)
+
+COMMA                   ","
+
+IMMVAL                  \-?(([0-9]+)|([0-9]*\.[0-9]+))(f)?
+
+ANY                     (.)
+
+%%
+
+    /* Common instructions(vertex and pixel shaders) */
+mov                     {return INSTR_MOV;          }
+
+{REG_TEMP}              {
+                            yylval->regnum = atoi(yytext + 1);
+                            return REG_TEMP;
+                        }
+{REG_CONSTFLOAT}        {
+                            yylval->regnum = atoi(yytext + 1);
+                            return REG_CONSTFLOAT;
+                        }
+
+    /* Shader versions. These are important to select the correct
+     * parser profile.
+     */
+vs\.1\.0|vs_1_0         {return VER_VS10;       }
+vs\.1\.1|vs_1_1         {return VER_VS11;       }
+
+vs_2_0                  {return VER_VS20;       }
+vs_2_x                  {return VER_VS2X;       }
+vs_3_0                  {return VER_VS30;       }
+
+ps\.1\.0|ps_1_0         {return VER_PS10;       }
+ps\.1\.1|ps_1_1         {return VER_PS11;       }
+ps\.1\.2|ps_1_2         {return VER_PS12;       }
+ps\.1\.3|ps_1_3         {return VER_PS13;       }
+ps\.1\.4|ps_1_4         {return VER_PS14;       }
+
+ps_2_0                  {return VER_PS20;       }
+ps_2_x                  {return VER_PS2X;       }
+ps_3_0                  {return VER_PS30;       }
+
+{COMMA}                 {return yytext[0];          }
+-                       {return yytext[0];          }
+\(                      {return yytext[0];          }
+\)                      {return yytext[0];          }
+
+{PREPROCESSORDIRECTIVE} {
+                            /* TODO: update current line information */
+                            TRACE("line info update: %s", yytext);
+                        }
+
+    /* Skip comments */
+{DOUBLESLASHCOMMENT}    {                           }
+{SEMICOLONCOMMENT}      {                           }
+
+{WHITESPACE}            { /* Do nothing */          }
+{NEWLINE}               {
+                            struct asm_parser *ctx = yyget_extra(yyscanner);
+                            ctx->line_no++;
+                        }
+
+{ANY}                   {
+                            struct asm_parser *ctx = yyget_extra(yyscanner);
+                            asmparser_message(ctx, "Line %u: Unexpected input %s\n", ctx->line_no, yytext);
+                            set_parse_status(ctx, PARSE_ERR);
+                        }
+
+%%
+
+struct bwriter_shader *SlAssembleShader(const char *text, char **messages) {
+    struct asm_parser asm_ctx;
+    struct bwriter_shader *ret = NULL;
+    YY_BUFFER_STATE buffer;
+    TRACE("%p, %p\n", text, messages);
+
+    asmshader_lex_init(&asm_ctx.yyscanner);
+    asmshader_set_extra(&asm_ctx, asm_ctx.yyscanner);
+    buffer = asmshader__scan_string(text, asm_ctx.yyscanner);
+    asmshader__switch_to_buffer(buffer, asm_ctx.yyscanner);
+
+    ret = parse_asm_shader(&asm_ctx, messages);
+
+    asmshader__delete_buffer(buffer, asm_ctx.yyscanner);
+    asmshader_lex_destroy(asm_ctx.yyscanner);
+
+    return ret;
+}
diff --git a/dlls/d3dx9_36/asmshader.y b/dlls/d3dx9_36/asmshader.y
new file mode 100644
index 0000000..9f384fc
--- /dev/null
+++ b/dlls/d3dx9_36/asmshader.y
@@ -0,0 +1,347 @@
+/*
+ * Direct3D shader assembler
+ *
+ * Copyright 2008 Stefan Dösinger
+ * Copyright 2009 Matteo Bruni
+ *
+ * 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
+ */
+
+%{
+#include "config.h"
+#include "wine/port.h"
+#include "wine/debug.h"
+
+#include "d3dx9_36_private.h"
+#include "asmshader.tab.h"
+
+#include <stdio.h>
+
+WINE_DEFAULT_DEBUG_CHANNEL(asmshader);
+
+/* Needed lexer functions declarations */
+void asmshader_error (struct asm_parser *ctx, void *scanner, char const *s);
+int asmshader_lex (YYSTYPE * yylval_param, void *scanner);
+
+void set_rel_reg(struct shader_reg *reg, struct rel_reg *rel) {
+    reg->rel_reg = NULL;
+}
+
+%}
+
+%pure-parser
+%parse-param{struct asm_parser *ctx}
+%parse-param{void *scanner}
+%lex-param{yyscan_t *scanner}
+
+%union {
+    struct {
+        float           val;
+        BOOL            integer;
+    } immval;
+    BOOL                immbool;
+    unsigned int        regnum;
+    struct shader_reg   reg;
+    DWORD               srcmod;
+    struct {
+        DWORD           swizzle;
+        DWORD           writemask;
+    } swizzle_wmask;
+    DWORD               writemask;
+    DWORD               swizzle;
+    struct {
+        DWORD           mod;
+        DWORD           shift;
+    } modshift;
+    BWRITER_COMPARISON_TYPE comptype;
+    struct {
+        DWORD           dclusage;
+        unsigned int    regnum;
+    } declaration;
+    struct rel_reg      rel_reg;
+    struct src_regs     sregs;
+}
+
+/* Common instructions between vertex and pixel shaders */
+%token INSTR_MOV
+
+/* Registers */
+%token <regnum> REG_TEMP
+%token <regnum> REG_CONSTFLOAT
+
+/* Version tokens */
+%token VER_VS10
+%token VER_VS11
+%token VER_VS20
+%token VER_VS2X
+%token VER_VS30
+
+%token VER_PS10
+%token VER_PS11
+%token VER_PS12
+%token VER_PS13
+%token VER_PS14
+%token VER_PS20
+%token VER_PS2X
+%token VER_PS30
+
+
+%type <reg> dreg_name
+%type <reg> dreg
+%type <reg> sreg_name
+%type <reg> sreg
+%type <swizzle> swizzle
+%type <modshift> omods
+%type <rel_reg> rel_reg
+%type <sregs> sregs
+
+%%
+
+shader:               version_marker instructions
+                        {
+                            ctx->funcs->end(ctx);
+                        }
+
+version_marker:       VER_VS10
+                        {
+                            TRACE("Vertex shader 1.0\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_VS11
+                        {
+                            TRACE("Vertex shader 1.1\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_VS20
+                        {
+                            TRACE("Vertex shader 2.0\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_VS2X
+                        {
+                            TRACE("Vertex shader 2.x\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_VS30
+                        {
+                            TRACE("Vertex shader 3.0\n");
+                            create_vs30_parser(ctx);
+                        }
+                    | VER_PS10
+                        {
+                            TRACE("Pixel  shader 1.0\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_PS11
+                        {
+                            TRACE("Pixel  shader 1.1\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_PS12
+                        {
+                            TRACE("Pixel  shader 1.2\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_PS13
+                        {
+                            TRACE("Pixel  shader 1.3\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_PS14
+                        {
+                            TRACE("Pixel  shader 1.4\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_PS20
+                        {
+                            TRACE("Pixel  shader 2.0\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_PS2X
+                        {
+                            TRACE("Pixel  shader 2.x\n");
+                            create_fake_parser(ctx);
+                        }
+                    | VER_PS30
+                        {
+                            TRACE("Pixel  shader 3.0\n");
+                            create_fake_parser(ctx);
+                        }
+
+instructions:         /* empty */
+                    | instructions complexinstr
+                            {
+                                /* Nothing to do */
+                            }
+
+complexinstr:         instruction
+                            {
+
+                            }
+
+instruction:          INSTR_MOV omods dreg ',' sregs
+                            {
+                                TRACE("MOV\n");
+                                ctx->funcs->instr(ctx, BWRITERSIO_MOV, $2.mod, $2.shift, 0, &$3, &$5, 1);
+                            }
+
+dreg:                 dreg_name rel_reg
+                            {
+                                $$.regnum = $1.regnum;
+                                $$.type = $1.type;
+                                $$.writemask = BWRITERSP_WRITEMASK_ALL;
+                                $$.srcmod = BWRITERSPSM_NONE;
+                                set_rel_reg(&$$, &$2);
+                            }
+
+dreg_name:            REG_TEMP
+                        {
+                            $$.regnum = $1; $$.type = BWRITERSPR_TEMP;
+                        }
+
+swizzle:              /* empty */
+                        {
+                            $$ = BWRITERVS_NOSWIZZLE;
+                            TRACE("Default swizzle: %08x\n", $$);
+                        }
+
+omods:                 /* Empty */
+                        {
+                            $$.mod = 0;
+                            $$.shift = 0;
+                        }
+
+sregs:                sreg
+                        {
+                            $$.reg[0] = $1;
+                            $$.count = 1;
+                        }
+                    | sregs ',' sreg
+                        {
+                            if($$.count == MAX_SRC_REGS){
+                                asmparser_message(ctx, "Line %u: Too many source registers in this instruction\n",
+                                                  ctx->line_no);
+                                set_parse_status(ctx, PARSE_ERR);
+                            }
+                            else
+                                $$.reg[$$.count++] = $3;
+                        }
+
+sreg:                   sreg_name rel_reg swizzle
+                        {
+                            $$.type = $1.type;
+                            $$.regnum = $1.regnum;
+                            $$.swizzle = $3;
+                            $$.srcmod = BWRITERSPSM_NONE;
+                            set_rel_reg(&$$, &$2);
+                        }
+
+rel_reg:               /* empty */
+                        {
+                            $$.has_rel_reg = FALSE;
+                            $$.additional_offset = 0;
+                        }
+
+sreg_name:            REG_TEMP
+                        {
+                            $$.regnum = $1; $$.type = BWRITERSPR_TEMP;
+                        }
+                    | REG_CONSTFLOAT
+                        {
+                            $$.regnum = $1; $$.type = BWRITERSPR_CONST;
+                        }
+
+%%
+
+void asmshader_error (struct asm_parser *ctx, void *scanner, char const *s) {
+    asmparser_message(ctx, "Line %u: Error \"%s\" from bison\n", ctx->line_no, s);
+    set_parse_status(ctx, PARSE_ERR);
+}
+
+/* Error reporting function */
+void asmparser_message(struct asm_parser *ctx, const char *fmt, ...) {
+    va_list args;
+    char* newbuffer;
+    int rc, newsize;
+
+    if(ctx->messagecapacity == 0) {
+        ctx->messages = asm_alloc(MESSAGEBUFFER_INITIAL_SIZE);
+        if(ctx->messages == NULL) {
+            ERR("Error allocating memory for parser messages\n");
+            return;
+        }
+        ctx->messagecapacity = MESSAGEBUFFER_INITIAL_SIZE;
+    }
+
+    while(1) {
+        va_start(args, fmt);
+        rc = vsnprintf(ctx->messages + ctx->messagesize,
+                       ctx->messagecapacity - ctx->messagesize, fmt, args);
+        va_end(args);
+
+        if (rc < 0 ||                                           /* C89 */
+            rc >= ctx->messagecapacity - ctx->messagesize) {    /* C99 */
+            /* Resize the buffer */
+            newsize = ctx->messagecapacity * 2;
+            newbuffer = asm_realloc(ctx->messages, newsize);
+            if(newbuffer == NULL){
+                ERR("Error reallocating memory for parser messages\n");
+                return;
+            }
+            ctx->messages = newbuffer;
+            ctx->messagecapacity = newsize;
+        } else {
+            ctx->messagesize += rc;
+            return;
+        }
+    }
+}
+
+/* new status is the worse between current status and parameter value */
+void set_parse_status(struct asm_parser *ctx, enum parse_status status) {
+    if(status == PARSE_ERR) ctx->status = PARSE_ERR;
+    else if(status == PARSE_WARN && ctx->status == PARSE_SUCCESS) ctx->status = PARSE_WARN;
+}
+
+struct bwriter_shader *parse_asm_shader(struct asm_parser *asm_ctx, char **messages) {
+    struct bwriter_shader *ret = NULL;
+
+    asm_ctx->shader = NULL;
+    asm_ctx->status = PARSE_SUCCESS;
+    asm_ctx->messagesize = asm_ctx->messagecapacity = 0;
+    asm_ctx->line_no = 1;
+
+    asmshader_parse(asm_ctx, asm_ctx->yyscanner);
+
+    if(asm_ctx->status != PARSE_ERR) ret = asm_ctx->shader;
+    else if(asm_ctx->shader) SlDeleteShader(asm_ctx->shader);
+
+    if(messages) {
+        if(asm_ctx->messagesize) {
+            /* Shrink the buffer to the used size */
+            *messages = asm_realloc(asm_ctx->messages, asm_ctx->messagesize + 1);
+            if(!*messages) {
+                ERR("Out of memory, no messages reported\n");
+                asm_free(asm_ctx->messages);
+            }
+        } else {
+            *messages = NULL;
+        }
+    } else {
+        if(asm_ctx->messagecapacity) asm_free(asm_ctx->messages);
+    }
+
+    return ret;
+}
diff --git a/dlls/d3dx9_36/asmutils.c b/dlls/d3dx9_36/asmutils.c
new file mode 100644
index 0000000..54097d7
--- /dev/null
+++ b/dlls/d3dx9_36/asmutils.c
@@ -0,0 +1,65 @@
+/*
+ * Direct3D shader library utility routines
+ *
+ * Copyright 2008 Stefan Dösinger
+ * Copyright 2009 Matteo Bruni
+ *
+ * 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
+ *
+ */
+
+#include "config.h"
+#include "wine/debug.h"
+
+#include "d3dx9_36_private.h"
+
+/* This file needs the original d3d9 definitions. The bwriter ones
+ * aren't useable because they are wine-internal things. We're writing
+ * d3d8/9 shaders here, so we need the d3d9 definitions (which are
+ * equal to the d3d8 ones)
+ */
+#include "d3d9types.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(asmshader);
+
+static const char *get_regname(const struct shader_reg *reg, shader_type st) {
+    switch(reg->type) {
+        case BWRITERSPR_TEMP:
+            return wine_dbg_sprintf("r%u", reg->regnum);
+        case BWRITERSPR_CONST:
+            return wine_dbg_sprintf("c%u", reg->regnum);
+        default: return "unknown regname";
+    }
+}
+
+const char *debug_print_dstreg(const struct shader_reg *reg, shader_type st) {
+    return wine_dbg_sprintf("%s", get_regname(reg, st));
+}
+
+const char *debug_print_srcreg(const struct shader_reg *reg, shader_type st) {
+    switch(reg->srcmod) {
+        case BWRITERSPSM_NONE:
+            return wine_dbg_sprintf("%s", get_regname(reg, st));
+    }
+    return "Unknown modifier";
+}
+
+const char *debug_print_opcode(DWORD opcode) {
+    switch(opcode){
+        case BWRITERSIO_MOV:          return "mov";
+
+        default:                      return "unknown";
+    }
+}
diff --git a/dlls/d3dx9_36/bytecodewriter.c b/dlls/d3dx9_36/bytecodewriter.c
new file mode 100644
index 0000000..1c05829
--- /dev/null
+++ b/dlls/d3dx9_36/bytecodewriter.c
@@ -0,0 +1,146 @@
+/*
+ * Direct3D bytecode output functions
+ *
+ * Copyright 2008 Stefan Dösinger
+ * Copyright 2009 Matteo Bruni
+ *
+ * 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
+ *
+ */
+
+#include "config.h"
+#include "wine/port.h"
+#include "wine/debug.h"
+
+#include "d3dx9_36_private.h"
+
+/* This file needs the original d3d9 definitions. The bwriter ones
+ * aren't useable because they are wine-internal things. We're writing
+ * d3d8/9 shaders here, so we need the d3d9 definitions (which are
+ * equal to the d3d8 ones)
+ */
+#include "d3d9types.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(asmshader);
+
+/****************************************************************
+ * General assembler shader construction helper routines follow *
+ ****************************************************************/
+/* struct instruction *alloc_instr
+ *
+ * Allocates a new instruction structure with srcs registers
+ *
+ * Parameters:
+ *  srcs: Number of source registers to allocate
+ *
+ * Returns:
+ *  A pointer to the allocated instruction structure
+ *  NULL in case of an allocation failure
+ */
+struct instruction *alloc_instr(unsigned int srcs) {
+    struct instruction *ret = asm_alloc(sizeof(*ret));
+    if(!ret) {
+        ERR("Failed to allocate memory for an instruction structure\n");
+        return NULL;
+    }
+
+    if(srcs) {
+        ret->src = asm_alloc(srcs * sizeof(*ret->src));
+        if(!ret->src) {
+            ERR("Failed to allocate memory for instruction registers\n");
+            asm_free(ret);
+            return NULL;
+        }
+        ret->num_srcs = srcs;
+    }
+    return ret;
+}
+
+/* void add_instruction
+ *
+ * Adds a new instruction to the shader's instructions array and grows the instruction array
+ * if needed.
+ *
+ * The function does NOT copy the instruction structure. Make sure not to release the
+ * instruction or any of its substructures like registers.
+ *
+ * Parameters:
+ *  shader: Shader to add the instruction to
+ *  instr: Instruction to add to the shader
+ */
+BOOL add_instruction(struct bwriter_shader *shader, struct instruction *instr) {
+    struct instruction      **new_instructions;
+
+    if(!shader) return FALSE;
+
+    if(shader->instr_alloc_size == 0) {
+        shader->instr = asm_alloc(sizeof(*shader->instr) * INSTRARRAY_INITIAL_SIZE);
+        if(!shader->instr) {
+            ERR("Failed to allocate the shader instruction array\n");
+            return FALSE;
+        }
+        shader->instr_alloc_size = INSTRARRAY_INITIAL_SIZE;
+    } else if(shader->instr_alloc_size == shader->num_instrs) {
+        new_instructions = asm_realloc(shader->instr,
+                                       sizeof(*shader->instr) * (shader->instr_alloc_size) * 2);
+        if(!new_instructions) {
+            ERR("Failed to grow the shader instruction array\n");
+            return FALSE;
+        }
+        shader->instr = new_instructions;
+        shader->instr_alloc_size = shader->instr_alloc_size * 2;
+    } else if(shader->num_instrs > shader->instr_alloc_size) {
+        ERR("More instructions than allocated. This should not happen\n");
+        return FALSE;
+    }
+
+    shader->instr[shader->num_instrs] = instr;
+    shader->num_instrs++;
+    return TRUE;
+}
+
+void SlDeleteShader(struct bwriter_shader *shader) {
+    unsigned int i, j;
+
+    TRACE("Deleting shader %p\n", shader);
+
+    for(i = 0; i < shader->num_cf; i++) {
+        asm_free(shader->constF[i]);
+    }
+    asm_free(shader->constF);
+    for(i = 0; i < shader->num_ci; i++) {
+        asm_free(shader->constI[i]);
+    }
+    asm_free(shader->constI);
+    for(i = 0; i < shader->num_cb; i++) {
+        asm_free(shader->constB[i]);
+    }
+    asm_free(shader->constB);
+
+    asm_free(shader->inputs);
+    asm_free(shader->outputs);
+    asm_free(shader->samplers);
+
+    for(i = 0; i < shader->num_instrs; i++) {
+        for(j = 0; j < shader->instr[i]->num_srcs; j++) {
+            asm_free(shader->instr[i]->src[j].rel_reg);
+        }
+        asm_free(shader->instr[i]->src);
+        asm_free(shader->instr[i]);
+    }
+    asm_free(shader->instr);
+
+    asm_free(shader);
+}
diff --git a/dlls/d3dx9_36/d3dx9_36_main.c b/dlls/d3dx9_36/d3dx9_36_main.c
index e9c7004..b7f74f9 100644
--- a/dlls/d3dx9_36/d3dx9_36_main.c
+++ b/dlls/d3dx9_36/d3dx9_36_main.c
@@ -32,6 +32,7 @@
 #include "winuser.h"
 
 #include "d3dx9.h"
+#include "d3dx9_36_private.h"
 
 /***********************************************************************
  * DllMain.
@@ -44,8 +45,10 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
         return FALSE; /* prefer native version */
     case DLL_PROCESS_ATTACH:
         DisableThreadLibraryCalls(inst);
+        InitializeCriticalSection(&wpp_mutex);
         break;
     case DLL_PROCESS_DETACH:
+        DeleteCriticalSection(&wpp_mutex);
         break;
     }
     return TRUE;
diff --git a/dlls/d3dx9_36/d3dx9_36_private.h b/dlls/d3dx9_36/d3dx9_36_private.h
index cca4403..c299992 100644
--- a/dlls/d3dx9_36/d3dx9_36_private.h
+++ b/dlls/d3dx9_36/d3dx9_36_private.h
@@ -2,6 +2,8 @@
  * Copyright (C) 2002 Raphael Junqueira
  * Copyright (C) 2008 David Adam
  * Copyright (C) 2008 Tony Wasserka
+ * Copyright (C) 2008 Stefan Dösinger
+ * Copyright (C) 2009 Matteo Bruni
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -128,5 +130,302 @@ typedef struct ID3DXSpriteImpl
     int allocated_sprites; /* number of (pre-)allocated sprites */
 } ID3DXSpriteImpl;
 
+/* Shader assembler definitions */
+typedef enum _shader_type {
+    ST_VERTEX,
+    ST_PIXEL,
+} shader_type;
+
+typedef enum BWRITER_COMPARISON_TYPE {
+    BWRITER_COMPARISON_NONE = 0,
+} BWRITER_COMPARISON_TYPE;
+
+struct shader_reg {
+    DWORD                   type;
+    DWORD                   regnum;
+    struct shader_reg       *rel_reg;
+    DWORD                   srcmod;
+    union {
+        DWORD                   swizzle;
+        DWORD                   writemask;
+    };
+};
+
+struct instruction {
+    DWORD                   opcode;
+    DWORD                   dstmod;
+    DWORD                   shift;
+    BWRITER_COMPARISON_TYPE comptype;
+    BOOL                    has_dst;
+    struct shader_reg       dst;
+    struct shader_reg       *src;
+    unsigned int            num_srcs; /* For freeing the rel_regs */
+};
+
+struct declaration {
+    DWORD                   usage, usage_idx;
+    DWORD                   regnum;
+    DWORD                   writemask;
+};
+
+struct samplerdecl {
+    DWORD                   type;
+    DWORD                   regnum;
+    unsigned int            line_no; /* for error messages */
+};
+
+#define INSTRARRAY_INITIAL_SIZE 8
+struct bwriter_shader {
+    shader_type             type;
+
+    /* Shader version selected */
+    DWORD                   version;
+
+    /* Local constants. Every constant that is not defined below is loaded from
+     * the global constant set at shader runtime
+     */
+    struct constant         **constF;
+    struct constant         **constI;
+    struct constant         **constB;
+    unsigned int            num_cf, num_ci, num_cb;
+
+    /* Declared input and output varyings */
+    struct declaration      *inputs, *outputs;
+    unsigned int            num_inputs, num_outputs;
+    struct samplerdecl      *samplers;
+    unsigned int            num_samplers;
+
+    /* Are special pixel shader 3.0 registers declared? */
+    BOOL                    vPos, vFace;
+
+    /* Array of shader instructions - The shader code itself */
+    struct instruction      **instr;
+    unsigned int            num_instrs, instr_alloc_size;
+};
+
+static inline LPVOID asm_alloc(SIZE_T size) {
+    return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
+}
+
+static inline LPVOID asm_realloc(LPVOID ptr, SIZE_T size) {
+    return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
+}
+
+static inline BOOL asm_free(LPVOID ptr) {
+    return HeapFree(GetProcessHeap(), 0, ptr);
+}
+
+struct asm_parser;
+
+/* This structure is only used in asmshader.y, but since the .l file accesses the semantic types
+ * too it has to know it as well
+ */
+struct rel_reg {
+    BOOL            has_rel_reg;
+    DWORD           type;
+    DWORD           additional_offset;
+    DWORD           rel_regnum;
+    DWORD           swizzle;
+};
+
+#define MAX_SRC_REGS 4
+
+struct src_regs {
+    struct shader_reg reg[MAX_SRC_REGS];
+    unsigned int      count;
+};
+
+struct asmparser_backend {
+    void (*constF)(struct asm_parser *This, DWORD reg, float x, float y, float z, float w);
+    void (*constI)(struct asm_parser *This, DWORD reg, INT x, INT y, INT z, INT w);
+    void (*constB)(struct asm_parser *This, DWORD reg, BOOL x);
+
+    void (*dstreg)(struct asm_parser *This, struct instruction *instr,
+                   const struct shader_reg *dst);
+    void (*srcreg)(struct asm_parser *This, struct instruction *instr, int num,
+                   const struct shader_reg *src);
+
+    void (*predicate)(struct asm_parser *This,
+                      const struct shader_reg *predicate);
+    void (*coissue)(struct asm_parser *This);
+
+    void (*dcl_output)(struct asm_parser *This, DWORD usage, DWORD num,
+                       const struct shader_reg *reg);
+    void (*dcl_input)(struct asm_parser *This, DWORD usage, DWORD num,
+                      const struct shader_reg *reg);
+    void (*dcl_sampler)(struct asm_parser *This, DWORD samptype, DWORD regnum,
+                        unsigned int line_no);
+
+    void (*end)(struct asm_parser *This);
+
+    void (*instr)(struct asm_parser *This, DWORD opcode, DWORD mod, DWORD shift,
+                  BWRITER_COMPARISON_TYPE comp, const struct shader_reg *dst,
+                  const struct src_regs *srcs, int expectednsrcs);
+};
+
+struct instruction *alloc_instr(unsigned int srcs);
+BOOL add_instruction(struct bwriter_shader *shader, struct instruction *instr);
+
+#define MESSAGEBUFFER_INITIAL_SIZE 1024
+struct asm_parser {
+    /* The function table of the parser implementation */
+    struct asmparser_backend *funcs;
+
+    /* Private data follows */
+    struct bwriter_shader    *shader;
+    unsigned int              m3x3pad_count;
+
+    /* Reentrant lexer pointer */
+    void *yyscanner;
+
+    enum parse_status{
+        PARSE_SUCCESS = 0,
+        PARSE_WARN = 1,
+        PARSE_ERR = 2
+    } status;
+    char *messages;
+    unsigned int messagesize;
+    unsigned int messagecapacity;
+    unsigned int line_no;
+};
+
+void create_fake_parser(struct asm_parser *ret);
+void create_vs30_parser(struct asm_parser *ret);
+
+struct bwriter_shader *parse_asm_shader(struct asm_parser *asm_ctx, char **messages);
+
+#ifdef __GNUC__
+#define PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
+#else
+#define PRINTF_ATTR(fmt,args)
+#endif
+
+void asmparser_message(struct asm_parser *ctx, const char *fmt, ...) PRINTF_ATTR(2,3);
+void set_parse_status(struct asm_parser *ctx, enum parse_status status);
+
+/* Declaration for reentrant lexer/parser */
+#define YY_EXTRA_TYPE struct asm_parser *
+
+/* A reasonable value as initial size */
+#define BYTECODEBUFFER_INITIAL_SIZE 32
+struct bytecode_buffer {
+    DWORD *data;
+    DWORD size;
+    DWORD alloc_size;
+    /* For tracking rare out of memory situations without passing
+     * return values around everywhere
+     */
+    HRESULT state;
+};
+
+struct bc_writer; /* Predeclaration for use in vtable parameters */
+
+typedef void (*instr_writer)(struct bc_writer *This,
+                             const struct instruction *instr,
+                             struct bytecode_buffer *buffer);
+
+struct bytecode_backend {
+    void (*header)(struct bc_writer *This, const struct bwriter_shader *shader, struct bytecode_buffer *buffer);
+    void (*end)(struct bc_writer *This, const struct bwriter_shader *shader,
+                struct bytecode_buffer *buffer);
+    void (*srcreg)(struct bc_writer *This, const struct shader_reg *reg,
+                   struct bytecode_buffer *buffer);
+    void (*dstreg)(struct bc_writer *This, const struct shader_reg *reg,
+                   struct bytecode_buffer *buffer, DWORD shift, DWORD mod);
+    void (*opcode)(struct bc_writer *This, const struct instruction *instr,
+                   DWORD token, struct bytecode_buffer *buffer);
+
+    struct instr_handler_table {
+        DWORD opcode;
+        instr_writer func;
+    } *instructions;
+};
+
+/* Bytecode writing stuff */
+struct bc_writer {
+    struct bytecode_backend     *funcs;
+
+    /* Avoid result checking */
+    HRESULT                     state;
+
+    DWORD                       version;
+};
+
+
+/* Debug utility routines. Some are not reentrant, check asmutils.c */
+const char *debug_print_dstreg(const struct shader_reg *reg, shader_type st);
+const char *debug_print_srcreg(const struct shader_reg *reg, shader_type st);
+const char *debug_print_opcode(DWORD opcode);
+
+/*
+  Below there are some enumerations and defines used in the bytecode writer
+  intermediate representation
+*/
+
+typedef enum _BWRITERSHADER_INSTRUCTION_OPCODE_TYPE
+{
+    BWRITERSIO_MOV = 1,
+
+    BWRITERSIO_COMMENT = 0xfffe,
+    BWRITERSIO_END = 0Xffff,
+} BWRITERSHADER_INSTRUCTION_OPCODE_TYPE;
+
+typedef enum _BWRITERSHADER_PARAM_REGISTER_TYPE
+{
+    BWRITERSPR_TEMP = 0,
+    BWRITERSPR_CONST = 2,
+} BWRITERSHADER_PARAM_REGISTER_TYPE;
+
+#define BWRITERSP_WRITEMASK_0   0x1 /* .x r */
+#define BWRITERSP_WRITEMASK_1   0x2 /* .y g */
+#define BWRITERSP_WRITEMASK_2   0x4 /* .z b */
+#define BWRITERSP_WRITEMASK_3   0x8 /* .w a */
+#define BWRITERSP_WRITEMASK_ALL 0xf /* all */
+
+typedef enum _BWRITERSHADER_PARAM_SRCMOD_TYPE
+{
+    BWRITERSPSM_NONE = 0,
+} BWRITERSHADER_PARAM_SRCMOD_TYPE;
+
+#define BWRITER_SM1_VS  0xfffe
+#define BWRITER_SM1_PS  0xffff
+
+#define BWRITERPS_VERSION(major, minor) ((BWRITER_SM1_PS << 16) | ((major) << 8) | (minor))
+#define BWRITERVS_VERSION(major, minor) ((BWRITER_SM1_VS << 16) | ((major) << 8) | (minor))
+
+#define BWRITERVS_SWIZZLE_SHIFT      16
+#define BWRITERVS_SWIZZLE_MASK       (0xFF << BWRITERVS_SWIZZLE_SHIFT)
+#define BWRITERSP_SWIZZLE_SHIFT      16
+#define BWRITERSP_SWIZZLE_MASK       (0xFF << BWRITERSP_SWIZZLE_SHIFT)
+
+#define BWRITERVS_X_X       (0 << BWRITERVS_SWIZZLE_SHIFT)
+#define BWRITERVS_X_Y       (1 << BWRITERVS_SWIZZLE_SHIFT)
+#define BWRITERVS_X_Z       (2 << BWRITERVS_SWIZZLE_SHIFT)
+#define BWRITERVS_X_W       (3 << BWRITERVS_SWIZZLE_SHIFT)
+
+#define BWRITERVS_Y_X       (0 << (BWRITERVS_SWIZZLE_SHIFT + 2))
+#define BWRITERVS_Y_Y       (1 << (BWRITERVS_SWIZZLE_SHIFT + 2))
+#define BWRITERVS_Y_Z       (2 << (BWRITERVS_SWIZZLE_SHIFT + 2))
+#define BWRITERVS_Y_W       (3 << (BWRITERVS_SWIZZLE_SHIFT + 2))
+
+#define BWRITERVS_Z_X       (0 << (BWRITERVS_SWIZZLE_SHIFT + 4))
+#define BWRITERVS_Z_Y       (1 << (BWRITERVS_SWIZZLE_SHIFT + 4))
+#define BWRITERVS_Z_Z       (2 << (BWRITERVS_SWIZZLE_SHIFT + 4))
+#define BWRITERVS_Z_W       (3 << (BWRITERVS_SWIZZLE_SHIFT + 4))
+
+#define BWRITERVS_W_X       (0 << (BWRITERVS_SWIZZLE_SHIFT + 6))
+#define BWRITERVS_W_Y       (1 << (BWRITERVS_SWIZZLE_SHIFT + 6))
+#define BWRITERVS_W_Z       (2 << (BWRITERVS_SWIZZLE_SHIFT + 6))
+#define BWRITERVS_W_W       (3 << (BWRITERVS_SWIZZLE_SHIFT + 6))
+
+#define BWRITERVS_NOSWIZZLE (BWRITERVS_X_X | BWRITERVS_Y_Y | BWRITERVS_Z_Z | BWRITERVS_W_W)
+
+/* Mutex used to guarantee a single invocation
+   of the D3DXAssembleShader function (or its variants) at a time.
+   This is needed as wpp isn't thread-safe */
+extern CRITICAL_SECTION wpp_mutex;
+
+struct bwriter_shader *SlAssembleShader(const char *text, char **messages);
+void SlDeleteShader(struct bwriter_shader *shader);
 
 #endif /* __WINE_D3DX9_36_PRIVATE_H */
diff --git a/dlls/d3dx9_36/shader.c b/dlls/d3dx9_36/shader.c
index c05f7b7..7c8d1e8 100644
--- a/dlls/d3dx9_36/shader.c
+++ b/dlls/d3dx9_36/shader.c
@@ -1,5 +1,6 @@
 /*
  * Copyright 2008 Luis Busquets
+ * Copyright 2009 Matteo Bruni
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,6 +24,7 @@
 #include "windef.h"
 #include "wingdi.h"
 #include "d3dx9.h"
+#include "wine/wpp.h"
 #include "d3dx9_36_private.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
@@ -134,6 +136,281 @@ LPCSTR WINAPI D3DXGetVertexShaderProfile(LPDIRECT3DDEVICE9 device)
     return NULL;
 }
 
+#define BUFFER_INITIAL_CAPACITY 256
+
+struct mem_file_desc
+{
+    char *buffer;
+    unsigned int size;
+    unsigned int pos;
+};
+
+struct mem_file_desc current_shader;
+LPD3DXINCLUDE current_include;
+char *wpp_output;
+int wpp_output_capacity, wpp_output_size;
+CRITICAL_SECTION wpp_mutex;
+
+char *wpp_messages;
+int wpp_messages_capacity, wpp_messages_size;
+
+/* Preprocessor error reporting functions */
+void wpp_write_message(const char *fmt, va_list args)
+{
+    char* newbuffer;
+    int rc, newsize;
+
+    if(wpp_messages_capacity == 0)
+    {
+        wpp_messages = HeapAlloc(GetProcessHeap(), 0, MESSAGEBUFFER_INITIAL_SIZE);
+        if(wpp_messages == NULL)
+        {
+            ERR("Error allocating memory for parser messages\n");
+            return;
+        }
+        wpp_messages_capacity = MESSAGEBUFFER_INITIAL_SIZE;
+    }
+
+    while(1)
+    {
+        rc = vsnprintf(wpp_messages + wpp_messages_size,
+                       wpp_messages_capacity - wpp_messages_size, fmt, args);
+
+        if (rc < 0 ||                                           /* C89 */
+            rc >= wpp_messages_capacity - wpp_messages_size) {    /* C99 */
+            /* Resize the buffer */
+            newsize = wpp_messages_capacity * 2;
+            newbuffer = HeapReAlloc(GetProcessHeap(), 0, wpp_messages, newsize);
+            if(newbuffer == NULL)
+            {
+                ERR("Error reallocating memory for parser messages\n");
+                return;
+            }
+            wpp_messages = newbuffer;
+            wpp_messages_capacity = newsize;
+        }
+        else
+        {
+            wpp_messages_size += rc;
+            return;
+        }
+    }
+}
+
+void wpp_write_message_var(const char *fmt, ...)
+{
+    va_list args;
+
+    va_start(args, fmt);
+    wpp_write_message(fmt, args);
+    va_end(args);
+}
+
+void wpp_error(const char *file, int line, int col, const char *near,
+               const char *msg, va_list ap)
+{
+    wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
+                          line, col, "Error");
+    wpp_write_message(msg, ap);
+    wpp_write_message_var("\n");
+}
+
+void wpp_warning(const char *file, int line, int col, const char *near,
+                 const char *msg, va_list ap)
+{
+    wpp_write_message_var("%s:%d:%d: %s: ", file ? file : "'main file'",
+                          line, col, "Warning");
+    wpp_write_message(msg, ap);
+    wpp_write_message_var("\n");
+}
+
+char *wpp_lookup_mem(const char *filename, const char *parent_name,
+                     char **include_path, int include_path_count)
+{
+    /* Here we return always ok. We will maybe fail on the next wpp_open_mem */
+    char *path;
+
+    path = malloc(strlen(filename) + 1);
+    if(!path) return NULL;
+    memcpy(path, filename, strlen(filename) + 1);
+    return path;
+}
+
+void *wpp_open_mem(const char *filename, int type)
+{
+    struct mem_file_desc *desc;
+
+    if(filename[0] == '\0') /* "" means to load the initial shader */
+    {
+        current_shader.pos = 0;
+        return &current_shader;
+    }
+    else
+    {
+        HRESULT hr;
+
+        if(current_include == NULL) return NULL;
+        desc = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mem_file_desc));
+        if(!desc)
+        {
+            ERR("Error allocating memory\n");
+            return NULL;
+        }
+        hr = ID3DXInclude_Open(current_include,
+                               type ? D3DXINC_SYSTEM : D3DXINC_LOCAL,
+                               filename, NULL, (LPCVOID *)&desc->buffer,
+                               &desc->size);
+        if(FAILED(hr))
+        {
+            HeapFree(GetProcessHeap(), 0, desc);
+            return NULL;
+        }
+    }
+    desc->pos = 0;
+    return desc;
+}
+
+void wpp_close_mem(void *file)
+{
+    struct mem_file_desc *desc = file;
+
+    if(desc != &current_shader)
+    {
+        if(current_include == NULL)
+        {
+            ERR("current_include == NULL, desc == %p, buffer = %s\n",
+                desc, desc->buffer);
+            HeapFree(GetProcessHeap(), 0, desc);
+            return;
+        }
+        ID3DXInclude_Close(current_include, desc->buffer);
+        HeapFree(GetProcessHeap(), 0, desc);
+        return;
+    }
+    /* This is the main file */
+    HeapFree(GetProcessHeap(), 0, desc->buffer);
+    desc->buffer = NULL;
+}
+
+int wpp_read_mem(void *file, char *buffer, unsigned int len)
+{
+    struct mem_file_desc *desc = file;
+
+    if(desc->pos + len > desc->size) len = desc->size - desc->pos;
+    memcpy(buffer, &desc->buffer[desc->pos], len);
+    desc->pos += len;
+    return len;
+}
+
+void wpp_write_mem(const char *buffer, unsigned int len)
+{
+    char *new_wpp_output;
+
+    if(wpp_output_capacity == 0)
+    {
+        wpp_output = HeapAlloc(GetProcessHeap(), 0, BUFFER_INITIAL_CAPACITY);
+        if(!wpp_output)
+        {
+            ERR("Error allocating memory\n");
+            return;
+        }
+        wpp_output_capacity = BUFFER_INITIAL_CAPACITY;
+    }
+    if(wpp_output_size + len > wpp_output_capacity)
+    {
+        while(wpp_output_size + len > wpp_output_capacity)
+        {
+            wpp_output_capacity *= 2;
+        }
+        new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
+                                     wpp_output_capacity);
+        if(!new_wpp_output)
+        {
+            ERR("Error allocating memory\n");
+            return;
+        }
+        wpp_output = new_wpp_output;
+    }
+    memcpy(wpp_output+wpp_output_size, buffer, len);
+    wpp_output_size += len;
+}
+
+int wpp_close_output(void)
+{
+    /* trim buffer to the effective size */
+    char *new_wpp_output = HeapReAlloc(GetProcessHeap(), 0, wpp_output,
+                                       wpp_output_size + 1);
+    if(!new_wpp_output) return 0;
+    wpp_output[wpp_output_size]='\0';
+    return 1;
+}
+
+HRESULT assemble_shader(char *preprocShader, char *preprocMessages,
+                        LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs)
+{
+    struct bwriter_shader *shader;
+    char *messages = NULL;
+    HRESULT hr;
+    LPD3DXBUFFER buffer;
+    int size;
+    char *pos;
+
+    shader = SlAssembleShader(preprocShader, &messages);
+
+    if(messages || preprocMessages)
+    {
+        if(preprocMessages)
+        {
+            TRACE("Preprocessor messages:\n");
+            TRACE("%s", preprocMessages);
+        }
+        if(messages)
+        {
+            TRACE("Assembler messages:\n");
+            TRACE("%s", messages);
+        }
+
+        TRACE("Shader source:\n");
+        TRACE("%s", preprocShader);
+
+        size = (messages ? strlen(messages) : 0) +
+            (preprocMessages ? strlen(preprocMessages) : 0) + 1;
+        hr = D3DXCreateBuffer(size, &buffer);
+        if(FAILED(hr))
+        {
+            HeapFree(GetProcessHeap(), 0, messages);
+            HeapFree(GetProcessHeap(), 0, preprocShader);
+            HeapFree(GetProcessHeap(), 0, preprocMessages);
+            if(shader) SlDeleteShader(shader);
+            return hr;
+        }
+        pos = ID3DXBuffer_GetBufferPointer(buffer);
+        if(preprocMessages)
+        {
+            CopyMemory(pos, preprocMessages, strlen(preprocMessages)+1);
+            pos += strlen(preprocMessages);
+        }
+        if(messages)
+            CopyMemory(pos, messages, strlen(messages)+1);
+
+        *ppErrorMsgs = buffer;
+
+        HeapFree(GetProcessHeap(), 0, messages);
+        HeapFree(GetProcessHeap(), 0, preprocMessages);
+    }
+    HeapFree(GetProcessHeap(), 0, preprocShader);
+
+    if(shader == NULL)
+    {
+        ERR("Asm reading failed\n");
+        return D3DXERR_INVALIDDATA;
+    }
+
+    /* TODO: generate bytecode from the shader */
+    SlDeleteShader(shader);
+    return D3DXERR_INVALIDDATA;
+}
+
 HRESULT WINAPI D3DXAssembleShader(LPCSTR data,
                                   UINT data_len,
                                   CONST D3DXMACRO* defines,
@@ -142,8 +419,106 @@ HRESULT WINAPI D3DXAssembleShader(LPCSTR data,
                                   LPD3DXBUFFER* shader,
                                   LPD3DXBUFFER* error_messages)
 {
-    FIXME("stub\n");
-    return D3DERR_INVALIDCALL;
+    int ret;
+    HRESULT hr;
+    CONST D3DXMACRO* def = defines;
+
+    struct wpp_callbacks wpp_callbacks = {
+        wpp_lookup_mem,
+        wpp_open_mem,
+        wpp_close_mem,
+        wpp_read_mem,
+        wpp_write_mem,
+        wpp_error,
+        wpp_warning,
+    };
+
+    EnterCriticalSection(&wpp_mutex);
+
+    /* TODO: flags */
+    if(flags) FIXME("flags: %x\n", flags);
+
+    if(def != NULL)
+    {
+        while(def->Name != NULL)
+        {
+            wpp_add_define(def->Name, def->Definition);
+            def++;
+        }
+    }
+    current_include = include;
+
+    *shader = *error_messages = NULL;
+    wpp_output_size = wpp_output_capacity = 0;
+    wpp_output = NULL;
+
+    /* Preprocess shader */
+    wpp_set_callbacks(&wpp_callbacks);
+    wpp_messages_size = wpp_messages_capacity = 0;
+    wpp_messages = NULL;
+    current_shader.buffer = HeapAlloc(GetProcessHeap(), 0, data_len + 1);
+    if(!current_shader.buffer)
+    {
+        ERR("Not enough free memory\n");
+        hr = E_OUTOFMEMORY;
+        goto cleanup;
+    }
+    memcpy(current_shader.buffer, data, data_len);
+    current_shader.buffer[data_len] = '\0';
+    current_shader.size = data_len;
+
+    ret = wpp_parse("", NULL);
+    if(!wpp_close_output())
+        ret = 1;
+    if(ret)
+    {
+        int i;
+
+        TRACE("Error during shader preprocessing\n");
+        HeapFree(GetProcessHeap(), 0, current_shader.buffer);
+        if(wpp_messages)
+        {
+            int size;
+            LPD3DXBUFFER buffer;
+
+            TRACE("Preprocessor messages:\n");
+            TRACE("%s", wpp_messages);
+
+            size = strlen(wpp_messages) + 1;
+            hr = D3DXCreateBuffer(size, &buffer);
+            if(FAILED(hr)) goto cleanup;
+            CopyMemory(ID3DXBuffer_GetBufferPointer(buffer), wpp_messages, size);
+            *error_messages = buffer;
+        }
+        if(data)
+        {
+            TRACE("Shader source:\n");
+            for(i=0;i<data_len;i++)
+            {
+                TRACE("%c", data[i]);
+            }
+            TRACE("\n");
+        }
+        hr = D3DXERR_INVALIDDATA;
+        goto cleanup;
+    }
+
+    hr = assemble_shader(wpp_output, wpp_messages, shader, error_messages);
+
+cleanup:
+    /* Remove the previously added defines */
+    if(defines != NULL)
+    {
+        while(defines->Name != NULL)
+        {
+            wpp_del_define(defines->Name);
+            defines++;
+        }
+    }
+    HeapFree(GetProcessHeap(), 0, wpp_messages);
+    HeapFree(GetProcessHeap(), 0, wpp_output);
+    LeaveCriticalSection(&wpp_mutex);
+    return hr;
 }
 
 HRESULT WINAPI D3DXAssembleShaderFromFileA(LPCSTR filename,
@@ -164,7 +539,8 @@ HRESULT WINAPI D3DXAssembleShaderFromFileA(LPCSTR filename,
     if (!filename_w) return E_OUTOFMEMORY;
     MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
 
-    ret = D3DXAssembleShaderFromFileW(filename_w, defines, include, flags, shader, error_messages);
+    ret = D3DXAssembleShaderFromFileW(filename_w, defines, include,
+                                      flags, shader, error_messages);
 
     HeapFree(GetProcessHeap(), 0, filename_w);
     return ret;
diff --git a/dlls/d3dx9_36/tests/asm.c b/dlls/d3dx9_36/tests/asm.c
index 5c95a2f..6622390 100644
--- a/dlls/d3dx9_36/tests/asm.c
+++ b/dlls/d3dx9_36/tests/asm.c
@@ -1327,6 +1327,8 @@ static void assembleshader_test(void) {
         if(shader) ID3DXBuffer_Release(shader);
     } else skip("Couldn't create \"shader.vsh\"\n");
 
+    } /* todo_wine */
+
     /* NULL shader tests */
     shader = NULL;
     messages = NULL;
@@ -1340,6 +1342,8 @@ static void assembleshader_test(void) {
     }
     if(shader) ID3DXBuffer_Release(shader);
 
+    todo_wine {
+
     shader = NULL;
     messages = NULL;
     hr = D3DXAssembleShaderFromFileA("nonexistent.vsh",
@@ -1403,7 +1407,7 @@ START_TEST(asm)
     todo_wine vs_3_0_test();
     todo_wine ps_3_0_test();
 
-    todo_wine failure_test();
+    failure_test();
 
     assembleshader_test();
 }
-- 
1.6.4.4


More information about the wine-patches mailing list