HRESULT d3d_execute_buffer_execute(struct d3d_execute_buffer *buffer, struct d3d_device *device, struct d3d_viewport *viewport) { DWORD vs = buffer->data.dwVertexOffset; DWORD is = buffer->data.dwInstructionOffset; char *instr = (char *)buffer->desc.lpData + is; unsigned int i; if (viewport->active_device != device) { WARN("Viewport %p active device is %p.\n", viewport, viewport->active_device); return DDERR_INVALIDPARAMS; } /* Activate the viewport */ viewport_activate(viewport, FALSE); TRACE("ExecuteData :\n"); if (TRACE_ON(ddraw)) _dump_executedata(&(buffer->data)); for (;;) { D3DINSTRUCTION *current = (D3DINSTRUCTION *)instr; BYTE size; WORD count; count = current->wCount; size = current->bSize; instr += sizeof(D3DINSTRUCTION); switch (current->bOpcode) { case D3DOP_POINT: { WARN("POINT-s (%d)\n", count); instr += count * size; } break; case D3DOP_LINE: { WARN("LINE-s (%d)\n", count); instr += count * size; } break; case D3DOP_TRIANGLE: { D3DTLVERTEX *tl_vx = buffer->vertex_data; TRACE("TRIANGLE (%d)\n", count); if (buffer->nb_indices < count * 3) { buffer->nb_indices = count * 3; HeapFree(GetProcessHeap(), 0, buffer->indices); buffer->indices = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->indices) * buffer->nb_indices); } for (i = 0; i < count; ++i) { D3DTRIANGLE *ci = (D3DTRIANGLE *)instr; TRACE(" v1: %d v2: %d v3: %d\n",ci->u1.v1, ci->u2.v2, ci->u3.v3); TRACE(" Flags : "); if (TRACE_ON(ddraw)) { /* Wireframe */ if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1) TRACE("EDGEENABLE1 "); if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2) TRACE("EDGEENABLE2 "); if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1) TRACE("EDGEENABLE3 "); /* Strips / Fans */ if (ci->wFlags == D3DTRIFLAG_EVEN) TRACE("EVEN "); if (ci->wFlags == D3DTRIFLAG_ODD) TRACE("ODD "); if (ci->wFlags == D3DTRIFLAG_START) TRACE("START "); if ((ci->wFlags > 0) && (ci->wFlags < 30)) TRACE("STARTFLAT(%u) ", ci->wFlags); TRACE("\n"); } buffer->indices[(i * 3) ] = ci->u1.v1; buffer->indices[(i * 3) + 1] = ci->u2.v2; buffer->indices[(i * 3) + 2] = ci->u3.v3; instr += size; } if (count) IDirect3DDevice7_DrawIndexedPrimitive(&device->IDirect3DDevice7_iface, D3DPT_TRIANGLELIST, D3DFVF_TLVERTEX, tl_vx, buffer->nb_vertices, buffer->indices, count * 3, 0); } break; case D3DOP_MATRIXLOAD: WARN("MATRIXLOAD-s (%d)\n", count); instr += count * size; break; case D3DOP_MATRIXMULTIPLY: TRACE("MATRIXMULTIPLY (%d)\n", count); for (i = 0; i < count; ++i) { D3DMATRIXMULTIPLY *ci = (D3DMATRIXMULTIPLY *)instr; D3DMATRIX *a, *b, *c; a = ddraw_get_object(&device->handle_table, ci->hDestMatrix - 1, DDRAW_HANDLE_MATRIX); b = ddraw_get_object(&device->handle_table, ci->hSrcMatrix1 - 1, DDRAW_HANDLE_MATRIX); c = ddraw_get_object(&device->handle_table, ci->hSrcMatrix2 - 1, DDRAW_HANDLE_MATRIX); if (!a || !b || !c) { ERR("Invalid matrix handle (a %#x -> %p, b %#x -> %p, c %#x -> %p).\n", ci->hDestMatrix, a, ci->hSrcMatrix1, b, ci->hSrcMatrix2, c); } else { TRACE("dst %p, src1 %p, src2 %p.\n", a, b, c); multiply_matrix(a, c, b); } instr += size; } break; case D3DOP_STATETRANSFORM: TRACE("STATETRANSFORM (%d)\n", count); for (i = 0; i < count; ++i) { D3DSTATE *ci = (D3DSTATE *)instr; D3DMATRIX *m; m = ddraw_get_object(&device->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATRIX); if (!m) { ERR("Invalid matrix handle %#x.\n", ci->u2.dwArg[0]); } else { if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_WORLD) device->world = ci->u2.dwArg[0]; if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_VIEW) device->view = ci->u2.dwArg[0]; if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_PROJECTION) device->proj = ci->u2.dwArg[0]; IDirect3DDevice7_SetTransform(&device->IDirect3DDevice7_iface, ci->u1.dtstTransformStateType, m); } instr += size; } break; case D3DOP_STATELIGHT: TRACE("STATELIGHT (%d)\n", count); for (i = 0; i < count; ++i) { D3DSTATE *ci = (D3DSTATE *)instr; if (FAILED(IDirect3DDevice3_SetLightState(&device->IDirect3DDevice3_iface, ci->u1.dlstLightStateType, ci->u2.dwArg[0]))) WARN("Failed to set light state.\n"); instr += size; } break; case D3DOP_STATERENDER: TRACE("STATERENDER (%d)\n", count); for (i = 0; i < count; ++i) { D3DSTATE *ci = (D3DSTATE *)instr; if (FAILED(IDirect3DDevice3_SetRenderState(&device->IDirect3DDevice3_iface, ci->u1.drstRenderStateType, ci->u2.dwArg[0]))) WARN("Failed to set render state.\n"); instr += size; } break; case D3DOP_PROCESSVERTICES: { /* TODO: Share code with d3d_vertex_buffer7_ProcessVertices() * and / or wined3d_device_process_vertices(). */ D3DMATRIX view_mat, world_mat, proj_mat, mat; TRACE("PROCESSVERTICES (%d)\n", count); /* Get the transform and world matrix */ /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */ wined3d_device_get_transform(device->wined3d_device, D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat); wined3d_device_get_transform(device->wined3d_device, D3DTRANSFORMSTATE_PROJECTION, (struct wined3d_matrix *)&proj_mat); wined3d_device_get_transform(device->wined3d_device, WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat); if (TRACE_ON(ddraw)) { TRACE(" Projection Matrix:\n"); dump_D3DMATRIX(&proj_mat); TRACE(" View Matrix:\n"); dump_D3DMATRIX(&view_mat); TRACE(" World Matrix:\n"); dump_D3DMATRIX(&world_mat); } multiply_matrix(&mat, &view_mat, &world_mat); multiply_matrix(&mat, &proj_mat, &mat); for (i = 0; i < count; ++i) { D3DPROCESSVERTICES *ci = (D3DPROCESSVERTICES *)instr; D3DTLVERTEX *dst = (D3DTLVERTEX *)buffer->vertex_data + ci->wDest; DWORD op = ci->dwFlags & D3DPROCESSVERTICES_OPMASK; TRACE(" start %u, dest %u, count %u, flags %#x.\n", ci->wStart, ci->wDest, ci->dwCount, ci->dwFlags); if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS) FIXME("D3DPROCESSVERTICES_UPDATEEXTENTS not implemented.\n"); if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR) FIXME("D3DPROCESSVERTICES_NOCOLOR not implemented.\n"); switch (op) { case D3DPROCESSVERTICES_TRANSFORMLIGHT: { const D3DVERTEX *src = (D3DVERTEX *)((char *)buffer->desc.lpData + vs) + ci->wStart; unsigned int vtx_idx; static unsigned int once; if (!once++) FIXME("Lighting not implemented.\n"); for (vtx_idx = 0; vtx_idx < ci->dwCount; ++vtx_idx) { transform_vertex(&dst[vtx_idx], &mat, &viewport->viewports.vp1, src[vtx_idx].u1.x, src[vtx_idx].u2.y, src[vtx_idx].u3.z); /* No lighting yet */ dst[vtx_idx].u5.color = 0xffffffff; /* Opaque white */ dst[vtx_idx].u6.specular = 0xff000000; /* No specular and no fog factor */ dst[vtx_idx].u7.tu = src[vtx_idx].u7.tu; dst[vtx_idx].u8.tv = src[vtx_idx].u8.tv; } break; } case D3DPROCESSVERTICES_TRANSFORM: { const D3DLVERTEX *src = (D3DLVERTEX *)((char *)buffer->desc.lpData + vs) + ci->wStart; unsigned int vtx_idx; for (vtx_idx = 0; vtx_idx < ci->dwCount; ++vtx_idx) { transform_vertex(&dst[vtx_idx], &mat, &viewport->viewports.vp1, src[vtx_idx].u1.x, src[vtx_idx].u2.y, src[vtx_idx].u3.z); dst[vtx_idx].u5.color = src[vtx_idx].u4.color; dst[vtx_idx].u6.specular = src[vtx_idx].u5.specular; dst[vtx_idx].u7.tu = src[vtx_idx].u6.tu; dst[vtx_idx].u8.tv = src[vtx_idx].u7.tv; } break; } case D3DPROCESSVERTICES_COPY: { const D3DTLVERTEX *src = (D3DTLVERTEX *)((char *)buffer->desc.lpData + vs) + ci->wStart; memcpy(dst, src, ci->dwCount * sizeof(*dst)); break; } default: FIXME("Unhandled vertex processing op %#x.\n", op); break; } instr += size; } break; } case D3DOP_TEXTURELOAD: { WARN("TEXTURELOAD-s (%d)\n", count); instr += count * size; } break; case D3DOP_EXIT: { TRACE("EXIT (%d)\n", count); /* We did this instruction */ instr += size; /* Exit this loop */ goto end_of_buffer; } break; case D3DOP_BRANCHFORWARD: TRACE("BRANCHFORWARD (%d)\n", count); for (i = 0; i < count; ++i) { D3DBRANCH *ci = (D3DBRANCH *)instr; if ((buffer->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue) { if (!ci->bNegate) { TRACE(" Branch to %d\n", ci->dwOffset); if (ci->dwOffset) { instr = (char*)current + ci->dwOffset; break; } } } else { if (ci->bNegate) { TRACE(" Branch to %d\n", ci->dwOffset); if (ci->dwOffset) { instr = (char*)current + ci->dwOffset; break; } } } instr += size; } break; case D3DOP_SPAN: { WARN("SPAN-s (%d)\n", count); instr += count * size; } break; case D3DOP_SETSTATUS: TRACE("SETSTATUS (%d)\n", count); for (i = 0; i < count; ++i) { buffer->data.dsStatus = *(D3DSTATUS *)instr; instr += size; } break; default: ERR("Unhandled OpCode %d !!!\n",current->bOpcode); /* Try to save ... */ instr += count * size; break; } } end_of_buffer: return D3D_OK; }
static void test_process_vertices(void) { IDirect3DVertexBuffer7 *src_vb, *dst_vb1, *dst_vb2; D3DVERTEXBUFFERDESC vb_desc; IDirect3DDevice7 *device; struct vec4 *dst_data; struct vec3 *dst_data2; struct vec3 *src_data; IDirect3D7 *d3d7; D3DVIEWPORT7 vp; HWND window; HRESULT hr; static D3DMATRIX world = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, }; static D3DMATRIX view = { 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 3.0f, }; static D3DMATRIX proj = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, }; window = CreateWindowA("static", "d3d7_test", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, 0, 0, 0, 0); if (!(device = create_device(window, DDSCL_NORMAL))) { skip("Failed to create a ddraw object, skipping test.\n"); DestroyWindow(window); return; } hr = IDirect3DDevice7_GetDirect3D(device, &d3d7); ok(SUCCEEDED(hr), "Failed to get Direct3D7 interface, hr %#x.\n", hr); memset(&vb_desc, 0, sizeof(vb_desc)); vb_desc.dwSize = sizeof(vb_desc); vb_desc.dwFVF = D3DFVF_XYZ; vb_desc.dwNumVertices = 4; hr = IDirect3D7_CreateVertexBuffer(d3d7, &vb_desc, &src_vb, 0); ok(SUCCEEDED(hr), "Failed to create source vertex buffer, hr %#x.\n", hr); hr = IDirect3DVertexBuffer7_Lock(src_vb, 0, (void **)&src_data, NULL); ok(SUCCEEDED(hr), "Failed to lock source vertex buffer, hr %#x.\n", hr); src_data[0].x = 0.0f; src_data[0].y = 0.0f; src_data[0].z = 0.0f; src_data[1].x = 1.0f; src_data[1].y = 1.0f; src_data[1].z = 1.0f; src_data[2].x = -1.0f; src_data[2].y = -1.0f; src_data[2].z = 0.5f; src_data[3].x = 0.5f; src_data[3].y = -0.5f; src_data[3].z = 0.25f; hr = IDirect3DVertexBuffer7_Unlock(src_vb); ok(SUCCEEDED(hr), "Failed to unlock source vertex buffer, hr %#x.\n", hr); memset(&vb_desc, 0, sizeof(vb_desc)); vb_desc.dwSize = sizeof(vb_desc); vb_desc.dwFVF = D3DFVF_XYZRHW; vb_desc.dwNumVertices = 4; /* MSDN says that the last parameter must be 0 - check that. */ hr = IDirect3D7_CreateVertexBuffer(d3d7, &vb_desc, &dst_vb1, 4); ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr); memset(&vb_desc, 0, sizeof(vb_desc)); vb_desc.dwSize = sizeof(vb_desc); vb_desc.dwFVF = D3DFVF_XYZ; vb_desc.dwNumVertices = 5; /* MSDN says that the last parameter must be 0 - check that. */ hr = IDirect3D7_CreateVertexBuffer(d3d7, &vb_desc, &dst_vb2, 12345678); ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr); memset(&vp, 0, sizeof(vp)); vp.dwX = 64; vp.dwY = 64; vp.dwWidth = 128; vp.dwHeight = 128; vp.dvMinZ = 0.0f; vp.dvMaxZ = 1.0f; hr = IDirect3DDevice7_SetViewport(device, &vp); ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr); hr = IDirect3DVertexBuffer7_ProcessVertices(dst_vb1, D3DVOP_TRANSFORM, 0, 4, src_vb, 0, device, 0); ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr); hr = IDirect3DVertexBuffer7_ProcessVertices(dst_vb2, D3DVOP_TRANSFORM, 0, 4, src_vb, 0, device, 0); ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr); hr = IDirect3DVertexBuffer7_Lock(dst_vb1, 0, (void **)&dst_data, NULL); ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr); ok(compare_vec4(&dst_data[0], +1.280e+2f, +1.280e+2f, +0.000e+0f, +1.000e+0f, 4096), "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w); ok(compare_vec4(&dst_data[1], +1.920e+2f, +6.400e+1f, +1.000e+0f, +1.000e+0f, 4096), "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w); ok(compare_vec4(&dst_data[2], +6.400e+1f, +1.920e+2f, +5.000e-1f, +1.000e+0f, 4096), "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w); ok(compare_vec4(&dst_data[3], +1.600e+2f, +1.600e+2f, +2.500e-1f, +1.000e+0f, 4096), "Got unexpected vertex 3 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[3].x, dst_data[3].y, dst_data[3].z, dst_data[3].w); hr = IDirect3DVertexBuffer7_Unlock(dst_vb1); ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr); hr = IDirect3DVertexBuffer7_Lock(dst_vb2, 0, (void **)&dst_data2, NULL); ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr); /* Small thing without much practical meaning, but I stumbled upon it, * so let's check for it: If the output vertex buffer has no RHW value, * the RHW value of the last vertex is written into the next vertex. */ ok(compare_vec3(&dst_data2[4], +1.000e+0f, +0.000e+0f, +0.000e+0f, 4096), "Got unexpected vertex 4 {%.8e, %.8e, %.8e}.\n", dst_data2[4].x, dst_data2[4].y, dst_data2[4].z); hr = IDirect3DVertexBuffer7_Unlock(dst_vb2); ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr); /* Try a more complicated viewport, same vertices. */ memset(&vp, 0, sizeof(vp)); vp.dwX = 10; vp.dwY = 5; vp.dwWidth = 246; vp.dwHeight = 130; vp.dvMinZ = -2.0f; vp.dvMaxZ = 4.0f; hr = IDirect3DDevice7_SetViewport(device, &vp); ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr); hr = IDirect3DVertexBuffer7_ProcessVertices(dst_vb1, D3DVOP_TRANSFORM, 0, 4, src_vb, 0, device, 0); ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr); hr = IDirect3DVertexBuffer7_Lock(dst_vb1, 0, (void **)&dst_data, NULL); ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr); ok(compare_vec4(&dst_data[0], +1.330e+2f, +7.000e+1f, -2.000e+0f, +1.000e+0f, 4096), "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w); ok(compare_vec4(&dst_data[1], +2.560e+2f, +5.000e+0f, +4.000e+0f, +1.000e+0f, 4096), "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w); ok(compare_vec4(&dst_data[2], +1.000e+1f, +1.350e+2f, +1.000e+0f, +1.000e+0f, 4096), "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w); ok(compare_vec4(&dst_data[3], +1.945e+2f, +1.025e+2f, -5.000e-1f, +1.000e+0f, 4096), "Got unexpected vertex 3 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[3].x, dst_data[3].y, dst_data[3].z, dst_data[3].w); hr = IDirect3DVertexBuffer7_Unlock(dst_vb1); ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr); hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &world); ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr); hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &view); ok(SUCCEEDED(hr), "Failed to set view transform, hr %#x.\n", hr); hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &proj); ok(SUCCEEDED(hr), "Failed to set projection transform, hr %#x.\n", hr); hr = IDirect3DVertexBuffer7_ProcessVertices(dst_vb1, D3DVOP_TRANSFORM, 0, 4, src_vb, 0, device, 0); ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr); hr = IDirect3DVertexBuffer7_Lock(dst_vb1, 0, (void **)&dst_data, NULL); ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr); ok(compare_vec4(&dst_data[0], +2.560e+2f, +7.000e+1f, -2.000e+0f, +3.333e-1f, 4096), "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w); ok(compare_vec4(&dst_data[1], +2.560e+2f, +7.813e+1f, -2.750e+0f, +1.250e-1f, 4096), "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w); ok(compare_vec4(&dst_data[2], +2.560e+2f, +4.400e+1f, +4.000e-1f, +4.000e-1f, 4096), "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w); ok(compare_vec4(&dst_data[3], +2.560e+2f, +8.182e+1f, -3.091e+0f, +3.636e-1f, 4096), "Got unexpected vertex 3 {%.8e, %.8e, %.8e, %.8e}.\n", dst_data[3].x, dst_data[3].y, dst_data[3].z, dst_data[3].w); hr = IDirect3DVertexBuffer7_Unlock(dst_vb1); ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr); IDirect3DVertexBuffer7_Release(dst_vb2); IDirect3DVertexBuffer7_Release(dst_vb1); IDirect3DVertexBuffer7_Release(src_vb); IDirect3D7_Release(d3d7); IDirect3DDevice7_Release(device); DestroyWindow(window); }
HRESULT d3d_execute_buffer_execute(struct d3d_execute_buffer *buffer, struct d3d_device *device, struct d3d_viewport *viewport) { DWORD vs = buffer->data.dwVertexOffset; DWORD is = buffer->data.dwInstructionOffset; char *instr = (char *)buffer->desc.lpData + is; if (viewport->active_device != device) { WARN("Viewport %p active device is %p.\n", viewport, viewport->active_device); return DDERR_INVALIDPARAMS; } /* Activate the viewport */ viewport_activate(viewport, FALSE); TRACE("ExecuteData :\n"); if (TRACE_ON(ddraw)) _dump_executedata(&(buffer->data)); while (1) { LPD3DINSTRUCTION current = (LPD3DINSTRUCTION) instr; BYTE size; WORD count; count = current->wCount; size = current->bSize; instr += sizeof(D3DINSTRUCTION); switch (current->bOpcode) { case D3DOP_POINT: { WARN("POINT-s (%d)\n", count); instr += count * size; } break; case D3DOP_LINE: { WARN("LINE-s (%d)\n", count); instr += count * size; } break; case D3DOP_TRIANGLE: { int i; D3DTLVERTEX *tl_vx = buffer->vertex_data; TRACE("TRIANGLE (%d)\n", count); if (buffer->nb_indices < count * 3) { buffer->nb_indices = count * 3; HeapFree(GetProcessHeap(), 0, buffer->indices); buffer->indices = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->indices) * buffer->nb_indices); } for (i = 0; i < count; i++) { LPD3DTRIANGLE ci = (LPD3DTRIANGLE) instr; TRACE(" v1: %d v2: %d v3: %d\n",ci->u1.v1, ci->u2.v2, ci->u3.v3); TRACE(" Flags : "); if (TRACE_ON(ddraw)) { /* Wireframe */ if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1) TRACE("EDGEENABLE1 "); if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2) TRACE("EDGEENABLE2 "); if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1) TRACE("EDGEENABLE3 "); /* Strips / Fans */ if (ci->wFlags == D3DTRIFLAG_EVEN) TRACE("EVEN "); if (ci->wFlags == D3DTRIFLAG_ODD) TRACE("ODD "); if (ci->wFlags == D3DTRIFLAG_START) TRACE("START "); if ((ci->wFlags > 0) && (ci->wFlags < 30)) TRACE("STARTFLAT(%u) ", ci->wFlags); TRACE("\n"); } buffer->indices[(i * 3) ] = ci->u1.v1; buffer->indices[(i * 3) + 1] = ci->u2.v2; buffer->indices[(i * 3) + 2] = ci->u3.v3; instr += size; } IDirect3DDevice7_DrawIndexedPrimitive(&device->IDirect3DDevice7_iface, D3DPT_TRIANGLELIST, D3DFVF_TLVERTEX, tl_vx, 0, buffer->indices, count * 3, 0); } break; case D3DOP_MATRIXLOAD: WARN("MATRIXLOAD-s (%d)\n", count); instr += count * size; break; case D3DOP_MATRIXMULTIPLY: { int i; TRACE("MATRIXMULTIPLY (%d)\n", count); for (i = 0; i < count; ++i) { D3DMATRIXMULTIPLY *ci = (D3DMATRIXMULTIPLY *)instr; D3DMATRIX *a, *b, *c; a = ddraw_get_object(&device->handle_table, ci->hDestMatrix - 1, DDRAW_HANDLE_MATRIX); b = ddraw_get_object(&device->handle_table, ci->hSrcMatrix1 - 1, DDRAW_HANDLE_MATRIX); c = ddraw_get_object(&device->handle_table, ci->hSrcMatrix2 - 1, DDRAW_HANDLE_MATRIX); if (!a || !b || !c) { ERR("Invalid matrix handle (a %#x -> %p, b %#x -> %p, c %#x -> %p).\n", ci->hDestMatrix, a, ci->hSrcMatrix1, b, ci->hSrcMatrix2, c); } else { TRACE("dst %p, src1 %p, src2 %p.\n", a, b, c); multiply_matrix(a, c, b); } instr += size; } } break; case D3DOP_STATETRANSFORM: { int i; TRACE("STATETRANSFORM (%d)\n", count); for (i = 0; i < count; ++i) { D3DSTATE *ci = (D3DSTATE *)instr; D3DMATRIX *m; m = ddraw_get_object(&device->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATRIX); if (!m) { ERR("Invalid matrix handle %#x.\n", ci->u2.dwArg[0]); } else { if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_WORLD) device->world = ci->u2.dwArg[0]; if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_VIEW) device->view = ci->u2.dwArg[0]; if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_PROJECTION) device->proj = ci->u2.dwArg[0]; IDirect3DDevice7_SetTransform(&device->IDirect3DDevice7_iface, ci->u1.dtstTransformStateType, m); } instr += size; } } break; case D3DOP_STATELIGHT: { int i; TRACE("STATELIGHT (%d)\n", count); for (i = 0; i < count; i++) { LPD3DSTATE ci = (LPD3DSTATE) instr; TRACE("(%08x,%08x)\n", ci->u1.dlstLightStateType, ci->u2.dwArg[0]); if (!ci->u1.dlstLightStateType || (ci->u1.dlstLightStateType > D3DLIGHTSTATE_COLORVERTEX)) ERR("Unexpected Light State Type %d\n", ci->u1.dlstLightStateType); else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */) { struct d3d_material *m; m = ddraw_get_object(&device->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATERIAL); if (!m) ERR("Invalid material handle %#x.\n", ci->u2.dwArg[0]); else material_activate(m); } else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */) { switch (ci->u2.dwArg[0]) { case D3DCOLOR_MONO: ERR("DDCOLOR_MONO should not happen!\n"); break; case D3DCOLOR_RGB: /* We are already in this mode */ break; default: ERR("Unknown color model!\n"); } } else { D3DRENDERSTATETYPE rs = 0; switch (ci->u1.dlstLightStateType) { case D3DLIGHTSTATE_AMBIENT: /* 2 */ rs = D3DRENDERSTATE_AMBIENT; break; case D3DLIGHTSTATE_FOGMODE: /* 4 */ rs = D3DRENDERSTATE_FOGVERTEXMODE; break; case D3DLIGHTSTATE_FOGSTART: /* 5 */ rs = D3DRENDERSTATE_FOGSTART; break; case D3DLIGHTSTATE_FOGEND: /* 6 */ rs = D3DRENDERSTATE_FOGEND; break; case D3DLIGHTSTATE_FOGDENSITY: /* 7 */ rs = D3DRENDERSTATE_FOGDENSITY; break; case D3DLIGHTSTATE_COLORVERTEX: /* 8 */ rs = D3DRENDERSTATE_COLORVERTEX; break; default: break; } IDirect3DDevice7_SetRenderState(&device->IDirect3DDevice7_iface, rs, ci->u2.dwArg[0]); } instr += size; } } break; case D3DOP_STATERENDER: { int i; IDirect3DDevice2 *d3d_device2 = &device->IDirect3DDevice2_iface; TRACE("STATERENDER (%d)\n", count); for (i = 0; i < count; i++) { LPD3DSTATE ci = (LPD3DSTATE) instr; IDirect3DDevice2_SetRenderState(d3d_device2, ci->u1.drstRenderStateType, ci->u2.dwArg[0]); instr += size; } } break; case D3DOP_PROCESSVERTICES: { /* TODO: Share code with IDirect3DVertexBuffer::ProcessVertices and / or * IWineD3DDevice::ProcessVertices */ int i; D3DMATRIX view_mat, world_mat, proj_mat; TRACE("PROCESSVERTICES (%d)\n", count); /* Get the transform and world matrix */ /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */ wined3d_device_get_transform(device->wined3d_device, D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat); wined3d_device_get_transform(device->wined3d_device, D3DTRANSFORMSTATE_PROJECTION, (struct wined3d_matrix *)&proj_mat); wined3d_device_get_transform(device->wined3d_device, WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat); for (i = 0; i < count; i++) { LPD3DPROCESSVERTICES ci = (LPD3DPROCESSVERTICES) instr; TRACE(" Start : %d Dest : %d Count : %d\n", ci->wStart, ci->wDest, ci->dwCount); TRACE(" Flags : "); if (TRACE_ON(ddraw)) { if (ci->dwFlags & D3DPROCESSVERTICES_COPY) TRACE("COPY "); if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR) TRACE("NOCOLOR "); if (ci->dwFlags == D3DPROCESSVERTICES_OPMASK) TRACE("OPMASK "); if (ci->dwFlags & D3DPROCESSVERTICES_TRANSFORM) TRACE("TRANSFORM "); if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT) TRACE("TRANSFORMLIGHT "); if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS) TRACE("UPDATEEXTENTS "); TRACE("\n"); } /* This is where doing Direct3D on top on OpenGL is quite difficult. This method transforms a set of vertices using the CURRENT state (lighting, projection, ...) but does not rasterize them. They will only be put on screen later (with the POINT / LINE and TRIANGLE op-codes). The problem is that you can have a triangle with each point having been transformed using another state... In this implementation, I will emulate only ONE thing : each vertex can have its own "WORLD" transformation (this is used in the TWIST.EXE demo of the 5.2 SDK). I suppose that all vertices of the execute buffer use the same state. If I find applications that change other states, I will try to do a more 'fine-tuned' state emulation (but I may become quite tricky if it changes a light position in the middle of a triangle). In this case, a 'direct' approach (i.e. without using OpenGL, but writing our own 3D rasterizer) would be easier. */ /* The current method (with the hypothesis that only the WORLD matrix will change between two points) is like this : - I transform 'manually' all the vertices with the current WORLD matrix and store them in the vertex buffer - during the rasterization phase, the WORLD matrix will be set to the Identity matrix */ /* Enough for the moment */ if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT) { unsigned int nb; D3DVERTEX *src = ((D3DVERTEX *)((char *)buffer->desc.lpData + vs)) + ci->wStart; D3DTLVERTEX *dst = ((D3DTLVERTEX *)buffer->vertex_data) + ci->wDest; D3DVIEWPORT *Viewport = &viewport->viewports.vp1; D3DMATRIX mat; if (TRACE_ON(ddraw)) { TRACE(" Projection Matrix : (%p)\n", &proj_mat); dump_D3DMATRIX(&proj_mat); TRACE(" View Matrix : (%p)\n", &view_mat); dump_D3DMATRIX(&view_mat); TRACE(" World Matrix : (%p)\n", &world_mat); dump_D3DMATRIX(&world_mat); } multiply_matrix(&mat,&view_mat,&world_mat); multiply_matrix(&mat,&proj_mat,&mat); for (nb = 0; nb < ci->dwCount; nb++) { /* No lighting yet */ dst->u5.color = 0xFFFFFFFF; /* Opaque white */ dst->u6.specular = 0xFF000000; /* No specular and no fog factor */ dst->u7.tu = src->u7.tu; dst->u8.tv = src->u8.tv; /* Now, the matrix multiplication */ dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41); dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42); dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43); dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44); dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dvScaleX + Viewport->dwX + Viewport->dwWidth / 2; dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY + Viewport->dwY + Viewport->dwHeight / 2; dst->u3.sz /= dst->u4.rhw; dst->u4.rhw = 1 / dst->u4.rhw; src++; dst++; } } else if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORM) { unsigned int nb; D3DLVERTEX *src = ((D3DLVERTEX *)((char *)buffer->desc.lpData + vs)) + ci->wStart; D3DTLVERTEX *dst = ((D3DTLVERTEX *)buffer->vertex_data) + ci->wDest; D3DVIEWPORT *Viewport = &viewport->viewports.vp1; D3DMATRIX mat; if (TRACE_ON(ddraw)) { TRACE(" Projection Matrix : (%p)\n", &proj_mat); dump_D3DMATRIX(&proj_mat); TRACE(" View Matrix : (%p)\n",&view_mat); dump_D3DMATRIX(&view_mat); TRACE(" World Matrix : (%p)\n", &world_mat); dump_D3DMATRIX(&world_mat); } multiply_matrix(&mat,&view_mat,&world_mat); multiply_matrix(&mat,&proj_mat,&mat); for (nb = 0; nb < ci->dwCount; nb++) { dst->u5.color = src->u4.color; dst->u6.specular = src->u5.specular; dst->u7.tu = src->u6.tu; dst->u8.tv = src->u7.tv; /* Now, the matrix multiplication */ dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41); dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42); dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43); dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44); dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dvScaleX + Viewport->dwX + Viewport->dwWidth / 2; dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY + Viewport->dwY + Viewport->dwHeight / 2; dst->u3.sz /= dst->u4.rhw; dst->u4.rhw = 1 / dst->u4.rhw; src++; dst++; } } else if (ci->dwFlags == D3DPROCESSVERTICES_COPY) { D3DTLVERTEX *src = ((D3DTLVERTEX *)((char *)buffer->desc.lpData + vs)) + ci->wStart; D3DTLVERTEX *dst = ((D3DTLVERTEX *)buffer->vertex_data) + ci->wDest; memcpy(dst, src, ci->dwCount * sizeof(D3DTLVERTEX)); } else { ERR("Unhandled vertex processing flag %#x.\n", ci->dwFlags); } instr += size; } } break; case D3DOP_TEXTURELOAD: { WARN("TEXTURELOAD-s (%d)\n", count); instr += count * size; } break; case D3DOP_EXIT: { TRACE("EXIT (%d)\n", count); /* We did this instruction */ instr += size; /* Exit this loop */ goto end_of_buffer; } break; case D3DOP_BRANCHFORWARD: { int i; TRACE("BRANCHFORWARD (%d)\n", count); for (i = 0; i < count; i++) { LPD3DBRANCH ci = (LPD3DBRANCH) instr; if ((buffer->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue) { if (!ci->bNegate) { TRACE(" Branch to %d\n", ci->dwOffset); if (ci->dwOffset) { instr = (char*)current + ci->dwOffset; break; } } } else { if (ci->bNegate) { TRACE(" Branch to %d\n", ci->dwOffset); if (ci->dwOffset) { instr = (char*)current + ci->dwOffset; break; } } } instr += size; } } break; case D3DOP_SPAN: { WARN("SPAN-s (%d)\n", count); instr += count * size; } break; case D3DOP_SETSTATUS: { int i; TRACE("SETSTATUS (%d)\n", count); for (i = 0; i < count; i++) { LPD3DSTATUS ci = (LPD3DSTATUS) instr; buffer->data.dsStatus = *ci; instr += size; } } break; default: ERR("Unhandled OpCode %d !!!\n",current->bOpcode); /* Try to save ... */ instr += count * size; break; } } end_of_buffer: return D3D_OK; }
BOOL gldInitialiseMesa_DX( DGL_ctx *lpCtx) { GLD_driver_dx7 *gld = NULL; int MaxTextureSize, TextureLevels; BOOL bSoftwareTnL; if (lpCtx == NULL) return FALSE; gld = lpCtx->glPriv; if (gld == NULL) return FALSE; if (glb.bMultitexture) { lpCtx->glCtx->Const.MaxTextureUnits = gld->d3dCaps.wMaxSimultaneousTextures; // Only support MAX_TEXTURE_UNITS texture units. // ** If this is altered then the FVF formats must be reviewed **. if (lpCtx->glCtx->Const.MaxTextureUnits > GLD_MAX_TEXTURE_UNITS_DX7) lpCtx->glCtx->Const.MaxTextureUnits = GLD_MAX_TEXTURE_UNITS_DX7; } else { // Multitexture override lpCtx->glCtx->Const.MaxTextureUnits = 1; } lpCtx->glCtx->Const.MaxDrawBuffers = 1; // max texture size // MaxTextureSize = min(gld->d3dCaps8.MaxTextureHeight, gld->d3dCaps8.MaxTextureWidth); MaxTextureSize = min(gld->d3dCaps.dwMaxTextureHeight, gld->d3dCaps.dwMaxTextureWidth); if (MaxTextureSize == 0) MaxTextureSize = 256; // Sanity check // // HACK!! if (MaxTextureSize > 1024) MaxTextureSize = 1024; // HACK - CLAMP TO 1024 // HACK!! // // TODO: Check this again for Mesa 5 // Got to set MAX_TEXTURE_SIZE as max levels. // Who thought this stupid idea up? ;) TextureLevels = 0; // Calculate power-of-two. while (MaxTextureSize) { TextureLevels++; MaxTextureSize >>= 1; } lpCtx->glCtx->Const.MaxTextureLevels = (TextureLevels) ? TextureLevels : 8; // Defaults IDirect3DDevice7_SetRenderState(gld->pDev, D3DRENDERSTATE_LIGHTING, FALSE); IDirect3DDevice7_SetRenderState(gld->pDev, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE); IDirect3DDevice7_SetRenderState(gld->pDev, D3DRENDERSTATE_DITHERENABLE, TRUE); IDirect3DDevice7_SetRenderState(gld->pDev, D3DRENDERSTATE_SHADEMODE, D3DSHADE_GOURAUD); // Set texture coord set to be used with each stage IDirect3DDevice7_SetTextureStageState(gld->pDev, 0, D3DTSS_TEXCOORDINDEX, 0); IDirect3DDevice7_SetTextureStageState(gld->pDev, 1, D3DTSS_TEXCOORDINDEX, 1); // Set up Depth buffer IDirect3DDevice7_SetRenderState(gld->pDev, D3DRENDERSTATE_ZENABLE, (lpCtx->lpPF->dwDriverData!=D3DX_SF_UNKNOWN) ? D3DZB_TRUE : D3DZB_FALSE); // Set the view matrix { D3DXMATRIX vm; #if 1 D3DXMatrixIdentity(&vm); #else D3DXVECTOR3 Eye(0.0f, 0.0f, 0.0f); D3DXVECTOR3 At(0.0f, 0.0f, -1.0f); D3DXVECTOR3 Up(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtRH(&vm, &Eye, &At, &Up); vm._31 = -vm._31; vm._32 = -vm._32; vm._33 = -vm._33; vm._34 = -vm._34; #endif IDirect3DDevice7_SetTransform(gld->pDev, D3DTRANSFORMSTATE_VIEW, &vm); } // DX7 does not support D3DRS_SOFTWAREVERTEXPROCESSING /* if (gld->bHasHWTnL) { if (glb.dwTnL == GLDS_TNL_DEFAULT) bSoftwareTnL = FALSE; // HW TnL else { bSoftwareTnL = ((glb.dwTnL == GLDS_TNL_MESA) || (glb.dwTnL == GLDS_TNL_D3DSW)) ? TRUE : FALSE; } } else { // No HW TnL, so no choice possible bSoftwareTnL = TRUE; } IDirect3DDevice8_SetRenderState(gld->pDev, D3DRS_SOFTWAREVERTEXPROCESSING, bSoftwareTnL); */ // Dump this in a Release build as well, now. //#ifdef _DEBUG ddlogPrintf(DDLOG_INFO, "HW TnL: %s", // gld->bHasHWTnL ? (bSoftwareTnL ? "Disabled" : "Enabled") : "Unavailable"); gld->bHasHWTnL ? "Enabled" : "Unavailable"); //#endif // Set up interfaces to Mesa gldEnableExtensions_DX7(lpCtx->glCtx); gldInstallPipeline_DX7(lpCtx->glCtx); gldSetupDriverPointers_DX7(lpCtx->glCtx); // Signal a complete state update lpCtx->glCtx->Driver.UpdateState(lpCtx->glCtx, _NEW_ALL); // Start a scene IDirect3DDevice7_BeginScene(gld->pDev); lpCtx->bSceneStarted = TRUE; return TRUE; }