Ray tracing method, apparatus for ray tracing, device, computer-readable storage medium, and computer program product
By projecting rays and triangles to a plane coordinate system in ray tracing technology, and using center of gravity coordinates and FMA operations, the poor performance and common edge/vertex judgment problems in intersection detection of rays and triangles are solved, achieving more stable and accurate Intersection test results.
Patent Information
- Application Number
- PCT/CN2024/090693
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-05-05
- Filing Date
- 2024-04-29
- Publication Date
- 2024-11-14
AI Technical Summary
Existing ray tracing techniques have problems with poor performance in the intersection detection of rays and triangles, especially in the case of insufficient computational accuracy, which may cause cracks between triangles and rays hit the common sides or common sides of multiple triangles. It is difficult to accurately determine which triangle is hit at the vertex, affecting the image quality.
By projecting rays and triangles to the plane coordinate system, and determining the positional relationship between the intersection point and the triangle is determined based on the center of gravity coordinates of the intersection point between the ray and the plane coordinate system. When the intersection point is located at a common edge or a common vertex, the triangle vertex coordinates are used to determine whether it is hit by a ray. , and improve the calculation accuracy of the center of gravity coordinates through FMA operation to eliminate internal accuracy losses.
The performance and stability of the intersection detection of rays and triangles is improved, the robustness and watertightness of the intersection detection results are ensured, the crack problems between triangles are avoided, and the image quality is enhanced.
Smart Images

Figure CN2024090693_14112024_PF_FP_ABST
Abstract
Description
Ray tracing method, apparatus, device, computer-readable storage medium, and computer program product for ray tracing
[0001] CROSS-REFERENCE TO RELATED APPLICATIONS
[0002] This application is based on the Chinese patent application with application number 202310493798.7, application date May 5, 2023, and application name “Method of ray tracing, device and apparatus for ray tracing”, and claims the priority of the Chinese patent application. The entire content of the Chinese patent application is hereby introduced into this application as a reference. Technical Field
[0003] Embodiments of the present application relate to the field of image processing, and relate to a ray tracing method, an apparatus, a device, a computer-readable storage medium, and a computer program product for ray tracing. Background Art
[0004] Ray tracing is a common rendering technique that generates realistic images by projecting rays to intersect with a test object and shading the object based on the projection results. The basic geometric operation of ray tracing is to calculate the intersection point between the ray and the scene by testing all primitives in the scene. Primitives are typically triangles, arranged into a spatial data structure to accelerate intersection efficiency. This process is also called ray-triangle intersection detection. The results of intersection detection directly affect image quality, so improving the performance of intersection detection has become a pressing issue.
[0005] Summary of the Invention
[0006] Embodiments of the present application provide a ray tracing method, an apparatus, a device, a computer-readable storage medium, and a computer program product for ray tracing, which can improve the performance of intersection detection between rays and triangles.
[0007] In a first aspect, an embodiment of the present application provides a ray tracing method, the method comprising: projecting a ray and a triangle into a plane coordinate system; determining a positional relationship between the intersection point and a projected triangle of the triangle on the plane coordinate system based on the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system; when the intersection point is located on a common edge or common vertex shared by the projected triangle and other triangles projected onto the plane coordinate system, determining whether the triangle is hit by the ray based on the coordinates of at least two vertices of the projected triangle in the plane coordinate system.
[0008] Based on this technical solution, in the process of intersection detection between rays and triangles, the rays and triangles are projected into a plane coordinate system, and according to the centroid coordinates corresponding to the intersection of the ray and the plane coordinate system, the positional relationship between the intersection and the projected triangle of the triangle in the plane coordinate system is determined. In the case that the intersection is located on a common side or a common vertex of the projected triangle, it is accurately determined whether the ray hits the triangle according to the coordinates of at least two vertices of the triangle in the plane coordinate system, thereby ensuring the stability of the intersection detection results.
[0009] In some embodiments, when the intersection point is located at a common edge or common vertex shared by the projected triangle and other triangles projected onto the plane coordinate system, determining whether the triangle is hit by the ray is based on the coordinates of at least two vertices of the projected triangle in the plane coordinate system, including at least one of the following: when the intersection point is located at the common edge, determining whether the common edge satisfies a predetermined condition based on the coordinates of the two vertices of the common edge in the plane coordinate system, and when the common edge satisfies the predetermined condition, determining that the triangle is hit by the ray; when the intersection point is located at the common vertex, determining whether the two sides corresponding to the common vertex satisfy the predetermined condition, and when both sides corresponding to the common vertex satisfy the predetermined condition, determining that the triangle is hit by the ray.
[0010] In some embodiments, the predetermined condition includes at least one of the following: a predetermined relationship between the Y-axis coordinates of the two vertices of the common edge or the two vertices of each edge corresponding to the common vertex in the plane coordinate system; a predetermined relationship between the X-axis coordinates of the two vertices of the common edge or the two vertices of each edge corresponding to the common vertex in the plane coordinate system.
[0011] In some embodiments, the winding direction of the vertices of the projected triangle is clockwise CW, and along the CW direction, the two vertices of the common edge or the two vertices of the edge corresponding to the common vertex are respectively the first vertex and the second vertex of the projected triangle, and the predetermined condition includes one of the following: the Y-axis coordinate of the second vertex is greater than the Y-axis coordinate of the first vertex; the Y-axis coordinate of the second vertex is equal to the Y-axis coordinate of the first vertex, and the X-axis coordinate of the second vertex is greater than the X-axis coordinate of the first vertex.
[0012] In some embodiments, the winding direction of the vertices of the projected triangle is a counterclockwise CCW direction, and along the CCW direction, the two vertices of the common edge or the two vertices of the edge corresponding to the common vertex are respectively the first vertex and the second vertex of the projected triangle, and the predetermined condition includes one of the following: the Y-axis coordinate of the first vertex is greater than the Y-axis coordinate of the second vertex; the Y-axis coordinate of the first vertex is equal to the Y-axis coordinate of the second vertex, and the X-axis coordinate of the first vertex is greater than the X-axis coordinate of the second vertex.
[0013] In some embodiments, the method further comprises: determining that the triangle is hit by the ray if the intersection point is located inside the projected triangle.
[0014] In some embodiments, the plane coordinate system is perpendicular to the ray, and the intersection point is the origin of the plane coordinate system.
[0015] In some embodiments, the barycentric coordinates are used to represent the areas of three triangles formed by lines connecting the intersection point and three vertices of the projected triangle.
[0016] In some embodiments, the barycentric coordinates are used to represent the proportion of the area of the projected triangle occupied by three triangles formed by connecting the intersection point and the three vertices of the projected triangle.
[0017] In some embodiments, the barycentric coordinates are: U = Cx'·By'-Cy'·Bx'; V = Ax'·Cy'-Ay'·Cx'; W = Bx'·Ay'-By'·Ax'; wherein U, V and W are the barycentric coordinates, Ax' and Ay' are the X-axis coordinate and Y-axis coordinate of the first vertex of the projected triangle, respectively, Bx' and By' are the X-axis coordinate and Y-axis coordinate of the second vertex of the projected triangle, respectively, and Cx' and Cy' are the X-axis coordinate and Y-axis coordinate of the third vertex of the projected triangle, respectively.
[0018] In some embodiments, the positional relationship between the intersection point and the projected triangle of the triangle on the plane coordinate system is determined based on the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system, including: when one of U, V and W is 0, determining that the intersection point is located at the edge of the projected triangle; when two of U, V and W are 0, determining that the intersection point is located at the vertex of the projected triangle; when U, V and W are all positive, or U, V and W are all negative, determining that the intersection point is located inside the projected triangle.
[0019] In some embodiments, before determining the positional relationship between the intersection point and the projected triangle of the triangle on the plane coordinate system, the method further includes: determining the centroid coordinates based on a fused multiply-add FMA operation, wherein the FMA operation includes a multiplication operation and an addition operation, the multiplication operation is used to multiply the input value of the FMA operation, the input value of the FMA operation is a single-precision floating-point number, the addition operation is used to add the results of the multiplication operation, and the number of bits of the addition operation is greater than the number of bits of the multiplication operation.
[0020] In some embodiments, the number of bits of the addition operation is greater than or equal to 49 bits.
[0021] In some embodiments, the number of exponent bits aligned before the addition operation is greater than or equal to 9.
[0022] In some embodiments, the FMA operation includes two multiplication operations on two sets of input values and an addition operation on the results of the two multiplication operations, wherein, before the addition operation, the FMA operation also includes a sign inversion operation on the result of one of the multiplication operations.
[0023] In some embodiments, the FMA operation further includes a rounding operation on the result of the addition operation, so that the output value of the FMA operation is the single-precision floating-point number.
[0024] In some embodiments, before determining the positional relationship between the intersection point and the projected triangle of the triangle on the plane coordinate system, the method further includes: determining the principal dimension of the ray, and using the principal dimension as the Z axis of the three-dimensional coordinate system, the principal dimension being the dimension with the largest absolute value; moving the ray and the triangle to obtain that the origin of the ray is located at the origin of the three-dimensional coordinate system; transforming the ray and the triangle according to a transformation matrix to obtain that the direction of the ray is along the Z axis; and projecting the triangle along the direction of the ray to the plane coordinate system formed by the X axis and the Y axis.
[0025] In some embodiments, the transformation matrix comprises the product of two scaling matrices and a shear matrix.
[0026] In some embodiments, when the triangle is hit by the ray, the method further includes: determining the hitting distance of the ray in the three-dimensional coordinate system according to the barycentric coordinates: t=(U·A z '+V·B z '+W·C z ') / det, where t is the hit distance, U, V and W are the coordinates of the center of gravity, and A z '、B z' and C z ' are the Z-axis coordinates of the first vertex, second vertex and third vertex of the triangle in the three-dimensional coordinate system before it is projected onto the plane coordinate system.
[0027] In a second aspect, an embodiment of the present application provides a method for ray tracing, the method comprising: projecting a ray and a triangle onto a plane coordinate system; determining, based on an FMA operation, the centroid coordinates corresponding to the intersection of the ray and the plane coordinate system, wherein the FMA operation comprises a multiplication operation and an addition operation, the multiplication operation being used to multiply an input value of the FMA operation, the input value of the FMA operation being a single-precision floating-point number, the addition operation being used to add the results of the multiplication operation, the number of bits of the addition operation being greater than the number of bits of the multiplication operation; determining, based on the centroid coordinates, whether the triangle is hit by the ray.
[0028] Based on this technical solution, during the intersection detection process between a ray and a triangle, the ray and triangle are projected onto a plane coordinate system. The FMA operation is then used to determine the barycentric coordinates corresponding to the intersection of the ray and the plane coordinate system. Based on these barycentric coordinates, it is determined whether the triangle is hit by the ray. Because the calculation of the barycentric coordinates utilizes the FMA operation, which includes multiplication and addition operations, and the number of bits used in the addition operation is greater than the number of bits used in the single-precision floating-point input for the multiplication operation, the internal precision loss during the calculation of the barycentric coordinates is eliminated, enabling accurate acquisition of the barycentric coordinates and ensuring the watertightness of the intersection detection results.
[0029] In some embodiments, the number of bits of the addition operation is greater than or equal to 49 bits.
[0030] In some embodiments, the number of exponent bits aligned before the addition operation is greater than or equal to 9.
[0031] In some embodiments, the FMA operation includes two multiplication operations on two sets of input values and an addition operation on the results of the two multiplication operations, wherein, before the addition operation, the FMA operation also includes a sign inversion operation on the result of one of the multiplication operations.
[0032] In some embodiments, the FMA operation further includes a rounding operation on the result of the addition operation, so that the output value of the FMA operation is the single-precision floating-point number.
[0033] In some embodiments, before determining the centroid coordinates corresponding to the intersection of the ray and the plane coordinate system, the method further includes: determining the main dimension of the ray, and using the main dimension as the Z axis of the three-dimensional coordinate system, the main dimension being the dimension with the largest absolute value; moving the ray and the triangle to obtain that the origin of the ray is located at the origin of the three-dimensional coordinate system; transforming the ray and the triangle according to the transformation matrix to obtain that the direction of the ray is along the Z axis; and projecting the triangle along the direction of the ray to the plane coordinate system formed by the X axis and the Y axis.
[0034] In some embodiments, the transformation matrix comprises the product of two scaling matrices and a shear matrix.
[0035] In some embodiments, after determining that the triangle is hit by the ray, the method further includes: determining, based on the barycentric coordinates, a hit distance of the ray in the three-dimensional coordinate system: t = (U·A z '+V·B z '+W·C z ') / det, where t is the hit distance, U, V and W are the coordinates of the center of gravity, and A z '、B z ' and C z ' are the Z-axis coordinates of the first vertex, second vertex and third vertex of the triangle in the three-dimensional coordinate system before it is projected onto the plane coordinate system.
[0036] In a third aspect, an embodiment of the present application provides a device for ray tracing, the device comprising: a first processing part, configured to project a ray and a triangle into a plane coordinate system, and determine the positional relationship between the intersection point and the projected triangle of the triangle on the plane coordinate system based on the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system; a second processing part, configured to determine whether the triangle is hit by the ray based on the coordinates of at least two vertices of the projected triangle in the plane coordinate system when the intersection point is located on a common edge or common vertex shared by the projected triangle and other triangles projected onto the plane coordinate system.
[0037] In some embodiments, the second processing part is configured to: when the intersection point is located at the common edge, determine whether the common edge satisfies a predetermined condition based on the coordinates of the two vertices of the common edge in the plane coordinate system, and when the common edge satisfies the predetermined condition, determine that the triangle is hit by the ray; when the intersection point is located at the common vertex, determine whether the two sides corresponding to the common vertex meet the predetermined condition, and when both sides corresponding to the common vertex meet the predetermined condition, determine that the triangle is hit by the ray.
[0038] In some embodiments, the predetermined condition includes at least one of the following: a predetermined relationship between the Y-axis coordinates of the two vertices of the common edge or the two vertices of each edge corresponding to the common vertex in the plane coordinate system; a predetermined relationship between the X-axis coordinates of the two vertices of the common edge or the two vertices of each edge corresponding to the common vertex in the plane coordinate system.
[0039] In some embodiments, the winding direction of the vertices of the projected triangle is clockwise CW, and along the CW direction, the two vertices of the common edge or the two vertices of the edge corresponding to the common vertex are respectively the first vertex and the second vertex of the projected triangle, and the predetermined condition includes one of the following: the Y-axis coordinate of the second vertex is greater than the Y-axis coordinate of the first vertex; the Y-axis coordinate of the second vertex is equal to the Y-axis coordinate of the first vertex, and the X-axis coordinate of the second vertex is greater than the X-axis coordinate of the first vertex.
[0040] In some embodiments, the winding direction of the vertices of the projected triangle is a counterclockwise CCW direction, and along the CCW direction, the two vertices of the common edge or the two vertices of the edge corresponding to the common vertex are respectively the first vertex and the second vertex of the projected triangle, and the predetermined condition includes one of the following: the Y-axis coordinate of the first vertex is greater than the Y-axis coordinate of the second vertex; the Y-axis coordinate of the first vertex is equal to the Y-axis coordinate of the second vertex, and the X-axis coordinate of the first vertex is greater than the X-axis coordinate of the second vertex.
[0041] In some embodiments, the second processing portion is further configured to: determine that the triangle is hit by the ray if the intersection point is located inside the projected triangle.
[0042] In some embodiments, the plane coordinate system is perpendicular to the ray, and the intersection point is the origin of the plane coordinate system.
[0043] In some embodiments, the barycentric coordinates are used to represent the areas of three triangles formed by lines connecting the intersection point and three vertices of the projected triangle.
[0044] In some embodiments, the barycentric coordinates are used to represent the proportion of the area of the projected triangle occupied by three triangles formed by connecting the intersection point and the three vertices of the projected triangle.
[0045] In some embodiments, the barycentric coordinates are: U = Cx'·By'-Cy'·Bx'; V = Ax'·Cy'-Ay'·Cx'; W = Bx'·Ay'-By'·Ax'; wherein U, V and W are the barycentric coordinates, Ax' and Ay' are the X-axis coordinate and Y-axis coordinate of the first vertex of the projected triangle, respectively, Bx' and By' are the X-axis coordinate and Y-axis coordinate of the second vertex of the projected triangle, respectively, and Cx' and Cy' are the X-axis coordinate and Y-axis coordinate of the third vertex of the projected triangle, respectively.
[0046] In some embodiments, the first processing part is configured to: when one of U, V and W is 0, determine that the intersection is located at the edge of the projected triangle; when two of U, V and W are 0, determine that the intersection is located at the vertex of the projected triangle; when U, V and W are all positive, or U, V and W are all negative, determine that the intersection is located inside the projected triangle.
[0047] In some embodiments, the first processing part is further configured to: determine the center of gravity coordinates based on a fused multiply-add FMA operation, wherein the FMA operation includes a multiplication operation and an addition operation, the multiplication operation is used to multiply the input value of the FMA operation, the input value of the FMA operation is a single-precision floating-point number, the addition operation is used to add the results of the multiplication operation, and the number of bits of the addition operation is greater than the number of bits of the multiplication operation.
[0048] In some embodiments, the number of bits of the addition operation is greater than or equal to 49 bits.
[0049] In some embodiments, the number of exponent bits aligned before the addition operation is greater than or equal to 9.
[0050] In some embodiments, the FMA operation includes two multiplication operations on two sets of input values and an addition operation on the results of the two multiplication operations, wherein, before the addition operation, the FMA operation also includes a sign inversion operation on the result of one of the multiplication operations.
[0051] In some embodiments, the FMA operation further includes a rounding operation on the result of the addition operation, so that the output value of the FMA operation is the single-precision floating-point number.
[0052] In some embodiments, the first processing part is further configured to: determine the main dimension of the ray, and use the main dimension as the Z axis of the three-dimensional coordinate system, where the main dimension is the dimension with the largest absolute value; move the ray and the triangle to obtain that the origin of the ray is located at the origin of the three-dimensional coordinate system; transform the ray and the triangle according to the transformation matrix to obtain that the direction of the ray is along the Z axis; and project the triangle along the direction of the ray to the plane coordinate system formed by the X axis and the Y axis.
[0053] In some embodiments, the transformation matrix comprises the product of two scaling matrices and a shear matrix.
[0054] In some embodiments, the first processing part is further configured to: determine the hitting distance of the ray in the three-dimensional coordinate system according to the center of gravity coordinates: t = (U·A z '+V·B z '+W·C z ') / det, where t is the hit distance, U, V and W are the coordinates of the center of gravity, and A z '、B z ' and C z ' are the Z-axis coordinates of the first vertex, second vertex and third vertex of the triangle in the three-dimensional coordinate system before it is projected onto the plane coordinate system.
[0055] In a fourth aspect, an embodiment of the present application provides a device for ray tracing, the device comprising: a first processing part, configured to project rays and triangles onto a plane coordinate system, and determine the centroid coordinates corresponding to the intersection of the rays and the plane coordinate system based on an FMA operation, wherein the FMA operation comprises a multiplication operation and an addition operation, the multiplication operation is used to multiply the input value of the FMA operation, the input value of the FMA operation is a single-precision floating-point number, the addition operation is used to add the results of the multiplication operation, and the number of bits of the addition operation is greater than the number of bits of the multiplication operation; a second processing part, configured to determine whether the triangle is hit by the ray based on the centroid coordinates.
[0056] In some embodiments, the number of bits of the addition operation is greater than or equal to 49 bits.
[0057] In some embodiments, the number of exponent bits aligned before the addition operation is greater than or equal to 9.
[0058] In some embodiments, the FMA operation includes two multiplication operations on two sets of input values and an addition operation on the results of the two multiplication operations, wherein, before the addition operation, the FMA operation also includes a sign inversion operation on the result of one of the multiplication operations.
[0059] In some embodiments, the FMA operation further includes a rounding operation on the result of the addition operation, so that the output value of the FMA operation is the single-precision floating-point number.
[0060] In some embodiments, the first processing part is further configured to: determine the main dimension of the ray, and use the main dimension as the Z axis of the three-dimensional coordinate system, where the main dimension is the dimension with the largest absolute value; move the ray and the triangle to obtain that the origin of the ray is located at the origin of the three-dimensional coordinate system; transform the ray and the triangle according to the transformation matrix to obtain that the direction of the ray is along the Z axis; and project the triangle along the direction of the ray to the plane coordinate system formed by the X axis and the Y axis.
[0061] In some embodiments, the transformation matrix comprises the product of two scaling matrices and a shear matrix.
[0062] In some embodiments, after determining that the triangle is hit by the ray, the first processing portion is further configured to: determine a hit distance of the ray in the three-dimensional coordinate system according to the barycentric coordinates.
[0063] In a fifth aspect, an embodiment of the present application provides a device for ray tracing, comprising a processor and a memory, wherein the memory stores a computer program that can be run on the processor, and when the processor executes the program, it implements the steps of the method described in the first aspect or any implementation of the first aspect, as well as the steps of the method described in the second aspect or any implementation of the second aspect.
[0064] In a sixth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the method described in the first aspect or any implementation of the first aspect, as well as the steps of the method described in the second aspect or any implementation of the second aspect.
[0065] In the seventh aspect, an embodiment of the present application provides a computer program product, including a computer program or instructions, which, when executed by a processor, implements the steps of the method described in the first aspect or any implementation of the first aspect, as well as the steps of the method described in the second aspect or any implementation of the second aspect.
[0066] In an eighth aspect, an embodiment of the present application provides a chip comprising a processor, wherein the processor is configured to call and execute a computer program from a memory to implement the steps of the method described in the first aspect or any implementation of the first aspect, and the steps of the method described in the second aspect or any implementation of the second aspect.
[0067] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0068] The drawings herein are incorporated into and constitute a part of the specification. These drawings illustrate embodiments consistent with the present application and, together with the specification, are used to illustrate the technical solutions of the present application.
[0069] FIG1 is a schematic diagram of the structure of BVH.
[0070] FIG2 is a schematic flowchart of a ray tracing method according to an embodiment of the present application.
[0071] FIG3 is a schematic diagram of a process for detecting the intersection of rays and triangles according to an embodiment of the present application.
[0072] FIG4 is a schematic diagram of a process for detecting the intersection of a ray and a triangle according to an embodiment of the present application.
[0073] FIG5 is a schematic diagram of a common side of a triangle being hit by a ray according to an embodiment of the present application.
[0074] FIG6 is a schematic diagram of a common side of a triangle being hit by a ray according to an embodiment of the present application.
[0075] FIG7 is a schematic diagram of a common side of a triangle being hit by a ray according to an embodiment of the present application.
[0076] FIG8 is a schematic diagram of a common side of a triangle being hit by a ray according to an embodiment of the present application.
[0077] FIG9 is a schematic diagram of a common vertex of a triangle being hit by a ray and not being hit by a ray according to an embodiment of the present application.
[0078] FIG10 is a schematic flowchart of a ray tracing method according to another embodiment of the present application.
[0079] FIG11 is a schematic diagram of an FMA operation unit according to an embodiment of the present application.
[0080] FIG12 is a schematic block diagram of a ray tracing apparatus according to an embodiment of the present application. DETAILED DESCRIPTION
[0081] The technical solution in this application will be described below with reference to the accompanying drawings.
[0082] A ray is defined as a ray in a ray tracing system, including an origin and a direction. In this application, "ray" is used to refer to a ray in ray tracing technology. A data structure of a bounding volume hierarchy (BVH) is usually used in ray tracing to accelerate the intersection of rays with scenes. Figure 1 is a structural diagram of BVH. BVH is a tree structure. For example, as shown in Figure 1, it includes different types of nodes, including but not limited to box nodes 410 and triangle nodes 420. Box nodes 410 are usually internal nodes, and triangle nodes 420 are leaves that store primitives in the scene. In the bounding box, each box tightly surrounds the primitives in the leaves, and then these boxes are recursively grouped into larger bounding boxes, eventually forming a tree structure. There is only one bounding box at the top of the tree.
[0083] Generally, based on the intersection test between the ray and the box, it can be determined whether the ray hits the bounding box in the box node 410 ; based on the intersection test between the ray and the triangle, it can be determined whether the ray hits the triangle in the triangle node 420 .
[0084] However, as the scene becomes more and more complex, during the intersection detection process between rays and triangles, small triangles or subdivided tiny targets may be far away from the origin of the rays. If the calculation accuracy is insufficient, cracks may appear between the triangles. That is, triangles that were originally close together may have a certain distance between them due to problems with calculation accuracy.
[0085] In addition to the accuracy issue, during the intersection detection process between rays and triangles, it is also necessary to solve the problem of the ray hitting the common edge or common vertex between multiple triangles. When the ray hits the common edge or common vertex of multiple triangles, it is necessary to determine which triangle the ray hits.
[0086] To this end, an embodiment of the present application provides a solution for intersection detection between rays and triangles. When a ray hits a common edge or common vertex of a triangle, the coordinates of the vertices of the triangle are used to determine whether the triangle is hit by the ray, so as to solve the problem of the ray hitting the common edge or common vertex between multiple triangles and ensure the stability of the intersection detection results.
[0087] In addition, an embodiment of the present application also provides a solution for intersection detection of rays and triangles, which is based on the Fusion Multiplication and Addition (FMA) operation to calculate the centroid coordinates corresponding to the intersection of the ray and the triangle, and increase the number of bits of the addition operation in the FMA operation to eliminate the internal precision loss in the calculation process of the centroid coordinates, thereby accurately obtaining the centroid coordinates and ensuring the watertightness of the intersection detection results.
[0088] FIG2 is a schematic flowchart of a ray tracing method according to an embodiment of the present application. Ray tracing method 100 can be applied to a hardware accelerator, such as a hardware accelerator on a graphics processing unit (GPU), for use in gaming and other visualization applications. As shown in FIG2 , ray tracing method 100 may include some or all of the following steps.
[0089] In step 110 , the rays and triangles are projected into a planar coordinate system.
[0090] For example, the plane coordinate system is perpendicular to the ray, and the intersection of the ray and the plane coordinate system is the origin of the plane coordinate system. In other words, the triangle is projected onto the plane coordinate system along a direction perpendicular to the ray.
[0091] In step 120 , the positional relationship between the intersection point and the projection triangle of the triangle on the plane coordinate system is determined based on the barycentric coordinates corresponding to the intersection point of the ray and the plane coordinate system.
[0092] In step 130 , when the intersection point is located at a common edge or common vertex shared by the projected triangle and other triangles projected into the plane coordinate system, it is determined whether the triangle is hit by the ray based on the coordinates of at least two vertices of the projected triangle in the plane coordinate system.
[0093] During the intersection detection process between a ray and a triangle, the ray and triangle are projected onto a plane coordinate system. Based on the barycentric coordinates corresponding to the intersection of the ray and the plane coordinate system, the positional relationship between the intersection point and the projected triangle of the triangle in the plane coordinate system is determined. The intersection point may be located inside the projected triangle, outside the projected triangle, at a vertex of the projected triangle, or on an edge of the projected triangle. Since multiple triangles may share edges or vertices, when the intersection point is located on a common edge or vertex of the projected triangle, the coordinates of at least two vertices of the projected triangle can accurately determine whether the ray hits the triangle, thereby ensuring the stability of the intersection detection results.
[0094] It can be understood that since the transformation matrix used during projection is linear, the positional relationship between the ray and the triangle before and after projection is consistent. In other words, the positional relationship between the ray and the triangle before projection is consistent with the positional relationship between the intersection of the ray and the plane coordinate system and the projected triangle after projection onto the plane coordinate system. For example, if the ray hits the edge of the triangle, the intersection will also be on the edge of the projected triangle after projection; if the ray hits the vertex of the triangle, the intersection will also be on the vertex of the projected triangle after projection; if the ray hits the interior of the triangle, the intersection will also be inside the projected triangle after projection.
[0095] Before step 120 , it is necessary to obtain the centroid coordinates corresponding to the intersection point between the ray and the plane coordinate system based on the intersection detection process between the ray and the triangle.
[0096] For intersection detection between rays and triangles, a ray and a triangle need to be defined. Usually, the ray is transformed from world space to the object space of the geometry. After that, the ray and the triangle should be in the same three-dimensional coordinate system. After the transformation, the ray and the triangle can be projected to a selected plane coordinate system, such as the XY coordinate system. There is an intersection point between the ray and the plane coordinate system, and its corresponding centroid coordinates can be calculated using the known projection parameters. For example, the centroid coordinates corresponding to the intersection point can be calculated based on the X-axis coordinates and Y-axis coordinates of the three vertices of the projected triangle.
[0097] As an example, the embodiment of the present application can determine the center of gravity coordinates based on the intersection detection method of rays and triangles described below.
[0098] First, define rays and triangles in object space.
[0099] The ray passes through the origin direction And parameter t is expressed as formula (1):
[0100] in, Tmin≤t≤Tmax.
[0101] Among them, Tmin and Tmax are the minimum and maximum values respectively. x ,o y ,o z The origin The coordinates of the X-axis, Y-axis and Z-axis, d x ,d y ,d z Directions The coordinates on the X, Y, and Z axes.
[0102] Among them, a triangle is represented by three vertices, which are
[0103] Among them, a x ,a y ,a z Vertex In the X-axis, Y-axis and Z-axis coordinates, b x ,b y ,b z Vertex In the X-axis, Y-axis and Z-axis coordinates, c x ,c y ,c z Vertex The coordinates on the X, Y, and Z axes.
[0104] Secondly, the intersection test between the ray and the triangle is performed through the following steps.
[0105] In step 101, the main dimension of the ray direction is determined. For example, the dimension with the largest absolute value is taken as the main dimension, that is, formula (2):
[0106] Among them, major is the main dimension, Dim x , Dim y , Dim z These are the dimensions of the X-axis, Y-axis, and Z-axis respectively.
[0107] In step 102, the axis is rotated (swizzled) so that the main dimension becomes the Z axis of the three-dimensional coordinate system. In order to maintain the winding order, when the ray direction of the main dimension is negative, the X axis and the Y axis need to be swapped. That is, formula (3):
[0108] FIG3 is a schematic diagram of the intersection detection process of a ray and a triangle according to an embodiment of the present application, FIG3 shows the process shown in step 101 and step 102. ... x , and d x <0 as an example. In Figure 3, the three vertices of triangle 301, namely vertex A, vertex B, and vertex C, are located on the X-axis, Y-axis, and Z-axis, respectively. After rotation, triangle 302 is obtained, in which vertex A, vertex B, and vertex C are located on the Z-axis, X-axis, and Y-axis, respectively. The positions of vertex B and vertex C are swapped to obtain triangle 303.
[0109] Then, the origin O of the ray is expressed as formula (4):
[0110] Among them, O x ,O y ,Oz are the coordinates of the origin O on the X-axis, Y-axis, and Z-axis after rotation, respectively.
[0111] Direction D is expressed as formula (5):
[0112] Among them, D x ,D y ,D z are the coordinates of the rotated direction D on the X-axis, Y-axis, and Z-axis respectively.
[0113] The vertices A, B, and C of the triangle are expressed as formulas (6) to (8), respectively:
[0114] Among them, A x ,A y ,A z are the coordinates of the rotated vertex A on the X-axis, Y-axis, and Z-axis, respectively, x ,B y ,B z are the coordinates of the rotated vertex B on the X-axis, Y-axis, and Z-axis, respectively, x ,C y ,C z are the coordinates of the rotated vertex C on the X-axis, Y-axis, and Z-axis respectively.
[0115] In step 103 , the ray and the triangle are moved so that the origin of the ray is located at the origin of the three-dimensional coordinate system, thereby converting to the ray space.
[0116] At this time, the origin of the ray becomes (0,0,0), and the direction of the ray is still D=(D x ,D y ,D z ).
[0117] The vertices of the triangle are formula (9) to formula (11): A'=(AO) formula (9); B'=(BO) formula (10); C'=(CO) formula (11).
[0118] In step 104, the ray and triangle are transformed according to the transformation matrix so that the direction of the ray is along the Z axis. For example, the transformation matrix includes the product of two scaling matrices and a shear matrix, that is, formula (12):
[0119] Since the rounding error introduced by shearing is smaller, the computational efficiency is higher when using this transformation matrix.
[0120] After steps 103 and 104 , the origin of the ray becomes (0, 0, 0), and the direction of the ray becomes (0, 0, 1), that is, the ray becomes a unit vector pointing in the positive direction of the Z axis.
[0121] Among them, the vertices of the triangle become formula (13) to formula (15): A'=M*(AO) Formula (13); B'=M*(BO) Formula (14); C'=M*(CO) Formula (15).
[0122] FIG4 is a schematic diagram of the ray-triangle intersection detection process according to an embodiment of the present application, and illustrates the process shown in steps 103 and 104. In FIG4 , the origin of ray 401 is translated to the origin of the three-dimensional coordinate system to obtain the position of ray 402. The transformation matrix is then used to scale and shear the ray 403 so that the direction is along the positive Z axis.
[0123] In step 105 , the triangle is projected along the direction of the ray to the plane coordinate system of the ray space, that is, the plane coordinate system formed by the X-axis and the Y-axis, to obtain its projected triangle, and the barycentric coordinates are calculated.
[0124] In some embodiments, the barycentric coordinates are given by formulas (16) to (18): U = Cx'·By'-Cy'·Bx' (16); V = Ax'·Cy'-Ay'·Cx' (17); W = Bx'·Ay'-By'·Ax' (18);
[0125] Where U, V, and W are the barycentric coordinates, Ax' and Ay' are the X-axis and Y-axis coordinates of the first vertex of the projected triangle, Bx' and By' are the X-axis and Y-axis coordinates of the second vertex of the projected triangle, and Cx' and Cy' are the X-axis and Y-axis coordinates of the third vertex of the projected triangle. For example, the first, second, and third vertices correspond to Vertex A, Vertex B, and Vertex C described above, respectively.
[0126] U, V, and W can, for example, represent the areas of the three small triangles formed by the intersection of the ray and the plane coordinate system and the lines connecting the first, second, and third vertices of the projected triangle. Below, U, V, and W can also be replaced by u, v, and w, respectively. u, v, and w are obtained by normalizing U, V, and W, respectively. u, v, and w, for example, represent the ratio of the areas of the three triangles formed by the lines connecting the intersection and the first, second, and third vertices of the projected triangle to the area of the projected triangle. In this case, u+v+w=1.
[0127] The area of the triangle changes before and after projection, which is equivalent to scaling the triangle. However, the ratio of the area of each of the three small triangles formed by the line connecting the intersection point of the ray on the triangle and the three vertices of the triangle to the total area of the triangle before projection is the same as the ratio of the area of each of the three small triangles formed by the line connecting the intersection point and the three vertices of the projected triangle after projection to the total area of the projected triangle.
[0128] Next, the above step 120 is executed, that is, the positional relationship between the intersection point and the projected triangle is determined based on the values of U, V, and W. The positional relationship may include the following:
[0129] a) When U, V, and W are all positive, or when U, V, and W are all negative, the intersection point lies inside the projected triangle;
[0130] b) When U, V, and W are both positive and negative, the intersection point is outside the projected triangle;
[0131] c) When one of U, V, and W is 0, the intersection point is located on the edge of the projected triangle;
[0132] d) If two of U, V, and W are 0, the intersection point is determined to be at the vertex of the projected triangle;
[0133] e) When U, V, and W are all 0, it indicates that the ray is coplanar with the triangle, that is, the intersection is lost.
[0134] In step 106, the determinant is calculated, that is, formula (19):
[0135] det=U+V+W formula (19).
[0136] In step 107, when it is determined that the ray hits the triangle, the hitting distance of the ray in the three-dimensional coordinate system is determined according to the coordinates of the center of gravity, that is, formula (20): t=(U·A z '+V·B z '+W·C z ') / det formula (20);
[0137] Among them, t is the hit distance, U, V and W are the coordinates of the center of gravity, A z '、B z ' and C z ' are the Z-axis coordinates of the first, second, and third vertices of the triangle in the three-dimensional coordinate system before it is projected onto the plane coordinate system.
[0138] For case a) above, that is, the intersection point is inside the projected triangle, then the triangle is determined to be hit by the ray.
[0139] For cases b) and e) above, it is determined that the triangle is not hit by the ray.
[0140] For the above cases c) and d), that is, the intersection point is located at an edge or vertex of the projected triangle. In this case, if the edge where the intersection point is located is a common edge of the projected triangle, that is, the projected triangle shares this edge with other triangles projected into the same plane coordinate system, it is necessary to determine whether the triangle is hit by the ray through the above step 120; if the vertex where the intersection point is located is a common vertex of the projected triangle, that is, the projected triangle shares this vertex with other triangles, it is necessary to determine whether the triangle is hit by the ray through the above step 120.
[0141] In some embodiments, in step 120, when the intersection point of the ray and the plane coordinate system is located on a common edge of the projected triangle, it is determined whether the common edge satisfies a predetermined condition based on the coordinates of two vertices of the common edge in the plane coordinate system, and when the common edge satisfies the predetermined condition, it is determined that the triangle is hit by the ray;
[0142] When the intersection of the ray and the plane coordinate system is located at the common vertex of the projected triangle, it is determined whether the two sides corresponding to the common vertex meet the predetermined condition, and when the two sides corresponding to the common vertex both meet the predetermined condition, it is determined that the triangle is hit by the ray.
[0143] Figures 5 to 8 are schematic diagrams of a triangle's common edge being hit by a ray, and Figure 9 is a schematic diagram of a triangle's common vertex being hit by a ray and not being hit by a ray, according to an embodiment of the present application. Below, in conjunction with Figures 5 to 9 , we describe in detail how to determine whether a ray hits a triangle based on the coordinates of at least two vertices of the projected triangle, for the two cases where the intersection point is located on a common edge and the intersection point is located on a common vertex.
[0144] The winding direction, or winding order, of the projected triangle includes a clockwise (CW) direction and a counterclockwise (CCW) direction. The winding direction of the projected triangle can be determined based on the sign of the determinant calculated above. For example, if the sign of the determinant is positive, the projected triangle is in a CW direction, and if the sign of the determinant is negative, the projected triangle is in a CCW direction; alternatively, if the sign of the determinant is positive, the projected triangle is in a CCW direction, and if the sign of the determinant is negative, the projected triangle is in a CW direction.
[0145] The predetermined conditions include, for example: a predetermined relationship between the Y-axis coordinates of the two vertices of the common edge or the two vertices of each edge corresponding to the common vertex in the plane coordinate system; a predetermined relationship between the X-axis coordinates of the two vertices of the common edge or the two vertices of each edge corresponding to the common vertex in the plane coordinate system.
[0146] The predetermined condition when the projected triangle is CW is different from the predetermined condition when the projected triangle is CW, for example, they may be opposite.
[0147] 5 and 6 show the case where the winding direction is the CW direction, and FIG. 7 to FIG. 9 show the case where the winding direction is the CCW direction.
[0148] In some embodiments, the winding direction of the vertices of the projected triangle is a CW direction, and along the CW direction, the two vertices of the common side where the intersection is located, or the two vertices of the side corresponding to the common vertex where the intersection is located, are respectively the first vertex v0 and the second vertex v1 of the projected triangle, then the predetermined conditions in step 120 include:
[0149] The Y-axis coordinate of the second vertex v1 is greater than the Y-axis coordinate of the first vertex v0;
[0150] The Y-axis coordinate of the second vertex v1 is equal to the Y-axis coordinate of the first vertex v0, and the X-axis coordinate of the second vertex v1 is greater than the X-axis coordinate of the first vertex.
[0151] For example, as shown in Figure 5 , v0, v1, and v2 are the first, second, and third vertices of the projected triangle 501 in the plane coordinate system, respectively. As shown in Figure 5 , the winding direction of the projected triangle 501 is the CW direction, and the intersection of the ray and the plane coordinate system is located on the common edge v0v1 of the projected triangle. The Y-axis coordinate of the second vertex v1 is greater than the Y-axis coordinate of the first vertex v0, that is, v1.y>v0.y. Therefore, the common edge v0v1 is called the left boundary.
[0152] For another example, as shown in Figure 6, the winding direction of the projected triangle 601 is the CW direction, the intersection of the ray and the plane coordinate system is located on the common edge v0v1 of the projected triangle, the Y-axis coordinate of the second vertex v1 is equal to the Y-axis coordinate of the first vertex v0, and the X-axis coordinate of the second vertex v1 is greater than the X-axis coordinate of the first vertex, that is, v1.y=v0.y&&v1.x>v0.x, then the common edge v0v1 is called the upper boundary.
[0153] When the common edge v0v1 is the left boundary or the upper boundary, that is, the common edge v0v1 meets the predetermined conditions of the left boundary or the upper boundary, it is considered that the ray hits the common edge v0v1.
[0154] In some embodiments, the winding direction of the vertices of the projected triangle is a CCW direction, and along the CCW direction, the two vertices of the common side, or the two vertices of the side corresponding to the common vertex, are the first vertex v0 and the second vertex v1 of the projected triangle, respectively. Then, the predetermined conditions in step 120 include:
[0155] The Y-axis coordinate of the first vertex v0 is greater than the Y-axis coordinate of the second vertex v1;
[0156] The Y-axis coordinate of the first vertex v0 is equal to the Y-axis coordinate of the second vertex v1 , and the X-axis coordinate of the first vertex v0 is greater than the X-axis coordinate of the second vertex v1 .
[0157] For example, as shown in Figure 7, v0, v1, and v2 are the first, second, and third vertices of the projected triangle 701 in the plane coordinate system, respectively. As shown in Figure 7, the winding direction of the projected triangle 701 is CCW, and the intersection of the ray and the plane coordinate system is located on the common edge v0v1 of the projected triangle. The Y-axis coordinate of the first vertex v0 is greater than the Y-axis coordinate of the second vertex v1, that is, v0.y>v1.y. Therefore, the common edge v0v1 is called the left boundary.
[0158] For another example, as shown in FIG8 , the winding direction of the projected triangle 801 is the CCW direction, the intersection of the ray and the plane coordinate system is located on the common edge v0v1 of the projected triangle 801, the Y-axis coordinate of the first vertex v0 is equal to the Y-axis coordinate of the second vertex v1, and the X-axis coordinate of the first vertex v0 is greater than the X-axis coordinate of the second vertex v1, that is, v0.y=v1.y&&v0.x>v1.x, then the common edge v0v1 is called the upper boundary.
[0159] When the common edge v0v1 is the left boundary or the upper boundary, that is, the common edge v0v1 meets the predetermined conditions of the left boundary or the upper boundary, it is considered that the ray hits the common edge v0v1.
[0160] That is to say, when multiple triangles have a common edge in a plane coordinate system, the common edge is determined based on the above method to determine in which triangle the left boundary or the upper boundary of the triangle belongs, and it is considered that the ray hits the triangle but not the other triangles.
[0161] In addition, when the intersection of the ray and the plane coordinate system is located at the common vertex of the triangle, it is necessary to perform the above judgment on the two sides corresponding to the common vertex respectively, that is, to judge whether these two sides meet the predetermined conditions of the left boundary or the upper boundary.
[0162] For example, as shown in Figure 9, the winding direction of the projected triangle 901 is the CCW direction. When the intersection of the ray and the plane coordinate system is located at the common vertex v0 of the projected triangle 901, that is, intersection 1, the two edges corresponding to the common vertex v0 are v0v1 and v2v0. For the edge v0v1, the Y-axis coordinate of the first vertex v0 is greater than the Y-axis coordinate of the second vertex v1, that is, v0.y>v1.y, then the edge v0v1 is the left boundary. For the edge v2v0, the Y-axis coordinate of the third vertex v2 is greater than the Y-axis coordinate of the first vertex v0, that is, v2.y>v0.y, then the edge v2v0 is also the left boundary. Therefore, the triangle is hit by the ray.
[0163] When the intersection of the ray and the plane coordinate system is located at the common vertex v2 of the projected triangle 901, that is, intersection 2, the two edges corresponding to the common vertex v0 are v1v2 and v2v0. For v2v0, the Y-axis coordinate of the third vertex v2 is greater than the Y-axis coordinate of the first vertex v0, that is, v2.y>v0.y, then the edge v2v0 is also the left boundary. However, for the edge v1v2, the Y-axis coordinate of the second vertex v1 is less than the Y-axis coordinate of the third vertex v2, that is, v1.y<v2.y, then the edge v1v2 is not the left boundary. Therefore, the two edges corresponding to the common vertex v2 do not both meet the predetermined conditions of the left boundary and the upper boundary. Therefore, the triangle is not hit by the ray.
[0164] It can be seen that since the result of the intersection detection between rays and triangles is certain, the uncertainty caused by the ray hitting the common edge or common vertex of multiple triangles is eliminated, and the stability of the intersection detection result is guaranteed, which is also referred to as robustness here.
[0165] As mentioned above, barycentric coordinates play a crucial role in the results of ray-triangle intersection detection, as well as the ray's hit position within the triangle. Therefore, embodiments of the present application also provide a ray-triangle intersection detection solution to improve the calculation accuracy of barycentric coordinates and ensure the watertightness of the intersection detection results.
[0166] This embodiment of the present application also provides a ray tracing method 200. This ray tracing method 200 can be applied to a hardware accelerator, such as a hardware accelerator on a GPU, for use in gaming and other visualization applications. Figure 10 is a schematic flow chart of a ray tracing method according to another embodiment of the present application. As shown in Figure 10, the ray tracing method 200 includes some or all of the following steps.
[0167] In step 210 , the rays and triangles are projected into a planar coordinate system.
[0168] In step 220, the coordinates of the center of gravity corresponding to the intersection of the ray and the plane coordinate system are determined according to the FMA operation.
[0169] In step 230, it is determined whether the triangle is hit by the ray based on the barycenter coordinates.
[0170] Among them, the FMA operation includes multiplication operation and addition operation. The multiplication operation is used to multiply the input value of the FMA operation. The input value of the FMA operation is a single-precision floating-point number. The addition operation is used to add the results of the multiplication operation. The number of bits of the addition operation is greater than the number of bits of the multiplication operation.
[0171] During the intersection detection process between a ray and a triangle, the ray and triangle are projected onto a plane coordinate system. The FMA operation is used to determine the barycentric coordinates corresponding to the intersection of the ray and the plane coordinate system. Based on these barycentric coordinates, it is determined whether the triangle is hit by the ray. Because the calculation of the barycentric coordinates uses the FMA operation, which includes multiplication and addition operations, and the number of bits used in the addition operation is larger than the number of bits used in the single-precision floating-point input for the multiplication operation, the internal precision loss during the calculation of the barycentric coordinates is eliminated, and the barycentric coordinates can be accurately obtained, thus ensuring the watertightness of the intersection detection results.
[0172] For example, the number of bits used in the addition operation may be greater than or equal to 49 bits.
[0173] In some embodiments, the exponent aligned before the addition operation is greater than or equal to 9. In this way, it can be ensured that overflow or underflow does not occur before the addition operation.
[0174] Based on the calculation formulas of U, V and W mentioned above, namely U=Cx'·By'-Cy'·Bx', V=Ax'·Cy'-Ay'·Cx', W=Bx'·Ay'-By'·Ax', the FMA operation can be defined as A[0]*B[0]-A[1]*B[1]. That is, the FMA operation specifically includes two multiplication operations on two sets of input values, and an addition operation on the results of the two multiplication operations, wherein, before the addition operation, the FMA operation also includes a sign negation operation on the result of one of the multiplication operations. The FMA operation does not support subnormal numbers. If the input value of the FMA operation is a non-normal number, it will be refreshed to 0. In order to make the output value of the FMA operation also a single-precision floating-point number, the FMA operation also includes a rounding operation on the result of the addition operation.
[0175] Figure 11 is a schematic diagram of an FMA operation unit of an embodiment of the present application. The FMA operation unit includes two multipliers (510 and 520) and an adder 530. The input value of the FMA operation unit is a single-precision floating-point number. Usually, a single-precision floating-point number is called FP32, which includes a 1-bit sign bit, an 8-bit exponent, a 23-bit mantissa, and a 1-bit leading bit. Usually, s1.e8.m1.23 is used to represent an FP32 floating-point number, and the result of multiplying two FP32 floating-point numbers is represented here by s1.e9.m2.46.
[0176] After two multipliers (510 and 520) perform multiplication operations on the input values A[0] and B[0], and on the input values A[1] and B[1], respectively, two values in the format of s1.e9.m2.46 are obtained, namely A[0]*B[0] and A[1]*B[1]. The sign of the value of A[1]*B[1] is inverted 540 to obtain the value of -A[1]*B[1]. After that, the exponent bits are aligned through a shift operation 550, such as >>. The number of aligned exponent bits can be greater than or equal to 9. Finally, the adder 530 adds the value of A[0]*B[0] and the value of -A[1]*B[1] to obtain a value in the format of s1.e9.m3.46. The result of the adder is normalized and rounded 560, and the output value of the FMA operation unit is FP32.
[0177] Method 200 can be applied to hardware accelerators. Since hardware accelerators are relatively expensive, they usually do not have double-precision computing units. However, when using method 200 to calculate the center of gravity coordinates, even if single-precision floating-point operations are used, no cracks will appear between triangles due to calculation accuracy issues.
[0178] Both the ray tracing method 100 and the ray tracing method 200 provided in the embodiments of the present application can improve the performance of intersection detection between rays and triangles. According to the ray tracing method 100, when the intersection point of a ray and a plane coordinate system is located on a common edge or common vertex of a projected triangle, it can accurately determine whether the ray hits the triangle, thereby ensuring the robustness of the intersection detection results. According to the ray tracing method 200, the calculation accuracy of the center of gravity coordinates is improved, ensuring the watertightness of the intersection detection results.
[0179] Ray tracing method 100 and ray tracing method 200 can be performed separately or in combination. For example, when calculating the barycentric coordinates corresponding to the intersection of a ray and a plane coordinate system, an FMA operation can be used to meet the requirement of watertightness. Furthermore, when the intersection is determined to be located on a common edge or common vertex of a projected triangle based on the barycentric coordinates, whether the triangle is hit by the ray can be determined based on the coordinates of at least two vertices of the projected triangle in the plane coordinate system, thereby meeting the requirement of robustness.
[0180] An embodiment of the present application also provides a device for ray tracing. FIG12 is a schematic block diagram of the device for ray tracing according to an embodiment of the present application. As shown in FIG12 , the device 300 for ray tracing includes a first processing part 310 and a second processing part 320 .
[0181] The first processing part 310 is configured to project the ray and the triangle into a plane coordinate system, and determine the positional relationship between the intersection point and the projected triangle of the triangle on the plane coordinate system based on the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system.
[0182] The second processing part 320 is configured to determine whether the triangle is hit by the ray according to the coordinates of at least two vertices of the projected triangle in the plane coordinate system when the intersection point is located on a common edge or common vertex shared by the projected triangle and other triangles projected into the plane coordinate system.
[0183] In some embodiments, the second processing part 320 is configured to: when the intersection point is located at the common edge, determine whether the common edge meets the predetermined condition based on the coordinates of the two vertices of the common edge in the plane coordinate system, and when the common edge meets the predetermined condition, determine that the triangle is hit by the ray; when the intersection point is located at the common vertex, determine whether the two edges corresponding to the common vertex meet the predetermined condition, and when both sides corresponding to the common vertex meet the predetermined condition, determine that the triangle is hit by the ray.
[0184] In some embodiments, the predetermined condition includes at least one of the following: a predetermined relationship between the Y-axis coordinates of at least two vertices of the projected triangle in the plane coordinate system; a predetermined relationship between the X-axis coordinates of at least two vertices of the projected triangle in the plane coordinate system.
[0185] In some embodiments, the at least two vertices of the projected triangle include two vertices of the common side, or two vertices of the side corresponding to the common vertex.
[0186] In some embodiments, the winding direction of the vertices of the projected triangle is a CW direction, and along the CW direction, the two vertices of the common edge or the two vertices of the edge corresponding to the common vertex are respectively the first vertex and the second vertex of the projected triangle, and the predetermined condition includes one of the following: the Y-axis coordinate of the second vertex is greater than the Y-axis coordinate of the first vertex; the Y-axis coordinate of the second vertex is equal to the Y-axis coordinate of the first vertex, and the X-axis coordinate of the second vertex is greater than the X-axis coordinate of the first vertex.
[0187] In some embodiments, the winding direction of the vertices of the projected triangle is a CCW direction, and along the CCW direction, the two vertices of the common edge or the two vertices of the edge corresponding to the common vertex are respectively the first vertex and the second vertex of the projected triangle, and the predetermined condition includes one of the following: the Y-axis coordinate of the first vertex is greater than the Y-axis coordinate of the second vertex; the Y-axis coordinate of the first vertex is equal to the Y-axis coordinate of the second vertex, and the X-axis coordinate of the first vertex is greater than the X-axis coordinate of the second vertex.
[0188] In some embodiments, the second processing portion 320 is further configured to: determine that the triangle is hit by the ray if the intersection point is located inside the projected triangle.
[0189] In some embodiments, the barycentric coordinates are given by formulas (21) to (23): U = Cx'·By'-Cy'·Bx' Formula (21); V = Ax'·Cy'-Ay'·Cx' Formula (22); W = Bx'·Ay'-By'·Ax' Formula (23);
[0190] Wherein, U, V, and W are the coordinates of the center of gravity, Ax' and Ay' are the X-axis coordinate and Y-axis coordinate of the first vertex of the projected triangle, Bx' and By' are the X-axis coordinate and Y-axis coordinate of the second vertex of the projected triangle, and Cx' and Cy' are the X-axis coordinate and Y-axis coordinate of the third vertex of the projected triangle.
[0191] In some embodiments, the first processing portion 310 is configured to: when one of U, V, and W is 0, determine that the intersection is located at the edge of the projected triangle; when two of U, V, and W are 0, determine that the intersection is located at the vertex of the projected triangle; and when U, V, and W are all positive, or U, V, and W are all negative, determine that the intersection is located inside the projected triangle.
[0192] In some embodiments, the first processing part 310 is further configured to: determine the center of gravity coordinates based on an FMA operation, wherein the FMA operation includes a multiplication operation and an addition operation, the multiplication operation is used to multiply the input value of the FMA operation, the input value of the FMA operation is a single-precision floating-point number, the addition operation is used to add the results of the multiplication operation, and the number of bits of the addition operation is greater than the number of bits of the multiplication operation.
[0193] In some embodiments, the number of bits of the addition operation is greater than or equal to 49 bits.
[0194] In some embodiments, the number of exponent bits aligned before the addition operation is greater than or equal to 9.
[0195] In some embodiments, the FMA operation includes two multiplication operations on two sets of input values and an addition operation on the results of the two multiplication operations, wherein, before the addition operation, the FMA operation further includes a sign inversion operation on the result of one of the multiplication operations.
[0196] In some embodiments, the FMA operation further includes a rounding operation on the result of the addition operation, so that the output value of the FMA operation is a single-precision floating-point number.
[0197] In some embodiments, the first processing part 310 is further configured to: determine the main dimension of the ray, and use the main dimension as the Z axis of the three-dimensional coordinate system, where the main dimension is the dimension with the largest absolute value; move the ray and the triangle to obtain that the origin of the ray is located at the origin of the three-dimensional coordinate system; transform the ray and the triangle according to the transformation matrix to obtain that the direction of the ray is along the positive direction of the Z axis; and project the triangle along the direction of the ray to the plane coordinate system formed by the X axis and the Y axis.
[0198] In some embodiments, the transformation matrix comprises the product of two scaling matrices and a shear matrix.
[0199] In some embodiments, the first processing part 310 is further configured to: determine the hitting distance of the ray in the three-dimensional coordinate system according to the barycentric coordinates as formula (24): t=(U·A z '+V·B z '+W·C z ') / det formula (24);
[0200] Among them, t is the hitting distance, U, V and W are the coordinates of the center of gravity, A z '、B z ' and C z ' are the Z-axis coordinates of the first, second and third vertices of the triangle in the three-dimensional coordinate system before it is projected onto the plane coordinate system.
[0201] For specific details of the device 300 for ray tracing, reference may be made to the aforementioned description of the method embodiment, which will not be repeated here for the sake of brevity.
[0202] The present application also provides a device for ray tracing, including a processor and a memory, wherein the memory stores a computer program executable on the processor, and when the processor executes the program, the steps of the method in any of the above embodiments are implemented. For example, the processor may be a GPU.
[0203] The present application also provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the steps of the method in any of the above embodiments. For example, the computer-readable storage medium is a memory.
[0204] An embodiment of the present application further provides a computer program product, including a computer program or instructions, which, when executed by a processor, implements the steps of the method in any of the above embodiments.
[0205] The present application also provides a chip including a processor configured to call and execute a computer program from a memory to implement the steps of the method in any of the above embodiments. For example, the chip may be a GPU.
[0206] The above-mentioned memory may be a volatile memory or a non-volatile memory, or the memory may include both volatile memory and non-volatile memory. Among them, the volatile memory includes, for example, random access memory (RAM); the non-volatile memory includes, for example, read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory.
[0207] It should be noted that, under the premise of no conflict, the various embodiments and / or technical features in each embodiment described in this application can be arbitrarily combined with each other, and the technical solution obtained after the combination should also fall within the scope of protection of this application.
[0208] The systems, devices, and methods disclosed in the embodiments of the present application may be implemented in other ways. For example, some features of the method embodiments described above may be ignored or not performed. The device embodiments described above are merely schematic, and the division of parts is merely a logical function division. In actual implementation, there may be other division methods, and multiple parts or components may be combined or integrated into another system. In addition, the coupling between the parts or the coupling between the components may be direct coupling or indirect coupling, and the coupling may include electrical, mechanical, or other forms of connection.
[0209] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described devices and equipment and the technical effects produced can refer to the corresponding processes and technical effects in the aforementioned method embodiments, and will not be repeated here.
[0210] It should be understood that the specific examples in the embodiments of the present application are only to help those skilled in the art to better understand the embodiments of the present application, rather than to limit the scope of the embodiments of the present application. Those skilled in the art can make various improvements and modifications based on the above embodiments, and these improvements or modifications all fall within the scope of protection of the present application.
[0211] The above description is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims. Industrial Applicability
[0212] The embodiments of the present application provide a ray tracing method, an apparatus, a device, a computer-readable storage medium, and a computer program product for ray tracing, which can improve the performance of intersection detection between rays and triangles. The method includes: projecting the ray and the triangle into a plane coordinate system; determining the positional relationship between the intersection point and the projected triangle of the triangle on the plane coordinate system based on the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system; when the intersection point is located on a common edge or common vertex shared by the projected triangle and other triangles, determining whether the triangle is hit by the ray based on the coordinates of at least two vertices of the projected triangle in the plane coordinate system. In this way, it is possible to accurately determine whether the ray hits the triangle, ensuring the stability of the intersection detection result.
Claims
1. A ray tracing method, applied to a ray tracing device, the method comprising: Project rays and triangles to a plane coordinate system; Determine, according to the barycentric coordinates corresponding to the intersection of the ray and the plane coordinate system, a positional relationship between the intersection and a projection triangle of the triangle on the plane coordinate system; When the intersection point is located at a common edge or common vertex shared by the projected triangle and other triangles projected onto the plane coordinate system, it is determined whether the triangle is hit by the ray based on the coordinates of at least two vertices of the projected triangle in the plane coordinate system.
2. The method according to claim 1, wherein: The determining, when the intersection point is located at a common edge or common vertex shared by the projected triangle and other triangles projected onto the plane coordinate system, whether the triangle is hit by the ray according to coordinates of at least two vertices of the projected triangle in the plane coordinate system comprises at least one of the following: In the case where the intersection point is located on the common edge, determining whether the common edge satisfies a predetermined condition according to the coordinates of two vertices of the common edge in the plane coordinate system, and determining that the triangle is hit by the ray in the case where the common edge satisfies the predetermined condition; When the intersection point is located at the common vertex, it is determined whether the two edges corresponding to the common vertex meet the predetermined condition, and when both the two edges corresponding to the common vertex meet the predetermined condition, it is determined that the triangle is hit by the ray.
3. The method according to claim 2, wherein: The predetermined condition includes at least one of the following: A predetermined relationship between the Y-axis coordinates of two vertices of the common side or two vertices of the side corresponding to the common vertex in the plane coordinate system; A predetermined relationship between the X-axis coordinates of the two vertices of the common edge or the two vertices of the edge corresponding to the common vertex in the plane coordinate system.
4. The method according to claim 3, wherein: The winding direction of the vertices of the projected triangle is a clockwise CW direction, and along the CW direction, the two vertices of the common side or the two vertices of the side corresponding to the common vertex are the first vertex and the second vertex of the projected triangle, respectively, and the predetermined condition includes one of the following: The Y-axis coordinate of the second vertex is greater than the Y-axis coordinate of the first vertex; The Y-axis coordinate of the second vertex is equal to the Y-axis coordinate of the first vertex, and the X-axis coordinate of the second vertex is greater than the X-axis coordinate of the first vertex.
5. The method according to claim 3, wherein: The winding direction of the vertices of the projected triangle is a counterclockwise CCW direction, and along the CCW direction, the two vertices of the common side or the two vertices of the side corresponding to the common vertex are respectively the first vertex and the second vertex of the projected triangle, and the predetermined condition includes one of the following: The Y-axis coordinate of the first vertex is greater than the Y-axis coordinate of the second vertex; The Y-axis coordinate of the first vertex is equal to the Y-axis coordinate of the second vertex, and the X-axis coordinate of the first vertex is greater than the X-axis coordinate of the second vertex.
6. The method according to any one of claims 1 to 5, wherein: The method further comprises: In case the intersection point is located inside the projected triangle, it is determined that the triangle is hit by the ray.
7. The method according to any one of claims 1 to 5, wherein: The plane coordinate system is perpendicular to the ray, and the intersection point is the origin of the plane coordinate system.
8. The method according to any one of claims 1 to 5, wherein: The barycentric coordinates are used to represent the areas of the three triangles formed by the lines connecting the intersection point and the three vertices of the projected triangle.
9. The method according to any one of claims 1 to 5, wherein: The barycentric coordinates are used to represent the proportion of the area of the projected triangle occupied by three triangles formed by connecting the intersection point and the three vertices of the projected triangle.
10. The method according to any one of claims 1 to 5, wherein: The centroid coordinates are: U = Cx'·By'-Cy'·Bx'; V = Ax'·Cy'-Ay'·Cx'; W = Bx'·Ay'-By'·Ax'; Among them, U, V and W are the centroid coordinates, Ax' and Ay' are the X-axis coordinate and Y-axis coordinate of the first vertex of the projected triangle, Bx' and By' are the X-axis coordinate and Y-axis coordinate of the second vertex of the projected triangle, Cx' and Cy' are the X-axis coordinate and Y-axis coordinate of the third vertex of the projected triangle.
11. The method according to claim 10, wherein: The determining, according to the barycentric coordinates corresponding to the intersection of the ray and the plane coordinate system, a positional relationship between the intersection and a projection triangle of the triangle on the plane coordinate system comprises: When one of U, V and W is 0, determine that the intersection point is located at the edge of the projected triangle; When two of U, V and W are 0, determine that the intersection point is located at the vertex of the projected triangle; When U, V and W are all positive, or when U, V and W are all negative, it is determined that the intersection point is located inside the projected triangle.
12. The method according to any one of claims 1 to 5, wherein: Before determining the positional relationship between the intersection point and the projection triangle of the triangle on the plane coordinate system, the method further includes: The center of gravity coordinates are determined based on a fused multiplication-addition FMA operation, wherein the FMA operation includes a multiplication operation and an addition operation, the multiplication operation is used to multiply the input value of the FMA operation, the input value of the FMA operation is a single-precision floating point number, the addition operation is used to add the results of the multiplication operation, and the number of bits of the addition operation is greater than the number of bits of the multiplication operation.
13. The method according to claim 12, wherein: The number of bits of the addition operation is greater than or equal to 49 bits.
14. The method according to claim 12, wherein: The number of bits of the exponent aligned before the addition operation is greater than or equal to 9.
15. The method according to claim 12, wherein: The FMA operation includes two multiplication operations on two sets of input values and an addition operation on the results of the two multiplication operations, wherein before the addition operation, the FMA operation also includes a negation operation on the sign of the result of one of the multiplication operations.
16. The method according to claim 12, wherein: The FMA operation also includes a rounding operation on the result of the addition operation, so that the output value of the FMA operation is the single-precision floating point number.
17. The method according to any one of claims 1 to 5, wherein: Before determining the positional relationship between the intersection point and the projection triangle of the triangle on the plane coordinate system, the method further includes: Determine the principal dimension of the ray, and use the principal dimension as the Z axis of the three-dimensional coordinate system, wherein the principal dimension is the dimension with the largest absolute value; The ray and the triangle are moved so that the origin of the ray is located at the origin of the three-dimensional coordinate system; According to the transformation matrix, the ray and the triangle are transformed to obtain a direction of the ray along the Z axis; The triangle is projected along the direction of the ray onto the plane coordinate system formed by the X-axis and the Y-axis.
18. The method according to claim 17, wherein: The transformation matrix includes the product of two scaling matrices and a shearing matrix.
19. The method according to claim 17, wherein: In the event that the triangle is hit by the ray, the method further comprises: According to the barycentric coordinates, the hit distance of the ray in the three-dimensional coordinate system is determined as: t=(U·A z '+V·B z '+W·C z ') / det; Wherein, t is the hit distance, U, V and W are the barycentric coordinates, A z ', B z ' and C z ' are respectively the Z-axis coordinates of the first vertex, the second vertex and the third vertex of the triangle in the three-dimensional coordinate system before being projected onto the plane coordinate system.
20. An apparatus for ray tracing, the apparatus comprising: The first processing part is configured to project the ray and the triangle to a plane coordinate system, and The barycentric coordinates corresponding to the intersection of the triangle and the projection triangle of the triangle on the plane coordinate system are determined; The second processing part is configured to determine whether the triangle is hit by the ray according to the coordinates of at least two vertices of the projected triangle in the plane coordinate system when the intersection point is located on a common edge or common vertex shared by the projected triangle and other triangles projected onto the plane coordinate system.
21. A device for ray tracing, comprising a processor and a memory, wherein the memory stores a computer program executable on the processor, and when the processor executes the program, the steps in the method according to any one of claims 1 to 19 are implemented.
22. A computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the steps in the method according to any one of claims 1 to 19.
23. A chip comprising a processor, wherein the processor is configured to call and execute a computer program from a memory to implement the steps in the method according to any one of claims 1 to 19.
24. A computer program product, comprising a computer program or instructions, which, when executed by a processor, implements the steps of the method according to any one of claims 1 to 19.
Citation Information
Patent Citations
Intersection testing in ray tracing systems
CN115115759A
Intersection testing in a ray tracing system
GB202202657D0
Robust ray-triangle intersection
US20200193683A1
Primitive Testing for Ray Intersection at Multiple Precisions
US20220207690A1
Cited By
Excitation obtaining method for verifying boundary intersection point, electronic equipment and storage medium
CN120014142A
