Light ray tracing method and device and equipment for light ray tracing

By projecting rays and triangles to the plane coordinate system and using FMA operations, the problem of low intersect detection performance of rays and triangles in ray tracing technology is solved, and the accurate intersecting judgment between rays and triangles is achieved, and the stability and accuracy of the detection results are improved. .

CN118918235APending Publication Date: 2024-11-08MOORE THREADS TECH CO LTD

Patent Information

Application Number
CN202310493798.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-05-05
Publication Date
2024-11-08

AI Technical Summary

Technical Problem

Existing ray tracing technology has problems with poor performance in the intersection detection of rays and triangles, especially when the calculation accuracy is insufficient, resulting in cracks between triangles and it is difficult to accurately determine that the ray hits the common edge between multiple triangles. or public vertex.

Method used

By projecting rays and triangles to the plane coordinate system, and determining the positional relationship between intersection points and triangles based on the intersection points of rays and plane coordinate systems, the FMA operation is used to improve the calculation accuracy of center of gravity coordinates, ensuring the stability and watertightness of intersection detection results. .

Benefits of technology

Improves the performance of ray and triangle intersection detection, ensures robustness and watertightness of the results, and avoids uncertainty when rays hit multiple triangle common sides or common vertices.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118918235A_ABST
    Figure CN118918235A_ABST
Patent Text Reader

Abstract

The invention provides a ray tracing method, and a device and equipment for ray tracing, which can improve the intersection detection of rays and triangles. The method comprises the following steps: projecting a ray and a triangle to a plane coordinate system; according to barycentric coordinates corresponding to an intersection point of a ray and the plane coordinate system, determining a position relationship between the intersection point and a projection triangle of the triangle on the plane coordinate system; and if the intersection point is located at the common edge or the common vertex shared by the projection triangle and other triangles, determining whether the triangle is hit by the ray according to the coordinates of at least two vertexes of the projection triangle in the plane coordinate system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Embodiments of the present application relate to the field of image processing, and more specifically, to a ray tracing method, an apparatus and a device for ray tracing. Background Art

[0002] Ray tracing is a common rendering technique that generates realistic images by projecting rays to intersect with a test object and shading the test object based on the projection results. The basic geometric operation of ray tracing is to calculate the intersection of the ray and the scene by testing all the primitives in the scene. The primitives are usually triangles, and are arranged into a spatial data structure to accelerate the intersection efficiency. Therefore, this process is also called the intersection detection of rays and triangles. The result of the intersection detection directly affects the quality of the image. Therefore, how to improve the performance of the intersection detection has become an urgent problem to be solved. Summary of the invention

[0003] Embodiments of the present application provide a ray tracing method, an apparatus and a device for ray tracing, which can improve the performance of intersection detection between rays and triangles.

[0004] In a first aspect, a ray tracing method is provided, the method comprising: projecting a ray and a triangle onto 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; if 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.

[0005] 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, and when the intersection is located at a common edge or a common vertex of the projected triangle, according to the coordinates of at least two vertices of the triangle in the plane coordinate system, it is accurately determined whether the ray hits the triangle, thereby ensuring the stability of the intersection detection result.

[0006] In one implementation, if the intersection point is located at a common edge or common vertex shared by the projected triangle and other triangles, 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: if 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; and / or, if the intersection point is located at the common vertex, determining whether the two edges corresponding to the common vertex satisfy the predetermined condition, and when both edges corresponding to the common vertex satisfy the predetermined condition, determining that the triangle is hit by the ray.

[0007] In one implementation, the predetermined condition includes: 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; and / or 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.

[0008] In one implementation, 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 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: the Y-axis coordinate of the second vertex is greater than the Y-axis coordinate of the first vertex; or, 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.

[0009] In one implementation, 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: the Y-axis coordinate of the first vertex is greater than the Y-axis coordinate of the second vertex; or, 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.

[0010] In one implementation, the method further includes: if the intersection point is located inside the projected triangle, determining that the triangle is hit by the ray.

[0011] In one implementation, the plane coordinate system is perpendicular to the ray, and the intersection point is the origin of the plane coordinate system.

[0012] In one implementation, the barycentric coordinates are used to represent the areas of three triangles formed by connecting the intersection point and three vertices of the projected triangle.

[0013] In one implementation, 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 three vertices of the projected triangle.

[0014] In one implementation, 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 respectively the X-axis coordinate and Y-axis coordinate of the first vertex of the projected triangle, Bx' and By' are respectively the X-axis coordinate and Y-axis coordinate of the second vertex of the projected triangle, and Cx' and Cy' are respectively the X-axis coordinate and Y-axis coordinate of the third vertex of the projected triangle.

[0015] In one implementation, the positional relationship between the intersection point and the projected triangle is determined based on the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system, including: if one of U, V and W is 0, determining that the intersection point is located at the edge of the projected triangle; if two of U, V and W are 0, determining that the intersection point is located at the vertex of the projected triangle; if 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.

[0016] In one implementation, before determining the positional relationship between the intersection point and the projected triangle, 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 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.

[0017] In one implementation, the number of bits of the addition operation is greater than or equal to 49 bits.

[0018] In one implementation, the number of bits of the aligned exponent before the addition operation is greater than or equal to 9.

[0019] In one implementation, 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 negation operation on the sign of the result of one of the multiplication operations.

[0020] In one implementation, the FMA operation further includes a rounding operation on a result of the addition operation, so that an output value of the FMA operation is the single-precision floating-point number.

[0021] In one implementation, before determining the positional relationship between the intersection point and the projected triangle, 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 so 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 so 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.

[0022] In one implementation, the transformation matrix includes the product of two scaling matrices and a shear matrix.

[0023] In one implementation, if the triangle is hit by the ray, the method further includes: determining, according to 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 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.

[0024] In a second aspect, a ray tracing method is provided, 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 being used to add 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.

[0025] Based on this technical solution, in the process of intersection detection between rays and triangles, the rays and triangles are projected to a plane coordinate system, and the FMA operation is used to determine the barycentric coordinates corresponding to the intersection of the rays and the plane coordinate system, and based on the barycentric coordinates, it is determined whether the triangle is hit by the ray. Since the barycentric coordinates are calculated using the FMA operation, the FMA operation includes multiplication and addition operations, and the number of bits used in the addition operation is greater than the number of bits of the single-precision floating-point number input during the multiplication operation, the internal precision loss in the calculation process of the barycentric coordinates is eliminated, and the barycentric coordinates can be accurately obtained, thereby ensuring the watertightness of the intersection detection results.

[0026] In one implementation, the number of bits of the addition operation is greater than or equal to 49 bits.

[0027] In one implementation, the number of bits of the aligned exponent before the addition operation is greater than or equal to 9.

[0028] In one implementation, 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 negation operation on the sign of the result of one of the multiplication operations.

[0029] In one implementation, the FMA operation further includes a rounding operation on a result of the addition operation, so that an output value of the FMA operation is the single-precision floating-point number.

[0030] In one implementation, before determining the centroid coordinates corresponding to the intersection of the ray and the plane coordinate system, the method also 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 so 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 so 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.

[0031] In one implementation, the transformation matrix includes the product of two scaling matrices and a shear matrix.

[0032] In one implementation, after determining that the triangle is hit by the ray, the method further includes: determining, according to 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 barycentric coordinates, Az ', 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.

[0033] According to a third aspect, a device for ray tracing is provided, the device comprising: a first processing unit, for projecting a ray and a triangle onto a plane coordinate system, and determining a positional relationship between the intersection point and a projected triangle of the triangle based on the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system; and a second processing unit, for 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 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.

[0034] In one implementation, the second processing unit is specifically used to: if 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; and / or, if the intersection point is located at the common vertex, determine whether the two edges corresponding to the common vertex satisfy the predetermined condition, and when both of the two edges corresponding to the common vertex satisfy the predetermined condition, determine that the triangle is hit by the ray.

[0035] In one implementation, the predetermined condition includes: 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; and / or 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.

[0036] In one implementation, 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 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: the Y-axis coordinate of the second vertex is greater than the Y-axis coordinate of the first vertex; or, 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.

[0037] In one implementation, 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: the Y-axis coordinate of the first vertex is greater than the Y-axis coordinate of the second vertex; or, 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.

[0038] In one implementation, the second processing unit is further configured to: if the intersection point is located inside the projected triangle, determine that the triangle is hit by the ray.

[0039] In one implementation, the plane coordinate system is perpendicular to the ray, and the intersection point is the origin of the plane coordinate system.

[0040] In one implementation, the barycentric coordinates are used to represent the areas of three triangles formed by connecting the intersection point and three vertices of the projected triangle.

[0041] In one implementation, 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 three vertices of the projected triangle.

[0042] In one implementation, 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 respectively the X-axis coordinate and Y-axis coordinate of the first vertex of the projected triangle, Bx' and By' are respectively the X-axis coordinate and Y-axis coordinate of the second vertex of the projected triangle, and Cx' and Cy' are respectively the X-axis coordinate and Y-axis coordinate of the third vertex of the projected triangle.

[0043] In one implementation, the first processing unit is specifically used to: if one of U, V and W is 0, determine that the intersection is located at the edge of the projected triangle; if two of U, V and W are 0, determine that the intersection is located at the vertex of the projected triangle; if 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.

[0044] In one implementation, the first processing unit is further used to determine the center of gravity coordinates 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 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.

[0045] In one implementation, the number of bits of the addition operation is greater than or equal to 49 bits.

[0046] In one implementation, the number of bits of the aligned exponent before the addition operation is greater than or equal to 9.

[0047] In one implementation, 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 negation operation on the sign of the result of one of the multiplication operations.

[0048] In one implementation, the FMA operation further includes a rounding operation on a result of the addition operation, so that an output value of the FMA operation is the single-precision floating-point number.

[0049] In one implementation, the first processing unit is also used 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 so 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 so 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.

[0050] In one implementation, the transformation matrix includes the product of two scaling matrices and a shear matrix.

[0051] In one implementation, the first processing unit is further configured to: determine, according to the barycenter coordinates, the hit distance of the ray in the three-dimensional coordinate system as: t=(U·A z '+V·B z '+W·C z ') / det, where 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.

[0052] In a fourth aspect, a device for ray tracing is provided, the device comprising: a first processing unit, for projecting rays and triangles onto a plane coordinate system, and determining 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 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 unit, for determining whether the triangle is hit by the ray based on the centroid coordinates.

[0053] In one implementation, the number of bits of the addition operation is greater than or equal to 49 bits.

[0054] In one implementation, the number of bits of the aligned exponent before the addition operation is greater than or equal to 9.

[0055] In one implementation, 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 negation operation on the sign of the result of one of the multiplication operations.

[0056] In one implementation, the FMA operation further includes a rounding operation on a result of the addition operation, so that an output value of the FMA operation is the single-precision floating-point number.

[0057] In one implementation, the first processing unit is also used 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 so 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 so 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.

[0058] In one implementation, the transformation matrix includes the product of two scaling matrices and a shear matrix.

[0059] In one implementation, if the triangle is hit by the ray, the first processing unit is further used to: determine the hitting distance of the ray in the three-dimensional coordinate system according to the barycenter coordinates.

[0060] In a fifth aspect, a device for ray tracing is provided, comprising a processor and a memory, wherein the memory is used to store a computer program, and the processor is used to call and apply the computer program stored in the memory to execute the method described in the first aspect or any implementation of the first aspect, and / or the method described in the second aspect or any implementation of the second aspect.

[0061] In a sixth aspect, a computer-readable storage medium is provided for storing a computer program, wherein the computer program enables a computer to execute the method described in the first aspect or any implementation of the first aspect, and / or the method described in the second aspect or any implementation of the second aspect.

[0062] In the seventh aspect, a computer program product is provided, comprising computer program instructions, which enable a computer to execute the method described in the first aspect or any implementation of the first aspect, and / or the method described in the second aspect or any implementation of the second aspect.

[0063] In an eighth aspect, a chip is provided, comprising a processor, wherein the processor is used to call and apply a computer program from a memory so that a device equipped with the chip executes the method described in the first aspect or any implementation of the first aspect, and / or the method described in the second aspect or any implementation of the second aspect. BRIEF DESCRIPTION OF THE DRAWINGS

[0064] Figure 1 It is a schematic diagram of the structure of BVH.

[0065] Figure 2 It is a schematic flowchart of a ray tracing method according to an embodiment of the present application.

[0066] Figure 3 It is a schematic diagram of the intersection detection process of rays and triangles in an embodiment of the present application.

[0067] Figure 4 It is a schematic diagram of the intersection detection process of rays and triangles in an embodiment of the present application.

[0068] Figure 5 It is a schematic diagram of a common side of a triangle being hit by a ray according to an embodiment of the present application.

[0069] Figure 6 It is a schematic diagram of a common side of a triangle being hit by a ray according to an embodiment of the present application.

[0070] Figure 7 It is a schematic diagram of a common side of a triangle being hit by a ray according to an embodiment of the present application.

[0071] Figure 8It is a schematic diagram of a common side of a triangle being hit by a ray according to an embodiment of the present application.

[0072] Fig. 9 It is a schematic diagram of a common vertex of a triangle of an embodiment of the present application being hit by a ray and not being hit by a ray.

[0073] Fig.10 A schematic flowchart of a ray tracing method according to another embodiment of the present application.

[0074] Fig.11 Schematic diagram of an FMA operation unit according to an embodiment of the present application.

[0075] Fig.12 It is a schematic block diagram of a ray tracing device according to an embodiment of the present application. DETAILED DESCRIPTION

[0076] The technical solution in this application will be described below in conjunction with the accompanying drawings.

[0077] Rays are defined as rays in a ray tracing system, including an origin and a direction. In this application, "ray" is used to refer to rays in ray tracing technology. Ray tracing usually uses a bounding volume hierarchy (BVH) data structure to accelerate the intersection of rays with the scene. BVH is a tree structure, for example, Figure 1 As shown, it includes different types of nodes, including but not limited to box nodes and triangle nodes. Box nodes are usually internal nodes, and triangle nodes are leaves that store primitives in the scene. In the bounding box, each box tightly surrounds the primitives in the leaf, 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.

[0078] Generally, based on the intersection detection between the ray and the box, it can be determined whether the ray hits the bounding box in the box node; further, based on the intersection detection between the ray and the triangle, it can be determined whether the ray hits the triangle in the triangle node.

[0079] However, as the scene becomes more and more complex, in the process of intersection detection 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 triangles. That is, triangles that were originally closely together may have a certain distance from each other due to problems with calculation accuracy.

[0080] 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 edges or common vertices between multiple triangles. When the ray hits the common edges or common vertices of multiple triangles, it is necessary to determine which triangle the ray hits.

[0081] 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 vertex 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.

[0082] In addition, an embodiment of the present application also provides a solution for intersection detection of rays and triangles, which is based on a 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.

[0083] Figure 2 1 is a schematic flow chart of a ray tracing method according to an embodiment of the present application. The method 100 can be applied to a hardware accelerator, such as a hardware accelerator on a graphics processing unit (GPU), for use in games and other visualization applications. Figure 2 As shown, method 100 may include some or all of the following steps.

[0084] In step 110 , the rays and triangles are projected to a planar coordinate system.

[0085] 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. That is, the triangle is projected onto the plane coordinate system along a direction perpendicular to the ray.

[0086] In step 120, the positional relationship between the intersection point and the projection triangle of the triangle on the plane coordinate system is determined according to the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system.

[0087] In step 130, if 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.

[0088] In the process of intersection detection between rays and triangles, the rays and triangles are projected into a plane coordinate system, and the positional relationship between the intersection point and the projected triangle of the triangle in the plane coordinate system is determined based on the centroid coordinates corresponding to the intersection point between the ray and the plane coordinate system. The intersection point may be located inside the projected triangle, outside the projected triangle, at the vertex of the projected triangle, or at the edge of the projected triangle. Since multiple triangles may share edges or vertices, when the intersection point is located at a common edge or common vertex of the projected triangle, it is possible to accurately determine whether the ray hits the triangle based on the coordinates of at least two vertices of the projected triangle, thereby ensuring the stability of the intersection detection result.

[0089] It can be understood that since the transformation matrix used in projection is linear, the positional relationship between the ray and the triangle before and after projection is consistent, that is, 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 to the plane coordinate system. For example, if the ray hits the edge of the triangle, then the intersection is also located on the edge of the projected triangle after projection; if the ray hits the vertex of the triangle, then the intersection is also located on the vertex of the projected triangle after projection; if the ray hits the inside of the triangle, then the intersection is also located inside the projected triangle after projection.

[0090] Before step 120, it is necessary to obtain the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system based on the intersection detection process of the ray and the triangle.

[0091] For the intersection detection of rays and triangles, a ray and a triangle need to be defined. Usually, the ray is transformed from the 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 by 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 of the triangle.

[0092] 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.

[0093] First, define rays and triangles in object space.

[0094] Ray passing through the origin ,direction And parameter t represents, that is in, Tmin≤t≤Tmax.

[0095] A triangle is represented by three vertices,

[0096] Secondly, the intersection detection between the ray and the triangle is performed through the following steps.

[0097] 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:

[0098]

[0099] 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, if the ray direction of the main dimension is negative, the X axis and the Y axis need to be swapped. That is:

[0100]

[0101] Figure 3 The process shown in step 101 and step 102 is shown. Figure 3 The main dimension is Dim x , and d x <0 as an example. Figure 3 In (a), the three vertices of the triangle, namely vertex A, vertex B and vertex C, are located on the X-axis, Y-axis and Z-axis respectively; Figure 3 In (b), vertex A, vertex B, and vertex C are located on the Z axis, X axis, and Y axis respectively; Figure 3 In (c), the positions of vertices B and C are swapped.

[0102] Afterwards, the origin O and direction D of the ray are expressed as:

[0103]

[0104]

[0105] The vertices A, B, and C of the triangle are represented as follows:

[0106]

[0107]

[0108]

[0109] 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.

[0110] 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 ).

[0111] The vertices of the triangle are:

[0112] A'=(AO);

[0113] B' = (BO);

[0114] C'=(CO).

[0115] In step 104, the ray and the 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:

[0116]

[0117] Since the round-off error introduced by shearing is smaller, the computational efficiency is higher when using this transformation matrix.

[0118] After step 103 and step 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.

[0119] The vertices of the triangle become:

[0120] A' = M*(AO);

[0121] B' = M*(BO);

[0122] C'=M*(CO).

[0123] Figure 4 The process shown in step 103 and step 104 is shown. Figure 4 In (a) and (b), the origin of the ray is moved to the origin of the three-dimensional coordinate system. Figure 4 In (c), the transformation matrix is ​​used to make the direction of the ray along the positive direction of the Z axis.

[0124] 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 centroid coordinates are calculated.

[0125] In one implementation, the center of gravity coordinates are:

[0126] U = Cx'·By'-Cy'·Bx';

[0127] V = Ax'·Cy'-Ay'·Cx';

[0128] W = Bx'·Ay'-By'·Ax';

[0129] 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. The first vertex, the second vertex and the third vertex correspond to the above-mentioned vertex A, vertex B and vertex C, for example, respectively.

[0130] U, V and W can represent, for example, the area of ​​the three small triangles formed by the intersection of the ray and the plane coordinate system and the lines connecting the first vertex, the second vertex and the third vertex of the projected triangle. Below, U, V and W can also be replaced by u, v and w respectively, where u, v and w are obtained by normalizing U, V and W respectively, and u, v and w represent, for example, the ratio of the area of ​​the three triangles formed by the intersection and the lines connecting the first vertex, the second vertex and the third vertex of the projected triangle to the area of ​​the projected triangle, and in this case, u+v+w=1.

[0131] It can be understood that the area of ​​the triangle will change before and after projection, which is equivalent to scaling the triangle. However, the ratio of the area 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 before projection to the total area of ​​the triangle is the same as the ratio of the area 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.

[0132] Next, the above step 120 is performed, that is, the positional relationship between the intersection point and the projected triangle is determined according to the values ​​of U, V and W. The positional relationship may include the following types, for example:

[0133] a) If U, V, and W are all positive, or U, V, and W are all negative, the intersection point is inside the projected triangle;

[0134] b) If U, V and W are both positive and negative, the intersection point is outside the projected triangle;

[0135] c) If one of U, V and W is 0, the intersection point is located on the side of the projected triangle;

[0136] d) If two of U, V and W are 0, the intersection point is determined to be at the vertex of the projected triangle;

[0137] e) If U, V, and W are all 0, it means that the ray and the triangle are coplanar, that is, the intersection is lost.

[0138] In step 106, the determinant det=U+V+W is calculated.

[0139] In step 107, if 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 barycenter coordinates, that is:

[0140] t=(U·A z '+V·B z '+W·C z ') / det;

[0141] Among them, t is the hit distance, U, V and W are the center of gravity coordinates, 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.

[0142] For case a) above, that is, the intersection point is inside the projected triangle, then it is determined that the triangle is hit by the ray.

[0143] For cases b) and e) above, it is determined that the triangle is not hit by the ray.

[0144] For the above cases c) and d), that is, the intersection point is located at the edge or vertex of the projected triangle. At this time, 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; similarly, 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.

[0145] In one implementation, in step 120, if the intersection point of the ray and the plane coordinate system is located at the common edge of the projected triangle, it is determined 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 when the common edge satisfies the predetermined condition, it is determined that the triangle is hit by the ray; and / or,

[0146] If the intersection of the ray and the plane coordinate system is located at a common vertex of the projected triangle, it is determined whether the two edges corresponding to the common vertex meet a predetermined condition, and when the two edges corresponding to the common vertex both meet the predetermined condition, it is determined that the triangle is hit by the ray.

[0147] The following, combined Figures 5 to 9 For the two cases where the intersection point is located at a common edge and the intersection point is located at a common vertex, a detailed description is given of how to determine whether the ray hits the triangle based on the coordinates of at least two vertices of the projected triangle.

[0148] The winding direction, or winding order, of the projected triangle includes a clockwise (CW) direction and a counterclockwise (CCW) direction. Optionally, the winding direction of the projected triangle can be determined according to the sign of the determinant calculated above. For example, when the sign of the determinant is positive, the projected triangle is in the CW direction, and when the sign of the determinant is negative, the projected triangle is in the CCW direction; or, when the sign of the determinant is positive, the projected triangle is in the CCW direction, and when the sign of the determinant is negative, the projected triangle is in the CW direction.

[0149] The predetermined condition includes, 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; and / or 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.

[0150] 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.

[0151] Figure 5 and Figure 6 The winding direction is shown in the CW direction. Figures 7 to 9 The case where the winding direction is the CCW direction is shown.

[0152] In one implementation, the winding direction of the vertices of the projected triangle is the 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:

[0153] The Y-axis coordinate of the second vertex v1 is greater than the Y-axis coordinate of the first vertex v0;

[0154] 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.

[0155] For example, Figure 5 As shown, v0, v1 and v2 are respectively the first vertex, the second vertex and the third vertex of the projected triangle in the plane coordinate system. Figure 5 As shown, the winding direction of the projected triangle is CW, 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, then the common edge v0v1 is called the left boundary.

[0156] For example, Figure 6 As shown, the winding direction of the projected triangle is CW, 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.

[0157] If the common edge v0v1 is the left boundary or the upper boundary, that is, the common edge v0v1 satisfies the predetermined condition of the left boundary or the upper boundary, it is considered that the ray hits the common edge v0v1.

[0158] In another implementation, the winding direction of the vertices of the projected triangle is the 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 the first vertex v0 and the second vertex v1 of the projected triangle, respectively. Then, the predetermined conditions in step 120 include:

[0159] The Y-axis coordinate of the first vertex v0 is greater than the Y-axis coordinate of the second vertex v1;

[0160] 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.

[0161] For example, Figure 7 As shown, v0, v1 and v2 are respectively the first vertex, the second vertex and the third vertex of the projected triangle in the plane coordinate system. Figure 7 As shown, the winding direction of the projected triangle is CCW, 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, then the common edge v0v1 is called the left boundary.

[0162] For example, Figure 8 As shown, the winding direction of the projected triangle 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, 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.

[0163] If the common edge v0v1 is the left boundary or the upper boundary, that is, the common edge v0v1 satisfies the predetermined condition of the left boundary or the upper boundary, it is considered that the ray hits the common edge v0v1.

[0164] That is, when multiple triangles in a plane coordinate system have a common edge, based on the above method, it is determined in which triangle the common edge belongs to the left boundary or the upper boundary of the triangle, and it is considered that the ray hits the triangle but not the other triangles.

[0165] 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 the two sides meet the above predetermined conditions of the left boundary or the upper boundary.

[0166] For example, Fig. 9 As shown, the winding direction of the projected triangle is CCW. When the intersection of the ray and the plane coordinate system is located at the common vertex v0 of the projected triangle, 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.

[0167] When the intersection of the ray and the plane coordinate system is located at the common vertex v2 of the projected triangle, 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.

[0168] It can be seen that since the result of the intersection detection between the ray and the triangle is determined, the uncertainty caused by the ray hitting the common edge or common vertex of multiple triangles is eliminated, and the stability of the result of the intersection detection is guaranteed, which is also referred to as robustness here.

[0169] As mentioned above, the barycentric coordinates play a vital role in the result of the intersection detection between the ray and the triangle, and the hit position of the ray in the triangle. To this end, the embodiment of the present application also provides a solution for the intersection detection between the ray and the triangle to improve the calculation accuracy of the barycentric coordinates and ensure the watertightness of the intersection detection result.

[0170] The present application also provides a ray tracing method 200, which can be applied to a hardware accelerator, such as a hardware accelerator on a GPU, for use in games and other visualization applications. Fig.10 As shown, method 200 includes part or all of the following steps.

[0171] In step 210, the rays and triangles are projected to a planar coordinate system.

[0172] In step 220, the barycentric coordinates corresponding to the intersection point of the ray and the plane coordinate system are determined according to the FMA operation.

[0173] In step 230, it is determined whether the triangle is hit by the ray according to the barycenter coordinates.

[0174] 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.

[0175] In the process of intersection detection between rays and triangles, the rays and triangles are projected to a plane coordinate system, and the barycentric coordinates corresponding to the intersection of the rays and the plane coordinate system are determined by using the FMA operation, and whether the triangle is hit by the ray is determined based on the barycentric coordinates. Since the barycentric coordinates are calculated by using the FMA operation, the FMA operation includes multiplication and addition operations, and the number of bits used in the addition operation is greater than the number of bits of the single-precision floating-point number input during the multiplication operation, the internal precision loss in the calculation process of the barycentric coordinates is eliminated, and the barycentric coordinates can be accurately obtained, thereby ensuring the watertightness of the intersection detection results.

[0176] For example, the number of bits for the addition operation may be greater than or equal to 49 bits.

[0177] Optionally, 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.

[0178] Based on the calculation formulas of U, V and W, 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 to say, the FMA operation specifically includes two multiplication operations for two sets of input values, and an addition operation for the results of the two multiplication operations, wherein before the addition operation, the FMA operation also includes a sign negation operation for 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 for the result of the addition operation.

[0179] Fig.11 A schematic diagram of an FMA operation unit of an embodiment of the present application is shown. The FMA operation unit includes two multipliers and an adder. 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, we use s1.e8.m1.23 to represent an FP32 floating point number, and the result of multiplying two FP32 floating point numbers, here, we use s1.e9.m2.46 to represent it.

[0180] When the two multipliers 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 to obtain the value of -A[1]*B[1]. After that, the exponent bits are aligned through a shift operation such as >>. The number of aligned exponent bits can be greater than or equal to 9. Finally, the adder 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 output by the adder is normalized and rounded, and the output value of the FMA operation unit is FP32.

[0181] Method 200 can be applied to hardware accelerators. Since hardware accelerators are relatively expensive and usually do not have double-precision computing units, by using method 200 to calculate the centroid coordinates, even if single-precision floating-point operations are used, no cracks will appear between triangles due to calculation accuracy issues.

[0182] It can be understood that both the method 100 and the method 200 provided in the embodiment of the present application can improve the performance of the intersection detection between rays and triangles. According to the method 100, when the intersection point of the ray and the plane coordinate system is located at the common edge or common vertex of the projected triangle, it can be accurately determined whether the ray hits the triangle, thereby ensuring the robustness of the intersection detection result; according to the method 200, the calculation accuracy of the barycentric coordinates is improved, ensuring the watertightness of the intersection detection result.

[0183] Method 100 and method 200 may be performed separately or in combination. For example, when calculating the barycentric coordinates corresponding to the intersection of the ray and the plane coordinate system, the FMA operation may be used to meet the watertightness requirement; and when it is determined that the intersection is located at a common edge or common vertex of the projected triangle according to the barycentric coordinates, it may be determined 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, thereby meeting the robustness requirement.

[0184] The present application also provides a device for ray tracing, such as Fig.12 As shown, the apparatus 300 includes a first processing unit 310 and a second processing unit 320 .

[0185] The first processing unit 310 is used to project the ray and the triangle to 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 according to the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system.

[0186] The second processing unit 320 is used 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 to the plane coordinate system.

[0187] In one implementation, the second processing unit 320 is specifically used to: if 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; and / or, if 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 edges corresponding to the common vertex meet the predetermined condition, determine that the triangle is hit by the ray.

[0188] In one implementation, the predetermined condition includes: a predetermined relationship between Y-axis coordinates of at least two vertices of the projected triangle in the plane coordinate system; and / or a predetermined relationship between X-axis coordinates of at least two vertices of the projected triangle in the plane coordinate system.

[0189] In one implementation, the at least two vertices of the projected triangle include two vertices of the common edge, or two vertices of the edge corresponding to the common vertex.

[0190] In one implementation, 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: the Y-axis coordinate of the second vertex is greater than the Y-axis coordinate of the first vertex; or, 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.

[0191] In one implementation, 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: the Y-axis coordinate of the first vertex is greater than the Y-axis coordinate of the second vertex; or, 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.

[0192] In one implementation, the second processing unit 320 is further configured to: if the intersection point is located inside the projected triangle, determine that the triangle is hit by the ray.

[0193] In one implementation, 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 respectively the X-axis coordinate and Y-axis coordinate of the first vertex of the projected triangle, Bx' and By' are respectively the X-axis coordinate and Y-axis coordinate of the second vertex of the projected triangle, and Cx' and Cy' are respectively the X-axis coordinate and Y-axis coordinate of the third vertex of the projected triangle.

[0194] In one implementation, the first processing unit 310 is specifically used to: if one of U, V and W is 0, determine that the intersection is located at the edge of the projected triangle; if two of U, V and W are 0, determine that the intersection is located at the vertex of the projected triangle; if 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.

[0195] In one implementation, the first processing unit 310 is also used 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 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.

[0196] In one implementation, the number of bits of the addition operation is greater than or equal to 49 bits.

[0197] In one implementation, the number of exponent bits aligned before the addition operation is greater than or equal to 9.

[0198] In one implementation, 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 negation operation on the sign of the result of one of the multiplication operations.

[0199] In one implementation, 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.

[0200] In one implementation, the first processing unit 310 is also used to: determine the main dimension of the ray, use the main dimension as the Z axis of the three-dimensional coordinate system, and the main dimension is the dimension with the largest absolute value; move the ray and the triangle so 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 so 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.

[0201] In one implementation, the transformation matrix includes the product of two scaling matrices and a shear matrix.

[0202] In one implementation, the first processing unit 310 is further configured to: determine, based on the barycenter coordinates, the hit distance of the ray in the three-dimensional coordinate system as: 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, the second vertex and the third vertex of the triangle in the three-dimensional coordinate system before it is projected onto the plane coordinate system.

[0203] It should be understood that the specific details of the device 300 can be referred to the aforementioned description of the method embodiment, and for the sake of brevity, they are not repeated here.

[0204] The present application also provides a device for ray tracing, including a processor and a memory, wherein the memory is used to store a computer program, and the processor is used to call and use the computer program stored in the memory to execute the method in any of the above embodiments. The processor may be, for example, a GPU.

[0205] The present application also provides a computer-readable storage medium for storing a computer program, wherein the computer program enables a computer to execute the method in any of the above embodiments. The computer-readable storage medium is, for example, a memory.

[0206] The present application also provides a computer program product, including computer program instructions, which enable a computer to execute the method in any of the above embodiments.

[0207] The present application also provides a chip, including a processor, the processor is used to call and apply a computer program from a memory, so that a device equipped with the chip executes the method in any of the above embodiments. The chip can be, for example, a GPU.

[0208] The above-mentioned memory may be a volatile memory or a non-volatile memory, or the memory may include both a volatile memory and a non-volatile memory. Among them, the volatile memory includes, for example, a random access memory (RAM); the non-volatile memory includes, for example, a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM) or a flash memory.

[0209] It should be noted that, under the premise of no conflict, the various embodiments described in this application and / or the technical features in each embodiment can be arbitrarily combined with each other, and the technical solution obtained after the combination should also fall within the protection scope of this application.

[0210] 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 units is merely a logical function division. There may be other division methods in actual implementation, and multiple units or components may be combined or integrated into another system. In addition, the coupling between the units 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.

[0211] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process 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.

[0212] 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.

[0213] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.

Claims

1. A ray tracing method, characterized in that: The method comprises: Project rays and triangles to a plane coordinate system; Determine, according to the centroid 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; If 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, characterized in that If the intersection point is located at a common edge or common vertex shared by the projected triangle and other triangles, determining 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 includes: If the intersection point is located on the common edge, determining whether the common edge satisfies a predetermined condition according to the coordinates of the two vertices of the common edge in the plane coordinate system, and determining that the triangle is hit by the ray when the common edge satisfies the predetermined condition; and / or, If 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 the two edges corresponding to the common vertex both meet the predetermined condition, it is determined that the triangle is hit by the ray.

3. The method according to claim 2, characterized in that The predetermined conditions include: A predetermined relationship between the Y-axis coordinates of the two vertices of the common side or the two vertices of the side corresponding to the common vertex in the plane coordinate system; and / or, 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, characterized in that 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 respectively the first vertex and the second vertex of the projected triangle, and the predetermined conditions include: The Y-axis coordinate of the second vertex is greater than the Y-axis coordinate of the first vertex; or, 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, characterized in that: 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 conditions include: The Y-axis coordinate of the first vertex is greater than the Y-axis coordinate of the second vertex; or, 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, characterized in that The method further comprises: If 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, characterized in that 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, characterized in that 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, characterized in that 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, characterized in that The center of gravity 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, characterized in that The determining the positional relationship between the intersection point and the projected triangle according to the barycentric coordinates corresponding to the intersection point of the ray and the plane coordinate system comprises: If one of U, V and W is 0, determine that the intersection point is located on the edge of the projected triangle; If two of U, V and W are 0, it is determined that the intersection point is located at the vertex of the projected triangle; If U, V and W are all positive, or if 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, characterized in that Before determining the positional relationship between the intersection point and the projected triangle, 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 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, characterized in that The number of bits of the addition operation is greater than or equal to 49 bits.

14. The method according to claim 12, characterized in that 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, characterized in that 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 negation operation on the sign of the result of one of the multiplication operations.

16. The method according to claim 12, characterized in that The FMA operation further includes a rounding operation on a result of the addition operation, so that an 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, characterized in that Before determining the positional relationship between the intersection point and the projected triangle, 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; Moving the ray and the triangle so that the origin of the ray is located at the origin of the three-dimensional coordinate system; According to the transformation matrix, transform the ray and the triangle so that the direction of the ray is 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, characterized in that The transformation matrix includes the product of two scaling matrices and a shearing matrix.

19. The method according to claim 17, characterized in that If 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, where 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. A device for ray tracing, characterized in that: The device comprises: A first processing unit, configured to project the ray and the triangle to a plane coordinate system, and determine a positional relationship between the intersection point and a projected triangle of the triangle according to the centroid coordinates corresponding to the intersection point of the ray and the plane coordinate system; The second processing unit is used 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 at a common edge or common vertex shared by the projected triangle and other triangles.

21. A device for ray tracing, characterized in that The method comprises a processor and a memory, wherein the memory is used to store a computer program, and the processor is used to call and apply the computer program stored in the memory to execute the method according to any one of claims 1 to 19.

22. A computer-readable storage medium, characterized in that: Used to store a computer program, the computer program causing a computer to execute the method according to any one of claims 1 to 19.

23. A chip, characterized in that: The device comprises a processor, wherein the processor is used to call and execute a computer program from a memory, so that a device equipped with the chip executes the method according to any one of claims 1 to 19.

Citation Information

Patent Citations

  • Intersection testing in ray tracing systems

    CN115115760A

  • Robust ray-triangle intersection

    US20200193683A1

Cited By

  • Ray tracing method, apparatus for ray tracing, device, computer-readable storage medium, and computer program product

    EP4708204A1