d3dx9: Implement a basic bytecode writer.

Now the shader assembler is structurally complete and it correctly
assembles one trivial shader program.
This commit is contained in:
Matteo Bruni 2010-03-29 17:57:30 +02:00 committed by Alexandre Julliard
parent 95afbeadf5
commit 18c6f23a94
5 changed files with 496 additions and 27 deletions

View File

@ -27,6 +27,68 @@
WINE_DEFAULT_DEBUG_CHANNEL(asmshader);
/* bwriter -> d3d9 conversion functions */
DWORD d3d9_swizzle(DWORD bwriter_swizzle) {
/* Currently a NOP, but this allows changing the internal definitions
* without side effects
*/
DWORD ret = 0;
if((bwriter_swizzle & BWRITERVS_X_X) == BWRITERVS_X_X) ret |= D3DVS_X_X;
if((bwriter_swizzle & BWRITERVS_X_Y) == BWRITERVS_X_Y) ret |= D3DVS_X_Y;
if((bwriter_swizzle & BWRITERVS_X_Z) == BWRITERVS_X_Z) ret |= D3DVS_X_Z;
if((bwriter_swizzle & BWRITERVS_X_W) == BWRITERVS_X_W) ret |= D3DVS_X_W;
if((bwriter_swizzle & BWRITERVS_Y_X) == BWRITERVS_Y_X) ret |= D3DVS_Y_X;
if((bwriter_swizzle & BWRITERVS_Y_Y) == BWRITERVS_Y_Y) ret |= D3DVS_Y_Y;
if((bwriter_swizzle & BWRITERVS_Y_Z) == BWRITERVS_Y_Z) ret |= D3DVS_Y_Z;
if((bwriter_swizzle & BWRITERVS_Y_W) == BWRITERVS_Y_W) ret |= D3DVS_Y_W;
if((bwriter_swizzle & BWRITERVS_Z_X) == BWRITERVS_Z_X) ret |= D3DVS_Z_X;
if((bwriter_swizzle & BWRITERVS_Z_Y) == BWRITERVS_Z_Y) ret |= D3DVS_Z_Y;
if((bwriter_swizzle & BWRITERVS_Z_Z) == BWRITERVS_Z_Z) ret |= D3DVS_Z_Z;
if((bwriter_swizzle & BWRITERVS_Z_W) == BWRITERVS_Z_W) ret |= D3DVS_Z_W;
if((bwriter_swizzle & BWRITERVS_W_X) == BWRITERVS_W_X) ret |= D3DVS_W_X;
if((bwriter_swizzle & BWRITERVS_W_Y) == BWRITERVS_W_Y) ret |= D3DVS_W_Y;
if((bwriter_swizzle & BWRITERVS_W_Z) == BWRITERVS_W_Z) ret |= D3DVS_W_Z;
if((bwriter_swizzle & BWRITERVS_W_W) == BWRITERVS_W_W) ret |= D3DVS_W_W;
return ret;
}
DWORD d3d9_writemask(DWORD bwriter_writemask) {
DWORD ret = 0;
if(bwriter_writemask & BWRITERSP_WRITEMASK_0) ret |= D3DSP_WRITEMASK_0;
if(bwriter_writemask & BWRITERSP_WRITEMASK_1) ret |= D3DSP_WRITEMASK_1;
if(bwriter_writemask & BWRITERSP_WRITEMASK_2) ret |= D3DSP_WRITEMASK_2;
if(bwriter_writemask & BWRITERSP_WRITEMASK_3) ret |= D3DSP_WRITEMASK_3;
return ret;
}
DWORD d3d9_register(DWORD bwriter_register) {
if(bwriter_register == BWRITERSPR_TEMP) return D3DSPR_TEMP;
if(bwriter_register == BWRITERSPR_CONST) return D3DSPR_CONST;
FIXME("Unexpected BWRITERSPR %u\n", bwriter_register);
return -1;
}
DWORD d3d9_opcode(DWORD bwriter_opcode) {
switch(bwriter_opcode) {
case BWRITERSIO_MOV: return D3DSIO_MOV;
case BWRITERSIO_COMMENT: return D3DSIO_COMMENT;
case BWRITERSIO_END: return D3DSIO_END;
default:
FIXME("Unhandled BWRITERSIO token %u\n", bwriter_opcode);
return -1;
}
}
static const char *get_regname(const struct shader_reg *reg, shader_type st) {
switch(reg->type) {
case BWRITERSPR_TEMP:

View File

@ -104,6 +104,381 @@ BOOL add_instruction(struct bwriter_shader *shader, struct instruction *instr) {
return TRUE;
}
/* shader bytecode buffer manipulation functions.
* allocate_buffer creates a new buffer structure, put_dword adds a new
* DWORD to the buffer. In the rare case of a memory allocation failure
* when trying to grow the buffer a flag is set in the buffer to mark it
* invalid. This avoids return value checking and passing in many places
*/
static struct bytecode_buffer *allocate_buffer(void) {
struct bytecode_buffer *ret;
ret = asm_alloc(sizeof(*ret));
if(!ret) return NULL;
ret->alloc_size = BYTECODEBUFFER_INITIAL_SIZE;
ret->data = asm_alloc(sizeof(DWORD) * ret->alloc_size);
if(!ret->data) {
asm_free(ret);
return NULL;
}
ret->state = S_OK;
return ret;
}
static void put_dword(struct bytecode_buffer *buffer, DWORD value) {
if(FAILED(buffer->state)) return;
if(buffer->alloc_size == buffer->size) {
DWORD *newarray;
buffer->alloc_size *= 2;
newarray = asm_realloc(buffer->data,
sizeof(DWORD) * buffer->alloc_size);
if(!newarray) {
ERR("Failed to grow the buffer data memory\n");
buffer->state = E_OUTOFMEMORY;
return;
}
buffer->data = newarray;
}
buffer->data[buffer->size++] = value;
}
/******************************************************
* Implementation of the writer functions starts here *
******************************************************/
static void end(struct bc_writer *This, const struct bwriter_shader *shader, struct bytecode_buffer *buffer) {
put_dword(buffer, D3DSIO_END);
}
static void write_srcregs(struct bc_writer *This, const struct instruction *instr,
struct bytecode_buffer *buffer){
unsigned int i;
if(instr->has_predicate){
This->funcs->srcreg(This, &instr->predicate, buffer);
}
for(i = 0; i < instr->num_srcs; i++){
This->funcs->srcreg(This, &instr->src[i], buffer);
}
}
/* The length of an instruction consists of the destination register (if any),
* the number of source registers, the number of address registers used for
* indirect addressing, and optionally the predicate register
*/
static DWORD instrlen(const struct instruction *instr, unsigned int srcs, unsigned int dsts) {
unsigned int i;
DWORD ret = srcs + dsts + (instr->has_predicate ? 1 : 0);
if(dsts){
if(instr->dst.rel_reg) ret++;
}
for(i = 0; i < srcs; i++) {
if(instr->src[i].rel_reg) ret++;
}
return ret;
}
static void instr_handler(struct bc_writer *This,
const struct instruction *instr,
struct bytecode_buffer *buffer) {
DWORD token = d3d9_opcode(instr->opcode);
TRACE("token: %x\n", token);
This->funcs->opcode(This, instr, token, buffer);
if(instr->has_dst) This->funcs->dstreg(This, &instr->dst, buffer, instr->shift, instr->dstmod);
write_srcregs(This, instr, buffer);
}
static void sm_2_opcode(struct bc_writer *This,
const struct instruction *instr,
DWORD token, struct bytecode_buffer *buffer) {
/* From sm 2 onwards instruction length is encoded in the opcode field */
int dsts = instr->has_dst ? 1 : 0;
token |= instrlen(instr, instr->num_srcs, dsts) << D3DSI_INSTLENGTH_SHIFT;
put_dword(buffer,token);
}
static void sm_3_header(struct bc_writer *This, const struct bwriter_shader *shader, struct bytecode_buffer *buffer) {
/* Declare the shader type and version */
put_dword(buffer, This->version);
return;
}
static void sm_3_srcreg(struct bc_writer *This,
const struct shader_reg *reg,
struct bytecode_buffer *buffer) {
DWORD token = (1 << 31); /* Bit 31 of registers is 1 */
DWORD d3d9reg;
d3d9reg = d3d9_register(reg->type);
token |= (d3d9reg << D3DSP_REGTYPE_SHIFT) & D3DSP_REGTYPE_MASK;
token |= (d3d9reg << D3DSP_REGTYPE_SHIFT2) & D3DSP_REGTYPE_MASK2;
token |= reg->regnum & D3DSP_REGNUM_MASK;
token |= d3d9_swizzle(reg->swizzle) & D3DVS_SWIZZLE_MASK;
put_dword(buffer, token);
}
static void sm_3_dstreg(struct bc_writer *This,
const struct shader_reg *reg,
struct bytecode_buffer *buffer,
DWORD shift, DWORD mod) {
DWORD token = (1 << 31); /* Bit 31 of registers is 1 */
DWORD d3d9reg;
d3d9reg = d3d9_register(reg->type);
token |= (d3d9reg << D3DSP_REGTYPE_SHIFT) & D3DSP_REGTYPE_MASK;
token |= (d3d9reg << D3DSP_REGTYPE_SHIFT2) & D3DSP_REGTYPE_MASK2;
token |= reg->regnum & D3DSP_REGNUM_MASK; /* No shift */
token |= d3d9_writemask(reg->writemask);
put_dword(buffer, token);
}
static const struct instr_handler_table vs_3_handlers[] = {
{BWRITERSIO_MOV, instr_handler},
{BWRITERSIO_END, NULL},
};
static const struct bytecode_backend vs_3_backend = {
sm_3_header,
end,
sm_3_srcreg,
sm_3_dstreg,
sm_2_opcode,
vs_3_handlers
};
static void init_vs30_dx9_writer(struct bc_writer *writer) {
TRACE("Creating DirectX9 vertex shader 3.0 writer\n");
writer->funcs = &vs_3_backend;
}
static struct bc_writer *create_writer(DWORD version, DWORD dxversion) {
struct bc_writer *ret = asm_alloc(sizeof(*ret));
if(!ret) {
WARN("Failed to allocate a bytecode writer instance\n");
return NULL;
}
switch(version) {
case BWRITERVS_VERSION(1, 0):
if(dxversion != 9) {
WARN("Unsupported dxversion for vertex shader 1.0 requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERVS_VERSION(1, 1):
if(dxversion != 9) {
WARN("Unsupported dxversion for vertex shader 1.1 requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERVS_VERSION(2, 0):
if(dxversion != 9) {
WARN("Unsupported dxversion for vertex shader 2.0 requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERVS_VERSION(2, 1):
if(dxversion != 9) {
WARN("Unsupported dxversion for vertex shader 2.x requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERVS_VERSION(3, 0):
if(dxversion != 9) {
WARN("Unsupported dxversion for vertex shader 3.0 requested: %u\n", dxversion);
goto fail;
}
init_vs30_dx9_writer(ret);
break;
case BWRITERPS_VERSION(1, 0):
if(dxversion != 9) {
WARN("Unsupported dxversion for pixel shader 1.0 requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERPS_VERSION(1, 1):
if(dxversion != 9) {
WARN("Unsupported dxversion for pixel shader 1.1 requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERPS_VERSION(1, 2):
if(dxversion != 9) {
WARN("Unsupported dxversion for pixel shader 1.2 requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERPS_VERSION(1, 3):
if(dxversion != 9) {
WARN("Unsupported dxversion for pixel shader 1.3 requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERPS_VERSION(1, 4):
if(dxversion != 9) {
WARN("Unsupported dxversion for pixel shader 1.4 requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERPS_VERSION(2, 0):
if(dxversion != 9) {
WARN("Unsupported dxversion for pixel shader 2.0 requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERPS_VERSION(2, 1):
if(dxversion != 9) {
WARN("Unsupported dxversion for pixel shader 2.x requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
case BWRITERPS_VERSION(3, 0):
if(dxversion != 9) {
WARN("Unsupported dxversion for pixel shader 3.0 requested: %u\n", dxversion);
goto fail;
}
/* TODO: Set the appropriate writer backend */
break;
default:
WARN("Unexpected shader version requested: %08x\n", version);
goto fail;
}
ret->version = version;
return ret;
fail:
asm_free(ret);
return NULL;
}
static HRESULT call_instr_handler(struct bc_writer *writer,
const struct instruction *instr,
struct bytecode_buffer *buffer) {
DWORD i=0;
while(writer->funcs->instructions[i].opcode != BWRITERSIO_END) {
if(instr->opcode == writer->funcs->instructions[i].opcode) {
if(!writer->funcs->instructions[i].func) {
WARN("Opcode %u not supported by this profile\n", instr->opcode);
return E_INVALIDARG;
}
writer->funcs->instructions[i].func(writer, instr, buffer);
return S_OK;
}
i++;
}
FIXME("Unhandled instruction %u\n", instr->opcode);
return E_INVALIDARG;
}
/* SlWriteBytecode (wineshader.@)
*
* Writes shader version specific bytecode from the shader passed in.
* The returned bytecode can be passed to the Direct3D runtime like
* IDirect3DDevice9::Create*Shader.
*
* Parameters:
* shader: Shader to translate into bytecode
* version: Shader version to generate(d3d version token)
* dxversion: DirectX version the code targets
* result: the resulting shader bytecode
*
* Return values:
* S_OK on success
*/
DWORD SlWriteBytecode(const struct bwriter_shader *shader, int dxversion, DWORD **result) {
struct bc_writer *writer;
struct bytecode_buffer *buffer = NULL;
HRESULT hr;
unsigned int i;
if(!shader){
ERR("NULL shader structure, aborting\n");
return E_FAIL;
}
writer = create_writer(shader->version, dxversion);
*result = NULL;
if(!writer) {
WARN("Could not create a bytecode writer instance. Either unsupported version\n");
WARN("or out of memory\n");
hr = E_FAIL;
goto error;
}
buffer = allocate_buffer();
if(!buffer) {
WARN("Failed to allocate a buffer for the shader bytecode\n");
hr = E_FAIL;
goto error;
}
writer->funcs->header(writer, shader, buffer);
if(FAILED(writer->state)) {
hr = writer->state;
goto error;
}
for(i = 0; i < shader->num_instrs; i++) {
hr = call_instr_handler(writer, shader->instr[i], buffer);
if(FAILED(hr)) {
goto error;
}
}
if(FAILED(writer->state)) {
hr = writer->state;
goto error;
}
writer->funcs->end(writer, shader, buffer);
if(FAILED(buffer->state)) {
hr = buffer->state;
goto error;
}
/* Cut off unneeded memory from the result buffer */
*result = asm_realloc(buffer->data,
sizeof(DWORD) * buffer->size);
if(!*result) {
*result = buffer->data;
}
buffer->data = NULL;
hr = S_OK;
error:
if(buffer) {
asm_free(buffer->data);
asm_free(buffer);
}
asm_free(writer);
return hr;
}
void SlDeleteShader(struct bwriter_shader *shader) {
unsigned int i, j;

View File

@ -160,6 +160,8 @@ struct instruction {
struct shader_reg dst;
struct shader_reg *src;
unsigned int num_srcs; /* For freeing the rel_regs */
BOOL has_predicate;
struct shader_reg predicate;
};
struct declaration {
@ -342,6 +344,12 @@ 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);
/* Utilities for internal->d3d constant mapping */
DWORD d3d9_swizzle(DWORD bwriter_swizzle);
DWORD d3d9_writemask(DWORD bwriter_writemask);
DWORD d3d9_register(DWORD bwriter_register);
DWORD d3d9_opcode(DWORD bwriter_opcode);
/*
Enumerations and defines used in the bytecode writer
intermediate representation
@ -400,6 +408,7 @@ typedef enum _BWRITERSHADER_PARAM_SRCMOD_TYPE {
#define BWRITERVS_NOSWIZZLE (BWRITERVS_X_X | BWRITERVS_Y_Y | BWRITERVS_Z_Z | BWRITERVS_W_W)
struct bwriter_shader *SlAssembleShader(const char *text, char **messages);
DWORD SlWriteBytecode(const struct bwriter_shader *shader, int dxversion, DWORD **result);
void SlDeleteShader(struct bwriter_shader *shader);
#endif /* __WINE_D3DX9_36_PRIVATE_H */

View File

@ -388,6 +388,7 @@ HRESULT assemble_shader(const char *preprocShader, const char *preprocMessages,
struct bwriter_shader *shader;
char *messages = NULL;
HRESULT hr;
DWORD *res;
LPD3DXBUFFER buffer;
int size;
char *pos;
@ -439,9 +440,27 @@ HRESULT assemble_shader(const char *preprocShader, const char *preprocMessages,
return D3DXERR_INVALIDDATA;
}
/* TODO: generate bytecode from the shader */
hr = SlWriteBytecode(shader, 9, &res);
SlDeleteShader(shader);
return D3DXERR_INVALIDDATA;
if(FAILED(hr))
{
ERR("SlWriteBytecode failed with 0x%08x\n", hr);
return D3DXERR_INVALIDDATA;
}
size = HeapSize(GetProcessHeap(), 0, res);
hr = D3DXCreateBuffer(size, &buffer);
if(FAILED(hr))
{
HeapFree(GetProcessHeap(), 0, res);
return hr;
}
CopyMemory(ID3DXBuffer_GetBufferPointer(buffer), res, size);
*ppShader = buffer;
HeapFree(GetProcessHeap(), 0, res);
return D3D_OK;
}
HRESULT WINAPI D3DXAssembleShader(LPCSTR data,

View File

@ -1008,52 +1008,56 @@ static void ps_2_x_test(void) {
}
static void vs_3_0_test(void) {
/* FIXME: Some tests are temporarily commented out, because the
current implementation doesn't support the entire vs_3_0 syntax
and it is not trivial to remove todo_wine only from
a subset of the tests here */
struct shader_test tests[] = {
{ /* shader 0 */
"vs_3_0\n"
"mov r0, c0\n",
{0xfffe0300, 0x02000001, 0x800f0000, 0xa0e40000, 0x0000ffff}
},
{ /* shader 1 */
"vs_3_0\n"
/* {*/ /* shader 1 */
/* "vs_3_0\n"
"dcl_2d s0\n",
{0xfffe0300, 0x0200001f, 0x90000000, 0xa00f0800, 0x0000ffff}
},
{ /* shader 2 */
"vs_3_0\n"
},*/
/* {*/ /* shader 2 */
/* "vs_3_0\n"
"dcl_position o0\n",
{0xfffe0300, 0x0200001f, 0x80000000, 0xe00f0000, 0x0000ffff}
},
{ /* shader 3 */
"vs_3_0\n"
},*/
/* {*/ /* shader 3 */
/* "vs_3_0\n"
"dcl_texcoord12 o11\n",
{0xfffe0300, 0x0200001f, 0x800c0005, 0xe00f000b, 0x0000ffff}
},
{ /* shader 4 */
"vs_3_0\n"
},*/
/* {*/ /* shader 4 */
/* "vs_3_0\n"
"texldl r0, v0, s0\n",
{0xfffe0300, 0x0300005f, 0x800f0000, 0x90e40000, 0xa0e40800, 0x0000ffff}
},
{ /* shader 5 */
"vs_3_0\n"
},*/
/* {*/ /* shader 5 */
/* "vs_3_0\n"
"mov r0, c0[aL]\n",
{0xfffe0300, 0x03000001, 0x800f0000, 0xa0e42000, 0xf0e40800, 0x0000ffff}
},
{ /* shader 6 */
"vs_3_0\n"
},*/
/* {*/ /* shader 6 */
/* "vs_3_0\n"
"mov o[ a0.x + 12 ], r0\n",
{0xfffe0300, 0x03000001, 0xe00f200c, 0xb0000000, 0x80e40000, 0x0000ffff}
},
{ /* shader 7 */
"vs_3_0\n"
},*/
/* {*/ /* shader 7 */
/* "vs_3_0\n"
"add_sat r0, r0, r1\n",
{0xfffe0300, 0x03000002, 0x801f0000, 0x80e40000, 0x80e40001, 0x0000ffff}
},
{ /* shader 8 */
"vs_3_0\n"
},*/
/* {*/ /* shader 8 */
/* "vs_3_0\n"
"mov r2, r1_abs\n",
{0xfffe0300, 0x02000001, 0x800f0002, 0x8be40001, 0x0000ffff}
},
},*/
};
exec_tests("vs_3_0", tests, sizeof(tests) / sizeof(tests[0]));
@ -1404,7 +1408,7 @@ START_TEST(asm)
todo_wine vs_2_x_test();
todo_wine ps_2_0_test();
todo_wine ps_2_x_test();
todo_wine vs_3_0_test();
vs_3_0_test();
todo_wine ps_3_0_test();
failure_test();