260 likes | 405 Views
Computer Graphics SS 2014 Fragment Processing. Rüdiger Westermann Lehrstuhl für Computer Graphik und Visualisierung. Rasterization. normalized device. view. object. h omogeneous clip. window. v e r t e x. Modelview Matrix. Projection Matrix. Perspective Division. Viewport
E N D
Computer Graphics SS 2014 Fragment Processing Rüdiger Westermann Lehrstuhl für Computer Graphik und Visualisierung
Rasterization normalized device view object homogeneousclip window v e r t e x Modelview Matrix Projection Matrix Perspective Division Viewport Transform transformedvertices Rasterization Fragment Generation fragments Green: in thevertexshaderstage Orange: in therasterizerstage
Rasterization • Fragment generation: foreachcoveredpixel, onefragmentisgenerated • Foreachfragment: per-vertex attributes (color, normal, z-value, texturecoordinates,…) areinterpolated via barycentricinterpolation
Rasterization • Fragment generationbytherasterizer // therasterizertestseverygeneratedfragment // rejectsthose not insidethescreenrectangle rasterize(vertexv[3]) { intx, y; bboxb; getBsquare(v, &b); // calculatesthetrianglebounding box in screenspace for(y= b.ymin; y < b.ymax; y++) for(x= b.xmin; x < b.xmax; x++) If(inside(v, x, y) && insideScreenRectangle(x,y)) fragment(x,y); // generatefragementandinterpolate per-vertex attribs }
Rasterization • Resultofrasterizationstage:A setoffragments, eachstoringitspixelcoordinateaswellasinterpolated z-value, color, normal, texturecoordinate etc. A fragmentis in fact a surfacepointseenthroughtherespectivepixel X,Y z RGB u,v …
Rasterization • Why do wekeepthe z-valueforeachfragment? • The z-valuegivesthedistanceofthepointfromthenearplane; itisused in thefragmentprocessingstagetoperformthedepth-test • Dept-test: when multiple surfacepoints, i.e. fragments fall intothe same pixel, onlytheoneclosesttothenear plane iskept thisisperformed in thefragmentstage
Fragment processing • In therasterizationbasedpipeline, multiple buffersareusedduringrendering • Color (pixel) buffer: storesforeachpixel a RGBcolor, where istheopacity • Depthbuffer: storesforeachpixelthedepthoftheclosestsurfacepointrenderedintothispixel so far • Stencilbuffer: storesforeachpixel a bit-maskwhichcanbeusedtoprohibitrenderingintothispixel
Fragment processing • Fragments undergothepixelshaderstagebeforetheyarewrittenintothepixels • Note thatthefragment‘spixelpositionisfixedandcannotbemodified float4 PixelShader(PixelInputTypeinput) : SV_TARGET { float4 textureColor; float4 finalColor; // Sample thetextureusingthetexturecoordinate textureColor = shaderTexture.Sample(SampleType, input.tex); // Combine thetexturecolorandthefragmentcolor finalColor = textureColor * input.color; returnfinalColor; }
Fragment processing • When multiple trianglescoverthe same fragment, howistakencarethatonlytheoneclosesttotheviewerisseen? 4 1 3 2 Keep in mindthatobjectsarerendered in arbitraryorder
Fragment processing • Before a fragmentiswrittentothecolorbuffer, itundergoesthedethtest: thefragment‘sdepthvalueistestedagainstthedepthvalue in thedepthbuffer • The depthbufferisinitializedwiththehighestpossiblevalue • Whenever a fragmentwritesit‘scolorintothecolorbuffer, itwritesit‘sdepthvaluetothe same location in thedepthbuffer • A fragmentisallowedtowriteit‘scolorvalueonlyifitpassesthedepthtest Color buffer Depthbuffer
Fragment processing • Duringrenderingthedepthbufferstoresforeachpixeltheminimumdepthvalueof all fragmentswhichwerefallingintothepixel • When multiple surfacepoints fall intothe same pixel, onlytheoneclosesttothenear plane isstored DepthBuffer + New Triangle DepthBuffer + New Triangle DepthBuffer
Fragment processing • The depthtest: everyincomingfragment (src) testsitsdepthvalueagainstthedepthvalue in thedepthbuffer (dst) • When multiple objects fall intothe same pixel, onlytheonewiththesmallestdepthvaluesurvives
Fragment processing • The depthtestisenabled via API callsandthenperformedautomatically float4 ParticlePixelShader(PixelInputTypeinput) : SV_TARGET { float4 textureColor; float4 finalColor; // Sample thetextureusingthetexturecoordinate textureColor = shaderTexture.Sample(SampleType, input.tex); // Combine thetexturecolorandthefragmentcolor finalColor = textureColor * input.color; returnfinalColor; }
Fragment processing • Depthtest • Itisenabled via API calls (e.g. DirectX) andperformedautomatically // Forinstance, in DirectX11 (first, a depthbufferhastobeacquired)// Sets thedescriptionofthestencilstatetocontrolthedepthtest // enabledepthtesting depthStencilDesc.DepthEnable= true; // enablewritingtothedepthbuffer depthStencilDesc.DepthWriteMask= D3D11_DEPTH_WRITE_MASK_ALL; // selectthetestthatisperformed depthStencilDesc.DepthFunc= D3D11_COMPARISON_LESS;
Fragment processing • Blendingtellsushowtheincomingfragmentcolor(src) iscombinedwiththecoloralready in thecolorbuffer (dst) • Different blendfunctionsexist • Pixel colorcanbereplacedbyorcombinedwithfragmentcolor
Fragment processing • -blending allowsforthecombinationoffragment (src) andpixelcolors (dst) byconsideringthefragmentsandpixelsopacity • -valuescorrespondtoopacity [0,1] • Tells howmuch light frombehindisblocked
Fragment processing • -blending
Fragment processing • -blending
Fragment processing • -blending • Assume 2 objectswithcolorsand • Object 1 isbeforeobject 2 withrespecttotheviewer • Considercolorsand -valuestoblendthe 2 objectscorrectly: C= 1C1 + (1-1) 2C2 = 1 + (1-1)2 Example: =(1,0,0,0.8), =(0,0,1,0.5)
Fragment processing • Toblendtransparentobjectscorrectly, thefollowingapproachisperformed: • clearcolor/opacity (0,0,0,0) in thecolorbuffer • Sortobjectsfrom back to front (wrt. decreasingdistancetotheviewplane) • Rendertheobjects in sortedorder via theblendequation CD = sCs + (1-s)CD D = s + (1-s)Dhere, s (src) representstheincomingfragmentandd (dst) representsthevalues in thecolorbuffer
Fragments survivingthedepthtest Fragment processing • Blending in thegraphicspipeline • After thedepthtest, thesurvivingfragmentsareblendedintothecolorbuffer
Fragment processing • Blending • Itisenabled via API callsandperformedautomatically // Create an alphaenabled DX blendstatedescriptionblendStateDescription.RenderTarget[0].BlendEnable = TRUE; // sCs blendStateDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; // (1-s) CD blendStateDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; // sCs+ (1-s)CD blendStateDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;