1// Copyright (c) 2015-2018 Khronos Group. This work is licensed under a 2// Creative Commons Attribution 4.0 International License; see 3// http://creativecommons.org/licenses/by/4.0/ 4 5[[fxvertex]] 6= Fixed-Function Vertex Processing 7 8Vertex fetching is controlled via configurable state, as a logically 9distinct graphics pipeline stage. 10 11 12[[fxvertex-attrib]] 13== Vertex Attributes 14 15Vertex shaders can: define input variables, which receive _vertex attribute_ 16data transferred from one or more sname:VkBuffer(s) by drawing commands. 17Vertex shader input variables are bound to buffers via an indirect binding 18where the vertex shader associates a _vertex input attribute_ number with 19each variable, vertex input attributes are associated to _vertex input 20bindings_ on a per-pipeline basis, and vertex input bindings are associated 21with specific buffers on a per-draw basis via the 22fname:vkCmdBindVertexBuffers command. 23Vertex input attribute and vertex input binding descriptions also contain 24format information controlling how data is extracted from buffer memory and 25converted to the format expected by the vertex shader. 26 27There are sname:VkPhysicalDeviceLimits::pname:maxVertexInputAttributes 28number of vertex input attributes and 29sname:VkPhysicalDeviceLimits::pname:maxVertexInputBindings number of vertex 30input bindings (each referred to by zero-based indices), where there are at 31least as many vertex input attributes as there are vertex input bindings. 32Applications can: store multiple vertex input attributes interleaved in a 33single buffer, and use a single vertex input binding to access those 34attributes. 35 36In GLSL, vertex shaders associate input variables with a vertex input 37attribute number using the code:location layout qualifier. 38The code:component layout qualifier associates components of a vertex shader 39input variable with components of a vertex input attribute. 40 41.GLSL example 42[source,glsl] 43--------------------------------------------------- 44// Assign location M to variableName 45layout (location=M, component=2) in vec2 variableName; 46 47// Assign locations [N,N+L) to the array elements of variableNameArray 48layout (location=N) in vec4 variableNameArray[L]; 49--------------------------------------------------- 50 51In SPIR-V, vertex shaders associate input variables with a vertex input 52attribute number using the code:Location decoration. 53The code:Component decoration associates components of a vertex shader input 54variable with components of a vertex input attribute. 55The code:Location and code:Component decorations are specified via the 56code:OpDecorate instruction. 57 58.SPIR-V example 59[source,spirv] 60--------------------------------------------------- 61 ... 62 %1 = OpExtInstImport "GLSL.std.450" 63 ... 64 OpName %9 "variableName" 65 OpName %15 "variableNameArray" 66 OpDecorate %18 BuiltIn VertexIndex 67 OpDecorate %19 BuiltIn InstanceIndex 68 OpDecorate %9 Location M 69 OpDecorate %9 Component 2 70 OpDecorate %15 Location N 71 ... 72 %2 = OpTypeVoid 73 %3 = OpTypeFunction %2 74 %6 = OpTypeFloat 32 75 %7 = OpTypeVector %6 2 76 %8 = OpTypePointer Input %7 77 %9 = OpVariable %8 Input 78 %10 = OpTypeVector %6 4 79 %11 = OpTypeInt 32 0 80 %12 = OpConstant %11 L 81 %13 = OpTypeArray %10 %12 82 %14 = OpTypePointer Input %13 83 %15 = OpVariable %14 Input 84 ... 85--------------------------------------------------- 86 87 88[[fxvertex-attrib-location]] 89=== Attribute Location and Component Assignment 90 91Vertex shaders allow code:Location and code:Component decorations on input 92variable declarations. 93The code:Location decoration specifies which vertex input attribute is used 94to read and interpret the data that a variable will consume. 95The code:Component decoration allows the location to be more finely 96specified for scalars and vectors, down to the individual components within 97a location that are consumed. 98The components within a location are 0, 1, 2, and 3. 99A variable starting at component N will consume components N, N+1, N+2, ... 100up through its size. 101For single precision types, it is invalid if the sequence of components gets 102larger than 3. 103 104When a vertex shader input variable declared using a scalar or vector 32-bit 105data type is assigned a location, its value(s) are taken from the components 106of the input attribute specified with the corresponding 107sname:VkVertexInputAttributeDescription::pname:location. 108The components used depend on the type of variable and the code:Component 109decoration specified in the variable declaration, as identified in 110<<fxvertex-attrib-components>>. 111Any 32-bit scalar or vector input will consume a single location. 112For 32-bit data types, missing components are filled in with default values 113as described <<fxvertex-input-extraction,below>>. 114 115 116[[fxvertex-attrib-components]] 117.Input attribute components accessed by 32-bit input variables 118[width="65%",cols="<5,<3,<3",options="header"] 119|==== 120| 32-bit data type | code:Component decoration | Components consumed 121| scalar | 0 or unspecified | (x, o, o, o) 122| scalar | 1 | (o, y, o, o) 123| scalar | 2 | (o, o, z, o) 124| scalar | 3 | (o, o, o, w) 125| two-component vector | 0 or unspecified | (x, y, o, o) 126| two-component vector | 1 | (o, y, z, o) 127| two-component vector | 2 | (o, o, z, w) 128| three-component vector| 0 or unspecified | (x, y, z, o) 129| three-component vector| 1 | (o, y, z, w) 130| four-component vector | 0 or unspecified | (x, y, z, w) 131|==== 132 133Components indicated by "`o`" are available for use by other input variables 134which are sourced from the same attribute, and if used, are either filled 135with the corresponding component from the input format (if present), or the 136default value. 137 138When a vertex shader input variable declared using a 32-bit floating point 139matrix type is assigned a location _i_, its values are taken from 140consecutive input attributes starting with the corresponding 141sname:VkVertexInputAttributeDescription::pname:location. 142Such matrices are treated as an array of column vectors with values taken 143from the input attributes identified in <<fxvertex-attrib-matrix>>. 144The sname:VkVertexInputAttributeDescription::pname:format must: be specified 145with a elink:VkFormat that corresponds to the appropriate type of column 146vector. 147The code:Component decoration must: not be used with matrix types. 148 149[[fxvertex-attrib-matrix]] 150.Input attributes accessed by 32-bit input matrix variables 151[width="100%",cols="<10%,<24%,<21%,<45%",options="header"] 152|==== 153| Data type | Column vector type | Locations consumed | Components consumed 154| mat2 | two-component vector | i, i+1 | (x, y, o, o), (x, y, o, o) 155| mat2x3 | three-component vector | i, i+1 | (x, y, z, o), (x, y, z, o) 156| mat2x4 | four-component vector | i, i+1 | (x, y, z, w), (x, y, z, w) 157| mat3x2 | two-component vector | i, i+1, i+2 | (x, y, o, o), (x, y, o, o), (x, y, o, o) 158| mat3 | three-component vector | i, i+1, i+2 | (x, y, z, o), (x, y, z, o), (x, y, z, o) 159| mat3x4 | four-component vector | i, i+1, i+2 | (x, y, z, w), (x, y, z, w), (x, y, z, w) 160| mat4x2 | two-component vector | i, i+1, i+2, i+3 | (x, y, o, o), (x, y, o, o), (x, y, o, o), (x, y, o, o) 161| mat4x3 | three-component vector | i, i+1, i+2, i+3 | (x, y, z, o), (x, y, z, o), (x, y, z, o), (x, y, z, o) 162| mat4 | four-component vector | i, i+1, i+2, i+3 | (x, y, z, w), (x, y, z, w), (x, y, z, w), (x, y, z, w) 163|==== 164 165Components indicated by "`o`" are available for use by other input variables 166which are sourced from the same attribute, and if used, are either filled 167with the corresponding component from the input (if present), or the default 168value. 169 170When a vertex shader input variable declared using a scalar or vector 64-bit 171data type is assigned a location _i_, its values are taken from consecutive 172input attributes starting with the corresponding 173sname:VkVertexInputAttributeDescription::pname:location. 174The locations and components used depend on the type of variable and the 175code:Component decoration specified in the variable declaration, as 176identified in <<fxvertex-attrib-double>>. 177For 64-bit data types, no default attribute values are provided. 178Input variables must: not use more components than provided by the 179attribute. 180Input attributes which have one- or two-component 64-bit formats will 181consume a single location. 182Input attributes which have three- or four-component 64-bit formats will 183consume two consecutive locations. 184A 64-bit scalar data type will consume two components, and a 64-bit 185two-component vector data type will consume all four components available 186within a location. 187A three- or four-component 64-bit data type must: not specify a component. 188A three-component 64-bit data type will consume all four components of the 189first location and components 0 and 1 of the second location. 190This leaves components 2 and 3 available for other component-qualified 191declarations. 192A four-component 64-bit data type will consume all four components of the 193first location and all four components of the second location. 194It is invalid for a scalar or two-component 64-bit data type to specify a 195component of 1 or 3. 196 197[[fxvertex-attrib-double]] 198.Input attribute locations and components accessed by 64-bit input variables 199[width="100%",cols="<18%,^12%,<25%,^14%,^18%,<13%",options="header"] 200|==== 201^.^| Input format | Locations consumed 202 ^.^| 64-bit data type |code:Location decoration |code:Component decoration ^| 32-bit components consumed 203| R64 | i 204 | scalar | i | 0 or unspecified | (x, y, -, -) 205.3+<.^| R64G64 .3+^.^| i 206 | scalar | i | 0 or unspecified | (x, y, o, o) 207 | scalar | i | 2 | (o, o, z, w) 208 | two-component vector | i | 0 or unspecified | (x, y, z, w) 209.5+<.^| R64G64B64 .5+^.^| i, i+1 210 | scalar | i | 0 or unspecified | (x, y, o, o), (o, o, -, -) 211 | scalar | i | 2 | (o, o, z, w), (o, o, -, -) 212 | scalar | i+1 | 0 or unspecified | (o, o, o, o), (x, y, -, -) 213 | two-component vector | i | 0 or unspecified | (x, y, z, w), (o, o, -, -) 214 | three-component vector | i | unspecified | (x, y, z, w), (x, y, -, -) 215.8+<.^| R64G64B64A64 .8+^.^| i, i+1 216 | scalar | i | 0 or unspecified | (x, y, o, o), (o, o, o, o) 217 | scalar | i | 2 | (o, o, z, w), (o, o, o, o) 218 | scalar | i+1 | 0 or unspecified | (o, o, o, o), (x, y, o, o) 219 | scalar | i+1 | 2 | (o, o, o, o), (o, o, z, w) 220 | two-component vector | i | 0 or unspecified | (x, y, z, w), (o, o, o, o) 221 | two-component vector | i+1 | 0 or unspecified | (o, o, o, o), (x, y, z, w) 222 | three-component vector | i | unspecified | (x, y, z, w), (x, y, o, o) 223 | four-component vector | i | unspecified | (x, y, z, w), (x, y, z, w) 224|==== 225 226Components indicated by "`o`" are available for use by other input variables 227which are sourced from the same attribute. 228Components indicated by "`-`" are not available for input variables as there 229are no default values provided for 64-bit data types, and there is no data 230provided by the input format. 231 232When a vertex shader input variable declared using a 64-bit floating-point 233matrix type is assigned a location _i_, its values are taken from 234consecutive input attribute locations. 235Such matrices are treated as an array of column vectors with values taken 236from the input attributes as shown in <<fxvertex-attrib-double>>. 237Each column vector starts at the location immediately following the last 238location of the previous column vector. 239The number of attributes and components assigned to each matrix is 240determined by the matrix dimensions and ranges from two to eight locations. 241 242When a vertex shader input variable declared using an array type is assigned 243a location, its values are taken from consecutive input attributes starting 244with the corresponding 245sname:VkVertexInputAttributeDescription::pname:location. 246The number of attributes and components assigned to each element are 247determined according to the data type of the array elements and 248code:Component decoration (if any) specified in the declaration of the 249array, as described above. 250Each element of the array, in order, is assigned to consecutive locations, 251but all at the same specified component within each location. 252 253Only input variables declared with the data types and component decorations 254as specified above are supported. 255_Location aliasing_ is causing two variables to have the same location 256number. 257_Component aliasing_ is assigning the same (or overlapping) component number 258for two location aliases. 259Location aliasing is allowed only if it does not cause component aliasing. 260Further, when location aliasing, the aliases sharing the location must: all 261have the same SPIR-V floating-point component type or all have the same 262width integer-type components. 263 264 265[[fxvertex-input]] 266== Vertex Input Description 267 268Applications specify vertex input attribute and vertex input binding 269descriptions as part of graphics pipeline creation. 270The slink:VkGraphicsPipelineCreateInfo::pname:pVertexInputState points to a 271structure of type sname:VkPipelineVertexInputStateCreateInfo. 272 273[open,refpage='VkPipelineVertexInputStateCreateInfo',desc='Structure specifying parameters of a newly created pipeline vertex input state',type='structs'] 274-- 275 276The sname:VkPipelineVertexInputStateCreateInfo structure is defined as: 277 278include::../api/structs/VkPipelineVertexInputStateCreateInfo.txt[] 279 280 * pname:sType is the type of this structure. 281 * pname:pNext is `NULL` or a pointer to an extension-specific structure. 282 * pname:flags is reserved for future use. 283 * pname:vertexBindingDescriptionCount is the number of vertex binding 284 descriptions provided in pname:pVertexBindingDescriptions. 285 * pname:pVertexBindingDescriptions is a pointer to an array of 286 sname:VkVertexInputBindingDescription structures. 287 * pname:vertexAttributeDescriptionCount is the number of vertex attribute 288 descriptions provided in pname:pVertexAttributeDescriptions. 289 * pname:pVertexAttributeDescriptions is a pointer to an array of 290 sname:VkVertexInputAttributeDescription structures. 291 292.Valid Usage 293**** 294 * [[VUID-VkPipelineVertexInputStateCreateInfo-vertexBindingDescriptionCount-00613]] 295 pname:vertexBindingDescriptionCount must: be less than or equal to 296 sname:VkPhysicalDeviceLimits::pname:maxVertexInputBindings 297 * [[VUID-VkPipelineVertexInputStateCreateInfo-vertexAttributeDescriptionCount-00614]] 298 pname:vertexAttributeDescriptionCount must: be less than or equal to 299 sname:VkPhysicalDeviceLimits::pname:maxVertexInputAttributes 300 * [[VUID-VkPipelineVertexInputStateCreateInfo-binding-00615]] 301 For every pname:binding specified by each element of 302 pname:pVertexAttributeDescriptions, a 303 sname:VkVertexInputBindingDescription must: exist in 304 pname:pVertexBindingDescriptions with the same value of pname:binding 305 * [[VUID-VkPipelineVertexInputStateCreateInfo-pVertexBindingDescriptions-00616]] 306 All elements of pname:pVertexBindingDescriptions must: describe distinct 307 binding numbers 308 * [[VUID-VkPipelineVertexInputStateCreateInfo-pVertexAttributeDescriptions-00617]] 309 All elements of pname:pVertexAttributeDescriptions must: describe 310 distinct attribute locations 311**** 312 313include::../validity/structs/VkPipelineVertexInputStateCreateInfo.txt[] 314-- 315 316[open,refpage='VkPipelineVertexInputStateCreateFlags',desc='Reserved for future use',type='enums'] 317-- 318include::../api/flags/VkPipelineVertexInputStateCreateFlags.txt[] 319 320sname:VkPipelineVertexInputStateCreateFlags is a bitmask type for setting a 321mask, but is currently reserved for future use. 322-- 323 324Each vertex input binding is specified by an instance of the 325sname:VkVertexInputBindingDescription structure. 326 327[open,refpage='VkVertexInputBindingDescription',desc='Structure specifying vertex input binding description',type='structs'] 328-- 329 330The sname:VkVertexInputBindingDescription structure is defined as: 331 332include::../api/structs/VkVertexInputBindingDescription.txt[] 333 334 * pname:binding is the binding number that this structure describes. 335 * pname:stride is the distance in bytes between two consecutive elements 336 within the buffer. 337 * pname:inputRate is a elink:VkVertexInputRate value specifying whether 338 vertex attribute addressing is a function of the vertex index or of the 339 instance index. 340 341.Valid Usage 342**** 343 * [[VUID-VkVertexInputBindingDescription-binding-00618]] 344 pname:binding must: be less than 345 sname:VkPhysicalDeviceLimits::pname:maxVertexInputBindings 346 * [[VUID-VkVertexInputBindingDescription-stride-00619]] 347 pname:stride must: be less than or equal to 348 sname:VkPhysicalDeviceLimits::pname:maxVertexInputBindingStride 349**** 350 351include::../validity/structs/VkVertexInputBindingDescription.txt[] 352-- 353 354[open,refpage='VkVertexInputRate',desc='Specify rate at which vertex attributes are pulled from buffers',type='enums'] 355-- 356 357Possible values of slink:VkVertexInputBindingDescription::pname:inputRate, 358specifying the rate at which vertex attributes are pulled from buffers, are: 359 360include::../api/enums/VkVertexInputRate.txt[] 361 362 * ename:VK_VERTEX_INPUT_RATE_VERTEX specifies that vertex attribute 363 addressing is a function of the vertex index. 364 * ename:VK_VERTEX_INPUT_RATE_INSTANCE specifies that vertex attribute 365 addressing is a function of the instance index. 366 367-- 368 369[open,refpage='VkVertexInputAttributeDescription',desc='Structure specifying vertex input attribute description',type='structs'] 370-- 371 372Each vertex input attribute is specified by an instance of the 373sname:VkVertexInputAttributeDescription structure. 374 375The sname:VkVertexInputAttributeDescription structure is defined as: 376 377include::../api/structs/VkVertexInputAttributeDescription.txt[] 378 379 * pname:location is the shader binding location number for this attribute. 380 * pname:binding is the binding number which this attribute takes its data 381 from. 382 * pname:format is the size and type of the vertex attribute data. 383 * pname:offset is a byte offset of this attribute relative to the start of 384 an element in the vertex input binding. 385 386.Valid Usage 387**** 388 * [[VUID-VkVertexInputAttributeDescription-location-00620]] 389 pname:location must: be less than 390 sname:VkPhysicalDeviceLimits::pname:maxVertexInputAttributes 391 * [[VUID-VkVertexInputAttributeDescription-binding-00621]] 392 pname:binding must: be less than 393 sname:VkPhysicalDeviceLimits::pname:maxVertexInputBindings 394 * [[VUID-VkVertexInputAttributeDescription-offset-00622]] 395 pname:offset must: be less than or equal to 396 sname:VkPhysicalDeviceLimits::pname:maxVertexInputAttributeOffset 397 * [[VUID-VkVertexInputAttributeDescription-format-00623]] 398 pname:format must: be allowed as a vertex buffer format, as specified by 399 the ename:VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT flag in 400 sname:VkFormatProperties::pname:bufferFeatures returned by 401 fname:vkGetPhysicalDeviceFormatProperties 402**** 403 404include::../validity/structs/VkVertexInputAttributeDescription.txt[] 405-- 406 407[open,refpage='vkCmdBindVertexBuffers',desc='Bind vertex buffers to a command buffer',type='protos'] 408-- 409 410To bind vertex buffers to a command buffer for use in subsequent draw 411commands, call: 412 413include::../api/protos/vkCmdBindVertexBuffers.txt[] 414 415 * pname:commandBuffer is the command buffer into which the command is 416 recorded. 417 * pname:firstBinding is the index of the first vertex input binding whose 418 state is updated by the command. 419 * pname:bindingCount is the number of vertex input bindings whose state is 420 updated by the command. 421 * pname:pBuffers is a pointer to an array of buffer handles. 422 * pname:pOffsets is a pointer to an array of buffer offsets. 423 424The values taken from elements [eq]#i# of pname:pBuffers and pname:pOffsets 425replace the current state for the vertex input binding 426[eq]#pname:firstBinding {plus} i#, for [eq]#i# in [eq]#[0, 427pname:bindingCount)#. 428The vertex input binding is updated to start at the offset indicated by 429pname:pOffsets[i] from the start of the buffer pname:pBuffers[i]. 430All vertex input attributes that use each of these bindings will use these 431updated addresses in their address calculations for subsequent draw 432commands. 433 434.Valid Usage 435**** 436 * [[VUID-vkCmdBindVertexBuffers-firstBinding-00624]] 437 pname:firstBinding must: be less than 438 sname:VkPhysicalDeviceLimits::pname:maxVertexInputBindings 439 * [[VUID-vkCmdBindVertexBuffers-firstBinding-00625]] 440 The sum of pname:firstBinding and pname:bindingCount must: be less than 441 or equal to sname:VkPhysicalDeviceLimits::pname:maxVertexInputBindings 442 * [[VUID-vkCmdBindVertexBuffers-pOffsets-00626]] 443 All elements of pname:pOffsets must: be less than the size of the 444 corresponding element in pname:pBuffers 445 * [[VUID-vkCmdBindVertexBuffers-pBuffers-00627]] 446 All elements of pname:pBuffers must: have been created with the 447 ename:VK_BUFFER_USAGE_VERTEX_BUFFER_BIT flag 448 * [[VUID-vkCmdBindVertexBuffers-pBuffers-00628]] 449 Each element of pname:pBuffers that is non-sparse must: be bound 450 completely and contiguously to a single sname:VkDeviceMemory object 451**** 452 453include::../validity/protos/vkCmdBindVertexBuffers.txt[] 454-- 455 456ifdef::VK_EXT_vertex_attribute_divisor[] 457 458== Vertex Attribute Divisor in Instanced Rendering 459 460[open,refpage='VkPipelineVertexInputDivisorStateCreateInfoEXT',desc='Structure specifying vertex attributes assignment during instanced rendering',type='structs'] 461-- 462 463If the pname:pNext chain of slink:VkPipelineVertexInputStateCreateInfo 464includes a sname:VkPipelineVertexInputDivisorStateCreateInfoEXT structure, 465then that structure controls how vertex attributes are assigned to an 466instance when instanced rendering is enabled. 467 468The sname:VkPipelineVertexInputDivisorStateCreateInfoEXT structure is 469defined as: 470 471include::../api/structs/VkPipelineVertexInputDivisorStateCreateInfoEXT.txt[] 472 473 * pname:sType is the type of this structure 474 * pname:pNext is `NULL` or a pointer to an extension-specific structure 475 * pname:vertexBindingDivisorCount is the number of elements in the 476 pname:pVertexBindingDivisors array. 477 * pname:pVertexBindingDivisors is a pointer to an array of 478 sname:VkVertexInputBindingDivisorDescriptionEXT structures, which 479 specifies the divisor value for each binding. 480 481include::../validity/structs/VkPipelineVertexInputDivisorStateCreateInfoEXT.txt[] 482-- 483 484[open,refpage='VkVertexInputBindingDivisorDescriptionEXT',desc='Structure specifying a divisor used in instanced rendering',type='structs'] 485-- 486The individual divisor values per binding are specified using the 487sname:VkVertexInputBindingDivisorDescriptionEXT structure which is defined 488as: 489 490include::../api/structs/VkVertexInputBindingDivisorDescriptionEXT.txt[] 491 492 * pname:binding is the binding number for which the divisor is specified. 493 * pname:divisor is the number of successive instances that will use the 494 same value of the vertex attribute when instanced rendering is enabled. 495 For example, if the divisor is N, the same vertex attribute will applied 496 to N successive instances before moving on to the next vertex attribute. 497 The maximum value of divisor is implementation dependent and can be 498 queried using 499 sname:VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT::pname:maxVertexAttribDivisor. 500 501If this structure is not used to define a divisor value for an attribute 502then the divisor has a logical default value of 1. 503 504.Valid Usage 505**** 506 * [[VUID-VkVertexInputBindingDivisorDescriptionEXT-binding-01869]] 507 pname:binding must: be less than 508 sname:VkPhysicalDeviceLimits::pname:maxVertexInputBindings 509 * [[VUID-VkVertexInputBindingDivisorDescriptionEXT-divisor-01870]] 510 pname:divisor must: be a value between `1` and 511 sname:VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT::pname:maxVertexAttribDivisor, 512 inclusive. 513 * [[VUID-VkVertexInputBindingDivisorDescriptionEXT-inputRate-01871]] 514 slink:VkVertexInputBindingDescription::pname:inputRate must: be of type 515 ename:VK_VERTEX_INPUT_RATE_INSTANCE for this pname:binding. 516**** 517 518include::../validity/structs/VkVertexInputBindingDivisorDescriptionEXT.txt[] 519-- 520 521endif::VK_EXT_vertex_attribute_divisor[] 522 523The address of each attribute for each code:vertexIndex and 524code:instanceIndex is calculated as follows: 525 526 * Let code:attribDesc be the member of 527 sname:VkPipelineVertexInputStateCreateInfo::pname:pVertexAttributeDescriptions 528 with sname:VkVertexInputAttributeDescription::pname:location equal to 529 the vertex input attribute number. 530 * Let code:bindingDesc be the member of 531 sname:VkPipelineVertexInputStateCreateInfo::pname:pVertexBindingDescriptions 532 with sname:VkVertexInputAttributeDescription::pname:binding equal to 533 code:attribDesc.binding. 534 * Let code:vertexIndex be the index of the vertex within the draw (a value 535 between pname:firstVertex and pname:firstVertex+pname:vertexCount for 536 fname:vkCmdDraw, or a value taken from the index buffer for 537 fname:vkCmdDrawIndexed), and let code:instanceIndex be the instance 538 number of the draw (a value between pname:firstInstance and 539 pname:firstInstance+pname:instanceCount). 540ifdef::VK_EXT_vertex_attribute_divisor[] 541 * Let code:divisor be the member of 542 sname:VkPipelineVertexInputDivisorStateCreateInfoEXT::pname:pVertexBindingDivisors 543 with sname:VkVertexInputBindingDivisorDescriptionEXT::pname:binding 544 equal to code:attribDesc.binding. 545endif::VK_EXT_vertex_attribute_divisor[] 546 547[source,c] 548--------------------------------------------------- 549bufferBindingAddress = buffer[binding].baseAddress + offset[binding]; 550 551if (bindingDesc.inputRate == VK_VERTEX_INPUT_RATE_VERTEX) 552 vertexOffset = vertexIndex * bindingDesc.stride; 553else 554ifndef::VK_EXT_vertex_attribute_divisor[] 555 vertexOffset = instanceIndex * bindingDesc.stride; 556endif::VK_EXT_vertex_attribute_divisor[] 557ifdef::VK_EXT_vertex_attribute_divisor[] 558 vertexOffset = (firstInstance + ((instanceIndex - firstInstance) / divisor)) * bindingDesc.stride; 559endif::VK_EXT_vertex_attribute_divisor[] 560 561attribAddress = bufferBindingAddress + vertexOffset + attribDesc.offset; 562--------------------------------------------------- 563 564[[fxvertex-input-extraction]] 565For each attribute, raw data is extracted starting at `attribAddress` and is 566converted from the sname:VkVertexInputAttributeDescription's pname:format to 567either to floating-point, unsigned integer, or signed integer based on the 568base type of the format; the base type of the format must: match the base 569type of the input variable in the shader. 570If pname:format is a packed format, `attribAddress` must: be a multiple of 571the size in bytes of the whole attribute data type as described in 572<<features-formats-packed,Packed Formats>>. 573Otherwise, `attribAddress` must: be a multiple of the size in bytes of the 574component type indicated by pname:format (see <<features-formats,Formats>>). 575If the format does not include G, B, or A components, then those are filled 576with [eq]#(0,0,1)# as needed (using either 1.0f or integer 1 based on the 577format) for attributes that are not 64-bit data types. 578The number of components in the vertex shader input variable need not 579exactly match the number of components in the format. 580If the vertex shader has fewer components, the extra components are 581discarded. 582 583[[fxvertex-example]] 584== Example 585 586To create a graphics pipeline that uses the following vertex description: 587 588[source,c++] 589--------------------------------------------------- 590struct Vertex 591{ 592 float x, y, z, w; 593 uint8_t u, v; 594}; 595--------------------------------------------------- 596 597The application could use the following set of structures: 598 599[source,c++] 600--------------------------------------------------- 601const VkVertexInputBindingDescription binding = 602{ 603 0, // binding 604 sizeof(Vertex), // stride 605 VK_VERTEX_INPUT_RATE_VERTEX // inputRate 606}; 607 608const VkVertexInputAttributeDescription attributes[] = 609{ 610 { 611 0, // location 612 binding.binding, // binding 613 VK_FORMAT_R32G32B32A32_SFLOAT, // format 614 0 // offset 615 }, 616 { 617 1, // location 618 binding.binding, // binding 619 VK_FORMAT_R8G8_UNORM, // format 620 4 * sizeof(float) // offset 621 } 622}; 623 624const VkPipelineVertexInputStateCreateInfo viInfo = 625{ 626 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO, // sType 627 NULL, // pNext 628 0, // flags 629 1, // vertexBindingDescriptionCount 630 &binding, // pVertexBindingDescriptions 631 2, // vertexAttributeDescriptionCount 632 &attributes[0] // pVertexAttributeDescriptions 633}; 634--------------------------------------------------- 635