Method and device for efficient rendering in Unity built-in rendering pipeline

By creating two rendering buffers in Unity's built-in rendering pipeline and replacing the rendering target, the performance bottleneck caused by GrabPass is solved, efficient rendering effect is achieved, and the user experience of mobile devices is improved.

CN120339487APending Publication Date: 2025-07-18BEIJING XUEJING TECHNOLOGY CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510482682.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-17
Publication Date
2025-07-18

AI Technical Summary

Technical Problem

The prior art uses GrabPass in Unity’s built-in rendering pipelines to cause frame rate reduction and performance bottlenecks, especially on mobile devices that are prone to heat generation and frame dropping problems.

Method used

Create two render buffers in Unity's built-in rendering pipeline, replace the rendering target of the main camera as the first rendering buffer, and set the second rendering buffer to the global accessible texture of the shader. At different stages, the contents of the first rendering buffer are copied to the second rendering buffer and the screen buffer, and finally perform special rendering effect calculations in the shader.

Benefits of technology

By reducing the interruption of rendering state and screen replication, the rendering performance is improved and the user experience of mobile devices is improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120339487A_ABST
    Figure CN120339487A_ABST
Patent Text Reader

Abstract

The invention provides a method and device for efficient rendering in a Unity built-in rendering pipeline, and the method comprises the steps: creating two rendering buffer regions in a script, and copying the content of a first rendering buffer region to a second rendering buffer region and a screen buffer region at different rendering stages, and finally, completing calculation of a special rendering effect in the shader according to the second rendering buffer area, and then realizing final rendering. According to the embodiment of the invention, Grab operation is replaced by a common drawing command, interruption of a rendering state and copying of a screen are reduced, shaders in the same frame use the same global texture to carry out one-time operation, and meanwhile, the rendering performance is improved by reducing the resolution of a rendering buffer area to carry out downsampling, so that the rendering efficiency is improved. And good user experience is obtained at the mobile terminal.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of three-dimensional rendering technology, and particularly to a method and apparatus for efficient rendering in the Unity built-in rendering pipeline, a computing device, and a computer-readable storage medium. Background Art

[0002] In existing Unity development projects, when using the built-in rendering pipeline, if the content of the screen buffer needs to be obtained in the shader, GrabPass is usually used. GrabPass is a unique mechanism in the Unity built-in rendering pipeline. It will save an additional copy of the screen buffer content when rendering the object and allows the content of the screen buffer to be referenced and used in subsequent rendering steps. However, using GrabPass is very likely to reach the rendering bottleneck, resulting in a decrease in the frame rate of the application or even lag. Therefore, a scheme that uses ordinary drawing operations in the Unity built-in rendering pipeline to replace GrabPass is needed. Summary of the Invention

[0003] In view of this, embodiments of the present application provide a method and apparatus for efficient rendering in the Unity built-in rendering pipeline, a computing device, and a computer-readable storage medium to solve the technical defects existing in the prior art.

[0004] According to the first aspect of the embodiments of the present application, a method for efficient rendering in the Unity built-in rendering pipeline is provided, including:

[0005] Create two render buffers, a first render buffer and a second render buffer; replace the rendering target of the main camera with the first render buffer, and set the globally accessible texture of the shader to the second render buffer;

[0006] When the main camera target rendering stage is completed, copy the content of the first render buffer to the second render buffer;

[0007] When the current frame rendering ends, copy the content of the first render buffer to the screen buffer;

[0008] Perform special rendering effect calculations according to the content of the second render buffer in the shader and then display it on the screen.

[0009] According to the second aspect of the embodiments of the present application, an apparatus for efficient rendering in the Unity built-in rendering pipeline is provided, including:

[0010] Creation unit, used to create two render buffers, a first render buffer and a second render buffer; replace the render target of the main camera with the first render buffer, and set the globally accessible texture of the shader to the second render buffer;

[0011] First copy unit, used to copy the content of the first render buffer to the second render buffer when the main camera target rendering stage is completed;

[0012] Second copy unit, used to copy the content of the first render buffer to the screen buffer when the current frame rendering ends;

[0013] Calculation unit, used to perform special rendering effect calculation according to the content of the second render buffer in the shader and then display it on the screen.

[0014] According to the third aspect of the embodiments of the present application, a computing device is provided, including a memory, a processor, and computer instructions stored on the memory and executable on the processor. When the processor executes the instructions, the steps of the method for performing efficient rendering in the Unity built-in rendering pipeline are implemented.

[0015] According to the fourth aspect of the embodiments of the present application, a computer-readable storage medium is provided, which stores computer instructions. When the instructions are executed by a processor, the steps of the method for performing efficient rendering in the Unity built-in rendering pipeline are implemented.

[0016] In the embodiments of the present application, when using the Unity built-in rendering pipeline, two render buffers are first created in the script. Then, the render target of the main camera is replaced with the first render buffer, and the second render buffer is set as the globally accessible texture of the shader. In different rendering stages, the content of the first render buffer is respectively copied to the second render buffer and the screen buffer. Finally, after the special rendering effect is calculated according to the second render buffer in the shader, the final rendering is completed. The embodiments of the present application replace the Grab operation with ordinary draw commands, reduce the interruption of the rendering state and the screen copy, and the shaders within the same frame all use the same global texture for one operation. At the same time, the rendering performance is improved by reducing the resolution of the render buffer for downsampling, and a good user experience is achieved on mobile devices. Description of the Drawings

[0017] Figure 1 is the structural block diagram of the computing device provided by the embodiments of the present application;

[0018] Figure 2 is the schematic diagram of a method for performing efficient rendering in the Unity built-in rendering pipeline provided by the embodiments of the present application;

[0019] Figure 3 It is a schematic structural diagram of a device for efficient rendering in the Unity built-in rendering pipeline provided by an embodiment of the present application. Detailed implementation manners

[0020] Many specific details are set forth in the following description in order to provide a thorough understanding of the present application. However, the present application can be implemented in many other ways different from those described herein, and those skilled in the art can make similar extensions without departing from the connotation of the present application. Therefore, the present application is not limited by the specific implementations disclosed below.

[0021] The terms used in one or more embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of the present application. The singular forms "a", "the", and "said" used in one or more embodiments of the present application and the appended claims are also intended to include the plural forms, unless the context clearly indicates otherwise. It should also be understood that the term "and / or" used in one or more embodiments of the present application refers to and includes any or all possible combinations of one or more of the associated listed items.

[0022] It should be understood that although the terms first, second, etc. may be used in one or more embodiments of the present application to describe various information, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from each other. For example, without departing from the scope of one or more embodiments of the present application, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "in response to determining".

[0023] In the present application, a method, a device, a computing device, and a computer-readable storage medium for efficient rendering in the Unity built-in rendering pipeline are provided, and will be described in detail one by one in the following embodiments.

[0024] Figure 1 A structural block diagram of a computing device 100 according to an embodiment of the present application is shown. The components of the computing device 100 include, but are not limited to, a memory 110 and a processor 120. The processor 120 is connected to the memory 110 through a bus 130, and a database 150 is used to store data.

[0025] The computing device 100 further includes an access device 140, which enables the computing device 100 to communicate via one or more networks 160. Examples of such networks include the Public Switched Telephone Network (PSTN), Local Area Network (LAN), Wide Area Network (WAN), Personal Area Network (PAN), or a combination of communication networks such as the Internet. The access device 140 may include one or more of any type of wired or wireless network interface (e.g., Network Interface Card (NIC)), such as an IEEE802.11 Wireless Local Area Network (WLAN) wireless interface, Worldwide Interoperability for Microwave Access (Wi-MAX) interface, Ethernet interface, Universal Serial Bus (USB) interface, cellular network interface, Bluetooth interface, Near Field Communication (NFC) interface, and so on.

[0026] In one embodiment of the present application, the above components of the computing device 100, as well as Figure 1 other components not shown, may also be connected to each other, for example, via a bus. It should be understood that Figure 1 the block diagram of the computing device shown is merely for illustrative purposes and is not a limitation on the scope of the present application. Those skilled in the art can add or replace other components as needed.

[0027] The computing device 100 can be any type of stationary or mobile computing device, including mobile computers or mobile computing devices (e.g., tablet computers, personal digital assistants, laptop computers, notebook computers, netbooks, etc.), mobile phones (e.g., smartphones), wearable computing devices (e.g., smartwatches, smart glasses, etc.) or other types of mobile devices, or stationary computing devices such as desktop computers or PCs. The computing device 100 can also be a mobile or stationary server.

[0028] In the prior art, the Built-in Rendering Pipeline is an early rendering pipeline of Unity and has existed for a long time. Therefore, there is a large amount of documentation and usage cases. It supports a wide range of platforms and hardware devices, is suitable for rapid development, and can achieve the rendering of complex scenes without much configuration. GrabPass is a unique mechanism in the Unity Built-in Rendering Pipeline. It will save an additional copy of the screen buffer content when rendering this object and allows this content to be referenced and used in subsequent rendering steps, which is very useful when making some special rendering effects. However, using GrabPass will incur certain performance costs because it requires additional memory and processing time. For example, GrabPass occupies a relatively large amount of video memory bandwidth, and it is more likely to cause heating, frequency reduction, and frame drops on mobile devices; GrabPass is controlled by a shader switch, which easily causes multiple shaders to enable this function in the same frame, resulting in repeated operations and exacerbating performance issues; the texture generated by GrabPass has the same resolution as the texture of the render buffer, and performance cannot be improved by downsampling.

[0029] Therefore, in order to solve the above problems, in the embodiments of the present application, a method for efficient rendering in the Unity Built-in Rendering Pipeline is proposed. Refer to Figure 2 and this method includes steps 202 to 208.

[0030] Step 202: Create two render buffers, a first render buffer and a second render buffer; replace the rendering target of the main camera with the first render buffer, and set the globally accessible texture of the shader to the second render buffer.

[0031] In the embodiments of the present application, when using the Built-in Rendering Pipeline, the rendering texture object is managed in the Unity script. Among them, when managing the rendering texture object, two custom render buffers are created: a first render buffer and a second render buffer.

[0032] Specifically, in a feasible implementation, the size of the first render buffer is set to be the same as the current screen size. The schematic code is as follows:

[0033] renderTextures[0] = RenderTexture.GetTemporary(Screen.width, Screen.height, 24, RenderTextureFormat.Default);

[0034] That is, the resolution of the RenderTexture is set using the width and height of the current screen to ensure that the first render buffer matches the current screen size.

[0035] The second rendering buffer can have a custom size. For example, its resolution size can be set by multiplying the width and height of the current screen by a scale factor Resolution.

[0036] renderTextures[1] = RenderTexture.GetTemporary((int)(Screen.width * Resolution)

[0037] , (int)(Screen.height * Resolution), 0, RenderTextureFormat.Default);

[0038] Furthermore, in the script, replace the rendering target of the main camera with the first rendering buffer. This step can be achieved through the camera's SetTargetBuffers function. For example, set the first rendering buffer as the camera's rendering target through the SetTargetBuffers function:

[0039] mainCamera.SetTargetBuffers(renderTextures[0].colorBuffer, renderTextures[0].depthBuffer);

[0040] Furthermore, set the second rendering buffer as a globally accessible texture for the shader. This step can be achieved through the shader's SetGlobalTexture function. For example:

[0041] Shader.SetGlobalTexture("_CameraExtraTexture", renderTextures[1]); / /

[0042] _CameraExtraTexture is a globally accessible texture variable defined in the shader.

[0043] Step 204: When the main camera target rendering phase is completed, copy the content of the first rendering buffer to the second rendering buffer.

[0044] In this step, according to the target rendering content to be copied, when the camera rendering is completed, copy the result of the camera rendering from the first rendering buffer to the second rendering buffer.

[0045] In a feasible implementation, when the content to be copied is the rendering result of an opaque object, after the AfterSkybox event of the camera, the content of the first rendering buffer is copied to the second rendering buffer. AfterSkybox is a camera event that is triggered after rendering the Skybox. For example:

[0046] cmdBlit1 = new CommandBuffer();

[0047] cmdBlit1.Blit(renderTextures[0], renderTextures[1]); / / Execute the Blit operation to copy a first rendering buffer to the second rendering buffer

[0048] mainCamera.AddCommandBuffer(CameraEvent.AfterSkybox, cmdBlit1); / / Add the cmdBlit1 command buffer to the main camera to make it execute after the AfterSkybox event

[0049] Preferably, according to the content to be copied and the performance requirements, the content of the first rendering buffer is copied at a fixed frequency. Specifically, the content of the first rendering buffer is copied to the second rendering buffer every other frame. For example, it is copied by judging whether a counter is even or odd. Increment the counter blitCount and check whether it is even. If blitCount is even, the condition is judged to be true for content copying, otherwise it is skipped, thus achieving copying once every other frame.

[0050] Those skilled in the art should be aware that the specific cases involved in the above code are only examples and not exhaustive. Those skilled in the art can select the copy timing and frequency of the rendering texture according to actual needs, which will not be elaborated here.

[0051] Step 206: Draw the content of the first rendering buffer to the screen buffer at the end of the current frame rendering.

[0052] In this step, at the end of the current frame rendering, the content of the first rendering buffer, that is, the rendering result of the main camera, is drawn to the screen buffer as the display content of the current screen. Since the rendering target of the main camera is the first rendering buffer, the content of the first rendering buffer is always the latest rendering result of the main camera at each stage of rendering.

[0053] In a feasible implementation, before performing any post-processing effects, the content of the first rendering buffer is copied to the screen buffer, such as:

[0054] cmd.Blit(renderTextures[0], BuiltinRenderTextureType.CameraTarget); / / The Blit method adds an operation to the command buffer to copy the content of the first render buffer to the built-in render target texture BuiltinRenderTextureType.CameraTarget, where CameraTarget represents the rendering result of the current camera.

[0055] mainCamera.AddCommandBuffer(CameraEvent.BeforeImageEffects, cmd); / / This means to execute the above copy command before any image post-processing effects.

[0056] Step 208: After performing special rendering effect calculations based on the content of the second render buffer in the shader, display it on the screen.

[0057] In this step, since the second render buffer is copied from the first render buffer and the second render buffer is a globally accessible texture in the shader, special rendering effect calculations can be completed based on the content of the second render buffer in the shader.

[0058] In the shader code, define a globally accessible texture variable:

[0059] uniform sampler2D _CameraExtraTexture;

[0060] According to the settings in Step 202, _CameraExtraTexture is the content of the second render buffer input by the script, and then special rendering effect calculations are completed based on the content of this texture, such as:

[0061] half3 color = tex2Dproj(_CameraExtraTexture, screenUV).rgb; / / Extract the RGB color for subsequent calculations, such as distortion calculations, etc.

[0062] Furthermore, in order to correctly display the obtained special rendering effect on the screen, first draw it to the first render buffer with the same size as the screen, and then draw the content of the first render buffer to the screen to complete the final rendering effect display.

[0063] In the above embodiments of the present application, when using Unity's built-in rendering pipeline, in order to avoid a series of performance problems brought by Grab Pass for capturing the content of the screen buffer, two render buffers are first created in the script, and then the rendering target of the main camera is replaced with the first render buffer, and the second render buffer is set as the globally accessible texture of the shader. In different rendering stages, the content of the first render buffer is copied to the second render buffer and the screen buffer respectively. Finally, after the special rendering effect is calculated according to the second render buffer in the shader, the final rendering is completed. The embodiments of the present application replace the Grab operation with ordinary drawing commands, reduce the interruption of the rendering state and the copying of the screen, and the shaders within the same frame all operate with the same global texture once. At the same time, the rendering performance is improved by downsampling by reducing the resolution of the render buffer, and a good user experience is achieved on mobile devices.

[0064] Corresponding to the above method embodiments, the present application also provides a device for efficient rendering in the Unity built-in rendering pipeline, as Figure 3 shown, the device includes:

[0065] A creation unit, configured to create two render buffers, a first render buffer and a second render buffer; replace the rendering target of the main camera with the first render buffer, and set the globally accessible texture of the shader as the second render buffer;

[0066] A first copy unit, configured to copy the content of the first render buffer to the second render buffer when the main camera target rendering stage is completed;

[0067] A second copy unit, configured to copy the content of the first render buffer to the screen buffer when the current frame rendering ends;

[0068] A calculation unit, configured to perform special rendering effect calculation according to the content of the second render buffer in the shader and then display it on the screen.

[0069] The above is a schematic solution of a device for efficient rendering in the Unity built-in rendering pipeline in this embodiment. It should be noted that the technical solution of this device and the technical solution of the above method for efficient rendering in the Unity built-in rendering pipeline belong to the same concept. For the details not described in detail in the technical solution of this device, reference can be made to the description of the technical solution of the above method for efficient rendering in the Unity built-in rendering pipeline.

[0070] In an embodiment of the present application, a computing device is further provided, including a memory, a processor, and computer instructions stored on the memory and executable on the processor. When the processor executes the instructions, the steps of the method for efficient rendering in the Unity built-in rendering pipeline are implemented.

[0071] The above is a schematic solution of a computing device in this embodiment. It should be noted that the technical solution of this computing device and the technical solution of the above method for efficient rendering in the Unity built-in rendering pipeline belong to the same concept. For the details not described in the technical solution of the computing device, reference can be made to the description of the technical solution of the above method for efficient rendering in the Unity built-in rendering pipeline.

[0072] An embodiment of the present application further provides a computer-readable storage medium, which stores computer instructions. When the instructions are executed by a processor, the steps of the method for efficient rendering in the Unity built-in rendering pipeline as described above are implemented.

[0073] The above is a schematic solution of a computer-readable storage medium in this embodiment. It should be noted that the technical solution of this storage medium and the technical solution of the above method for efficient rendering in the Unity built-in rendering pipeline belong to the same concept. For the details not described in the technical solution of the storage medium, reference can be made to the description of the technical solution of the above method for efficient rendering in the Unity built-in rendering pipeline.

[0074] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be performed in a different order than in the embodiments and still achieve the desired result. Additionally, the processes depicted in the figures do not necessarily require the particular order or sequential order shown to achieve the desired result. In certain implementations, multitasking and parallel processing are also possible or may be advantageous.

[0075] The computer instructions include computer program code, which may be in the form of source code, object code, executable files, or some intermediate forms, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording media, USB flash drives, mobile hard disks, magnetic disks, optical disks, computer memories, read-only memories (ROMs), random access memories (RAMs), electrical carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the content included in the computer-readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice within the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, the computer-readable medium does not include electrical carrier signals and telecommunication signals.

[0076] It should be noted that for the foregoing method embodiments, for the sake of simple description, they are all expressed as a series of action combinations. However, those skilled in the art should know that this application is not limited by the described action sequence, because according to this application, some steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily essential to this application.

[0077] In the above embodiments, the descriptions of the respective embodiments have their own emphases. For the parts not detailed in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0078] The preferred embodiments of the present application disclosed above are only used to help explain the present application. The optional embodiments do not elaborate on all the details, nor do they limit the invention to the specific embodiments described. Obviously, many modifications and variations can be made according to the content of this application. The present application selects and specifically describes these embodiments to better explain the principles and practical applications of the present application, so that those skilled in the art can understand and utilize the present application well. The present application is only limited by the claims and their full scope and equivalents.

Claims

1. A method for efficient rendering in the Unity built-in rendering pipeline, characterized in that, Including: Create two rendering buffers, a first rendering buffer and a second rendering buffer; replace the rendering target of the main camera with the first rendering buffer, and set the globally accessible texture of the shader to the second rendering buffer; When the main camera target rendering stage is completed, copy the content of the first rendering buffer to the second rendering buffer; When the current frame rendering ends, copy the content of the first rendering buffer to the screen buffer; In the shader, perform special rendering effect calculations based on the content of the second rendering buffer and then display it on the screen.

2. The method according to claim 1, wherein, The method further includes: the size of the first rendering buffer is the same as the current screen size, and the size of the second rendering buffer is a custom size.

3. The method according to claim 1, wherein When the main camera target rendering stage is completed, copying the content of the first rendering buffer to the second rendering buffer includes: When the AfterSkybox event of the main camera is triggered, copy the content of the first rendering buffer to the second rendering buffer.

4. The method according to claim 1, wherein Copying the content of the first rendering buffer to the second rendering buffer includes: Copy the content of the first rendering buffer to the second rendering buffer every n frames, where n is greater than 0.

5. The method according to claim 4, wherein When the current frame rendering ends, drawing the content of the first rendering buffer to the screen buffer includes: Before performing any post-processing effects, copy the content of the first rendering buffer to the screen buffer as the display content of the current screen.

6. The method according to claim 1, wherein In the shader, performing special rendering effect calculations based on the content of the second rendering buffer and then displaying it on the screen includes: In the shader, perform special rendering effect calculations based on the incoming second rendering buffer, display the calculation result to the first rendering buffer, and then draw the content of the first rendering buffer to the screen.

7. The method according to claim 1, wherein The special rendering effect calculations include but are not limited to distortion calculations and / or refraction calculations.

8. An apparatus for efficient rendering in the Unity built-in rendering pipeline, characterized in that, Including: A creation unit for creating two rendering buffers, a first rendering buffer and a second rendering buffer; replacing the rendering target of the main camera with the first rendering buffer, and setting the globally accessible texture of the shader to the second rendering buffer; A first copy unit for copying the content of the first rendering buffer to the second rendering buffer when the main camera target rendering stage is completed; A second copy unit for copying the content of the first rendering buffer to the screen buffer when the current frame rendering ends; A calculation unit for performing special rendering effect calculations based on the content of the second rendering buffer in the shader and then displaying it on the screen.

9. A computing device, comprising a memory, a processor, and computer instructions stored on the memory and executable on the processor, characterized in that, When the processor executes the instructions, it implements the steps of the method according to any one of claims 1-7.

10. A computer-readable storage medium storing computer instructions, characterized in that, When the instructions are executed by the processor, it implements the steps of the method according to any one of claims 1-7.