Vertex Buffers. Mike Bailey. What is a Vertex Buffer?

Similar documents
Vertex Buffers. Mike Bailey. Computer Graphics VertexBuffers.pptx

9/19/2018. Introduction. Acknowledgements. What Prompted the Move to Vulkan? Who was the original Vulcan? Why is it so important to keep the GPU Busy?

11/19/2018. Data Buffers. Mike Bailey. From the Quick Reference Card. Computer Graphics DataBuffers.pptx


Introduction. Mike Bailey. Computer Graphics Intro.pptx

3/15/2018. Introduction. Acknowledgements. What Prompted the Move to Vulkan?

3/15/2018. Introduction. Acknowledgements. Mike Bailey

11/19/2018. Shaders and SPIR-V. The Shaders View of the Basic Computer Graphics Pipeline. Mike Bailey. Vulkan: GLSL Differences from OpenGL

Vulkan Sample Code. Mike Bailey. Computer Graphics SampleCode.pptx

The Transition from RenderMan to the OpenGL Shading Language (GLSL)

Computer Graphics (CS 543) Lecture 10: Soft Shadows (Maps and Volumes), Normal and Bump Mapping

Rendering Objects. Need to transform all geometry then

Could you make the XNA functions yourself?

The Application Stage. The Game Loop, Resource Management and Renderer Design

The Graphics Pipeline

Vertex Buffer Objects

Vertex Buffer Objects. Vertex Buffer Objects: The Big Idea

The Graphics Pipeline

CS130 : Computer Graphics. Tamar Shinar Computer Science & Engineering UC Riverside

2016 Seoul DevU Pipeline Cache Object. Bill Licea-Kane Engineer, Senior Staff Qualcomm Technologies, Inc

Rasterization Overview

Lecture 2. Shaders, GLSL and GPGPU

E.Order of Operations

The Graphics Pipeline

Performance OpenGL Programming (for whatever reason)

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

Vulkan (including Vulkan Fast Paths)

Shaders (some slides taken from David M. course)

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Vulkan on Android: Gotchas and best practices

NVIDIA RTX: Enabling Ray Tracing in Vulkan. Nuno Subtil, Sr. Devtech Engineer / Eric Werness, Sr. System Software Engineer March 27, 2018

Ciril Bohak. - INTRODUCTION TO WEBGL

CS475/CS675 - Computer Graphics. OpenGL Drawing

Introduction to the OpenGL Shading Language (GLSL)

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

CS130 : Computer Graphics Lecture 2: Graphics Pipeline. Tamar Shinar Computer Science & Engineering UC Riverside

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

INF3320 Computer Graphics and Discrete Geometry

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

CS4620/5620: Lecture 14 Pipeline

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

Pipeline Operations. CS 4620 Lecture 14

Queues and Command Buffers

Rendering. Mike Bailey. Rendering.pptx. The Rendering Equation

CS770/870 Fall 2015 Advanced GLSL

GLSL Geometry Shaders

Real-Time Graphics Architecture

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

VULKAN TECHNOLOGY UPDATE Christoph Kubisch, NVIDIA GTC 2017 Ingo Esser, NVIDIA

last time put back pipeline figure today will be very codey OpenGL API library of routines to control graphics calls to compile and load shaders

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

General Purpose Computation (CAD/CAM/CAE) on the GPU (a.k.a. Topics in Manufacturing)

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

Understanding M3G 2.0 and its Effect on Producing Exceptional 3D Java-Based Graphics. Sean Ellis Consultant Graphics Engineer ARM, Maidenhead

API Background. Prof. George Wolberg Dept. of Computer Science City College of New York

CHAPTER 1 Graphics Systems and Models 3

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Tutorial on GPU Programming #2. Joong-Youn Lee Supercomputing Center, KISTI

Geometry Shaders. And how to use them

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

CS559 Computer Graphics Fall 2015

Queues and Command Buffers

:24 sample.cpp Page 1

Using Shaders for Lighting

VISIBILITY & CULLING. Don t draw what you can t see. Thomas Larsson, Afshin Ameri DVA338, Spring 2018, MDH

Project 1, 467. (Note: This is not a graphics class. It is ok if your rendering has some flaws, like those gaps in the teapot image above ;-)

Models and Architectures

a.k.a. glnext!! Did the future just arrive? (Spring 2006)!! Next-generation OpenGL?

The Rendering Pipeline (1)

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

EECE 478. Learning Objectives. Learning Objectives. Rasterization & Scenes. Rasterization. Compositing

How to Work on Next Gen Effects Now: Bridging DX10 and DX9. Guennadi Riguer ATI Technologies

Achieving High-performance Graphics on Mobile With the Vulkan API

HW-Tessellation Basics

Vulkan: Mark My DWORDS

Sign up for crits! Announcments

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

Introduction to Computer Graphics with WebGL

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Metal. GPU-accelerated advanced 3D graphics rendering and data-parallel computation. source rebelsmarket.com

Pipeline Barriers: A case of Gate-ing and Wait-ing

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Module 13C: Using The 3D Graphics APIs OpenGL ES

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Texture Mapping. Mike Bailey.

:27 sample.cpp Page 1

CS 432 Interactive Computer Graphics

Graphics Hardware. Instructor Stephen J. Guy

OpenGL refresher. Advanced Computer Graphics 2012

Keeping your GPU fed without getting bitten

Surface Graphics. 200 polys 1,000 polys 15,000 polys. an empty foot. - a mesh of spline patches:

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

Vulkan Multipass mobile deferred done right

Recall: Indexing into Cube Map

Transcription:

Vertex Buffers This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License Mike Bailey mjb@cs.oregonstate.edu VertexBuffers.pptx What is a Vertex Buffer? Vertex Buffers are how you draw things in Vulkan. They are very much like Vertex Buffer Objects in OpenGL, but more detail is exposed to you (a lot more ). But, the good news is that Vertex Buffers are really just ordinary Data Buffers, so some of the functions will look familiar to you. First, a quick review of computer graphics geometry...

Geometry vs. Topology Original Object 4 Geometry: 4 Where things are (e.g., coordinates) Geometry = changed Topology = same ( 4 ) Topology: 4 Geometry = same Topology = changed ( 4 ) How things are connected Vulkan Topologies 4 typedef enum VkPrimitiveTopology VK_PRIMITIVE_TOPOLOGY_POINT_LIST = 0, VK_PRIMITIVE_TOPOLOGY_LINE_LIST =, VK_PRIMITIVE_TOPOLOGY_LINE_STRIP =, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST =, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP = 4, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN = 5, VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY = 6, VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY = 7, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY = 8, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY = 9, VK_PRIMITIVE_TOPOLOGY_PATCH_LIST = 0, } VkPrimitiveTopology;

Vulkan Topologies VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST VK_PRIMITIVE_TOPOLOGY_POINT_LIST V 5 5 V 4 VK_PRIMITIVE_TOPOLOGY_LINE_LIST VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP V V 6 V V 4 7 V 5 VK_PRIMITIVE_TOPOLOGY_LINE_STRIP VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN V 4 V 5 OpenGL Topologies Polygon Requirements 6 Polygons must be: Convex and Planar

Vulkan Topologies Requirements and Orientation 7 Polygons must be: Convex and Planar Polygons are traditionally: CCW when viewed from outside the solid object GL_TRIANGLES V 5 V 4 It s not absolutely necessary, but there are possible optimizations if you are consistent OpenGL Topologies Vertex Order Matters 8 VK_LINE_STRIP VK_LINE_STRIP 4

What does Convex Polygon Mean? 9 We can go all mathematical here, but let s go visual instead. In a convex polygon, a line between any two points inside the polygon never leaves the inside of the polygon. Convex Not Convex Why is there a Requirement for Polygons to be Convex? 0 Graphics polygon-filling hardware can be highly optimized if you know that, no matter what direction you fill the polygon in, there will be two and only two intersections between the scanline and the polygon s edges Convex Not Convex 5

What if you need to display Polygons that are not Convex? There is an open source library to break a non convex polygon into convex polygons. It is called Polypartition, and is found here: https://github.com/ivanfratric/polypartition If you ever need to do this, contact me. I have working code Why is there a Requirement for Polygons to be Planar? Graphics hardware assumes that a polygon has a definite front and a definite back, and that you can only see one of them at a time OK OK Not OK 6

Vertex Orientation Issues Thanks to OpenGL, we are all used to drawing in a right-handed coordinate system. Y CCW 0 X Z Internally, however, the Vulkan pipeline uses a left-handed system: X 0 Z CW! Y The best way to handle this is to continue to draw in a RH coordinate system and then fix it up in the projection matrix, like this: ProjectionMatrix[][] *= -.; This is like saying Y = -Y. A Colored Cube Example 4 6 7 0 4 5 static GLuint CubeTriangleIndices[ ][] = 0,, }, 0,, }, 4, 5, 7 }, 4, 7, 6 },,, 7 },, 7, 5 }, 0, 4, 6 }, 0, 6, },, 6, 7 },, 7, }, 0,, 5 } 0, 5, 4 } }; 7

From the file SampleVertexData.cpp: struct vertex glm::vec }; position; normal; color; texcoord; Triangles in an Array of Structures 6 7 5 struct vertex VertexData[ ] = // triangle 0--: // vertex #0: -., -., -. }, 0., 0., -. }, 0., 0., 0. },., 0. } }, // vertex #: -.,., -. }, 0., 0., -. }, 0.,., 0. },.,. } }, 0 4 5 Modeled in right-handed coordinates // vertex #:.,., -. }, 0., 0., -. },.,., 0. }, 0.,. } }, Vertex Orientation Issues 6 This object was modeled such that triangles that face the viewer will look like their vertices are oriented CCW (this is detected by looking at vertex orientation at the start of the rasterization). Because this D object is closed, Vulkan can save rendering time by not even bothering with triangles whose vertices look like they are oriented CW. This is called backface culling. Vulkan s change in coordinate systems can mess up the backface culling. So I recommend, at least at first, that you do no culling. VkPipelineRasterizationStateCreateInfo vprsci;... vprsci.cullmode = VK_CULL_MODE_NONE vprsci.frontface = VK_FRONT_FACE_COUNTER_CLOCKWISE; 8

Filling the Vertex Buffer 7 MyBuffer MyVertexDataBuffer; Init05MyVertexDataBuffer( sizeof(vertexdata), &MyVertexDataBuffer ); Fill05DataBuffer( MyVertexDataBuffer, (void *) VertexData ); VkResult Init05MyVertexDataBuffer( IN VkDeviceSize size, OUT MyBuffer * pmybuffer ) VkResult result = Init05DataBuffer( size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, pmybuffer ); return result; } A Reminder of What Init05DataBuffer Does 8 VkResult Init05DataBuffer( VkDeviceSize size, VkBufferUsageFlags usage, OUT MyBuffer * pmybuffer ) VkResult result = VK_SUCCESS; VkBufferCreateInfo vbci; vbci.stype = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; vbci.pnext = nullptr; vbci.flags = 0; vbci.size = pmybuffer->size = size; vbci.usage = usage; vbci.sharingmode = VK_SHARING_MODE_EXCLUSIVE; vbci.queuefamilyindexcount = 0; vbci.pqueuefamilyindices = (const uint_t *)nullptr; result = vkcreatebuffer ( LogicalDevice, IN &vbci, PALLOCATOR, OUT &pmybuffer->buffer ); VkMemoryRequirements vmr; vkgetbuffermemoryrequirements( LogicalDevice, IN pmybuffer->buffer, OUT &vmr ); // fills vmr } VkMemoryAllocateInfo vmai; vmai.stype = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; vmai.pnext = nullptr; vmai.allocationsize = vmr.size; vmai.memorytypeindex = FindMemoryThatIsHostVisible( ); VkDeviceMemory vdm; result = vkallocatememory( LogicalDevice, IN &vmai, PALLOCATOR, OUT &vdm ); pmybuffer->vdm = vdm; result = vkbindbuffermemory( LogicalDevice, pmybuffer->buffer, IN vdm, 0 ); return result; // 0 is the offset 9

Vertex Shader module Specialization info Vertex Input binding Vertex Input attributes Topology Tesselation Shaders, Geometry Shader The Vulkan Pipeline Vertex Input Stage Input Assembly Tesselation, Geometry 9 Viewport Scissoring Viewport Depth Clamping DiscardEnable PolygonMode CullMode FrontFace LineWidth Rasterization Which states are dynamic DepthTestEnable DepthWriteEnable DepthCompareOp StencilTestEnable PipleineLayoutCreateInfo Which shaders are present Dynamic State Depth/Stencil Vertex Shader module Specialization info Computer Color Blending Graphics parameters Pipeline Layout Fragment Shader Stage Color Blending Stage Telling the Pipeline about its Input 0 We will come to the Pipeline later, but for now, know that a Vulkan pipeline is essentially a very large data structure that holds (what OpenGL would call) the state, including how to parse its input. struct vertex glm::vec }; position; normal; color; texcoord; layout( location = 0 ) in vec avertex; layout( location = ) in vec anormal; layout( location = ) in vec acolor; layout( location = ) in vec atexcoord; VkVertexInputBindingDescription vvibd[]; // one of these per buffer data buffer vvibd[0].binding = 0; // which binding # this is vvibd[0].stride = sizeof( struct vertex ); // bytes between successive structs vvibd[0].inputrate = VK_VERTEX_INPUT_RATE_VERTEX; 0

struct vertex glm::vec }; Telling the Pipeline about its Input position; normal; color; texcoord; layout( location = 0 ) in vec avertex; layout( location = ) in vec anormal; layout( location = ) in vec acolor; layout( location = ) in vec atexcoord; VkVertexInputAttributeDescription vviad[4]; // array per vertex input attribute // 4 = vertex, normal, color, texture coord vviad[0].location = 0; // location in the layout decoration vviad[0].binding = 0; // which binding description this is part of vviad[0].format = VK_FORMAT_VEC; // x, y, z vviad[0].offset = offsetof( struct vertex, position ); // 0 vviad[].location = ; vviad[].binding = 0; vviad[].format = VK_FORMAT_VEC; // nx, ny, nz vviad[].offset = offsetof( struct vertex, normal ); // vviad[].location = ; vviad[].binding = 0; vviad[].format = VK_FORMAT_VEC; // r, g, b vviad[].offset = offsetof( struct vertex, color ); // 4 vviad[].location = ; vviad[].binding = 0; vviad[].format = VK_FORMAT_VEC; // s, t vviad[].offset = offsetof( struct vertex, texcoord ); // 6 Telling the Pipeline about its Input We will come to the Pipeline later, but for now, know that a Vulkan Pipeline is essentially a very large data structure that holds (what OpenGL would call) the state, including how to parse its input. VkPipelineVertexInputStateCreateInfo vpvisci; // used to describe the input vertex attributes vpvisci.stype = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vpvisci.pnext = nullptr; vpvisci.flags = 0; vpvisci.vertexbindingdescriptioncount = ; vpvisci.pvertexbindingdescriptions = vvibd; vpvisci.vertexattributedescriptioncount = 4; vpvisci.pvertexattributedescriptions = vviad; VkPipelineInputAssemblyStateCreateInfo vpiasci; vpiasci.stype = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; vpiasci.pnext = nullptr; vpiasci.flags = 0; vpiasci.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;;

Telling the Pipeline about its Input We will come to the Pipeline later, but for now, know that a Vulkan Pipeline is essentially a very large data structure that holds (what OpenGL would call) the state, including how to parse its input. VkGraphicsPipelineCreateInfo vgpci; vgpci.stype = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; vgpci.pnext = nullptr; vgpci.flags = 0; vgpci.stagecount = ; // number of shader stages in this pipeline vgpci.pstages = vpssci; vgpci.pvertexinputstate = &vpvisci; vgpci.pinputassemblystate = &vpiasci; vgpci.ptessellationstate = (VkPipelineTessellationStateCreateInfo *)nullptr; // &vptsci vgpci.pviewportstate = &vpvsci; vgpci.prasterizationstate = &vprsci; vgpci.pmultisamplestate = &vpmsci; vgpci.pdepthstencilstate = &vpdssci; vgpci.pcolorblendstate = &vpcbsci; vgpci.pdynamicstate = &vpdsci; vgpci.layout = IN GraphicsPipelineLayout; vgpci.renderpass = IN RenderPass; vgpci.subpass = 0; // subpass number vgpci.basepipelinehandle = (VkPipeline) VK_NULL_HANDLE; vgpci.basepipelineindex = 0; result = vkcreategraphicspipelines( LogicalDevice, VK_NULL_HANDLE,, IN &vgpci, PALLOCATOR, OUT pgraphicspipeline ); Telling the Command Buffer what Vertices to Draw 4 We will come to Command Buffers later, but for now, know that you will specify the vertex buffer that you want drawn. VkBuffer buffers[] = MyVertexDataBuffer.buffer; vkcmdbindvertexbuffers( CommandBuffers[nextImageIndex], 0,, buffers, offsets ); const uint_t vertexcount = sizeof(vertexdata) / sizeof(vertexdata[0]); const uint_t instancecount = ; const uint_t firstvertex = 0; const uint_t firstinstance = 0; vkcmddraw( CommandBuffers[nextImageIndex], vertexcount, instancecount, firstvertex, firstinstance );