void SVGA3DUtil_InitFullscreen(uint32 cid, // IN uint32 width, // IN uint32 height) // IN { SVGA3dRenderState *rs; gFullscreen.screen.x = 0; gFullscreen.screen.y = 0; gFullscreen.screen.w = width; gFullscreen.screen.h = height; Intr_Init(); Intr_SetFaultHandlers(SVGA_DefaultFaultHandler); SVGA_Init(); SVGA_SetMode(width, height, 32); VMBackdoor_MouseInit(TRUE); SVGA3D_Init(); gFullscreen.colorImage.sid = SVGA3DUtil_DefineSurface2D(width, height, SVGA3D_X8R8G8B8); gFullscreen.depthImage.sid = SVGA3DUtil_DefineSurface2D(width, height, SVGA3D_Z_D16); SVGA3D_DefineContext(cid); SVGA3D_SetRenderTarget(cid, SVGA3D_RT_COLOR0, &gFullscreen.colorImage); SVGA3D_SetRenderTarget(cid, SVGA3D_RT_DEPTH, &gFullscreen.depthImage); SVGA3D_SetViewport(cid, &gFullscreen.screen); SVGA3D_SetZRange(cid, 0.0f, 1.0f); /* * The device defaults to flat shading, but to retain compatibility * across OpenGL and Direct3D it may be much slower in this * mode. Usually we don't want flat shading, so go ahead and switch * into smooth shading mode. * * Note that this is a per-context render state. * * XXX: There is also a bug in VMware Workstation 6.5.2 which shows * up if you're in flat shading mode and you're using a drawing * command which does not include an SVGA3dVertexDivisor array. * Avoiding flat shading is one workaround, another is to include * a dummy SVGA3dVertexDivisor array on every draw. */ SVGA3D_BeginSetRenderState(cid, &rs, 1); { rs[0].state = SVGA3D_RS_SHADEMODE; rs[0].uintValue = SVGA3D_SHADEMODE_SMOOTH; } SVGA_FIFOCommitAll(); }
static enum pipe_error emit_viewport( struct svga_context *svga, unsigned dirty ) { const struct pipe_viewport_state *viewport = &svga->curr.viewport; struct svga_prescale prescale; SVGA3dRect rect; /* Not sure if this state is relevant with POSITIONT. Probably * not, but setting to 0,1 avoids some state pingponging. */ float range_min = 0.0; float range_max = 1.0; float flip = -1.0; boolean degenerate = FALSE; boolean invertY = FALSE; enum pipe_error ret; float fb_width = svga->curr.framebuffer.width; float fb_height = svga->curr.framebuffer.height; float fx = viewport->scale[0] * -1.0 + viewport->translate[0]; float fy = flip * viewport->scale[1] * -1.0 + viewport->translate[1]; float fw = viewport->scale[0] * 2; float fh = flip * viewport->scale[1] * 2; memset( &prescale, 0, sizeof(prescale) ); /* Examine gallium viewport transformation and produce a screen * rectangle and possibly vertex shader pre-transformation to * get the same results. */ SVGA_DBG(DEBUG_VIEWPORT, "\ninitial %f,%f %fx%f\n", fx, fy, fw, fh); prescale.scale[0] = 1.0; prescale.scale[1] = 1.0; prescale.scale[2] = 1.0; prescale.scale[3] = 1.0; prescale.translate[0] = 0; prescale.translate[1] = 0; prescale.translate[2] = 0; prescale.translate[3] = 0; prescale.enabled = TRUE; if (fw < 0) { prescale.scale[0] *= -1.0; prescale.translate[0] += -fw; fw = -fw; fx = viewport->scale[0] * 1.0 + viewport->translate[0]; } if (fh < 0.0) { prescale.translate[1] = fh - 1 + fy * 2; fh = -fh; fy -= fh; prescale.scale[1] = -1.0; invertY = TRUE; } if (fx < 0) { prescale.translate[0] += fx; prescale.scale[0] *= fw / (fw + fx); fw += fx; fx = 0; } if (fy < 0) { if (invertY) { prescale.translate[1] -= fy; } else { prescale.translate[1] += fy; } prescale.scale[1] *= fh / (fh + fy); fh += fy; fy = 0; } if (fx + fw > fb_width) { prescale.scale[0] *= fw / (fb_width - fx); prescale.translate[0] -= fx * (fw / (fb_width - fx)); prescale.translate[0] += fx; fw = fb_width - fx; } if (fy + fh > fb_height) { prescale.scale[1] *= fh / (fb_height - fy); if (invertY) { float in = fb_height - fy; /* number of vp pixels inside view */ float out = fy + fh - fb_height; /* number of vp pixels out of view */ prescale.translate[1] += fy * out / in; } else { prescale.translate[1] -= fy * (fh / (fb_height - fy)); prescale.translate[1] += fy; } fh = fb_height - fy; } if (fw < 0 || fh < 0) { fw = fh = fx = fy = 0; degenerate = TRUE; goto out; } /* D3D viewport is integer space. Convert fx,fy,etc. to * integers. * * TODO: adjust pretranslate correct for any subpixel error * introduced converting to integers. */ rect.x = fx; rect.y = fy; rect.w = fw; rect.h = fh; SVGA_DBG(DEBUG_VIEWPORT, "viewport error %f,%f %fx%f\n", fabs((float)rect.x - fx), fabs((float)rect.y - fy), fabs((float)rect.w - fw), fabs((float)rect.h - fh)); SVGA_DBG(DEBUG_VIEWPORT, "viewport %d,%d %dx%d\n", rect.x, rect.y, rect.w, rect.h); /* Finally, to get GL rasterization rules, need to tweak the * screen-space coordinates slightly relative to D3D which is * what hardware implements natively. */ if (svga->curr.rast->templ.gl_rasterization_rules) { float adjust_x = 0.0; float adjust_y = 0.0; switch (svga->curr.reduced_prim) { case PIPE_PRIM_LINES: adjust_x = -0.5; adjust_y = 0; break; case PIPE_PRIM_POINTS: case PIPE_PRIM_TRIANGLES: adjust_x = -0.5; adjust_y = -0.5; break; } if (invertY) adjust_y = -adjust_y; prescale.translate[0] += adjust_x; prescale.translate[1] += adjust_y; prescale.translate[2] = 0.5; /* D3D clip space */ prescale.scale[2] = 0.5; /* D3D clip space */ } range_min = viewport->scale[2] * -1.0 + viewport->translate[2]; range_max = viewport->scale[2] * 1.0 + viewport->translate[2]; /* D3D (and by implication SVGA) doesn't like dealing with zmax * less than zmin. Detect that case, flip the depth range and * invert our z-scale factor to achieve the same effect. */ if (range_min > range_max) { float range_tmp; range_tmp = range_min; range_min = range_max; range_max = range_tmp; prescale.scale[2] = -prescale.scale[2]; } if (prescale.enabled) { float H[2]; float J[2]; int i; SVGA_DBG(DEBUG_VIEWPORT, "prescale %f,%f %fx%f\n", prescale.translate[0], prescale.translate[1], prescale.scale[0], prescale.scale[1]); H[0] = (float)rect.w / 2.0; H[1] = -(float)rect.h / 2.0; J[0] = (float)rect.x + (float)rect.w / 2.0; J[1] = (float)rect.y + (float)rect.h / 2.0; SVGA_DBG(DEBUG_VIEWPORT, "H %f,%f\n" "J %fx%f\n", H[0], H[1], J[0], J[1]); /* Adjust prescale to take into account the fact that it is * going to be applied prior to the perspective divide and * viewport transformation. * * Vwin = H(Vc/Vc.w) + J * * We want to tweak Vwin with scale and translation from above, * as in: * * Vwin' = S Vwin + T * * But we can only modify the values at Vc. Plugging all the * above together, and rearranging, eventually we get: * * Vwin' = H(Vc'/Vc'.w) + J * where: * Vc' = SVc + KVc.w * K = (T + (S-1)J) / H * * Overwrite prescale.translate with values for K: */ for (i = 0; i < 2; i++) { prescale.translate[i] = ((prescale.translate[i] + (prescale.scale[i] - 1.0) * J[i]) / H[i]); } SVGA_DBG(DEBUG_VIEWPORT, "clipspace %f,%f %fx%f\n", prescale.translate[0], prescale.translate[1], prescale.scale[0], prescale.scale[1]); } out: if (degenerate) { rect.x = 0; rect.y = 0; rect.w = 1; rect.h = 1; prescale.enabled = FALSE; } if (memcmp(&rect, &svga->state.hw_clear.viewport, sizeof(rect)) != 0) { ret = SVGA3D_SetViewport(svga->swc, &rect); if(ret != PIPE_OK) return ret; memcpy(&svga->state.hw_clear.viewport, &rect, sizeof(rect)); assert(sizeof(rect) == sizeof(svga->state.hw_clear.viewport)); } if (svga->state.hw_clear.depthrange.zmin != range_min || svga->state.hw_clear.depthrange.zmax != range_max) { ret = SVGA3D_SetZRange(svga->swc, range_min, range_max ); if(ret != PIPE_OK) return ret; svga->state.hw_clear.depthrange.zmin = range_min; svga->state.hw_clear.depthrange.zmax = range_max; } if (memcmp(&prescale, &svga->state.hw_clear.prescale, sizeof prescale) != 0) { svga->dirty |= SVGA_NEW_PRESCALE; svga->state.hw_clear.prescale = prescale; } return PIPE_OK; }
static void drawCube(void) { static float angle = 0.5f; SVGA3dRect *rect; Matrix perspectiveMat; SVGA3dTextureState *ts; SVGA3dRenderState *rs; SVGA3dRect viewport = { 0, 0, surfWidth, surfHeight }; SVGA3D_SetRenderTarget(CID, SVGA3D_RT_COLOR0, &colorImage); SVGA3D_SetRenderTarget(CID, SVGA3D_RT_DEPTH, &depthImage); SVGA3D_SetViewport(CID, &viewport); SVGA3D_SetZRange(CID, 0.0f, 1.0f); SVGA3D_BeginSetRenderState(CID, &rs, 5); { rs[0].state = SVGA3D_RS_BLENDENABLE; rs[0].uintValue = FALSE; rs[1].state = SVGA3D_RS_ZENABLE; rs[1].uintValue = TRUE; rs[2].state = SVGA3D_RS_ZWRITEENABLE; rs[2].uintValue = TRUE; rs[3].state = SVGA3D_RS_ZFUNC; rs[3].uintValue = SVGA3D_CMP_LESS; rs[4].state = SVGA3D_RS_LIGHTINGENABLE; rs[4].uintValue = FALSE; } SVGA_FIFOCommitAll(); SVGA3D_BeginSetTextureState(CID, &ts, 4); { ts[0].stage = 0; ts[0].name = SVGA3D_TS_BIND_TEXTURE; ts[0].value = SVGA3D_INVALID_ID; ts[1].stage = 0; ts[1].name = SVGA3D_TS_COLOROP; ts[1].value = SVGA3D_TC_SELECTARG1; ts[2].stage = 0; ts[2].name = SVGA3D_TS_COLORARG1; ts[2].value = SVGA3D_TA_DIFFUSE; ts[3].stage = 0; ts[3].name = SVGA3D_TS_ALPHAARG1; ts[3].value = SVGA3D_TA_DIFFUSE; } SVGA_FIFOCommitAll(); /* * Draw a red border around the render target, to test edge * accuracy in Present. */ SVGA3D_BeginClear(CID, SVGA3D_CLEAR_COLOR | SVGA3D_CLEAR_DEPTH, 0xFF0000, 1.0f, 0, &rect, 1); *rect = viewport; SVGA_FIFOCommitAll(); /* * Draw the background color */ SVGA3D_BeginClear(CID, SVGA3D_CLEAR_COLOR | SVGA3D_CLEAR_DEPTH, 0x336699, 1.0f, 0, &rect, 1); rect->x = viewport.x + 1; rect->y = viewport.y + 1; rect->w = viewport.w - 2; rect->h = viewport.h - 2; SVGA_FIFOCommitAll(); SVGA3dVertexDecl *decls; SVGA3dPrimitiveRange *ranges; Matrix view; Matrix_Copy(view, gIdentityMatrix); Matrix_Scale(view, 0.5, 0.5, 0.5, 1.0); Matrix_RotateX(view, 30.0 * M_PI / 180.0); Matrix_RotateY(view, angle); Matrix_Translate(view, 0, 0, 2.2); angle += 0.02; Matrix_Perspective(perspectiveMat, 45.0f, 4.0f / 3.0f, 0.1f, 100.0f); SVGA3D_SetTransform(CID, SVGA3D_TRANSFORM_WORLD, gIdentityMatrix); SVGA3D_SetTransform(CID, SVGA3D_TRANSFORM_PROJECTION, perspectiveMat); SVGA3D_SetTransform(CID, SVGA3D_TRANSFORM_VIEW, view); SVGA3D_BeginDrawPrimitives(CID, &decls, 2, &ranges, 1); { decls[0].identity.type = SVGA3D_DECLTYPE_FLOAT3; decls[0].identity.usage = SVGA3D_DECLUSAGE_POSITION; decls[0].array.surfaceId = vertexSid; decls[0].array.stride = sizeof(MyVertex); decls[0].array.offset = offsetof(MyVertex, position); decls[1].identity.type = SVGA3D_DECLTYPE_D3DCOLOR; decls[1].identity.usage = SVGA3D_DECLUSAGE_COLOR; decls[1].array.surfaceId = vertexSid; decls[1].array.stride = sizeof(MyVertex); decls[1].array.offset = offsetof(MyVertex, color); ranges[0].primType = SVGA3D_PRIMITIVE_LINELIST; ranges[0].primitiveCount = numLines; ranges[0].indexArray.surfaceId = indexSid; ranges[0].indexArray.stride = sizeof(uint16); ranges[0].indexWidth = sizeof(uint16); } SVGA_FIFOCommitAll(); }