The application relates to the field of electronic technologies, and in particular, to a rendering optimization method, an electronic device, and a computer-readable storage medium.
With development of computer technologies, more and more application programs, for example, a game and a video, are required to display more realistic images, in other words, are required to display scenes with special effects, such as virtual simulation, virtual reality, and enhanced display; and a graphics rendering technology is used, to improve authenticity of a picture by using the rendering technology, thereby bringing a better experience to a user. Therefore, how to improve the authenticity of the picture becomes a main target of the industry.
A ray tracing technology is based on a real physical rule, and therefore can generate a rendering result close to the real world and attract much attention. As a hardware-accelerated real-time ray tracing technology is more and more widely applied in the field of personal computers (PCs) and consoles, vulkan-based real-time ray tracing at a mobile terminal is also supported by many mobile-terminal chip products. However, even if hardware acceleration is applied, ray tracking is still a technology with a heavy load at the mobile terminal. A large amount of calculation is still required. As a result, power consumption of a chip is greatly increased, and an operation frame rate of an electronic device is also affected.
In view of this, the present disclosure provides a rendering optimization method, an electronic device, and a computer-readable storage medium. The method can reduce an amount of calculation in an image rendering process, and reduce a chip power consumption load when keep running a frame rate with special effects.
Some embodiments of this application provide a rendering optimization method. The following describes this application from a plurality of aspects, and implementations and beneficial effects of the plurality of following aspects may refer to each other.
According to a first aspect, the present disclosure provides a rendering optimization method, applied to an electronic device, and the method includes: determining first polygonal mesh data corresponding to an object model in a scene, where the first polygonal mesh data is for describing the object model; creating a bounding box corresponding to the object model based on the first polygonal mesh data; determining a pixel range covered when the bounding box is projected onto a screen of the electronic device, obtaining a relationship between a quantity of pixels covered by the bounding box and a quantity of pixels of the screen, and determining a distance between an observation angle in the scene and the object model; determining a level of detail LOD level corresponding to the object model based on the distance and the relationship, and updating the first polygonal mesh data to second polygonal mesh data based on the LOD level; and rendering the object model based on the second polygonal mesh data.
According to the rendering optimization method in embodiments of this application, a simplification degree, namely, a LOD level of a model is determined by calculating a visual range proportion of the model in a screen and a distance between an observation angle and the model, to implement dynamic adjustment of the LOD level, so that a quantity of triangular meshes forming the model is reduced when geometry of the model is maintained. In this way, calculation consumption of ray tracing can be effectively reduced, and realistic image quality can be efficiently generated.
In an embodiment of the first aspect of this application, the determining a LOD level corresponding to the object model based on the distance and the relationship includes: performing weighted summation calculation on the distance and the relationship respectively, to obtain a summation result; and determining the LOD level based on the summation result.
In an embodiment of the first aspect of this application, the determining a distance between an observation angle in the scene and the object model includes: obtaining first world coordinates of the observation angle in world space and second world coordinates of the object model in the world space; and determining the distance between the observation angle and the object model based on the first world coordinates and the second world coordinates.
In an embodiment of the first aspect of this application, the updating the first polygonal mesh information to second polygonal mesh information based on the LOD level includes: removing, based on the LOD level, a part of polygonal mesh data from the first polygonal mesh information by using a polygonal mesh tile reduction algorithm to obtain the second polygonal mesh data. The LOD level obtained through the algorithm can be dynamically adjusted based on a real scene, to ensure a realistic degree of a picture when reducing an amount of calculation as much as possible.
In an embodiment of the first aspect of this application, the rendering the object model based on the second polygonal mesh data includes: when the electronic device determines that a rendering mode corresponding to the object model is ray tracing rendering, obtaining the second polygonal mesh information; and calculating an intersection of a ray and the object model in the scene by using a ray tracing shader based on the second polygonal mesh information, and shading the object model based on the intersection.
In an embodiment of the first aspect of this application, the method further includes: when the electronic device determines that the rendering mode corresponding to the object model is normal rendering, obtaining the first polygonal mesh information corresponding to the object model in the scene, and shading the object model by using a rasterized shader.
In an embodiment of the first aspect of this application, the creating a bounding box corresponding to the object model based on the first polygonal mesh information includes: determining, based on the first polygonal mesh information, maximum values and minimum values of the object model in an X direction, a Y direction, and a Z direction in coordinates of the object model; and determining the bounding box based on the maximum values and the minimum values.
In an embodiment of the first aspect of this application, the relationship between the quantity of pixels covered by the bounding box and the quantity of pixels of the screen is a ratio of the quantity of pixels covered by the bounding box to the quantity of pixels of the screen.
In an embodiment of the first aspect of this application, a weight of the distance is set to range from 0.1 to 0.5, and a weight of the ratio ranges from 0.5 to 0.9.
In an embodiment of the first aspect of this application, the weight of the distance is set to 0.3, and the weight of the ratio is 0.7. The ratio is more beneficial to ensuring the realistic degree of the picture when reducing the amount of calculation as much as possible.
In an embodiment of the first aspect of this application, the first polygonal mesh information includes a quantity of polygons corresponding to the object model not simplified through the LOD level, and vertex coordinates and vertex indexes of the polygon.
In an embodiment of the first aspect of this application, the second polygonal mesh information includes a quantity of polygons corresponding to the object model simplified based on the LOD level, and the vertex coordinates and the vertex indexes of the polygon.
In an embodiment of the first aspect of this application, the polygon is a triangle.
According to a second aspect, this application provides an electronic apparatus, including:
According to the electronic apparatus in embodiments of this application, a simplification degree, namely, a LOD level of a model is determined by calculating a visual range proportion of the model in a screen and a distance between an observation angle and the model, to implement dynamic adjustment of the LOD level, so that a quantity of triangular meshes forming the model is reduced when geometry of the model is maintained. In this way, calculation consumption of ray tracing can be effectively reduced, and realistic image quality can be efficiently generated.
In an embodiment of the second aspect of this application, the LOD calculation module is further configured to perform weighted summation calculation on the distance and the relationship respectively, to obtain a summation result; and determine the LOD level based on the summation result. The LOD level obtained through the algorithm can be dynamically adjusted based on a real scene, to ensure a realistic degree of a picture when reducing an amount of calculation as much as possible.
In an embodiment of the second aspect of this application, the ratio determining module is further configured to obtain first world coordinates of the observation angle in world space and second world coordinates of the object model in the world space; and determine the distance between the observation angle and the object model based on the first world coordinates and the second world coordinates.
In an embodiment of the second aspect of this application, the LOD calculation module is configured to: remove, based on the LOD level, a part of polygonal mesh data from the first polygonal mesh information by using a polygonal mesh tile reduction algorithm to obtain the second polygonal mesh data.
In an embodiment of the second aspect of this application, the rendering module is configured to:
In an embodiment of the second aspect of this application, the rendering module is further configured to:
In an embodiment of the second aspect of this application, the bounding box creation module is configured to determine, based on the first polygonal mesh information, maximum values and minimum values of the object model in an X direction, a Y direction, and a Z direction in coordinates of the object model; and determine the bounding box based on the maximum values and the minimum values.
In an embodiment of the second aspect of this application, the relationship between the quantity of pixels covered by the bounding box and the quantity of pixels of the screen is a ratio of the quantity of pixels covered by the bounding box to the quantity of pixels of the screen.
In an embodiment of the second aspect of this application, a weight of the distance is set to range from 0.1 to 0.5, and a weight of the ratio ranges from 0.5 to 0.9.
In an embodiment of the second aspect of this application, the weight of the distance is set to 0.3, and the weight of the ratio is 0.7. The ratio is more beneficial to ensuring the realistic degree of the picture when reducing the amount of calculation as much as possible.
In an embodiment of the second aspect of this application, the first polygonal mesh information includes a quantity of polygons corresponding to the object model not simplified through the LOD level, and vertex coordinates and vertex indexes of the polygon.
In an embodiment of the second aspect of this application, the second polygonal mesh information includes a quantity of polygons corresponding to the object model simplified based on the LOD level, and the vertex coordinates and the vertex indexes of the polygon.
In an embodiment of the second aspect of this application, the polygon is a triangle.
According to a third aspect, this application further provides an electronic device, including: a memory, configured to store instructions executed by one or more processors of the device; and a processor, configured to execute the instructions, to enable the electronic device to perform any method in the technical solutions of the first aspect.
According to a fourth aspect, this application further provides a computer-readable storage medium, storing a computer program, where the computer program, when run by a processor, enables the processor to perform any method in the technical solutions of the first aspect.
According to a fifth aspect, this application further provides a computer program product including instructions, where the computer program product, when run on an electronic device, enables a processor to perform any method in the technical solutions of the first aspect.
With reference to the accompanying drawings in embodiments of this application, the following clearly describes the technical solutions in embodiments of this application.
To facilitate understanding of the technical solutions of this application, a technical problem to be resolved in this application is first described below.
To facilitate understanding of the technical solutions of this application, terms in this application are first explained.
A level of detail (LOD) level can perform hierarchical display on details of an object model in world space when a distance between a camera (an observation angle) and the object model (an object in a scene) is different. When the observation angle is close to the object model, a LOD level with high object fineness is displayed; and when the observation angle is far away from the object model, a LOD level with low object fineness is displayed, thereby reducing performance costs of an electronic device.
A tile refers to a plane formed by a smallest plane formation unit, namely, a polygon, in two-dimensional or three-dimensional space. Generally, a model in space needs to be divided into countless tiny tiles during rendering, where a LOD level determines a quantity of tiles.
A view plane image refers to a plane image corresponding to a screen of an electronic device, that is, a two dimensional (2D) plane, and the view plane image is correspondingly provided with pixels, in other words, the view plane image presents a rendering result by using a pixel as a unit.
Rasterization is a process in which an object model in a 3D scene is segmented through a polygon, such as, a triangle, and three-dimensional coordinates of vertices of the triangle are converted into a two-dimensional view plane image, so that the object model corresponds to some pixels on the view plane image, to be specific, mathematical description of the object model and color information related to the object model are converted into pixels corresponding to positions and colors for filling the pixels on a screen, to obtain a raster image.
Raytracing is also referred to as light trace tracing or ray tracing, and is a method for presenting a three-dimensional (3D) image on a two-dimensional (2D) screen. The ray tracing tracks a ray interacting with an optical surface, to obtain an object model with a ray path. A principle is that when the ray is projected onto a surface of an object, one or more of the following phenomena usually occur, for example, light absorption, light reflection, light refraction, and light diffuse reflection. After the phenomena, a light spectrum changes, to be specific, the ray always goes through a series of attenuation and enters human eyes at last, and the ray tracing is to figure out a case in which the ray goes through the series of attenuation after being emitted and then enters the human eyes. Therefore, a sense of reality in a scene is virtualized through simulation.
The ray tracing may include forward ray tracing and backward ray tracing. The forward ray tracing means that a ray propagates along a straight line from a light source, and interaction between the ray and a surface of an object occurs in a propagation process in a scene. Some rays are absorbed by the object, some rays are projected through the object or reflected by the object, and a part of rays are even reflected a plurality of times among a plurality of objects, and enter the human eyes (an observation angle) at last.
The backward ray tracing means that a ray is emitted from the observation angle (for example, a camera in a scene), the ray interacts with a surface of an object in the scene after passing through a view plane corresponding to a screen. Some rays are absorbed by the object, some rays are projected through the object or reflected by the object, and a part of rays are even reflected a plurality of times among a plurality of objects. Color information in these ray paths is calculated and accumulated, and then rendered to corresponding pixels, to obtain a final render target.
To facilitate understanding of the technical solutions of this application, the technical problem to be resolved in this application is first described.
With reference to the accompanying drawings, the following describes a rendering optimization method in embodiments of this application.
Refer to
In some embodiments, a quantity of pixels corresponding to the view plane image 20 is equal to or in a proportional relationship with a quantity of pixels corresponding to the screen. When the quantity of pixels corresponding to the view plane image is in a proportional relationship with the quantity of pixels corresponding to the screen, an image, when displayed on the screen, can be properly scaled up or scaled down based on the proportional relationship, to adapt to the pixels corresponding to the screen.
The ray tracing described above has a strong gain effect in improving authenticity of a rendered picture, for example, has the gain effect in the following aspects: shadow overlaying, reflection, ambient light shielding, semi-transparent material, and global illumination. For algorithms used in the foregoing aspects, a large number of intersections of ray vectors and a three-dimensional scene needs to be calculated. Objects in the scene are all formed by many tiles. The tiles form a spatial structure of the object model. One pixel in the view plane image (corresponding to the screen) may correspond to one or more tiles, that is, a path of the ray passing through the pixel may pass through one or more tiles, and a color value corresponding to the pixel is calculated based on the tiles (the path corresponding to the ray). When there are many object models in the three-dimensional scene, or the spatial structure of the object model is complex (there are many tiles), a quantity of intersections calculated between rays and a surface of an object is increased, and calculation efficiency is increased with a data volume and complexity of a spatial structure in the three-dimensional scene. However, in real life, when the observation angle is far away from an object, many details are not clear to naked eyes. In this case, if a data volume of a spatial structure at a close distance is used for calculating an intersection of the ray and the object, an amount of calculation is large, but a good visual effect is not achieved. As a result, an amount of calculation of the device is large, calculation efficiency is low, and even an image output frame rate of the device is affected.
Based on the existing problem, this application provides a rendering optimization method. A data volume of a ray tracing acceleration structure is dynamically changed based on a change in a scene, thereby optimizing ray intersection calculation efficiency of a ray and an object model in the scene, and improving calculation performance of ray tracing.
With reference to the scene shown in
Based on the rendering optimization method in embodiments of this application, in an update process of a three-dimensional scene, a simplification degree of a model is determined by calculating a visual range proportion of an object model in a screen, and model simplification is performed on the object model in parallel, so that a quantity of triangular meshes forming the model is reduced when geometry of the model is maintained. In this way, when the object model is far away from an observation angle, ray intersection calculation efficiency can be optimized, thereby reducing an amount of calculation, and improving calculation performance of ray tracing.
The electronic device in embodiments of this application may be a mobile phone, a tablet computer, a notebook computer, a PC, a mobile Internet device (MID), or a wearable device, a virtual reality (VR) device, an augmented reality (AR) device, a wireless electronic device in industrial control, a wireless electronic device in self driving, a wireless electronic device in remote medical surgery, a wireless electronic device in smart grid, a wireless electronic device in transportation safety, a wireless electronic device in smart city, a wireless electronic device in smart home, or the like. The electronic device may be a device running a Harmony operating system (HarmonyOS), an Android system, an iOS system, a Windows system, or another system. An application program that needs to render a 3D scene to obtain a two-dimensional image, for example, an application such as a game or a video, may be run in the electronic device.
The following describes the rendering optimization method in embodiments of this application with reference to a specific structure of the electronic device.
It may be understood that, the structure shown in this embodiment of the present disclosure does not constitute a specific limitation on the electronic device 300. In some other embodiments of this application, the electronic device 300 may include more or fewer components than those shown in the figure, or combine some components, or split some components, or arrange different components. The components shown in the figure may be implemented by hardware, software, or a combination of the software and the hardware.
The processor 310 may include one or more processing units. In some embodiments, as shown in
In some other embodiments, the processor may further include an application processor (AP), a modem processor, an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), a baseband processor, a neural-network processing unit (NPU), and/or the like. Different processing units may be independent devices, or may be integrated into one or more processors.
The processor 310 may generate an operation control signal based on instruction operation code and a time sequence signal, to complete control of instruction fetching and instruction execution.
The processor 310 may further arrange a memory, to store instructions and data. In some embodiments, the memory in the processor 310 is a cache. The memory may store instructions or data just used or cyclically used by the processor 310. If the processor 310 needs to use the instructions or data again, the instructions or data may be directly invoked from the memory. For example, during real-time rendering, data corresponding to a to-be-rendered scene, for example, vertex data, coordinate data, a material, and illumination that are of geometry corresponding to a person, a house, and an article may be stored, to avoid repeated access, and reduce waiting time of the processor 310, thereby improving system efficiency.
In an embodiment of this application, a CPU 311 may run an operating system and various application programs. An application program may be a graphics application program, for example, a game or video player. The operating system provides a system graphics library interface. The application program may generate an instruction stream for rendering a graphic or an image frame and required related render data through the system graphics library interface and a drive program provided by the operating system. The instruction stream includes a series of instructions, and the instructions are generally invoke instructions for the system graphics library interface.
A GPU 312 is configured to receive a graphic instruction stream sent by the CPU 311; generate a view plane image in which some pixels are covered by a bounding box through a graphics rendering pipeline; obtain a trajectory of a ray in world space combined with a ray tracing technology, that is, obtain a tile intersecting with the ray; and finally calculate a color value based on the trajectory of the ray in the world space, and output the color value to a corresponding pixel, to obtain a render target. Through a procedure, for example, image layer synthesis of the operating system, the render target is displayed on the display 330 The graphics rendering pipeline, or the rendering pipeline (the pipeline), may be referred to as a rendering assembly line, a pixel assembly line, or a pixel pipeline, which is a parallel processing unit for processing a graphics signal inside the graphics processing unit.
A main function of the graphics rendering pipeline is to generate (or render) a two-dimensional image based on a given virtual camera, a three-dimensional object, a light source, and the like. The GPU may include a plurality of rendering pipelines, and the plurality of rendering pipelines may independently process the graphic signal in parallel. One rendering pipeline may perform a series of operations in a process of rendering the graphic or the image frame. For example, the operations of the one rendering pipeline may be roughly divided into the following: vertex processing, primitive processing, rasterization, fragment processing, and the like.
In this embodiment of this application, the communication unit 340 may include an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, the modem processor, the baseband processor, and the like, and communication with another electronic device is implemented through cooperation of components.
In some embodiments, the electronic device 300 may be connected to a cloud server through a communication unit, to obtain data of a to-be-rendered object in a 3D scene, or may communicate with the another electronic device to obtain data of a to-be-rendered object.
The electronic device 100 implements a display function through the GPU, a display 194, the application processor, and the like. The GPU is a microprocessor for image processing, and is connected to the display 194 and the application processor. The GPU is configured to perform mathematical and geometric calculation for graphics rendering. The processor 110 may include one or more GPUs, and the one or more GPUs execute program instructions to generate or change display information.
The display 194 is configured to display an image, a video, and the like. The display 194 includes a display panel. The display panel may use a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light emitting diode (AMOLED), a flex light-emitting diode (FLED), a miniled, a microled, a micro-oLed, a quantum dot light-emitting diode (QLED), or the like. In some embodiments, the electronic device 100 may include one or N displays 194, where N is a positive integer greater than 1.
In an embodiment of this application, when the CPU 311 submits set parameters and vertex data content to the GPU 312, the GPU 312 completes rendering of an object model in a scene based on the data, to obtain a render target, and stores the render target into a frame buffer. A video controller reads data of the frame buffer frame by frame, and the render target is finally displayed on the display 330 after data conversion. A user may observe a render graph with a special effect through the display 330.
The memory 320 may include an internal memory and an external memory. The internal memory may be configured to store computer-executable program code, and the executable program code includes instructions. The internal memory may include a storage program area and a storage data area. The storage program area may store the operating system, an application program required by at least one function (for example, a game or an image playback function), and the like. The storage data area may store data created during use of the electronic device 300 (for example, render target image data obtained through CPU and GPU rendering) and the like. In addition, the internal memory may include a high-speed random access memory, or may include a non-volatile memory, for example, at least one magnetic disk storage device, a flash memory device, or a universal flash storage (UFS).
In some embodiments, the processor 310 runs the instructions stored in the internal memory, for example, the instruction stream for rendering the graphic or the image frame, to enable the GPU to process to-be-rendered data based on the instruction stream.
A software system of the electronic device 300 may use a layered architecture, an event-driven architecture, a micro-core architecture, a micro-service architecture, or a cloud architecture. In embodiments of the present disclosure, an Android system with a layered architecture is used as an example to describe a software structure of the electronic device 300.
The layered architecture divides software into a plurality of layers, and each layer has a clear role and division. The layers communicate with each other through a software interface. In some embodiments, the Android system is divided into four layers, which are respectively an application program layer, an application program framework layer, Android runtime and a system library, and a kernel layer from top to bottom.
The application program layer may include a series of application program packages.
As shown in
The application program framework layer provides an application programming interface (API) and a programming framework for an application program of the application program layer. The application program framework layer includes some predefined functions.
As shown in
The window manager is configured to manage a window program. The window manager may obtain a display size, so that a final rendered image is displayed on the display based on a corresponding proportion.
The content provider is configured to store and obtain data, and enable the data to be accessed by an application program. The data may include data corresponding to an object model in a game scene, for example, may be a video, an image, an audio, calls made and answered, a browsing history, a bookmark, an address book, and the like.
The view system includes visual control such as a control for displaying a text and a control for displaying a picture. The view system may be configured to construct an application program. A display interface may be formed by one or more views. For example, a display interface including an SMS message notification icon may include a view for displaying a text and a view for displaying a picture.
The resource manager provides various resources, such as a localized character string, an icon, a picture, a layout file, and a video file for an application program.
The Android runtime includes a core library and a virtual machine. The Android runtime is responsible for scheduling and management of the Android system.
The core library includes two parts: one part is a function that needs to be invoked in a Java language, and another part is an Android core library.
The application program layer and the application program framework layer run in the virtual machine. The virtual machine executes Java files of the application program layer and the application program framework layer as binary files. The virtual machine is configured to perform functions such as object lifecycle management, stack management, thread management, security and exception management, and garbage collection.
The system library may include a plurality of functional modules, for example, a surface manager, a media library (Media Libraries), a three-dimensional graphics processing library (for example, OpenGL ES), and a 2D graphics engine (for example, SGL).
The surface manager is configured to manage a display subsystem, and provide fusion of a 2D layer and a 3D layer for a plurality of application programs.
The media library supports playback and recording in a plurality of common audio and video formats, a static image file, and the like. The media library may support a variety of audio and video encoding formats, for example, MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG.
The three-dimensional graphics processing library is configured to implement an image rendering process based on the graphics rendering pipeline, obtain the render target, further perform layer processing on the render target, and the like.
The 2D graphics engine is a drawing engine for 2D drawing.
The kernel layer is a layer between the hardware and the software. The kernel layer includes at least a display driver, a camera driver, an audio driver, and a sensor driver.
The foregoing describes a specific structure of the electronic device to which the rendering optimization method provided in embodiments of this application is applied. The following uses an example in which the electronic device is a mobile phone to describe a procedure of the rendering optimization method provided in embodiments of this application in detail.
Refer to
S501: Obtain a to-be-rendered object model in a scene.
In this embodiment of this application, geometry corresponding to a to-be-rendered object in the scene may include a model in a 2D or 3D scene and attribute information assigned to the model. For example, geometry models may be sky, a cloud, a person, a building, grass, a tree, an article, and the like, and such models may be drawn by using a three-dimensional modeling or two-dimensional modeling application program. The model in the scene has own coordinates, and the attribute information of the model may include attribute information such as a color of the model, a material, illumination intensity, and a ray direction.
It may be understood that, for a mobile phone, a geometry model corresponding to the to-be-rendered object is formed by associated data. Therefore, the geometry model in this application may also be understood as to-be-rendered data.
It should be understood that, the to-be-rendered data may be stored in a memory of the mobile phone in a process in which the mobile phone downloads and installs an application; or in a process in which the mobile phone runs an application program, for example, an online game, the mobile phone may communicate with a server in real time and obtain to-be-rendered data in a game scene, so that the mobile phone can implement image rendering based on the to-be-rendered data.
In other words, the mobile phone may obtain to-be-rendered data by reading local data; or the mobile phone may obtain, through a communication connection with another device, to-be-rendered data from the another device in real time. A specific manner of obtaining the to-be-rendered data is not limited in this application.
In some embodiments, the mobile phone may further provide a position and an orientation in world space where a naked eye should look toward an object in the world space, that is, set a position and an orientation of a camera, and determine a projection manner and a projection matrix. The projection manner may be an orthographic projection or a perspective projection, and the projection matrix may include matrix coordinates, position coordinates of the camera, a size of a screen, a size of a view plane image, and the like.
In this embodiment of this application, the process described in S501 may be performed by the CPU shown in
S502: Create a bounding box corresponding to the object model.
In this embodiment of this application, the bounding box may be an axis aligned bounding box (AABB), an oriented bounding box (OBB), a spherical bounding box, or the like.
As shown in
The following uses an example in which the object model is the sphere and the AABB bounding box is created for the sphere to describe determining of vertex data of the bounding box.
Refer to
As shown in a in
S503: Determine a pixel range covered when the bounding box is projected onto a screen of the electronic device, and obtain a ratio of a quantity of pixels covered by the bounding box to a quantity of pixels of the screen.
In this embodiment of this application, the screen of the electronic device corresponds to a view plane image. When a width and a height of the view plane image are equal to a width and a height of the screen, and resolution of the view plane image is the same as resolution of the screen, that is, it indicates that a quantity of pixels of the view plane image is equal to the quantity of pixels of the screen, a ratio of the size of the view plane image to the size of the screen is 1:1. When the electronic device displays, through the screen, the render target after rendering, the view plane image does not need to be scaled up. However, in some embodiments, the size of the view plane image is in a proportion to the size of the screen, and the quantity of pixels of the view plane image is also in a proportion to the quantity of pixels of the screen. In this case, when the render target is displayed, it is necessary to scale up or scale down a rendered view plane image, so that a corresponding image can be properly displayed. Because the quantity of pixels of the view plane image is in the proportion to the quantity of pixels of the screen, for example, the ratio is r1, when the bounding box is projected onto the view plane image, the pixel range of the screen covered by the bounding box may also be obtained based on a pixel range of the view plane image covered by the bounding box. In the following embodiments, the covered pixel range after the bounding box is projected is first described.
Refer to
In an embodiment of this application, when the bounding box is a bounding sphere, a calculation formula for the ratio of the quantity of pixels covered by the bounding box to the quantity of pixels of the view plane image is 4πR2/(W×H). R indicates a quantity of pixels on a radius of the bounding sphere, W indicates a width of the resolution of the view plane image, and H indicates a height of the resolution of the view plane image. Then, the ratio is multiplied by L to obtain a final ratio of the quantity of pixels covered by the bounding box to the quantity of pixels of the screen.
In this embodiment of this application, a ratio of a quantity a of pixels covered by the bounding box to a quantity b of pixels of the screen indicates a relationship of the two, that is, a/b. In some embodiments, a relationship a/c may also be indicated through a ratio c (c=b−a) of the quantity of pixels covered by the bounding box to a quantity of pixels not covered by the bounding box. Alternatively, a relationship b/c may be indicated based on a ratio of the quantity b of pixels of the screen to the quantity c of pixels not covered by the bounding box. In addition, the relationship may be nth power of a/b or a multiple. A form expressing the relationship is not limited in this application.
In some embodiments, before the projection, an identifier of a pixel corresponding to the view plane image may be initialized to 0, and after the GPU obtains a two-dimensional bounding box model, to be specific, after the bounding box is projected onto the view plane image, a value of a pixel covered by the bounding box model is changed from 0 to 1, to obtain a view plane image with an identifier shown in (b) in
S504: Determine a distance (D) between an observation angle in the scene and the object model.
The observation angle refers to the aforementioned camera, and world coordinates of the object model may be obtained through conversion and calculation based on the vertex coordinates of the triangular meshes of the object model and model coordinates. The distance between the camera and the object model may be calculated through the camera coordinates and the coordinates of the object model in the world space. The camera coordinates may be coordinate values preset based on an angle required for observing the object in the scene, and the model coordinates are also preset values. A specific calculation process of the distance D is as follows:
1. Obtain a three-dimensional vector (X, Y, Z) indicating world space coordinates in a spatial matrix of the model and world space coordinates (X0, Y0, Z0) of the camera.
2. Calculate a modulus, namely, the distance D, of the vector formed by the two coordinates through Formula (1).
It should be noted that, this step is a step parallel to S503, and there is no limitation on a sequence.
S505: Determine a level of detail LOD level corresponding to the object model based on the distance and the ratio.
In an embodiment of this application, the distance D and the ratio r3 are weighted respectively, and a weighted sum is calculated. For example, a weight of the distance D may be 0.3, and a weight of the ratio r3 may be 0.7. That the LOD level=0.3D+0.7×r3 is calculated.
In some embodiments, a weight may also be another proportional relationship. For example, the weight of the distance D is 0.1, and the weight of the ratio r3 is 0.9; or the weight of the distance D is 0.5, the weight of the ratio r3 is 0.5. By setting the ratio, the weight of the distance D and the weight of the ratio r3 can be adjusted more properly, which is beneficial to obtaining a LOD model with a proper level.
S506: Update polygonal mesh data based on the LOD level, to obtain simplified polygonal mesh data (second polygonal mesh data).
In some embodiments of this application, part of the polygonal mesh data may be removed by using a tile reduction algorithm. A triangular tile reduction algorithm is used as an example for description.
Refer to
After the tile reduction algorithm, polygonal mesh data of the object model is re-obtained. For a structure diagram, refer to
The GPU obtains vertex coordinates and index data of triangles of a simplified object model, updates data of a vertex buffer and data of an index buffer, to obtain and reconstruct a vulkan ray tracing acceleration structure, including the vertex coordinates and the index data of the triangles, the quantity of triangular meshes, and the like. A specific implementation process is as follows:
1. Obtain a GPU address of the vertex buffer and the index buffer of the model after tile reduction by using a vulkan instruction vkGetBufferDeviceAddressKHR.
2. Fill VkAccelerationStructureGeometryKHR and a data structure of corresponding BuildGeometryInfo (that is, initialize a ray tracing geometry information structure, and fill the address of the vertex buffer and the index buffer obtained in the foregoing step into the geometric information structure).
3. Invoke a vulkan instruction vkCmdBuildAccelerationStructuresKHR, to construct the ray tracing acceleration structure.
S507: Render the object model based on the simplified polygonal mesh data.
In an embodiment of this application, before rendering the object model, the GPU may first determine whether the object model needs ray tracing rendering. If the object model needs ray tracing rendering, the GPU performs S507. By binding the ray tracing acceleration structure obtained in S506, a shader that supports vulkan ray tracing is used, to calculate an intersection between a ray and the scene, to shade the object model.
Refer to
In some other embodiments of this application, the GPU determines that a rendering mode of the sphere model is normal rendering, for example, rasterization rendering. In this case, in an actual rendering process, an original triangular mesh data structure may also be for performing rasterization rendering on the sphere model.
It should be noted that, in the foregoing rendering process, for a calculation method that is not mentioned, for example, calculation of an intersection shading value, a method in the conventional technologies may be used for calculation. This is not described in detail in this application again.
It should be noted that, in the foregoing embodiments of this application, steps performed by the GPU may alternatively be performed by the CPU. In some embodiments, steps performed by the CPU may alternatively be performed by the GPU, or all steps may be performed by the GPU, or all steps may be performed by the CPU. This is not limited in this application.
The rendering optimization method described in
S1301: Perform a scene update.
The process may be understood as obtaining an object model in a scene, and performing coordinate conversion processing on the model, and the like. The rendering engine executes the scene update stage. Based on service logic information, the rendering engine performs an animation update and a physical update on the model. An updated result is displayed as a change in spatial coordinates of the model and a spatial matrix, and a change in the spatial coordinates caused by model vertex displacement. Updated data is synchronized to a vertex buffer of the model, including vertex coordinates and index data of triangular meshes, and the like. The step corresponds to S501 in
S1302: Determine whether a current object model needs ray tracing.
There are many object models in a scene, some object models need rendering, and some models do not need rendering. For an object model that does not need ray tracing rendering, simplification calculation may not be required for the model. For an object model that needs ray tracing, the processor performs S1303.
S1303: Calculate a LOD level (a LOD value) of the object model. To be specific, a distance between the object model and a camera in a three-dimensional scene (world space) and a pixel proportion of the object model rendered to a screen picture are calculated first, and then weighted summation is performed on the distance and the proportion, to obtain the LOD level. For a specific calculation process, refer to S502 to S505 in
S1304: Simplify the model. In the simplification process, a triangular mesh tile reduction algorithm may be used, to dynamically remove triangular meshes of the model. For example, edge collapse and vertex merging is used, to reduce a quantity of triangles in the triangular meshes and a quantity of corresponding vertices. The step corresponds to S506 in
The model is simplified based on the obtained LOD value, to obtain the simplified object model.
A simplified scene in
S1305: Construct a ray tracing acceleration structure. The step corresponds to descriptions of the ray tracing acceleration structure in S506 in
During scene rendering, a GPU needs to determine whether each model in the scene needs ray tracing rendering when performing rendering. That is, S1306 is performed.
S1306: Determine a rendering mode of the model, that is, determine whether to need ray tracing.
When determining that the model does not need ray tracing, the GPU performs S1307, to perform model normal rendering on the current model.
When determining that the model needs ray tracing, the GPU performs S1308, to perform ray tracing rendering. The step corresponds to description of ray tracing in S507 in
According to the rendering optimization method in embodiments of this application, a simplification degree, namely, a LOD level of a model is determined by calculating a visual range proportion of the model in a screen and a distance between an observation angle and the model, to implement dynamic adjustment of a LOD level, so that a quantity of triangular meshes forming the model is reduced when geometry of the model is maintained. In this way, calculation consumption of ray tracing can be effectively reduced, and a ray tracing rendering result with similar image quality can be efficiently generated.
Refer to
According to an embodiment of this application, the LOD calculation module 1440 is further configured to perform weighted summation calculation on the distance and the relationship respectively, to obtain a summation result; and determine the LOD level based on the summation result.
According to an embodiment of this application, the ratio determining module 1430 is further configured to obtain first world coordinates of the observation angle in world space and second world coordinates of the object model in the world space, and determine the distance between the observation angle and the object model based on the first world coordinates and the second world coordinates.
According to an embodiment of this application, the LOD calculation module 1440 is configured to: remove, based on the LOD level, a part of polygonal mesh data from the first polygonal mesh information by using a polygonal mesh tile reduction algorithm to obtain the second polygonal mesh data.
According to an embodiment of this application, the rendering module 1450 is configured to:
According to an embodiment of this application, the rendering module 1450 is further configured to:
According to an embodiment of this application, the bounding box creation module 1420 is configured to determine, based on the first polygonal mesh information, maximum values and minimum values of the object model in an X direction, a Y direction, and a Z direction in coordinates of the object model; and determine the bounding box based on the maximum values and the minimum values.
According to an embodiment of this application, the relationship between the quantity of pixels covered by the bounding box and the quantity of pixels of the screen is a ratio of the quantity of pixels covered by the bounding box to the quantity of pixels of the screen.
According to an embodiment of this application, a weight of the distance is set to range from 0.1 to 0.5, and a weight of the ratio ranges from 0.5 to 0.9.
According to an embodiment of this application, the weight of the distance is set to 0.3, and the weight of the ratio is 0.7.
According to an embodiment of this application, the first polygonal mesh information includes a quantity of polygons corresponding to the object model not simplified through the LOD level, and vertex coordinates and vertex indexes of the polygon.
According to an embodiment of this application, the second polygonal mesh information includes a quantity of polygons corresponding to the object model simplified based on the LOD level, and the vertex coordinates and the vertex indexes of the polygon.
According to an embodiment of this application, the polygon is a triangle.
It should be noted that, functions and effects of the foregoing modules are described in detail in the embodiments shown in
This application further provides an electronic device, including:
This application further provides a computer-readable storage medium, storing a computer program, where the computer program, when run by a processor, enables the processor to perform the method explained with reference to
This application further provides a computer program product including instructions, where the computer program product, when run on an electronic device, enables a processor to perform the method shown in
Now refer to
The static random access memory (SRAM) unit 1530 may include one or more computer-readable media for storing data and/or instructions. A computer-readable storage medium may store instructions, and specifically, store temporary and permanent copies of the instructions. The instructions may include the following: When the instructions are executed by at least one unit in a processor, the SoC 1500 is enabled to perform the method according to the foregoing embodiments. For details, refer to the method explained in
Embodiments of all mechanisms disclosed in this application may be implemented in hardware, software, firmware, or a combination of these implementation methods. Embodiments of this application may be implemented as a computer program or program code executed on a programmable system. The programmable system includes at least one processor, a storage system (including a volatile and non-volatile memory and/or a storage element), at least one input device, and at least one output device.
The program code may be used for inputting instructions, to execute functions described in this application and generate output information. The output information may be used in one or more output devices in a known manner. For an objective of this application, a processing system includes any system having a processor such as a digital signal processor (DSP), a microcontroller, an application specific integrated circuit (ASIC), or a microprocessor.
The program code may be implemented in a high-level programming language or an object-oriented programming language, to communicate with the processing system. When necessary, an assembly language or a machine language may also be used for implementing the program code. In fact, the mechanisms described in this application are not limited to the scope of any particular programming language. In any case, the language may be a compiled language or an interpreted language.
In some cases, the disclosed embodiments may be implemented in hardware, firmware, software, or any combination thereof. The disclosed embodiments may also be implemented as instructions carried or stored thereon by one or more temporary or non-transitory machine-readable (for example, computer-readable) storage media, which may be read and executed by one or more processors. For example, the instructions may be distributed through a network or through another computer-readable medium. Therefore, the machine-readable medium may include any mechanism for storing or transmitting information in a machine-readable form (for example, a computer), including but not limited to a floppy disk, a compact disk, an optical disk, a compact disc read only memory (CD-ROM), a magneto-optical disk, a read only memory (ROM), a random access memory (RAM), an erasable programmable read only memory (EPROM), an electrically erasable programmable read only memory (EEPROM), a magnetic card or an optical card, a flash memory, or a tangible machine-readable memory for transmitting information (for example, a carrier, an infrared signal, or a digital signal) using the Internet in electrical, optical, acoustic, or another form of a propagation signal. Therefore, the machine-readable medium includes any type of machine-readable medium suitable for storing or transmitting electronic instructions or information in the machine-readable form (for example, the computer).
In the accompanying drawings, some structures or method features may be shown in a particular arrangement and/or order. However, it should be understood that, such specific arrangement and/or order may not be required. In some embodiments, the features may be arranged in a different manner and/or order from a manner and/or order shown in the accompanying drawings of the specification. In addition, structures or method features included in a particular figure are not meant to imply that the features are required in all embodiments. In some embodiments, the features may not be included or may be combined with other features.
It should be noted that, units/modules mentioned in device embodiments of this application are logical units/modules. Physically, one logical unit/module may be a physical unit/module, or may be a part of a physical unit/module, or may be implemented through a combination of a plurality of physical units/modules. Physical implementations of the logical units/modules are not the most important, but a combination of functions implemented by the logical units/modules is only a key for resolving the technical problem provided in this application. In addition, to highlight the innovative part of this application, the foregoing device embodiments of this application do not introduce a unit/module that is not very relevant to the technical problem provided in this application. However, this does not indicate that other units/modules do not exist in the foregoing device embodiments.
It should be noted that, relational terms herein such as first and second are used only to differentiate an entity or operation from another entity or operation, and do not require or imply any actual relationship or sequence between these entities or operations. Moreover, the terms “include”, “comprise”, or any variants thereof are intended to cover a non-exclusive inclusion. Therefore, a process, method, article, or device that includes a series of elements not only includes such elements, but also includes other elements not specified expressly, or may include inherent elements of the process, method, article, or device. Unless otherwise specified, an element limited by “include a/an . . . ” does not exclude other same elements existing in the process, the method, the article, or the device that includes the element.
Although this application has been illustrated and described with reference to some preferential embodiments of this application, a person of ordinary skill in the art should understand that, various changes may be made in forms and details without departing from the spirit and scope of this application.
Number | Date | Country | Kind |
---|---|---|---|
202210934834.4 | Aug 2022 | CN | national |
This application is a national stage of International Application No. PCT/CN2023/091244, filed on Apr. 27, 2023, which claims priority to Chinese Patent Application No. 202210934834.4, filed on Aug. 4, 2022. The disclosures of both of the aforementioned applications are hereby incorporated by reference in their entireties.
Filing Document | Filing Date | Country | Kind |
---|---|---|---|
PCT/CN2023/091244 | 4/27/2023 | WO |