1 / 24

Computer Graphics SS 2014 Fragment Processing

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

peers
Download Presentation

Computer Graphics SS 2014 Fragment Processing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Graphics SS 2014 Fragment Processing Rüdiger Westermann Lehrstuhl für Computer Graphik und Visualisierung

  2. 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

  3. Rasterization • Fragment generation: foreachcoveredpixel, onefragmentisgenerated • Foreachfragment: per-vertex attributes (color, normal, z-value, texturecoordinates,…) areinterpolated via barycentricinterpolation

  4. 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 }

  5. Rasterization • Resultofrasterizationstage:A setoffragments, eachstoringitspixelcoordinateaswellasinterpolated z-value, color, normal, texturecoordinate etc. A fragmentis in fact a surfacepointseenthroughtherespectivepixel X,Y z RGB u,v …

  6. 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

  7. Fragment processing • In therasterizationbasedpipeline, multiple buffersareusedduringrendering • Color (pixel) buffer: storesforeachpixel a RGBcolor, where istheopacity • Depthbuffer: storesforeachpixelthedepthoftheclosestsurfacepointrenderedintothispixel so far • Stencilbuffer: storesforeachpixel a bit-maskwhichcanbeusedtoprohibitrenderingintothispixel

  8. 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; }

  9. Fragment processing • When multiple trianglescoverthe same fragment, howistakencarethatonlytheoneclosesttotheviewerisseen? 4 1 3 2 Keep in mindthatobjectsarerendered in arbitraryorder

  10. 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

  11. Fragment processing • Duringrenderingthedepthbufferstoresforeachpixeltheminimumdepthvalueof all fragmentswhichwerefallingintothepixel • When multiple surfacepoints fall intothe same pixel, onlytheoneclosesttothenear plane isstored DepthBuffer + New Triangle DepthBuffer + New Triangle DepthBuffer

  12. Fragment processing • The depthtest: everyincomingfragment (src) testsitsdepthvalueagainstthedepthvalue in thedepthbuffer (dst) • When multiple objects fall intothe same pixel, onlytheonewiththesmallestdepthvaluesurvives

  13. Fragment processing

  14. Fragment processing

  15. 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; }

  16. 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;

  17. Fragment processing • Blendingtellsushowtheincomingfragmentcolor(src) iscombinedwiththecoloralready in thecolorbuffer (dst) • Different blendfunctionsexist • Pixel colorcanbereplacedbyorcombinedwithfragmentcolor

  18. Fragment processing • -blending allowsforthecombinationoffragment (src) andpixelcolors (dst) byconsideringthefragmentsandpixelsopacity • -valuescorrespondtoopacity [0,1] • Tells howmuch light frombehindisblocked

  19. Fragment processing • -blending

  20. Fragment processing • -blending

  21. 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)

  22. 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

  23. Fragments survivingthedepthtest Fragment processing • Blending in thegraphicspipeline • After thedepthtest, thesurvivingfragmentsareblendedintothecolorbuffer

  24. 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;

More Related