15bd8deadSopenharmony_ciName 25bd8deadSopenharmony_ci 35bd8deadSopenharmony_ci EXT_draw_instanced 45bd8deadSopenharmony_ci 55bd8deadSopenharmony_ciName Strings 65bd8deadSopenharmony_ci 75bd8deadSopenharmony_ci GL_EXT_draw_instanced 85bd8deadSopenharmony_ci 95bd8deadSopenharmony_ciContact 105bd8deadSopenharmony_ci 115bd8deadSopenharmony_ci Michael Gold, NVIDIA Corporation (gold 'at' nvidia.com) 125bd8deadSopenharmony_ci 135bd8deadSopenharmony_ciStatus 145bd8deadSopenharmony_ci 155bd8deadSopenharmony_ci Shipping for GeForce 8 Series (November 2006) 165bd8deadSopenharmony_ci 175bd8deadSopenharmony_ciVersion 185bd8deadSopenharmony_ci 195bd8deadSopenharmony_ci Last Modified Date: June 26, 2013 205bd8deadSopenharmony_ci Author Revision: 2.0 215bd8deadSopenharmony_ci 225bd8deadSopenharmony_ciNumber 235bd8deadSopenharmony_ci 245bd8deadSopenharmony_ci OpenGL Extension #327 255bd8deadSopenharmony_ci OpenGL ES Extension #157 265bd8deadSopenharmony_ci 275bd8deadSopenharmony_ciDependencies 285bd8deadSopenharmony_ci 295bd8deadSopenharmony_ci OpenGL 2.0 or OpenGL ES 2.0 is required. 305bd8deadSopenharmony_ci 315bd8deadSopenharmony_ci EXT_gpu_shader4 or NV_vertex_shader4 is required if the GL is not OpenGL ES 2.0. 325bd8deadSopenharmony_ci 335bd8deadSopenharmony_ci OES_element_index_uint affects the definition of this extension. 345bd8deadSopenharmony_ci 355bd8deadSopenharmony_ciOverview 365bd8deadSopenharmony_ci 375bd8deadSopenharmony_ci This extension provides the means to render multiple instances of 385bd8deadSopenharmony_ci an object with a single draw call, and an "instance ID" variable 395bd8deadSopenharmony_ci which can be used by the vertex program to compute per-instance 405bd8deadSopenharmony_ci values, typically an object's transform. 415bd8deadSopenharmony_ci 425bd8deadSopenharmony_ciNew Tokens 435bd8deadSopenharmony_ci 445bd8deadSopenharmony_ci None 455bd8deadSopenharmony_ci 465bd8deadSopenharmony_ciNew Procedures and Functions 475bd8deadSopenharmony_ci 485bd8deadSopenharmony_ci void DrawArraysInstancedEXT(enum mode, int first, sizei count, 495bd8deadSopenharmony_ci sizei primcount); 505bd8deadSopenharmony_ci void DrawElementsInstancedEXT(enum mode, sizei count, enum type, 515bd8deadSopenharmony_ci const void *indices, sizei primcount); 525bd8deadSopenharmony_ci 535bd8deadSopenharmony_ciAdditions to Chapter 2 of the OpenGL 2.0 Specification 545bd8deadSopenharmony_ci(OpenGL Operation) 555bd8deadSopenharmony_ci 565bd8deadSopenharmony_ci Modify section 2.8 (Vertex Arrays), p. 23 575bd8deadSopenharmony_ci 585bd8deadSopenharmony_ci (insert before the final paragraph, p. 30) 595bd8deadSopenharmony_ci 605bd8deadSopenharmony_ci The internal counter <instanceID> is a 32-bit integer value which 615bd8deadSopenharmony_ci may be read by a vertex program as <vertex.instance>, as described 625bd8deadSopenharmony_ci in section 2.X.3.2, or vertex shader as <gl_InstanceID>, as 635bd8deadSopenharmony_ci described in section 2.15.4.2. The value of this counter is 645bd8deadSopenharmony_ci always zero, except as noted below. 655bd8deadSopenharmony_ci 665bd8deadSopenharmony_ci The command 675bd8deadSopenharmony_ci 685bd8deadSopenharmony_ci void DrawArraysInstancedEXT(enum mode, int first, sizei count, 695bd8deadSopenharmony_ci sizei primcount); 705bd8deadSopenharmony_ci 715bd8deadSopenharmony_ci behaves identically to DrawArrays except that <primcount> 725bd8deadSopenharmony_ci instances of the range of elements are executed and the value of 735bd8deadSopenharmony_ci <instanceID> advances for each iteration. It has the same effect 745bd8deadSopenharmony_ci as: 755bd8deadSopenharmony_ci 765bd8deadSopenharmony_ci if (mode, count, or primcount is invalid) 775bd8deadSopenharmony_ci generate appropriate error 785bd8deadSopenharmony_ci else { 795bd8deadSopenharmony_ci for (i = 0; i < primcount; i++) { 805bd8deadSopenharmony_ci instanceID = i; 815bd8deadSopenharmony_ci DrawArrays(mode, first, count); 825bd8deadSopenharmony_ci } 835bd8deadSopenharmony_ci instanceID = 0; 845bd8deadSopenharmony_ci } 855bd8deadSopenharmony_ci 865bd8deadSopenharmony_ci The command 875bd8deadSopenharmony_ci 885bd8deadSopenharmony_ci void DrawElementsInstancedEXT(enum mode, sizei count, enum type, 895bd8deadSopenharmony_ci const void *indices, sizei primcount); 905bd8deadSopenharmony_ci 915bd8deadSopenharmony_ci behaves identically to DrawElements except that <primcount> 925bd8deadSopenharmony_ci instances of the set of elements are executed, and the value of 935bd8deadSopenharmony_ci <instanceID> advances for each iteration. It has the same effect 945bd8deadSopenharmony_ci as: 955bd8deadSopenharmony_ci 965bd8deadSopenharmony_ci if (mode, count, primcount, or type is invalid ) 975bd8deadSopenharmony_ci generate appropriate error 985bd8deadSopenharmony_ci else { 995bd8deadSopenharmony_ci for (int i = 0; i < primcount; i++) { 1005bd8deadSopenharmony_ci instanceID = i; 1015bd8deadSopenharmony_ci DrawElements(mode, count, type, indices); 1025bd8deadSopenharmony_ci } 1035bd8deadSopenharmony_ci instanceID = 0; 1045bd8deadSopenharmony_ci } 1055bd8deadSopenharmony_ci 1065bd8deadSopenharmony_ciAdditions to Chapter 5 of the OpenGL 2.0 Specification 1075bd8deadSopenharmony_ci(Special Functions) 1085bd8deadSopenharmony_ci 1095bd8deadSopenharmony_ci The error INVALID_OPERATION is generated if DrawArraysInstancedEXT 1105bd8deadSopenharmony_ci or DrawElementsInstancedEXT is called during display list 1115bd8deadSopenharmony_ci compilation. 1125bd8deadSopenharmony_ci 1135bd8deadSopenharmony_ciDependencies on OpenGL ES 2.0 1145bd8deadSopenharmony_ci 1155bd8deadSopenharmony_ci If the GL is OpenGL ES 2.0, all references to vertex programs and display lists 1165bd8deadSopenharmony_ci are deleted, and primcount is replaced by instanceCount in the function prototype 1175bd8deadSopenharmony_ci and pseudocode. 1185bd8deadSopenharmony_ci 1195bd8deadSopenharmony_ci Add a new section in 2.10.5 called "Shader Inputs" between "Texture Access" and 1205bd8deadSopenharmony_ci "Validation" 1215bd8deadSopenharmony_ci 1225bd8deadSopenharmony_ci "Besides having access to vertex attributes and uniform variables, 1235bd8deadSopenharmony_ci vertex shaders can access the read-only built-in variable 1245bd8deadSopenharmony_ci gl_InstanceIDEXT. The variable gl_InstanceIDEXT holds the integer 1255bd8deadSopenharmony_ci index of the current primitive in an instanced draw call. See also 1265bd8deadSopenharmony_ci section 7.1 of the OpenGL ES Shading Language Specification." 1275bd8deadSopenharmony_ci 1285bd8deadSopenharmony_ci 1295bd8deadSopenharmony_ci Additionally, the following is added to The OpenGL ES Shading Language Specification, 1305bd8deadSopenharmony_ci Version 1.00.17: 1315bd8deadSopenharmony_ci 1325bd8deadSopenharmony_ci "Including the following line in a shader can be used to control the 1335bd8deadSopenharmony_ci language features described in this extension: 1345bd8deadSopenharmony_ci 1355bd8deadSopenharmony_ci #extension GL_EXT_draw_instanced : <behavior> 1365bd8deadSopenharmony_ci 1375bd8deadSopenharmony_ci where <behavior> is as specified in section 3.4. 1385bd8deadSopenharmony_ci 1395bd8deadSopenharmony_ci A new preprocessor #define is added to the OpenGL Shading Language: 1405bd8deadSopenharmony_ci 1415bd8deadSopenharmony_ci #define GL_EXT_draw_instanced 1 1425bd8deadSopenharmony_ci 1435bd8deadSopenharmony_ci Change Section 7.1 "Vertex Shader Special Variables" 1445bd8deadSopenharmony_ci 1455bd8deadSopenharmony_ci Add the following definitions to the list of built-in variable definitions: 1465bd8deadSopenharmony_ci 1475bd8deadSopenharmony_ci highp int gl_InstanceIDEXT; // read-only 1485bd8deadSopenharmony_ci 1495bd8deadSopenharmony_ci Add the following paragraph at the end of the section: 1505bd8deadSopenharmony_ci 1515bd8deadSopenharmony_ci The variable gl_InstanceIDEXT is available as a read-only variable 1525bd8deadSopenharmony_ci from within vertex shaders and holds the integer index of the current 1535bd8deadSopenharmony_ci primitive in an instanced draw call (DrawArraysInstancedEXT, 1545bd8deadSopenharmony_ci DrawElementsInstancedEXT). If the current primitive does not come 1555bd8deadSopenharmony_ci from an instanced draw call, the value of gl_InstanceIDEXT is zero." 1565bd8deadSopenharmony_ci 1575bd8deadSopenharmony_ci 1585bd8deadSopenharmony_ciDependencies on NV_vertex_program4 1595bd8deadSopenharmony_ci 1605bd8deadSopenharmony_ci If NV_vertex_program4 is not supported and the GL is not OpenGL ES 2.0, 1615bd8deadSopenharmony_ci all references to vertex.instance are deleted. 1625bd8deadSopenharmony_ci 1635bd8deadSopenharmony_ciDependencies on EXT_gpu_shader4 1645bd8deadSopenharmony_ci 1655bd8deadSopenharmony_ci If EXT_gpu_shader4 is not supported and the GL is not OpenGL ES 2.0, 1665bd8deadSopenharmony_ci all references to gl_InstanceID are deleted. 1675bd8deadSopenharmony_ci 1685bd8deadSopenharmony_ciDependencies on OES_element_index_uint 1695bd8deadSopenharmony_ci 1705bd8deadSopenharmony_ci If OES_element_index_uint is not supported and the GL is OpenGL ES 2.0, 1715bd8deadSopenharmony_ci omit UNSIGNED_INT and uint from the description of DrawElementsInstancedEXT. 1725bd8deadSopenharmony_ci 1735bd8deadSopenharmony_ciErrors 1745bd8deadSopenharmony_ci 1755bd8deadSopenharmony_ci INVALID_ENUM is generated by DrawElementsInstancedEXT if <type> is 1765bd8deadSopenharmony_ci not one of UNSIGNED_BYTE, UNSIGNED_SHORT or UNSIGNED_INT. 1775bd8deadSopenharmony_ci 1785bd8deadSopenharmony_ci INVALID_VALUE is generated by DrawArraysInstancedEXT if <first> is 1795bd8deadSopenharmony_ci less than zero. 1805bd8deadSopenharmony_ci 1815bd8deadSopenharmony_ciIssues 1825bd8deadSopenharmony_ci 1835bd8deadSopenharmony_ci (1) Should instanceID be provided by this extension, or should it be 1845bd8deadSopenharmony_ci provided by EXT_gpu_shader4, thus creating a dependence on that 1855bd8deadSopenharmony_ci spec? 1865bd8deadSopenharmony_ci 1875bd8deadSopenharmony_ci Resolved: While this extension could stand alone, its utility 1885bd8deadSopenharmony_ci would be limited without the additional functionality provided 1895bd8deadSopenharmony_ci by EXT_gpu_shader4; also, the spec language is cleaner if 1905bd8deadSopenharmony_ci EXT_gpu_shader4 assumes instanceID is always available, even 1915bd8deadSopenharmony_ci if its value is always zero without this extension. 1925bd8deadSopenharmony_ci 1935bd8deadSopenharmony_ci For OpenGL ES 2.0: This extension does stand alone, introducing 1945bd8deadSopenharmony_ci gl_InstanceID in GLSL-ES 1.00. 1955bd8deadSopenharmony_ci 1965bd8deadSopenharmony_ci (2) Should MultiDrawArrays and MultiDrawElements affect the value of 1975bd8deadSopenharmony_ci instanceID? 1985bd8deadSopenharmony_ci 1995bd8deadSopenharmony_ci Resolved: No, this may cause implementation difficulties and 2005bd8deadSopenharmony_ci is considered unlikely to provide any real benefit. 2015bd8deadSopenharmony_ci 2025bd8deadSopenharmony_ci (3) Should DrawArraysInstanced and DrawElementsInstanced be compiled 2035bd8deadSopenharmony_ci into display lists? 2045bd8deadSopenharmony_ci 2055bd8deadSopenharmony_ci Resolved: No, calling these during display list compilation 2065bd8deadSopenharmony_ci generate INVALID_OPERATION. 2075bd8deadSopenharmony_ci 2085bd8deadSopenharmony_ci (4) What is the maximum range of instances that gl_InstanceIDEXT can index 2095bd8deadSopenharmony_ci in an OpenGL ES 2.0 vertex shader? 2105bd8deadSopenharmony_ci 2115bd8deadSopenharmony_ci Resolved: According to the The OpenGL ES Shading Language 2125bd8deadSopenharmony_ci 1.00.17 Specification, section 4.5.2 Precision Qualifiers, highp integer 2135bd8deadSopenharmony_ci range is (-2^16, 2^16). So even though the DrawArraysInstancedEXT and 2145bd8deadSopenharmony_ci DrawElementsInstancedEXT take instanceCount as a 32-bit unsigned int, 2155bd8deadSopenharmony_ci the maximum instance the gl_InstanceIDEXT variable can index is 2165bd8deadSopenharmony_ci 2^16 - 1. If Instance count exceeds 2^16 - 1, it results in an undefined 2175bd8deadSopenharmony_ci value due to integer overflow and it is possible that gl_InstanceIDEXT 2185bd8deadSopenharmony_ci wraps, or that it does not. 2195bd8deadSopenharmony_ci 2205bd8deadSopenharmony_ciRevision History 2215bd8deadSopenharmony_ci 2225bd8deadSopenharmony_ci Rev. Date Author Changes 2235bd8deadSopenharmony_ci ---- -------- -------- ----------------------------------------- 2245bd8deadSopenharmony_ci 1.5 05/09/08 gold Removed extraneous parameters to DrawArrays 2255bd8deadSopenharmony_ci and DrawElements in chapter 2 pseudocode 2265bd8deadSopenharmony_ci 2275bd8deadSopenharmony_ci 2.0 06/26/13 benj Add OpenGL ES 2.0 interactions 2285bd8deadSopenharmony_ci 229