Render to texture PBuffers vs' Framebuffer - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Render to texture PBuffers vs' Framebuffer

Description:

switching between Framebuffers is faster than switching between PBuffers (wglMakeCurrent) ... Latta, Lutz; Building a Million Particle System. ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 34
Provided by: yal2
Category:

less

Transcript and Presenter's Notes

Title: Render to texture PBuffers vs' Framebuffer


1
Render to texturePBuffers vs. Framebuffer
2
Later, PBuffers
  • Pixel buffers
  • Designed for off-screen rendering
  • Similar to windows, but non-visible
  • Window system specific extension
  • Select from an enumerated list of available pixel
    formats using
  • ChoosePixelFormat()
  • DescribePixelFormat()
  • ...

3
Now, Framebuffer Object
  • Only requires a single OpenGL context Only
  • switching between Framebuffers is faster than
    switching between PBuffers (wglMakeCurrent)
  • No need for complicated pixel format selection
  • Format of Framebuffer is determined by texture or
    Renderbuffer format
  • Puts burden of finding compatible formats on
    developer
  • More similar to Direct3D render target model
  • Makes porting code easier
  • Renderbuffer images and texture images can be
    shared among Framebuffers
  • e.g. share depth buffers between color targets
  • saves memory

4
Framebuffer Object
  • OpenGL Framebuffer is a collection of logical
    buffers
  • Color, depth, stencil, accumulation
  • Framebuffer object extension provides a new
    mechanism for rendering to destinations other
    than those provided by the window system
  • Window system independent
  • Destinations known as Framebuffer attachable
    images. Can be
  • Off-screen buffers (Renderbuffers)
  • Textures

5
Framebuffer Object
  • Framebuffer-attachable image
  • 2D array of pixels that can be attached to a
    framebuffer
  • Texture images and renderbuffer images are
    examples.
  • Attachment point
  • State that references a framebuffer-attachable
    image.
  • One each for color, depth and stencil buffer of a
    framebuffer.
  • Attach
  • The act of connecting one object to another.
    Similar to bind.
  • Framebuffer attachment completeness

6
Framebuffer Object
  • Introduces two new OpenGL objects
  • Framebuffers and Renderbuffers
  • Framebuffer (FBO)
  • Collection of framebuffer attachable images (e.g.
    color, depth, stencil)
  • Plus state defining where output of GL rendering
    is directed
  • Equivalent to window system
  • When a framebuffer object is bound its attached
    images are the source and destination for
    fragment operations
  • Color and depth textures
  • Supports multiple color attachments for MRT
  • Depth or stencil renderbuffers

7
Framebuffer Object arquitecture
8
Framebuffer Object API
  • Creating a FBO (similar to create a texture)
  • void GenFramebuffersEXT(sizei n, uint
    framebuffers)
  • Delete a FBO
  • void DeleteFramebuffersEXT(sizei n, const
    uint framebuffers)
  • Check if a given identifier is a FBO
  • boolean IsFramebufferEXT(uint framebuffer)
  • Binding a FBO
  • void BindFramebufferEXT(enum target, uint
    framebuffer)
  • Check status of the FBO
  • enum CheckFramebufferStatusEXT(enum target)

9
Framebuffer Object API
  • Attaches image from a texture object to one the
    logical buffers of the current bound FBO
  • void FramebufferTexture1DEXT(enum target,
    enum attachment,
    enum textarget, uint texture,
    int level)
  • void FramebufferTexture2DEXT(enum target,
    enum attachment, enum textarget,
    uint texture, int level)
  • void FramebufferTexture3DEXT(enum target,
    enum attachment, enum
    textarget, uint texture, int level,
    int zoffset)
  • Automatically generate mipmaps for texture images
    attached to target
  • void GenerateMipmapEXT(enum target)

10
Binding a FBO
  • void BindFramebufferEXT(enum target, uint
    framebuffer)
  • target must be FRAMEBUFFER_EXT
  • framebuffer is FBO identifier
  • All GL operations occur on attached images
  • If (framebuffer 0), GL operations operate on
    window system provided framebuffer (its default)

11
Attaching textures to a FBO
  • void FramebufferTexturenDEXT(enum target, enum
    attachment, enum
    textarget, uint texture, int level)
  • target must be FRAMEBUFFER_EXT
  • attachment Is one of
  • GL_COLOR_ATTACHMENTn_EXT
  • DEPTH_ATTACHMENT_EXT
  • STENCIL_ATTACHMENT_EXT
  • textarget must be one of
  • GL_TEXTURE_2D
  • GL_TEXTURE_RECTANGLE_ARB
  • GL_TEXTURE_CUBE_MAP_...
  • level is the mipmap level of the texture to
    attach
  • texture is the texture object to attach
  • If (texture0), the image is detached from the
    framebuffer

12
Framebuffer completeness
  • Framebuffer is complete if all attachments are
    consistent
  • Texture formats make sense for attachment points
    i.e., dont try and attach a depth texture to a
    color attachment
  • All attached images must have the same width and
    height
  • All images attached to COLOR_ATTACHMENTn_EXT must
    have same format
  • If not complete, glBegin will generate error
    INVALID_FRAMEBUFFER_OPERATION

13
Checking Framebuffer Status
  • enum CheckFramebufferStatusEXT(enum target)
  • Should always be called after setting up FBOs
  • Returns enum indicating why FBO is incomplete
  • FRAMEBUFFER_COMPLETE COMPLETEFRAMEBUFFER_INCOMPL
    ETE_ATTACHMENTFRAMEBUFFER_INCOMPLETE_MISSING_ATTA
    CHMENTFRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT
    FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXTFRAMEBUFFER
    _INCOMPLETE_FORMATS_EXTFRAMEBUFFER_INCOMPLETE_DRA
    W_BUFFER_EXTFRAMEBUFFER_INCOMPLETE_READ_BUFFER_EX
    TFRAMEBUFFER_UNSUPPORTED FRAMEBUFFER_STATUS_ERRO
    R
  • If result is FRAMEBUFFER_UNSUPPORTED,
    application should try different format
    combinations until one succeeds

14
FBO usage
  • FBO allows several ways of switching between
    rendering destinations
  • In order of increasing performance
  • Multiple FBOs
  • Create a separate FBO for each texture you want
    to render to???
  • Single FBO, multiple texture attachments
  • Textures should have same format and dimensions
  • Use FramebufferTexture() to switch between
    textures
  • Single FBO, multiple texture attachments
  • Attach textures to different color attachments
  • Use glDrawBuffer() to switch rendering to
    different color attachments

15
FBO Performance Tips
  • Dont create and destroy FBO every frame
  • Try to avoid modifying textures used as rendering
    destinations using TexImage, CopyTexImage, etc.
  • More references and examples
  • http//www.gpgpu.org/developer/
  • http//gpgpu.org/developer/legacy-gpgpu-graphics-a
    pis

16
Brief overview
  • Use textures to represent physical features (e.g.
    positions, velocities, forces)
  • Fragment shader calculates new values and render
    this results back to textures
  • Each rendering pass draws a single quad,
    calculating values to next time step on the
    simulation
  • Actually, graphic processors supports textures
    with NPT (non-power-of-two) and signed float
    values in each (RGBA) color channel
  • GL_TEXTURE_RECTANGLE_ARB
  • Float precision of fragment programs allows GPU
    physic simulation match CPU accuracy
  • New fragment programming model (longer programs,
    flexible dependent texture reads) allows much
    more interesting simulations

17
Examples
18
GPU Arrays
  • Large 1D Arrays
  • Current GPUs limit 1D array sizes to 2048 or 4096
  • Pack into 2D memory
  • 1D-to-2D address translation

19
GPU Arrays
  • 3D Arrays
  • Problem
  • GPUs do not have 3D frame buffers
  • Solutions
  • Stack of 2D slices
  • Render-to-slice-of-3D-texture

20
Basic Example Cloth simulation
Particle positions and connectivity
GPU texture
?
21
Basic Example Cloth simulation
  • Can be used the Verlet integrator
  • Jakobsen, GDC 2001
  • Avoids storing explicit velocity
  • x 2x x a.?t²x x
  • Not always accurate, but stable!
  • In a GPU version, current and previous positions
    should be stored in two RGB float textures
  • Then swap current and previous textures for each
    time step

22
Cloth simulation The algorithm
  • GPU
  • Two passes (two shaders)
  • Perform integration (move particles)
  • Satisfy constraints
  • Distance constraints between particles
  • Floor collision constraints
  • Collision constraints with other objects
    (spheres)
  • CPU
  • XYZ vertices ? RGB texture pixels (read pixels)
  • Compute normals and render final mesh

23
Cloth simulationSimulation diagram
old positions
TEX0
VERLET INTEGRATOR FRAGMENT SHADER
SATISFY CONSTRAINTS FRAGMENT SHADER
next positions
curr positions
TEX2
curr positions
TEX1
Adjacency
TEX3
24
Integration pass code
  • uniform sampler2DRect oldp_texuniform
    sampler2DRect currp_texvoid Integrate(inout
    vec3 x, vec3 oldx, vec3 a, float timestep2)
    x 2x - oldx atimestep2 void
    main() float timestep 0.002 vec3
    gravity vec3(0.0, -9.8, 0.0) vec2 uv
    gl_TexCoord0.st // get current and
    previous position vec3 oldx
    texture2DRect(oldp_tex, uv).rgb vec3 x
    texture2DRect(currp_tex, uv).rgb // move
    the particle Integrate(x, oldx, gravity,
    timesteptimestep) // satisfy world
    constraints x clamp(x, worldMin,
    worldMax) SphereConstraint(x, spherePos,
    1.0) gl_FragColor vec4(x, 1.0)

25
Satisfy constraints code
  • // constrain a particle to be a fixed distance
    from another particlevec3 DistanceConstraint(vec3
    x, vec3 x2, float restlength, float stiffness)
    vec3 delta x2 - x float deltalength
    length(delta) float diff (deltalength -
    restlength) / deltalength return
    deltastiffnessdiff// as above, but using
    sqrt approximation sqrt(a) r ((a- rr) /
    2r), if a rrvec3 DistanceConstraint2(vec3
    x, vec3 x2, float restlength, float stiffness)
    vec3 delta x2 - x float deltalength
    dot(delta, delta) deltalength restlength
    ((deltalength - restlengthrestlength) / (2.0
    restlength)) float diff (deltalength -
    restlength) / deltalength return
    deltastiffnessdiff// constrain particle to
    be outside volume of a spherevoid
    SphereConstraint(inout vec3 x, vec3 center, float
    r) vec3 delta x - center float dist
    length(delta) if (dist lt r) x
    center delta(r / dist)

26
Constraints pass code
  • uniform vec2 ms // mesh sizeuniform float
    constraintDistuniform vec3 spherePosuniform
    sampler2DRect x_texuniform vec3
    worldMinuniform vec3 worldMaxvoid main()
    const float stiffness 0.2 // this should
    really be 0.5 vec2 uv gl_TexCoord0.st
    // get current position vec3 x
    texture2DRect(x_tex, uv).rgb // get
    positions of neighbouring particles vec3 x1
    texture2DRect(x_tex, uv vec2(1.0, 0.0)).rgb
    vec3 x2 texture2DRect(x_tex, uv vec2(-1.0,
    0.0)).rgb vec3 x3 texture2DRect(x_tex, uv
    vec2(0.0, 1.0)).rgb vec3 x4
    texture2DRect(x_tex, uv vec2(0.0, -1.0)).rgb
    // apply distance constraints // this
    could be done more efficiently with separate
    passes for the edge cases vec3 dx
    vec3(0.0) if (uv.x lt ms.x) dx
    DistanceConstraint(x, x1, constraintDist,
    stiffness) if (uv.x gt 0.5) dx dx
    DistanceConstraint(x, x2, constraintDist,
    stiffness) if (uv.y lt ms.y) dx dx
    DistanceConstraint(x, x3, constraintDist,
    stiffness) if (uv.y gt 0.5) dx dx
    DistanceConstraint(x, x4, constraintDist,
    stiffness) x x dx
    gl_FragColor vec4(x, 1.0)

27
A screenshot
128x128 mesh
256x256 mesh
28
Used textures
Previous positions
Current positions
29
More complex exampleRigid and deformable bodies
v3
  • As cloth modeling
  • Vertices ? particles
  • Current and previous positions are mapped on
    textures
  • Edges ? contraints
  • A rigid tetrahedron
  • Four vertices
  • Six constraints

v1
v2
v0
Texture0
Texture1
ContraintsTexture
30
Rigid tetrahedron simulation
31
Complex deformable objects
  • k vertices
  • vertex ?pixel(rgba)
  • rgb ?3D coordinates (x, y, z)
  • a? of incident springs
  • m constraints
  • Pixel rgb
  • r ?first vertex id
  • g ?second vertex id
  • b ?spring rest length

32
Complex deformable objects
33
References
  • Jakobsen, Thomas Advanced Character Physics. In
    GDC Proceedings 2001
  • Latta, Lutz Building a Million Particle System.
    Graphics Hardware 2004 conference GDC 2004
    Session
Write a Comment
User Comments (0)
About PowerShow.com