Path: blob/21.2-virgl/src/gallium/frontends/d3d10umd/Rasterizer.cpp
4565 views
/**************************************************************************1*2* Copyright 2012-2021 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,17* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19* USE OR OTHER DEALINGS IN THE SOFTWARE.20*21* The above copyright notice and this permission notice (including the22* next paragraph) shall be included in all copies or substantial portions23* of the Software.24*25**************************************************************************/2627/*28* Rasterizer.cpp --29* Functions that manipulate rasterizer state.30*/313233#include "Rasterizer.h"34#include "State.h"3536#include "Debug.h"373839/*40* ----------------------------------------------------------------------41*42* SetViewports --43*44* The SetViewports function sets viewports.45*46* ----------------------------------------------------------------------47*/4849void APIENTRY50SetViewports(D3D10DDI_HDEVICE hDevice, // IN51UINT NumViewports, // IN52UINT ClearViewports, // IN53__in_ecount (NumViewports) const D3D10_DDI_VIEWPORT *pViewports) // IN54{55LOG_ENTRYPOINT();5657struct pipe_context *pipe = CastPipeContext(hDevice);58struct pipe_viewport_state states[PIPE_MAX_VIEWPORTS];5960ASSERT(NumViewports + ClearViewports <=61D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE);6263for (UINT i = 0; i < NumViewports; ++i) {64const D3D10_DDI_VIEWPORT *pViewport = &pViewports[i];65float width = pViewport->Width;66float height = pViewport->Height;67float x = pViewport->TopLeftX;68float y = pViewport->TopLeftY;69float z = pViewport->MinDepth;70float half_width = width / 2.0f;71float half_height = height / 2.0f;72float depth = pViewport->MaxDepth - z;7374states[i].scale[0] = half_width;75states[i].scale[1] = -half_height;76states[i].scale[2] = depth;7778states[i].translate[0] = half_width + x;79states[i].translate[1] = half_height + y;80states[i].translate[2] = z;81}82if (ClearViewports) {83memset(states + NumViewports, 0,84sizeof(struct pipe_viewport_state) * ClearViewports);85}86pipe->set_viewport_states(pipe, 0, NumViewports + ClearViewports,87states);88}899091/*92* ----------------------------------------------------------------------93*94* SetScissorRects --95*96* The SetScissorRects function marks portions of render targets97* that rendering is confined to.98*99* ----------------------------------------------------------------------100*/101102void APIENTRY103SetScissorRects(D3D10DDI_HDEVICE hDevice, // IN104UINT NumScissorRects, // IN105UINT ClearScissorRects, // IN106__in_ecount (NumRects) const D3D10_DDI_RECT *pRects) // IN107{108LOG_ENTRYPOINT();109110struct pipe_context *pipe = CastPipeContext(hDevice);111struct pipe_scissor_state states[PIPE_MAX_VIEWPORTS];112113ASSERT(NumScissorRects + ClearScissorRects <=114D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE);115116for (UINT i = 0; i < NumScissorRects; ++i) {117const D3D10_DDI_RECT *pRect = &pRects[i];118/* gallium scissor values are unsigned so lets make119* sure that we don't overflow */120states[i].minx = pRect->left < 0 ? 0 : pRect->left;121states[i].miny = pRect->top < 0 ? 0 : pRect->top;122states[i].maxx = pRect->right < 0 ? 0 : pRect->right;123states[i].maxy = pRect->bottom < 0 ? 0 : pRect->bottom;124}125if (ClearScissorRects) {126memset(states + NumScissorRects, 0,127sizeof(struct pipe_scissor_state) * ClearScissorRects);128}129pipe->set_scissor_states(pipe, 0, NumScissorRects + ClearScissorRects,130states);131}132133134/*135* ----------------------------------------------------------------------136*137* CalcPrivateRasterizerStateSize --138*139* The CalcPrivateRasterizerStateSize function determines the size140* of the user-mode display driver's private region of memory141* (that is, the size of internal driver structures, not the size142* of the resource video memory) for a rasterizer state.143*144* ----------------------------------------------------------------------145*/146147SIZE_T APIENTRY148CalcPrivateRasterizerStateSize(149D3D10DDI_HDEVICE hDevice, // IN150__in const D3D10_DDI_RASTERIZER_DESC *pRasterizerDesc) // IN151{152return sizeof(RasterizerState);153}154155156static uint157translate_cull_mode(D3D10_DDI_CULL_MODE CullMode)158{159switch (CullMode) {160case D3D10_DDI_CULL_NONE:161return PIPE_FACE_NONE;162case D3D10_DDI_CULL_FRONT:163return PIPE_FACE_FRONT;164case D3D10_DDI_CULL_BACK:165return PIPE_FACE_BACK;166default:167assert(0);168return PIPE_FACE_NONE;169}170}171172static uint173translate_fill_mode(D3D10_DDI_FILL_MODE FillMode)174{175switch (FillMode) {176case D3D10_DDI_FILL_WIREFRAME:177return PIPE_POLYGON_MODE_LINE;178case D3D10_DDI_FILL_SOLID:179return PIPE_POLYGON_MODE_FILL;180default:181assert(0);182return PIPE_POLYGON_MODE_FILL;183}184}185186187/*188* ----------------------------------------------------------------------189*190* CreateRasterizerState --191*192* The CreateRasterizerState function creates a rasterizer state.193*194* ----------------------------------------------------------------------195*/196197void APIENTRY198CreateRasterizerState(199D3D10DDI_HDEVICE hDevice, // IN200__in const D3D10_DDI_RASTERIZER_DESC *pRasterizerDesc, // IN201D3D10DDI_HRASTERIZERSTATE hRasterizerState, // IN202D3D10DDI_HRTRASTERIZERSTATE hRTRasterizerState) // IN203{204LOG_ENTRYPOINT();205206struct pipe_context *pipe = CastPipeContext(hDevice);207RasterizerState *pRasterizerState = CastRasterizerState(hRasterizerState);208209struct pipe_rasterizer_state state;210memset(&state, 0, sizeof state);211212state.flatshade_first = 1;213state.front_ccw = (pRasterizerDesc->FrontCounterClockwise ? 1 : 0);214state.cull_face = translate_cull_mode(pRasterizerDesc->CullMode);215state.fill_front = translate_fill_mode(pRasterizerDesc->FillMode);216state.fill_back = state.fill_front;217state.scissor = (pRasterizerDesc->ScissorEnable ? 1 : 0);218state.line_smooth = (pRasterizerDesc->AntialiasedLineEnable ? 1 : 0);219state.offset_units = (float)pRasterizerDesc->DepthBias;220state.offset_scale = pRasterizerDesc->SlopeScaledDepthBias;221state.offset_clamp = pRasterizerDesc->DepthBiasClamp;222state.multisample = /* pRasterizerDesc->MultisampleEnable */ 0;223state.half_pixel_center = 1;224state.bottom_edge_rule = 0;225state.clip_halfz = 1;226state.depth_clip_near = pRasterizerDesc->DepthClipEnable ? 1 : 0;227state.depth_clip_far = pRasterizerDesc->DepthClipEnable ? 1 : 0;228229state.point_quad_rasterization = 1;230state.point_size = 1.0f;231state.point_tri_clip = 1;232233state.line_width = 1.0f;234state.line_rectangular = 0;235236pRasterizerState->handle = pipe->create_rasterizer_state(pipe, &state);237}238239240/*241* ----------------------------------------------------------------------242*243* DestroyRasterizerState --244*245* The DestroyRasterizerState function destroys the specified246* rasterizer state object. The rasterizer state object can be247* destoyed only if it is not currently bound to a display device.248*249* ----------------------------------------------------------------------250*/251252void APIENTRY253DestroyRasterizerState(D3D10DDI_HDEVICE hDevice, // IN254D3D10DDI_HRASTERIZERSTATE hRasterizerState) // IN255{256LOG_ENTRYPOINT();257258struct pipe_context *pipe = CastPipeContext(hDevice);259RasterizerState *pRasterizerState = CastRasterizerState(hRasterizerState);260261pipe->delete_rasterizer_state(pipe, pRasterizerState->handle);262}263264265/*266* ----------------------------------------------------------------------267*268* SetRasterizerState --269*270* The SetRasterizerState function sets the rasterizer state.271*272* ----------------------------------------------------------------------273*/274275void APIENTRY276SetRasterizerState(D3D10DDI_HDEVICE hDevice, // IN277D3D10DDI_HRASTERIZERSTATE hRasterizerState) // IN278{279LOG_ENTRYPOINT();280281struct pipe_context *pipe = CastPipeContext(hDevice);282void *state = CastPipeRasterizerState(hRasterizerState);283284pipe->bind_rasterizer_state(pipe, state);285}286287288