A GPU polygon depth offset calculation method based on glPolygonOffset
Through the method based on glPolygonOffset, the maximum depth slope and depth offset factor of the polygon are calculated, and combined with the OpenGL formula, the problem of screen interlacing and flickering caused by insufficient accuracy of the GPU depth buffer is solved, and efficient polygon depth offset calculation is realized, improving the depth accuracy and computational efficiency.
Patent Information
- Application Number
- CN202011404211.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-05
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2040-12-05
AI Technical Summary
When the GPU draws the same pixel multiple times, the depth difference is very small, causing the picture to stagger and flicker. The depth buffer has limited accuracy and cannot correctly distinguish the pixels that need to be displayed. It is difficult for existing technology to efficiently handle high-precision polygon depth offset calculations.
A method based on glPolygonOffset is used to determine whether the basic primitive is a triangle, obtain its drawing mode, and calculate the polygon's maximum depth slope, depth offset factor, and unit value. Combined with the depth offset formula defined by OpenGL, the final depth is calculated and intercepted. value to ensure the efficiency and accuracy of depth offset calculation.
Correctly implements the polygon depth offset defined by OpenGL, solves the problem of GPU polygon depth offset calculation, improves depth accuracy and calculation efficiency, and avoids the phenomenon of screen interlacing and flickering.
Smart Images

Figure CN112581594B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of computer graphics, and relates to a method for calculating GPU polygon depth offset, and in particular to a method for calculating GPU polygon depth offset based on glPolygonOffset. Background Art
[0002] In the design of graphics processing unit chips (GPUs for short), the glPolygonOffset function defined by the OpenGL API can solve the phenomenon of interlaced flickering of the screen by increasing or decreasing the offset of the triangle depth. This phenomenon is caused by the limited precision of the depth buffer. The depth difference of drawing the same pixel multiple times is very small, and when the depth test is turned on, the GPU cannot correctly distinguish the pixels that need to be displayed. The precision of the GPU's depth buffer is defined according to its own computing power. How to correctly and efficiently process high-precision calculations, the calculation method of polygon depth offset and the definition of data format are very critical, which is a technical problem that needs to be solved. Summary of the invention
[0003] In order to solve the calculation problem of the depth buffer accuracy of the GPU mentioned in the background technology, the present invention provides a calculation method of GPU polygon depth offset based on glPolygonOffset.
[0004] The technical solution of the present invention is:
[0005] A method for calculating GPU polygon depth offset based on glPolygonOffset, wherein the method comprises the following steps:
[0006] 1) Input the basic primitive and determine whether it is a triangle; the above basic primitives include points, lines, and triangles; if the basic primitive is not a triangle, repeat step 1); if the basic primitive is a triangle, proceed to the next step;
[0007] 2) According to the drawing surface of the basic graphic element input in step 1), a triangle drawing mode is obtained; the triangle drawing mode includes a point drawing mode, a line drawing mode and a fill mode;
[0008] 3) Determine whether the corresponding offset calculation is enabled in the drawing mode; the order of determination is point drawing mode, line drawing mode, and fill mode;
[0009] 4) Calculate the maximum depth slope m of the polygon;
[0010] 5) Get the depth offset factor and units;
[0011] 6) Assign the minimum value r that can produce resolvable window coordinate depth values;
[0012] 7) Calculate the offset depth of the triangle;
[0013] 8) Calculate the offset depth value and capture it as the final depth value.
[0014] As a preference, the drawing surface in the above step 2) includes a front side and a back side.
[0015] As a preference: the above step 3) comprises:
[0016] 3.1) Determine whether the drawing mode is point drawing mode
[0017] When the drawing mode is the dot-stroke mode, determine whether the offset calculation of the dot-stroke is enabled. If enabled, proceed to step 4); if not enabled, the method ends; when the drawing mode is not the dot-stroke mode, proceed to step 3.2);
[0018] 3.2) Determine whether the drawing mode is line drawing mode
[0019] When the drawing mode is the line drawing mode, determine whether the offset calculation of the line drawing is enabled. If enabled, proceed to step 4); if not enabled, the method ends; when the drawing mode is not the line drawing mode, proceed to step 3.3);
[0020] 3.3) When the drawing mode is not the line drawing mode, determine whether the fill offset calculation is enabled. If enabled, proceed to step 4); if not enabled, this method ends.
[0021] As a preference: the above step 4) comprises:
[0022] 4.1) Convert the coordinate data types of the three vertices of a triangle
[0023] According to the data type of the three vertex coordinates of the input triangle (x, y, z), the x and y coordinates are converted to 1-15-16 type, and the z coordinate is converted to 0-32 type;
[0024] The above 1-15-16 indicates a data type with 1 sign bit, 15 integer bits, and 16 decimal bits, and 0-32 indicates a data type with 32-bit pure decimal bits;
[0025] 4.2) Calculate the area of a triangle
[0026] Calculate the area of the triangle according to the coordinates of the three vertices of the converted triangle. The data type of the area is 1-31-32.
[0027] The above 1-31-32 indicates a data type with 1 sign bit, 31 integer bits, and 32 decimal bits;
[0028] 4.3) Determine the area of a triangle
[0029] If the area of the triangle is 0, then m = 0xffffffff; otherwise, go to step 4.4);
[0030] The above m represents the maximum depth slope of the polygon;
[0031] 4.4) Determine the z value
[0032] Determine whether there are two points with the same z value. If they are, m=0; otherwise, go to step 4.5);
[0033] 4.5) Calculate partial derivatives
[0034] Calculate the partial derivative A of z with respect to x and the partial derivative B of z with respect to y respectively, and calculate the absolute value of the partial derivative A, partial derivative B and the area of the triangle;
[0035] 4.6) Calculate m
[0036] Determine whether the absolute value of partial derivative A is greater than the absolute value of partial derivative B. If so, m is equal to the absolute value of partial derivative A divided by the absolute value of the triangle area; otherwise, m is equal to the absolute value of partial derivative B divided by the absolute value of the triangle area.
[0037] As a preferred embodiment: the above step 5) is:
[0038] Get the depth offset factor factor and unit units, and convert the offset factor factor to 1-15-16 format and the unit units to 1-23-8 format;
[0039] The above 1-23-8 format indicates a data type with 1 sign bit, 23 integer bits, and 8 decimal places.
[0040] As a preferred embodiment: the above step 6) is:
[0041] Assign the minimum value r of the window coordinate depth value that can produce a resolvable difference, and set the r value to 0x100, where the data format of the r value is 0-32 format.
[0042] As a preferred embodiment: the above step 7) is:
[0043] According to the calculation formula of depth offset defined by OpenGL
[0044] offset=m*factor+r*units, calculate the offset of the triangle depth.
[0045] As a preferred embodiment: the above step 8) is:
[0046] The offset depth values of the three vertices of the triangle are calculated as their respective z values plus the offset. First, determine whether the offset depth value is less than 0. If it is less than 0, the final depth value is equal to 0; otherwise, determine whether the offset depth value is greater than 1. If it is greater than 1, the final depth value is equal to 0xffffffff; otherwise, the final depth value is equal to the offset depth value.
[0047] The specific advantages and effects of the present invention are:
[0048] The present invention solves the calculation problem of GPU polygon depth offset, correctly implements the polygon depth offset defined by OpenGL, defines the data format of parameters in the calculation based on high depth precision requirements, and efficiently completes the calculation of the final depth value. BRIEF DESCRIPTION OF THE DRAWINGS
[0049] Figure 1 It is the algorithm flow chart of the present invention;
[0050] Figure 2 This is a flow chart of the algorithm for calculating the maximum depth slope m of a polygon in step 4) of the present invention. DETAILED DESCRIPTION
[0051] The technical solution of the present invention is described clearly and completely below in conjunction with the accompanying drawings and specific embodiments. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the protection scope of the present invention.
[0052] See also Figure 1-2 The present invention provides a method for calculating GPU polygon depth offset based on glPolygonOffset, the method comprising the following steps:
[0053] 1) Determine whether the basic primitive is a triangle:
[0054] Determine based on the input basic primitive package type, where basic primitives include points, lines, and triangles.
[0055] 2) Get the triangle drawing mode:
[0056] According to the input triangle drawing surface, the triangle drawing mode is obtained, wherein the triangle drawing surface includes the front face and the back face, and the triangle drawing mode includes point drawing, line drawing and fill mode.
[0057] 3) Determine whether the corresponding offset calculation is enabled in each drawing mode:
[0058] 3.1) Determine whether the drawing mode is point drawing mode
[0059] When the drawing mode is the dot-stroke mode, determine whether the offset calculation of the dot-stroke is enabled. If enabled, proceed to step 4); if not enabled, the method ends; when the drawing mode is not the dot-stroke mode, proceed to step 3.2);
[0060] 3.2) Determine whether the drawing mode is line drawing mode
[0061] When the drawing mode is the line drawing mode, determine whether the offset calculation of the line drawing is enabled. If enabled, proceed to step 4); if not enabled, the method ends; when the drawing mode is not the line drawing mode, proceed to step 3.3);
[0062] 3.3) When the drawing mode is not the line drawing mode, determine whether the fill offset calculation is enabled. If enabled, proceed to step 4); if not enabled, this method ends.
[0063] 4) Calculate the maximum depth slope m of the polygon:
[0064] 4.1) Convert the coordinate data types of the three vertices of a triangle
[0065] According to the data type of the three vertex coordinates (x, y, z) of the input triangle, the x and y coordinates are converted to 1-15-16 type, and the z coordinate is converted to 0-32 type.
[0066] Among them, 1-15-16 represents a data type with 1 sign bit, 15 integer bits, and 16 decimal bits, and 0-32 represents a data type with 32-bit pure decimal bits.
[0067] 4.2) Calculate the area of a triangle
[0068] The area of the triangle is calculated based on the coordinates of the three vertices of the converted triangle. The data type of the area is 1-31-32.
[0069] Among them, 1-31-32 indicates a data type with 1 sign bit, 31 integer bits, and 32 decimal bits.
[0070] 4.3) Determine the area of a triangle
[0071] If the area of the triangle is 0, then m=0xffffffff; otherwise, go to step 4.4).
[0072] Where m represents the maximum depth slope of the polygon.
[0073] 4.4) Determine the z value
[0074] Determine whether there are two points with equal z values. If so, m=0; otherwise, go to step 4.5).
[0075] 4.5) Calculate partial derivatives
[0076] Calculate the partial derivative A of z with respect to x and the partial derivative B of z with respect to y respectively, and calculate the absolute value of the partial derivative A, partial derivative B and the area of the triangle.
[0077] 4.6) Calculate m
[0078] Determine whether the absolute value of partial derivative A is greater than the absolute value of partial derivative B. If so, m is equal to the absolute value of partial derivative A divided by the absolute value of the triangle area; otherwise, m is equal to the absolute value of partial derivative B divided by the absolute value of the triangle area.
[0079] 5) Get the depth offset factor and units:
[0080] Get the depth offset factor factor and unit units, and convert the offset factor factor to 1-15-16 format and the unit units to 1-23-8 format;
[0081] The 1-23-8 format indicates a data type with 1 sign bit, 23 integer bits, and 8 decimal places.
[0082] 6) Assign the minimum value r that can produce a resolvable window coordinate depth value:
[0083] Assign the minimum value r of the window coordinate depth value that can produce a resolvable difference, and set the r value to 0x100, where the data format of the r value is 0-32 format.
[0084] 7) Calculate the offset depth of the triangle:
[0085] According to the calculation formula of depth offset defined by OpenGL
[0086] offset=m*factor+r*units, calculate the offset of the triangle depth.
[0087] 8) Calculate the offset depth value and intercept it as the final depth value:
[0088] The offset depth values of the three vertices of the triangle are calculated as their respective z values plus the offset. First, determine whether the offset depth value is less than 0. If it is less than 0, the final depth value is equal to 0; otherwise, determine whether the offset depth value is greater than 1. If it is greater than 1, the final depth value is equal to 0xffffffff; otherwise, the final depth value is equal to the offset depth value.
[0089] Example:
[0090] The present invention is further described in detail below in conjunction with the accompanying drawings. Figure 1 , where step 4 refers to Figure 2 .
[0091] A method for calculating GPU polygon depth offset based on glPolygonOffset, comprising the following steps:
[0092] Step 1: Determine whether the basic primitive is a triangle: Determine based on the input basic primitive package type, where the basic primitives include points, lines, and triangles.
[0093] Step 2: Get the drawing mode of the triangle: Get the drawing mode of the triangle according to the input triangle drawing surface, where the drawing surface of the triangle includes the front and back sides, and the drawing modes of the triangle include point drawing, line drawing and fill mode.
[0094] Step 3: Determine whether the corresponding offset calculation is enabled in each drawing mode:
[0095] 3.1) Determine whether the drawing mode is point drawing mode
[0096] When the drawing mode is the dot-stroke mode, determine whether the offset calculation of the dot-stroke is enabled. If enabled, proceed to step 4); if not enabled, the method ends; when the drawing mode is not the dot-stroke mode, proceed to step 3.2);
[0097] 3.2) Determine whether the drawing mode is line drawing mode
[0098] When the drawing mode is the line drawing mode, determine whether the offset calculation of the line drawing is enabled. If enabled, proceed to step 4); if not enabled, the method ends; when the drawing mode is not the line drawing mode, proceed to step 3.3);
[0099] 3.3) When the drawing mode is not the line drawing mode, determine whether the fill offset calculation is enabled. If enabled, proceed to step 4); if not enabled, this method ends.
[0100] Step 4: Calculate the maximum depth slope m of the polygon:
[0101] 4.1) Convert the coordinate data types of the three vertices of a triangle
[0102] According to the data type of the three vertex coordinates (x, y, z) of the input triangle, the x and y coordinates are converted to 1-15-16 type, and the z coordinate is converted to 0-32 type.
[0103] Among them, 1-15-16 represents a data type with 1 sign bit, 15 integer bits, and 16 decimal bits, and 0-32 represents a data type with 32-bit pure decimal bits.
[0104] 4.2) Calculate the area of a triangle
[0105] The area of the triangle is calculated based on the coordinates of the three vertices of the converted triangle. The data type of the area is 1-31-32.
[0106] Among them, 1-31-32 indicates a data type with 1 sign bit, 31 integer bits, and 32 decimal bits.
[0107] 4.3) Determine the area of a triangle
[0108] If the area of the triangle is 0, then m=0xffffffff; otherwise, go to step 4.4).
[0109] Where m represents the maximum depth slope of the polygon.
[0110] 4.4) Determine the z value
[0111] Determine whether there are two points with equal z values. If so, m=0; otherwise, go to step 4.5).
[0112] 4.5) Calculate partial derivatives
[0113] Calculate the partial derivative A of z with respect to x and the partial derivative B of z with respect to y respectively, and calculate the absolute value of the partial derivative A, partial derivative B and the area of the triangle.
[0114] 4.6) Calculate m
[0115] Determine whether the absolute value of partial derivative A is greater than the absolute value of partial derivative B. If so, m is equal to the absolute value of partial derivative A divided by the absolute value of the triangle area; otherwise, m is equal to the absolute value of partial derivative B divided by the absolute value of the triangle area.
[0116] Step 5. Obtain the depth offset factor factor and unit units: Obtain the depth offset factor factor and unit units, and convert the offset factor factor into a 1-15-16 format, and convert the unit units into a 1-23-8 format;
[0117] The 1-23-8 format indicates a data type with 1 sign bit, 23 integer bits, and 8 decimal places.
[0118] Step 6: Assign the minimum value r that can produce a resolvable window coordinate depth value:
[0119] Assign the minimum value r of the window coordinate depth value that can produce a resolvable difference, and set the r value to 0x100, where the data format of the r value is 0-32 format.
[0120] Step 7: Calculate the offset depth of the triangle: Calculate the offset of the depth of the triangle according to the depth offset calculation formula defined by OpenGL: offset = m*factor + r*units.
[0121] Step 8: Calculate the offset depth value and capture it as the final depth value:
[0122] Calculate the offset depth values of the three vertices of the triangle respectively as their respective z values plus offset. First, determine whether the offset depth value is less than 0. If it is less than 0, the final depth value is equal to 0; otherwise, determine whether the offset depth value is greater than 1. If it is greater than 1, the final depth value is equal to 0xffffffff; otherwise, the final depth value is equal to the offset depth value.
[0123] The calculation method of GPU polygon depth offset based on glPolygonOffset is concluded.
Claims
1. A method for calculating GPU polygon depth offset based on glPolygonOffset, Features: The method comprises the following steps: 1) Input a basic primitive and determine whether it is a triangle; the basic primitives include points, lines, and triangles; if it is determined that the basic primitive is not a triangle, repeat step 1); if it is determined that the basic primitive is a triangle, proceed to the next step; 2) According to the drawing surface of the basic graphic element input in step 1), a triangle drawing mode is obtained; the triangle drawing mode includes a point drawing mode, a line drawing mode and a fill mode; 3) Determine whether the corresponding offset calculation is enabled in the drawing mode; the judgment order is point drawing mode, line drawing mode, and fill mode; 4) Calculate the maximum depth slope m of the polygon; 4.1) Convert the coordinate data types of the three vertices of a triangle According to the data type of the three vertex coordinates of the input triangle (x, y, z), the x and y coordinates are converted to 1-15-16 type, and the z coordinate is converted to 0-32 type; The 1-15-16 represents a data type with 1 sign bit, 15 integer bits, and 16 decimal bits, and the 0-32 represents a data type with 32 bits of pure decimal; 4.2) Calculate the area of a triangle Calculate the area of the triangle according to the coordinates of the three vertices of the converted triangle. The data type of the area is 1-31-32. The 1-31-32 indicates a data type of 1 sign bit, 31 integer bits, and 32 decimal bits; 4.3) Determine the area of a triangle If the area of the triangle is 0, then m=0xffffffff; otherwise, go to step 4.4); The m represents the maximum depth slope of the polygon; 4.4) Determine the z value Determine whether there are two points with equal z values. If so, m=0; otherwise, go to step 4.5); 4.5) Calculate partial derivatives Calculate the partial derivative A of z with respect to x and the partial derivative B of z with respect to y respectively, and calculate the absolute value of the partial derivative A, partial derivative B and the area of the triangle; 4.6) Calculate m Determine whether the absolute value of partial derivative A is greater than the absolute value of partial derivative B. If so, m is equal to the absolute value of partial derivative A divided by the absolute value of the triangle area; otherwise, m is equal to the absolute value of partial derivative B divided by the absolute value of the triangle area; 5) Get the depth offset factor and units; 6) Assign the minimum value r that can produce a resolvable window coordinate depth value; 7) Calculate the offset depth of the triangle; 8) Calculate the offset depth value and capture it as the final depth value.
2. The method for calculating GPU polygon depth offset based on glPolygonOffset according to claim 1, Features: The drawing surface in step 2) includes a front side and a back side.
3. The method for calculating GPU polygon depth offset based on glPolygonOffset according to claim 2, Features: The step 3) comprises: 3.1) Determine whether the drawing mode is point drawing mode When the drawing mode is the dot-stroke mode, determine whether the offset calculation of the dot-stroke is enabled. If enabled, proceed to step 4); if not enabled, this method ends; when the drawing mode is not the dot-stroke mode, proceed to step 3.2); 3.2) Determine whether the drawing mode is line drawing mode When the drawing mode is line drawing mode, determine whether the offset calculation of line drawing is enabled. If enabled, proceed to step 4); if not enabled, this method ends; when the drawing mode is not line drawing mode, proceed to step 3.3); 3.3) When the drawing mode is not the line drawing mode, determine whether the fill offset calculation is enabled. If enabled, proceed to step 4); if not enabled, this method ends.
4. The method for calculating GPU polygon depth offset based on glPolygonOffset according to claim 3, Features: The step 5) is: Get the depth offset factor factor and unit units, and convert the offset factor factor to 1-15-16 format and the unit units to 1-23-8 format; The 1-23-8 format indicates a data type of 1 sign bit, 23 integer bits, and 8 decimal bits.
5. The method for calculating GPU polygon depth offset based on glPolygonOffset according to claim 4, Features: The step 6) is: Assign the minimum value r of the window coordinate depth value that can produce a resolvable difference, and set the r value to 0x100, where the data format of the r value is 0-32 format.
6. The method for calculating GPU polygon depth offset based on glPolygonOffset according to claim 5, Features: The step 7) is: According to the depth offset calculation formula defined by OpenGL, offset = m*factor + r*units, the offset of the triangle depth is calculated.
7. The method for calculating GPU polygon depth offset based on glPolygonOffset according to claim 6, Features: The step 8) is: Calculate the offset depth values of the three vertices of the triangle respectively, which are their respective z values plus offset. First, determine whether the offset depth value is less than 0. If it is less than 0, the final depth value is equal to 0; otherwise, determine whether the offset depth value is greater than 1. If it is greater than 1, the final depth value is equal to 0xffffffff; Otherwise, the final depth value is equal to the offset depth value.
Citation Information
Patent Citations
object space blanking line calculation acceleration method using OPENGL
CN109829962A
Methods and systems for rendering virtual reality content based on two-dimensional ("2d") captured imagery of a three-dimensional ("3d") scene
CN110709892A
Rendering apparatus and method
US20160086340A1
