Shadow map drawing method and device, equipment and storage medium
By pre-assigning textures to multiple light sources and determining candidate light sources in the GPU, the problem of long CPU-to-GPU instruction sending time is solved, achieving efficient shadow texture drawing and improving the rendering efficiency of 3D scenes.
Patent Information
- Application Number
- CN202410625497.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-20
- Publication Date
- 2025-11-21
AI Technical Summary
When there are many light sources in a 3D scene, the data flow time from the CPU to the GPU to send drawing instructions is relatively long in the current technology, resulting in low efficiency of shadow map drawing and affecting scene rendering efficiency.
By acquiring light source allocation information, textures are pre-assigned to multiple light sources in the texture pool, and drawing instructions, light source information, and scene data are sent to the GPU, enabling the GPU to determine candidate light sources and draw shadow textures on the corresponding textures.
By using a single drawing command to render shadow maps of multiple light sources on virtual objects, the number of times the CPU sends commands to the GPU is reduced, communication latency and bandwidth usage are reduced, and the efficiency of shadow map rendering is improved.
Smart Images

Figure CN120997372A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of image rendering, and particularly relates to a shadow map drawing method and device, equipment and a storage medium. BACKGROUND
[0002] With the development of image rendering technology, in order to simulate a more realistic three-dimensional scene, an electronic device usually renders the shadow of a virtual object in a three-dimensional scene, which can be a virtual character.
[0003] In related technologies, when rendering a virtual object in a scene, if there are a large number of light sources in the scene, a large number of shadow maps of the virtual object in the scene need to be drawn, and each light source needs to send a drawing instruction to a graphics processing unit (GPU) through a central processing unit (CPU) to initiate the drawing of the shadow map of the light source. Because the process of sending a drawing instruction from the CPU to the GPU often requires a long data flow time, when there are a large number of light sources in the scene, it will lead to low efficiency of drawing the shadow map of the virtual scene, and further affect the rendering efficiency of the scene. SUMMARY
[0004] The present application provides a shadow map drawing method, device, equipment and storage medium, by obtaining light source allocation information, drawing multiple shadow maps based on the light source allocation information after the GPU receives the drawing instruction, thereby effectively improving the drawing efficiency of the shadow map.
[0005] The technical solution of the present application is as follows:
[0006] The present application provides a shadow map drawing method, a shadow map drawing method, comprising: obtaining light source allocation information, the light source allocation information indicates that one map is allocated to each light source in N light sources in a map pool, M maps in the map pool are obtained by regionally dividing each map layer in K map layers; K, M and N are all integers greater than 1, M is greater than K, and N is less than or equal to M; sending a drawing instruction, the light source allocation information, light source information of the N light sources and scene data of a target virtual scene to a graphics processing unit (GPU); the scene data includes model vertex data of each virtual object in the target virtual scene; determining, by the GPU, a candidate light source of a light ray intersecting each virtual object in the N light sources according to the light source information of the N light sources and the model vertex data of each virtual object; drawing, by the GPU in response to the drawing instruction, a shadow map of a shadow formed by a candidate light source on a virtual object on a map allocated to the candidate light source based on the light source information of the candidate light source determined for the virtual object, the model vertex data of the virtual object and the light source allocation information.
[0007] The application provides a shadow map drawing device, comprising: an information acquisition module, configured to acquire light source allocation information, wherein the light source allocation information indicates one map respectively allocated to each light source in N light sources in a map pool, M maps in the map pool are obtained by regionally dividing each map layer in K map layers; K, M and N are all integers greater than 1, M is greater than K, and N is less than or equal to M; an information sending module, configured to send drawing instructions, the light source allocation information, light source information of the N light sources and scene data of a target virtual scene to a graphics processing unit (GPU); the scene data comprises model vertex data of each virtual object in the target virtual scene; a light source determination module, configured to determine, by the GPU, candidate light sources in which light rays intersect each virtual object according to the light source information of the N light sources and the model vertex data of each virtual object; and a map drawing module, configured to draw, by the GPU, a shadow map of a shadow formed by a candidate light source on a virtual object on a map allocated to the candidate light source according to the drawing instructions, based on the light source information of the candidate light source determined for the virtual object, the model vertex data of the virtual object and the light source allocation information.
[0008] In an implementation manner, the device further comprises an information integration module and an instruction data determination module, the information integration module is configured to integrate, by the GPU, light source identifiers of candidate light sources intersecting the same virtual object to obtain light source integration information of each virtual object; the instruction data determination module is configured to determine, by the GPU, indirect drawing instruction data of each virtual object based on the number of instances corresponding to the virtual object and the model vertex data of the virtual object; the number of instances corresponding to a virtual object is equal to the number of candidate light sources intersecting the virtual object; and the map drawing module is further configured to draw, by the GPU, a shadow map of a shadow formed by a candidate light source on a virtual object on a map allocated to the candidate light source according to the drawing instructions, based on the indirect drawing instruction data of the virtual object, the light source information of the candidate light source indicated by the light source identifier in the light source integration information of the virtual object and the light source allocation information.
[0009] In an embodiment, the light source integration information of a virtual object includes offset interval information and light source identifiers of candidate light sources intersecting the virtual object, the GPU is provided with a first buffer and a second buffer, the offset interval information is stored in the first buffer, and the light source identifiers are stored in the second buffer; the offset interval information includes an object identifier of a virtual object and an offset interval corresponding to the virtual object; one offset value in the offset interval points to a storage location in the second buffer; and the information integration module includes a first storage submodule and a second storage submodule; the first storage submodule is configured to store, by the GPU, the offset interval corresponding to each virtual object in association with the object identifier of the virtual object in the first buffer; the number of offset values in the offset interval corresponding to a virtual object is equal to the number of candidate light sources intersecting the virtual object; and the second storage submodule is configured to store, by the GPU, the light source identifiers of the candidate light sources intersecting the virtual object in the storage location indicated by the offset value in the offset interval corresponding to the virtual object in the second buffer, wherein different light source identifiers correspond to different offset values in the storage location in the first buffer.
[0010] In an embodiment, the GPU is further provided with a third buffer and a fourth buffer, the third buffer stores the number of instances corresponding to each virtual object, and the number of instances corresponding to a virtual object is equal to the number of candidate light sources intersecting the virtual object; the fourth buffer stores a plurality of identifier groups, one identifier group includes one object identifier and one light source identifier, and the virtual object indicated by the object identifier in one identifier group has an intersection relationship with the light source indicated by the light source identifier in the identifier group; and the information integration module further includes an interval allocation submodule, which is configured to allocate, by the GPU, offset intervals for a plurality of virtual objects according to the number of instances corresponding to each virtual object in the third buffer, to obtain the offset interval corresponding to each virtual object; and the second storage submodule is further configured to store, by the GPU, the light source identifier in the identifier group at an available storage location in the storage space indicated by the offset interval corresponding to the object identifier in the identifier group in the second buffer.
[0011] In an implementation, the GPU is provided with a third buffer and a fourth buffer; the light source determining module includes an intersection judging sub-module, a third storing sub-module and a fourth storing sub-module. The intersection judging sub-module is configured to use the GPU to perform intersection judgment between each virtual object and each light source of the N light sources according to the model vertex data of each virtual object and the light source information of the N light sources, and obtain intersection judgment results of each virtual object and each light source. The third storing sub-module is configured to use the GPU to determine candidate light sources of which light rays intersect with each virtual object based on the intersection judgment results, and store the object identifier of each virtual object and the light source identifier of a candidate light source corresponding to the virtual object as an identifier group in the fourth buffer. The fourth storing sub-module is configured to use the GPU to store the number of candidate light sources of which light rays intersect with each virtual object and the object identifier of the virtual object in the third buffer in association.
[0012] In an implementation, the GPU is further provided with a fifth buffer, and the fifth buffer is configured to store the value of a target field. The fourth storing sub-module is further configured to, for each intersection judgment result, if the intersection judgment result indicates an intersection relationship, use the GPU to accumulate and add 1 to the value of the target field in the fifth buffer; and use the GPU to store the object identifier of a virtual object involved in the intersection judgment result and the light source identifier of a candidate light source involved as an identifier group at a storage position indicated by a reference offset in the fourth buffer, where the reference offset refers to the value of the target field before the accumulation and addition of 1.
[0013] In an implementation, the device further includes a thread allocating module configured to use the GPU to allocate one thread to each virtual object in P threads, where P is an integer greater than 1 and P does not exceed the number of virtual objects. The intersection judging sub-module is further configured to use the GPU to run the P threads in parallel, and one thread is configured to perform intersection judgment between the virtual object corresponding to the thread and each light source of the N light sources according to the model vertex data of the virtual object and the light source information of the N light sources, and obtain intersection judgment results of the virtual object and each light source.
[0014] In an implementation, the map drawing module comprises an information determining submodule, an object identification determining submodule, a map determining submodule and a map drawing submodule. The information determining submodule is configured to determine, by the GPU, a target light source identification stored in a storage location pointed to by the second buffer based on a target instance value of a current instance to be drawn as an offset value, and determine a target offset interval in which the target instance value is located in the first buffer. The object identification determining submodule is configured to determine, by the GPU, a target object identification stored in the first buffer in association with the target offset interval. The map determining submodule is configured to determine, by the GPU, a target map assigned to the target light source indicated by the target light source identification according to the light source assignment information. The map drawing submodule is configured to draw, by the GPU, a shadow map of a shadow formed by the target light source on a target virtual object on the target map according to indirect drawing instruction data of the target virtual object indicated by the target object identification and light source information of the target light source indicated by the target light source identification.
[0015] In an implementation, the light source assignment information comprises a plurality of identification pairs, each identification pair comprising a light source identification and a map identification, and the map indicated by the map identification in each identification pair is the map assigned to the light source indicated by the light source identification in the identification pair. The map identification of a map comprises a level identification of a map layer in which the map is located and a viewport identification of a viewport in the map layer in which the map is located. The map determining submodule is further configured to obtain, by the GPU, a target map identification associated with the target light source identification from the light source assignment information, and the map indicated by the target map identification is the target map. The map determining submodule is further configured to obtain, by the GPU, the target map based on the level identification in the target map identification and the viewport identification in the target map identification.
[0016] In an implementation, the information obtaining module comprises a contribution value obtaining submodule and a map assignment submodule. The contribution value obtaining submodule is configured to obtain a light contribution value of each light source based on light source information of each light source. The map assignment submodule is configured to assign a map to each light source based on the light contribution value of each light source and an area of each map, and the light contribution value of each light source is positively correlated with the area of the map assigned to the light source.
[0017] In an embodiment, the contribution value obtaining sub-module is further configured to determine, according to the light source information of each of the light sources, a coverage area of each of the light sources in the target virtual scene and a distance between each of the light sources and a virtual camera in the target virtual scene; and obtain, for each of the N light sources, a light contribution value of the light source based on the distance between the light source and the corresponding virtual camera of the target virtual scene and the coverage area of the light source in the target virtual scene, wherein the light contribution value is negatively correlated with the distance and positively correlated with the coverage area.
[0018] The present application provides an electronic device comprising one or more processors, a memory, and one or more programs; the one or more programs are stored in the memory and configured to be executed by the processor to implement the above method.
[0019] The present application provides a computer readable storage medium, the computer readable storage medium has a computer program stored therein, wherein the computer program, when executed by a processor, performs the above method.
[0020] The present application provides a computer program product, which comprises a computer program stored in a computer readable storage medium. The processor of the electronic device obtains the computer program from the computer readable storage medium, and the processor executes the computer program, so that the electronic device executes the above method.
[0021] The present application has the following beneficial effects:
[0022] This application provides a method, apparatus, device, and storage medium for drawing shadow maps, comprising: acquiring light source allocation information; sending drawing instructions, light source allocation information, light source information of N light sources, and scene data of a target virtual scene to a graphics processing unit (GPU); the scene data including model vertex data of each virtual object in the target virtual scene; the GPU determining candidate light sources for each virtual object from among the N light sources based on the light source information of the N light sources and the model vertex data of each virtual object; and the GPU, in response to the drawing instructions, drawing a shadow map of the shadows formed by the candidate light sources on the virtual objects on a map allocated to the candidate light sources, based on the light source information of the candidate light sources determined for the virtual objects and the model vertex data of the virtual objects. In this method, by pre-determining the light source allocation information, it is ensured that each light source can be allocated a map in the map pool, and the CPU sends the drawing instructions, light source allocation information, light source information, and scene data (including model vertex data of virtual objects) to the GPU, and the GPU determines candidate light sources for each virtual object based on the received information. Subsequently, the GPU executes drawing instructions, which allows it to draw the shadow cast by each candidate light source on the corresponding virtual object within the texture area allocated to that candidate light source. In other words, it achieves shadow mapping of multiple light sources on virtual objects with a single drawing instruction. Compared to related technologies that call drawing instructions separately for each light source to generate shadow maps, the shadow mapping method provided in this application can draw shadow maps of multiple light sources on multiple virtual objects with a single drawing instruction transmission, thereby effectively improving the drawing efficiency of shadow maps. Therefore, the mapping method of this application can significantly reduce the number of times the CPU sends instructions to the GPU, thereby reducing communication latency and bandwidth usage, and improving the overall efficiency of shadow map drawing. Attached Figure Description
[0023] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0024] Figure 1 This is a schematic diagram of the structure of an electronic device in related technologies;
[0025] Figure 2 This is a flowchart illustrating a shadow mapping method provided in an embodiment of this application.
[0026] Figure 3 This is a schematic diagram of texture partitioning in a shadow texture pool provided in an embodiment of this application;
[0027] Figure 4 is another map division schematic diagram in a shadow map pool provided by an embodiment of the present application;
[0028] Figure 5 is another flow schematic diagram of a shadow map drawing method provided by an embodiment of the present application;
[0029] Figure 6 is a data storage schematic diagram of a plurality of buffers in a GPU provided by an embodiment of the present application;
[0030] Figure 7 is Figure 5 is a flow schematic diagram of step S140 in the method;
[0031] Figure 8 is Figure 5 is a flow schematic diagram of step S130 in the method;
[0032] Figure 9 is another data storage schematic diagram of a plurality of buffers in a GPU provided by an embodiment of the present application;
[0033] Figure 10 is a schematic diagram of the time length and the amount of data transmitted when a map drawing method in the related art draws a target virtual scene with multiple light sources;
[0034] Figure 11 is a schematic diagram of the time length and the amount of data transmitted when a map drawing method of the present application draws a target virtual scene with multiple light sources. DETAILED DESCRIPTION
[0035] In order to make the purposes, technical solutions and advantages of the present application clearer, the present application will be further described in detail below with reference to the drawings, and the described embodiments should not be regarded as limiting the present application, and all other embodiments obtained by those skilled in the art without creative labor are within the scope of protection of the present application.
[0036] In the following description, “some embodiments” are related to a subset of all possible embodiments, but it can be understood that “some embodiments” can be the same subset or different subsets of all possible embodiments, and can be combined with each other without conflict. In the following description, the term “a plurality of” refers to at least two.
[0037] In the following description, the terms "first\second" are merely used to distinguish similar objects, and do not represent a specific order or sequence of the objects. It can be understood that the "first\second" can be interchanged in a specific order or sequence as allowed, so that the embodiments described herein can be implemented in an order other than that illustrated or described herein.
[0038] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used in the description herein is for describing the embodiments of the application only and is not intended to limit the application.
[0039] In the embodiments of the present application, the term "module" or "unit" refers to a computer program or a part of a computer program with a predetermined function, and works together with other related parts to achieve a predetermined target, and can be implemented entirely or partially by using software, hardware (such as a processing circuit or a memory), or a combination thereof. Similarly, one processor (or multiple processors or memories) can be used to implement one or more modules or units. In addition, each module or unit can be a part of an overall module or unit that includes the functions of the module or unit.
[0040] It is particularly noted that in the subsequent specific embodiments of the present application, data related to the client is involved, and when the above embodiments of the present application are applied to specific products or technologies, the permission or consent of the user concerned needs to be obtained, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of the country and region concerned.
[0041] In the following, the terms involved in the present application are explained.
[0042] A virtual scene refers to a digital and interactive three-dimensional environment created by computer technology and software systems. This environment can simulate various scenes in the real world, or be a completely fictional and surreal fantasy space. In a virtual scene, users can interact with the environment through various sensory channels such as vision, hearing, and even touch, and obtain an experience close to or beyond reality. The above target virtual scene can be applied to the entertainment industry, education and training, etc. When applied to the entertainment industry, it can be specifically applied to electronic games, virtual reality or interactive movies, etc., to bring users an immersive gaming or viewing experience; when applied to education and training, it can simulate real or hypothetical scenes to provide a safe and repeatable learning and training environment for students and professionals, such as flight simulation, surgical practice, etc.
[0043] The virtual scene can include virtual objects, light sources, etc. In the embodiments of the present application, the virtual object refers to an object in the virtual scene that can generate a shadow under the action of lighting. The virtual object can be at least one of a virtual character, a virtual animal, and a virtual article. In the case of a virtual scene displayed (or provided) on a game client when the virtual scene is a game running, the virtual scene can be a simulated environment scene of the real world, a semi-simulated and semi-fictional three-dimensional environment scene, or a purely fictional three-dimensional environment scene. The virtual scene can be any one of a two-dimensional virtual scene, a 2.5-dimensional virtual scene, and a three-dimensional virtual scene.
[0044] The light source refers to a key element for creating a scene lighting effect in the virtual scene, which is used to simulate the light source in the real world and has a decisive influence on the atmosphere, depth, and realism of the scene. The number and setting position of the light source in different virtual scenes are usually different.
[0045] The map pool is a resource management technology in computer graphics and game development. It is a memory area pre-allocated by the operating system of an electronic device or the game engine of a certain game, which is specially used to store and manage the map resources currently being used or about to be used.
[0046] Referring to Figure 1 , Figure 1 is an architecture schematic diagram of an electronic device provided by the drawing method for performing shadow mapping.
[0047] Figure 1 The electronic device shown can include but is not limited to a terminal device or a server: the terminal device can be but is not limited to a mobile phone, a tablet computer, a notebook computer, a palm computer, a mobile internet device (MID), a smart voice interaction device, a vehicle-mounted terminal, a roadside device, an aircraft, a wearable device, a smart home appliance, etc., and the server can be a server or a server cluster composed of multiple servers, or any one of a cloud computing platform and a virtualization center, which are not limited by the embodiments of the present application. As long as it can be used to execute the drawing method for performing shadow mapping of the present application.
[0048] The electronic device provided by the embodiments of the present application can include a central processing unit 110 (CPU, Central Processing Unit) and a graphics processing unit 120 (GPU, Graphics Processing Unit), a memory 150, and at least one network interface 130. The various components in the electronic device are coupled together through a bus system 140. It can be understood that the bus system 140 is used to realize the connection communication between the components. In addition to including a data bus, the bus system 140 also includes a power bus, a control bus, and a status signal bus. However, for the purpose of clear illustration, all the buses are marked as the bus system 140 in the Figure 1
[0049] The central processing unit 110 is one of the core components of the computer system. It is responsible for executing instructions in the computer program, coordinating and controlling various operations of the computer. The CPU usually consists of one or more processing cores, and each core can process instruction streams and data.
[0050] The graphics processing unit 120 is a processor specially designed for processing graphics and image related calculations. The GPU is mainly used for computer graphics rendering and can process multiple data items simultaneously. It has many parallel processing units, each of which can execute the same instruction but process different data. The graphics processor is usually also equipped with a large capacity of video memory for storing and processing texture, vertex and pixel data required for graphics rendering, etc. It also provides a special graphics processing pipeline, including vertex processing, geometry processing, rasterization and pixel processing stages.
[0051] The memory 150 can be removable, non-removable or a combination thereof. Exemplary hardware devices include solid state memory, hard drives, optical drives, etc. The memory 150 optionally includes one or more storage devices physically located in proximity to the processor 110.
[0052] The memory 150 includes volatile memory or non-volatile memory, and can also include both volatile and non-volatile memory. The non-volatile memory can be read only memory (ROM), and the volatile memory can be random access memory (RAM). The memory 250 described in the embodiments of the present application is intended to include any suitable type of memory.
[0053] In some embodiments, the memory 150 is capable of storing data to support various operations, examples of which include programs, modules, and data structures or subsets or supersets thereof, which are exemplarily illustrated below.
[0054] The operating system 151 includes system programs for processing various basic system services and performing hardware-related tasks, such as a framework layer, a core library layer, a driver layer, and the like, for implementing various basic services and processing hardware-based tasks;
[0055] The network communication module 152 is configured to reach other electronic devices via one or more (wired or wireless) network interfaces 130, exemplary network interfaces 130 including Bluetooth, wireless compatibility authentication (WiFi), and Universal Serial Bus (USB), and the like;
[0056] In some embodiments, the shadow map rendering device provided by the embodiments of the present application can be implemented in a software manner, Figure 1 A shadow map rendering device 155 stored in the memory 150 is shown, which can be software in the form of programs and plug-ins, and includes the following software modules: an information acquisition module 1551, an information sending module 1552, a light source determination module 1553, and a map rendering module 1554. These modules are logical, and thus can be combined or further split according to the implemented functions. The functions of each module will be described below.
[0057] On the basis of Figure 1 The following will describe the shadow map rendering method provided by the embodiments of the present application in combination with exemplary applications and implementations of the electronic device provided by the embodiments of the present application.
[0058] As Figure 2 shown, the embodiments of the present application provide a shadow map rendering method, which can be applied to an electronic device, and the electronic device can be provided with a plurality of processors. The above-mentioned shadow map rendering method can be implemented by interaction of the plurality of processors and a graphics processor, for example, by the graphics processor GPU and the central processing unit CPU jointly executing.
[0059] The shadow map rendering method provided by the present application can be applied in the fields of movies, games, animations, or short videos, and the like, so as to render the shadow of a virtual object in a virtual scene while rendering the virtual object in the virtual scene, thereby improving the fidelity of the virtual scene under the light condition.
[0060] Taking a game scene or a movie scene as an example, if a room scene, for example, a living room, is to be simulated realistically, there are multiple point light sources (for example, ceiling lamps) and spotlights (for example, wall lamps); for each light source, a shadow map of a shadow formed by the light source on each virtual object (for example, a table, a chair, a sofa, a person, etc.) in the room scene is drawn from the position and direction of the light source; when a scene image of the room scene is obtained based on the shadow map, the shadows of each virtual object in the room scene on the ground and the wall can be accurately presented in the scene image, thereby enhancing the realism of the scene. If a scene with complex light and shadow effects, for example, a city night scene, is to be simulated realistically, there are multiple light sources (for example, one or more of street lamps, car lights, lighthouses, and indoor lights) acting simultaneously, and there are overlapping and occlusion relationships among the light sources; at this time, for each light source in the city night scene, a shadow map of a shadow formed by the light source on each virtual object (for example, a building, a street lamp, a tree, a lantern, a vehicle, and a pedestrian, etc.) in the city night scene is drawn; when a scene image of the city night scene is obtained based on the shadow map, the shadows of each virtual object in the city night scene on the ground or the surface of each virtual object can be accurately presented in the scene image, thereby enhancing the realism of the scene.
[0061] The method comprises steps S110-S140.
[0062] Step S110: Obtain light source distribution information.
[0063] The light source distribution information indicates that one map in the map pool is respectively allocated to each light source in the N light sources, and the M maps in the map pool are obtained by regionally dividing each map layer in the K map layers; K, M, and N are all integers greater than 1, M is greater than K, and N is less than or equal to M. That is, the M maps in the present application are at least obtained by regionally dividing two map layers.
[0064] The N light sources refer to virtual light sources deployed in a target virtual scene to be subjected to shadow map drawing. The target virtual scene refers to a virtual scene to be currently drawn, for example, a game virtual scene, a movie virtual scene, a virtual scene part in a mixed reality scene, or a virtual scene simulating a certain real scene, which is not limited here.
[0065] The number of map layers in the map pool can be set according to actual needs, for example, according to the number of light sources (for example, N) in the target virtual scene and the number of maps that can be divided for each map layer. For example, when each map layer is regionally divided, if the number of maps obtained by regionally dividing each map layer does not exceed a preset threshold, the number of map layers can be determined according to the ratio between the number of light sources in the virtual scene and the preset threshold.
[0066] The division manners of different map layers can be the same or different, the shapes of the maps obtained by dividing one map layer can be the same or different, and the sizes of different maps in the same map layer can be the same or different, which can be set according to actual requirements. The shape of a map can be a rectangle, a square or a circle, etc. One map layer is one Render Target (drawing target), and one map in a map layer is one Viewport (view port) in a Render Target.
[0067] Specifically, if the division manners of different map layers are the same, and the shapes and sizes of different maps obtained by dividing one map layer are the same, the shapes and sizes of different maps obtained by dividing M map layers are the same; if the division manners of different map layers are the same, but the sizes or shapes of two maps obtained by dividing one map layer are different, there are maps with the same shape and size in the M maps; if the division manners of different map layers are different, and the shapes and sizes of different maps obtained by dividing one map layer are different, the shapes and sizes of different maps obtained by dividing M map layers can be different.
[0068] For example, as shown in Figure 3 , four map layers, target0-target3, are shown in Figure 3 , the shapes and sizes of the four map layers are the same, each map layer can be divided in the same manner, the shapes of the maps obtained by dividing each map layer are squares, and the number of maps obtained by dividing each map layer does not exceed a preset number threshold (for example, 16). Figure 3 In , the number of maps included in one map layer obtained by dividing the map layer in the region is 8, that is, each map layer is divided into 8 maps, view0-view7. In order to distinguish different maps, a map identifier can be assigned to each map, which can be a string composed of one or more of numbers, letters and symbols to identify the uniqueness of the map.
[0069] In an implementation manner, the map identifier can also include a layer identifier for distinguishing different map layers, and a viewport identifier for distinguishing different maps in the same map layer. For example, please refer to Figure 3, the 4 map layers in the map pool are divided into 8 maps, at this time, the number of maps included in the map pool is 28, wherein the level identifiers corresponding to the 4 map layers are target0, target1,..., target3 respectively, and the viewport identifiers corresponding to the 8 maps are viewport0, viewport1,..., viewport7 respectively. When the map identifier includes the level identifier and the viewport identifier, the map identifiers of the 28 maps can be target0-viewport0, target0-viewport1,..., target3-viewport7 respectively.
[0070] Similarly, in order to distinguish each light source, for N light sources, a unique light source identifier can be set for each light source, which can be composed of one or more of letters, numbers, characters or symbols. For example, the light source identifiers of the N light sources can be set as Light0, Light1,..., LightN-1 respectively.
[0071] At this time, the light source allocation information can include the light source identifiers of the N light sources and the map identifiers of the maps allocated to each light source indicated by the light source identifier, wherein the light source identifier in the light source allocation information and the map identifier of the map allocated to the light source corresponding to the light source identifier have a corresponding relationship.
[0072] The way to obtain the light source allocation information can be: for the N light sources in the scene, allocating a map to each light source to obtain the light source allocation information. Wherein the maps allocated to different light sources are different. When allocating a map to each light source, a random allocation method can be used to allocate a map to each light source, or a certain allocation rule can be used to allocate a map to each light source.
[0073] In some embodiments, the plurality of maps can be sorted to obtain a map sorting, and the plurality of light sources can be sorted to obtain a light source sorting, and then the map assignment is performed for the light sources based on the map sorting and the light source sorting, for example, the light source sorted as m in the light source sorting is assigned the map sorted as m in the map sorting, m is a positive integer, m≤N. For example, if the light source sorting obtained by sorting N light sources is Light0, Light1,..., LightN-1, the map pool includes p layers of maps, each map layer includes q maps, p and q are integers greater than 1, and the map sorting obtained by sorting p*q maps is: target0-viewport0, target0-viewport1, target0-viewport2,..., target0-viewportq-1, target1-viewport0,..., targetp-1viewportq-1; then the light source with the light source identifier Light0 can be assigned the map with the map identifier target0-viewport0, the light source with the light source identifier Light1 can be assigned the map with the map identifier target0-viewport1, the light source with the light source identifier Lightq can be assigned the map with the map identifier target0-viewportq-1, and the light source with the light source identifier Lightq+1 can be assigned the map with the map identifier target1-viewport0.
[0074] In some embodiments, the maps can be assigned to the light sources according to the distance of each light source to the ground in the target virtual scene and the area corresponding to each map, and the distance of the light source to the ground in the target virtual scene is negatively correlated with the area of the corresponding assigned map.
[0075] In some embodiments, the maps can be assigned to the light sources according to the distance of each light source to the ground in the target virtual scene and the area corresponding to each map, and the distance of the light source to the ground in the target virtual scene is negatively correlated with the area of the corresponding assigned map.
[0076] In some embodiments, the maps can be assigned to the light sources according to the distance of each light source to the ground in the target virtual scene and the area corresponding to each map, and the distance of the light source to the ground in the target virtual scene is negatively correlated with the area of the corresponding assigned map.
[0077] The coverage area of the light of each light source in the target virtual scene can refer to the size of the light spot formed when each light source illuminates the ground in the target virtual scene without occlusion. It should be understood that the ground plane has a boundary, and the boundary of the ground plane is the boundary of the virtual scene.
[0078] For a light source with a large coverage area of light in the target virtual scene, such as sunlight or a large artificial light source, the more virtual objects intersected by the light of the light source, the more area is allocated to the shadow map for the light source with a large coverage area of light. By allocating a larger shadow map to the light source with a large coverage area of light, the shadow details of each virtual object intersected by the light of the light source can be ensured to be fully rendered, and a smaller shadow map is allocated to a light source with a small coverage area of light, achieving efficient use of map resources.
[0079] In other embodiments, the maps can also be allocated to the light sources according to the light intensity of each light source and the area corresponding to each map, wherein the light intensity of the light source is positively correlated with the area of the corresponding allocated map.
[0080] The stronger the light intensity of the light source, the more area it can illuminate or the farther distance it can project light from. If the resolution of the shadow map used is fixed, but the area illuminated by the light source increases, the actual scene area covered by each pixel on the shadow map of the same size will also increase. This will cause obvious aliasing or blocky effects on the edges of the shadow, reducing the realism of the shadow. By allocating a larger shadow map to a light source with a stronger intensity, the scene area represented by each pixel is reduced, which can improve the shadow details and reduce these undesirable visual effects.
[0081] In an optional implementation, the step S110 includes: obtaining a light contribution value of each light source based on the light source information of each light source; and allocating a map to each light source based on the light contribution value of each light source and the area of each map, wherein the light contribution value of each light source is positively correlated with the area of the map corresponding to the light source.
[0082] The light contribution value of each light source can be determined according to at least one of the following light source information: the coverage area of the light of each light source in the target virtual scene, the light intensity of each light source, the attenuation degree of each light source, the distance between each light source and the ground in the target virtual scene, and the distance between each light source and the virtual camera in the target virtual scene.
[0083] Specifically, for each light source, the illumination contribution value can be obtained by weighted summation based on the coverage area, light intensity, attenuation degree, distance between the light source and the ground, and distance between the light source and the virtual camera in the target virtual scene. Alternatively, the illumination contribution value can be calculated according to certain calculation rules. The coverage area of the light source in the target virtual scene is positively correlated with the illumination contribution value; the light intensity is positively correlated with the illumination contribution; the attenuation degree is negatively correlated with the illumination contribution; the distance between the light source and the ground is negatively correlated with the illumination contribution; and the distance between the light source and the virtual camera in the target virtual scene is negatively correlated with the illumination contribution. Since the intensity of the light from the light source usually weakens due to diffusion when it reaches the virtual camera (i.e., the observation point), the illumination contribution value is negatively correlated with distance, meaning that the farther the light source is from the observation point, the smaller its impact on the overall lighting of the scene.
[0084] The larger the illumination contribution value of a light source, the more significant its impact on the target virtual scene, or the greater its contribution to the shadows of virtual objects in the scene. A larger texture area allows for more content to be drawn on it. Therefore, assigning larger texture areas to light sources with high illumination contribution values ensures higher-quality shadow rendering for light sources with significant impact on the scene, while assigning smaller texture areas to light sources with less impact reduces the resources required for subsequent rendering. This effectively balances rendering quality and computational resource consumption. Especially with limited hardware resources, assigning larger texture areas to light sources with high illumination contribution values means richer and more accurate shadow details can be drawn on the texture, enhancing the realism and depth perception of the scene and ensuring key visual effects.
[0085] In some embodiments, the coverage area of each light source in the target virtual scene and the distance between each light source and the virtual camera in the target virtual scene can be determined based on the light source information of each light source. For each of the N light sources, the illumination contribution value of the light source is obtained based on the distance between the light source and the virtual camera corresponding to the target virtual scene and the coverage area of the light source in the target virtual scene. The illumination contribution value is negatively correlated with the distance and positively correlated with the coverage area.
[0086] For example, please refer to Figure 4 , Figure 4 Different texture layers are divided using the same method. This texture pool includes textures of various area types (Area Type 1, Area Type 2, and Area Type 3, where Area Type 1 has a larger area than Area Type 2, and Area Type 2 has a larger area than Area Type 3). The areas of textures of different area types are different. Figure 4In the specific implementation, the number of the maps of the area type 1 is 4, the number of the maps of the area type 2 is 8, and the number of the maps of the area type 3 is 32. After the N light sources are sorted according to the light contribution value, the distance to the map plane in the target virtual scene, the coverage area in the target virtual scene, or the light intensity, and so on, each light source ranked in the top 4 in the light source sorting order can be assigned a map of the area type 1. Each light source ranked from 5 to 12 in the light source sorting order can be assigned a map of the area type 2. Each light source ranked from 13 onwards in the light source sorting order can be assigned a map of the area type 3.
[0087] In step S120, a drawing instruction, light source allocation information, light source information of the N light sources, and scene data of the target virtual scene are sent to the graphics processor GPU. The scene data includes model vertex data of each virtual object in the target virtual scene.
[0088] The drawing instruction is a DrawCall, which is an instruction sent by the central processing unit (CPU) to the graphics processing unit (GPU) to execute drawing. The CPU sends the drawing instruction to the GPU, which is an operation of calling a bottom-level graphics interface, such as a DirectX interface (Direct eXtension), an OpenGL interface (Open Graphics Library), and the like.
[0089] The drawing instruction can be generated after a rendering instruction for a picture (for example, a picture in a scene of a game, an animation, or a movie) is obtained. The drawing instruction can also be generated when a change in an object or a light source in a virtual scene is detected. The drawing instruction can also be generated in response to an interactive operation of a user, such as a key press or mouse movement of the user, which causes a picture or a visual effect to be updated. The drawing instruction can also be generated when a game is started. It should be understood that the foregoing ways of generating the drawing instruction are merely illustrative, and the foregoing ways of generating the drawing instruction cannot be used as a limitation on the present embodiment.
[0090] A virtual object in the target virtual scene is a model of an object presented in the target virtual scene. The virtual object can be various furniture, plants, rocks, virtual characters in a game, virtual props, and the like.
[0091] The light source information of the N light sources and the scene data of the target virtual scene serve as a data basis for the GPU to perform shadow map drawing. The light source allocation information is used to enable the GPU to determine a map used to carry a shadow of each drawing.
[0092] The light source information of the light source can include light source type, light source position and direction, color and intensity of the light source, and attenuation information of the light source, etc. Among them, the type of light source can be divided into point light source, directional light source, spotlight, environment light, etc., and different types of light sources have different effects on the scene.
[0093] The position of the light source refers to the position of the light source in the target virtual scene, and the direction of the light source refers to the radiation direction of the light of the light source in the target virtual scene. The radiation direction of the light of the light source in the target virtual scene can be a fixed direction or a range of directions.
[0094] The color of the light source is usually represented by red, green and blue color values, and of course other ways can also be used to represent the color. The light intensity of the light source is the light emitting intensity of the light source (i.e. the strength of the light emitted by the light source), which can determine the brightness of the environment where the light source is located, or the strength of the light emitted by the light source, which determines the strength of the light when it is irradiated onto an object; The attenuation information of the light source refers to the characteristics of the light intensity of the light source gradually decreasing with the increase of the distance, which can be described by an attenuation function.
[0095] The scene data of the target virtual scene includes model vertex data and material and texture of virtual objects in the target virtual scene. Among them, the model vertex data of the virtual object is obtained by processing the model data of the virtual object (the model data is used to describe the shape and structure of the virtual object), and the model vertex data of the virtual object indicates the vertex information (such as texture coordinates) of the vertices in each primitive of the virtual object; The material and texture of the virtual object are used to represent the visual characteristics of the surface of the virtual object, including diffuse reflection color, mirror reflection characteristics, transparency, and texture, etc. The scene data of the target virtual scene also includes environment information of the target virtual scene, such as background, ambient light color, fog effect, etc. in the target virtual scene, which affect the global visual effect. It is worth mentioning that the light source information of the N light sources in the target virtual scene is also part of the scene data of the target virtual scene.
[0096] In some embodiments, the above step S120 can be executed in the case that the command buffer constructed by the CPU is filled or reaches the timing condition.
[0097] Step S130: determining, by the GPU, the candidate light sources of the intersection of the light and the virtual object for each virtual object in the N light sources according to the light source information of the N light sources and the model vertex data of each virtual object.
[0098] In the present application, the light source whose light ray intersects with the virtual object is referred to as a candidate light source. It can be understood that for one virtual object, the determined candidate light source can be one or multiple. For different virtual objects, the determined candidate light source can be the same or different.
[0099] As described above, the model vertex data of the virtual object indicates the vertex information of the vertices in each primitive of the virtual object, such as the coordinate normal, texture coordinate, depth, and the like of the vertices.
[0100] In some embodiments, the propagation and attenuation of the light ray of the light source in the target virtual scene can be simulated based on the light source information of the light source. If the light ray of a light source can reach the surface of a virtual object (i.e., it is determined whether the light ray of the light source intersects with the vertex on the virtual object), and the light intensity of the light ray of the light source when reaching the surface of the virtual object is not equal to zero, it can be determined that the light ray of the light source intersects with the virtual object. Wherein, the simulation of the propagation and attenuation of the light ray of the light source in the target virtual scene is a process of ray tracing of the light source in the target virtual scene according to the light source information of the light source.
[0101] In some embodiments, the GPU can determine the view cone of each light source in the target virtual scene based on the light source information of the light source. The region in the view cone is the region that can be illuminated by the light ray of the light source in the target virtual scene. The view cone of the light source is a region formed from the light source along the radiation direction of the light ray. In other words, the light ray of the light source cannot radiate to the region outside the view cone of the light source, or the light intensity of the light ray of the light source at the region outside the view cone of the light source is zero. Based on the position information of the view cone of the light source in the target virtual scene and the position information of each virtual object in the target virtual scene (reflected by the vertex position information of the vertices on the virtual object), it is determined whether the light ray of the light source intersects with the virtual object. That is, if there is at least one vertex on the virtual object located in the view cone of the light source, it can be determined that the light ray of the light source intersects with the virtual object; otherwise, if all the vertices on the virtual object are located outside the view cone of the light source, it can be determined that the light ray of the light source does not intersect with the virtual object.
[0102] Step S140: In response to the drawing instruction, the GPU draws the shadow map of the shadow formed by the candidate light source on the virtual object on the map allocated for the candidate light source based on the light source information of the candidate light source determined for the virtual object, the model vertex data of the virtual object, and the light source allocation information.
[0103] The candidate light source determined for the virtual object is the light source whose light ray intersects with the virtual object as determined above. The shadow map of the shadow formed by the candidate light source on the virtual object is used to present the shadow effect of the shadow presented by the light ray of the candidate light source illuminating on the virtual object.
[0104] In some embodiments, based on the light source distribution information, a map assigned to the candidate light source is determined, a depth value of each vertex of a model of the candidate light source virtual object in the target virtual scene is obtained, a distance between each vertex of the model of the virtual object and the candidate light source is obtained based on the light source information of the determined candidate light source for the virtual object and the vertex data of the model of the virtual object, a shadow state of each vertex is determined based on the depth value and the distance corresponding to each vertex, and a shadow map of the shadow formed by the candidate light source on the virtual object is drawn on the map assigned to the candidate light source according to the shadow state of each vertex.
[0105] The manner of determining the shadow state of each vertex based on the depth value and the distance corresponding to each vertex can be that, if the distance between a certain vertex and the candidate light source is greater than the depth value of the vertex, it is determined that the shadow state corresponding to the vertex is in the shadow, and if the distance between a certain vertex and the candidate light source is not greater than the depth value of the vertex, it is determined that the shadow state corresponding to the vertex is not in the shadow.
[0106] When the depth value of each vertex of the model of the candidate light source virtual object in the target virtual scene is obtained, for each vertex of the model of the virtual object, one or more light rays (depending on the accuracy requirement of the algorithm, it can be a single ray or a ray bundle) are emitted from the candidate light source to the vertex. These light rays will pass through the scene space until they hit the first object (which can be the virtual object itself or other occluders) or reach the predetermined maximum distance without colliding with any object. When the light rays intersect with a certain surface, the distance from the intersection point to the light source is recorded. This distance is the depth value of the vertex relative to the current light source.
[0107] By using the shadow map drawing method provided in the present application, M maps are obtained by pre-dividing K map layers into regions, where M>K, which can realize that each light source is assigned a map area with sufficient resolution to record the shadow information generated by the light source. Considering that the data transmission speed between CPU and GPU is much slower than the internal operation speed of GPU, the drawing instruction, light source distribution information, light source information and scene data are submitted to the GPU in the present application, so that the GPU executes the drawing instruction. For each determined candidate light source, the shadow generated by the candidate light source on the corresponding virtual object is drawn on the map area assigned to the candidate light source. Compared with the related art in which a drawing instruction is separately called for each light source to generate a shadow map, the shadow map drawing method provided in the present application can significantly reduce the number of times of sending instructions from CPU to GPU, thereby reducing communication delay and bandwidth occupation, and thus improving the overall efficiency of drawing the shadow map. Subsequently, the GPU can directly locate the map area corresponding to each light source according to the provided light source distribution information, and perform shadow calculation on these areas, so as to efficiently complete the drawing of the shadow map of multiple light sources in a single drawing instruction.
[0108] Referring to Figure 5 In an implementation, before step S140, the method further comprises steps S150-S160 as follows:
[0109] Step S150: integrating, by the GPU, the light source identifiers of the candidate light sources intersecting the same virtual object by the light ray to obtain light source integration information of each virtual object.
[0110] The light source integration information of a virtual object includes the light source identifiers of all the candidate light sources intersecting the virtual object by the light ray. The integration, i.e., the integration of the light source identifiers of the candidate light sources intersecting the same virtual object by the light ray, facilitates subsequent shadow map rendering in units of virtual objects. It can be understood that if multiple candidate light sources are determined for a virtual object, the model vertex data of the virtual object needs to be used in the process of rendering the shadow map of the shadows formed by the multiple candidate light sources on the virtual object. Thus, in the process of shadow map rendering in units of virtual objects, the model vertex data of the virtual object can be pulled once to render the shadow map of the shadows formed by the multiple candidate light sources on the virtual object, without the need for multiple pulling, which can improve the rendering efficiency of the shadow map.
[0111] In some embodiments, the light source integration information of a virtual object is presented in the form of a set or a list, for example, if the light source integration information of a virtual object is presented in the form of a set, the light source identifiers of the candidate light sources intersecting the virtual object by the light ray are added to a set, and the set is taken as the light source integration information of the virtual object, where different virtual objects correspond to different sets. If the light source integration information of a virtual object is presented in the form of a list, the light source identifiers of the candidate light sources intersecting the virtual object by the light ray are added to a list, and the list is taken as the light source integration information of the virtual object, where different virtual objects correspond to different lists.
[0112] Step S160: determining, by the GPU, indirect rendering instruction data of each virtual object based on the instance number corresponding to each virtual object and the model vertex data of the virtual object; the instance number corresponding to a virtual object is equal to the number of the candidate light sources intersecting the virtual object by the light ray.
[0113] The indirect rendering instruction data of a virtual object refers to data commonly used for shadow rendering of the virtual object. In some embodiments, the instance number corresponding to a virtual object and the model vertex data of the virtual object can be combined as the indirect rendering instruction data of the virtual object. The instance number corresponding to a virtual object can also be understood as the number of shadow maps that need to be rendered for the virtual object. One shadow map rendered for a virtual object is regarded as one instance of the virtual object. The shadows formed by different light sources on a virtual object are different, and therefore, for a candidate light source intersecting with the virtual object, a shadow map corresponding to the shadow formed by the candidate light source on the virtual object is rendered, that is, the instance number corresponding to a virtual object is equal to the number of candidate light sources intersecting with the virtual object.
[0114] Further, the indirect rendering instruction data of a virtual object can further include model vertex number, model vertex index offset, vertex data offset, and model instance offset, wherein the model vertex number refers to the number of vertices that need to be rendered on the virtual object; the model vertex index offset is used to indicate the starting position of vertex index data (which refers to data used to index vertex information of a vertex); the vertex data offset is used to indicate the starting position of vertex data (that is, data related to a vertex, for example, the vertex information such as position, depth, and texture coordinate mentioned above); and the model instance offset is used to indicate the starting position of the instance to be rendered for the virtual object.
[0115] In some embodiments, the indirect rendering instruction data of a virtual object can be stored in a specified buffer on the GPU, for example, DrawIndirectCmdBuffer. DrawIndirectCmdBuffer is an indirectly invoked instruction buffer when a DrawCall is initiated. After the DrawCall is initiated, the GPU parses the specific rendering instruction to be executed according to the given buffer pointer. The indirect rendering instruction data of all virtual objects in the target virtual scene is stored in the DrawIndirectCmdBuffer, and correspondingly, the data stored in the DrawIndirectCmdBuffer can be regarded as the complete indirect rendering instruction data of the GPU for executing this DrawCall.
[0116] In Figure 5 In the corresponding embodiments, the step S140 includes: in response to the rendering instruction, the GPU renders, for each virtual object, a shadow map of the shadow formed by a candidate light source on the virtual object on a map allocated for the candidate light source of the virtual object, based on the indirect rendering instruction data of the virtual object, the light source information of the candidate light source indicated by the light source identifier in the light source integration information of the virtual object, and the light source allocation information.
[0117] Specifically, when performing shadow mapping, the GPU parses the indirect rendering instruction data for each virtual object. These instruction data contain various parameters required for rendering. For each candidate light source, the GPU checks the light source assignment information to determine whether the light source performs shadow calculation on the corresponding map. When performing shadow calculation, the GPU needs to re-calculate the position of the virtual object from the perspective of the target light source. That is, the virtual object is view-transformed, the geometry data of the virtual object is converted to the coordinate system of the light source, and the transformed object is projected onto a two-dimensional plane according to the type of the light source (perspective for point light source or orthogonal projection for parallel light source), which is the space of the target map. A depth buffer is initialized on the target map to record the shallowest depth value (i.e., the distance from the nearest point to the light source) of each point in the scene from the perspective of the target light source. The value of each pixel in the depth buffer represents the distance from the nearest point to the light source at that pixel position.
[0118] In an implementation, the light source integration information of a virtual object includes offset interval information and light source identifiers of candidate light sources intersecting with the virtual object, and the GPU is provided with a first buffer and a second buffer, the offset interval information is stored in the first buffer, and the light source identifiers are stored in the second buffer; the offset interval information includes an object identifier of the virtual object and an offset interval corresponding to the virtual object; an offset value in the offset interval points to a storage location in the second buffer. The offset value can be an offset relative to a storage location of a first stored light source identifier in the second buffer. For example, the offset of the storage location of the first stored light source identifier in the second buffer can be 0, and multiple light source identifiers are stored in multiple consecutive storage locations in the second buffer. Thus, the offset of a storage location of a next stored light source identifier in the second buffer is 1. The step S150 can include the following steps A1 and A2. In step A1, for each virtual object, the GPU stores the offset interval corresponding to the virtual object and the object identifier of the virtual object in the first buffer in association. The number of offset values in the offset interval corresponding to a virtual object is equal to the number of candidate light sources intersecting with the virtual object. In step A2, the GPU stores the light source identifiers of the candidate light sources intersecting with the virtual object in the second buffer at the storage locations indicated by the offset values in the offset interval corresponding to the virtual object. Different light source identifiers have different offset values corresponding to the storage locations in the first buffer.
[0119] In some embodiments, for each virtual object, the GPU stores the light source identifiers of the candidate light sources intersecting the virtual object by the ray into the second buffer, if there are multiple candidate light sources intersecting the virtual object, the light source identifiers of the multiple candidate light sources are stored in multiple consecutive storage locations in the second buffer; each storage location is used to store one light source identifier, and different light source identifiers correspond to different offset values of the storage locations in the second buffer. The offset interval corresponding to the virtual object is determined based on the offset value of the storage location in the second buffer corresponding to the light source identifier of the candidate light source intersecting the virtual object; and the object identifier of each virtual object and the corresponding offset interval are stored in the first buffer.
[0120] When the object identifiers of the virtual objects and the corresponding offset intervals are stored in the first buffer, the object identifiers of the virtual objects and the corresponding offset intervals can be stored randomly, or can be stored according to the object ordering obtained by sorting the virtual objects, that is, if a virtual object is earlier in the object ordering, the object identifier of the virtual object and the corresponding offset interval are stored in an earlier position in the first buffer. The object identifier of a virtual object and the corresponding offset interval are stored in the first buffer in association. In some embodiments, the storage locations (i.e., used to store the object identifier of a virtual object and the corresponding offset interval in association) allocated in the first buffer for multiple virtual objects can be consecutive. Of course, in other embodiments, there can be intervals, which are not specifically limited here.
[0121] In some embodiments, the length of the offset interval corresponding to a virtual object can be determined according to the number of candidate light sources intersecting the virtual object by the ray, and the length of the offset interval is the total number of storage locations in the second buffer corresponding to the offset interval, wherein the length of the offset interval corresponding to a virtual object is equal to the number of candidate light sources intersecting the virtual object by the ray; then, according to the object ordering, a storage location (for ease of description, referred to as a reference storage location) is allocated in the second buffer for the light source identifier of a virtual light source intersecting the first virtual object in the object ordering, and based on the offset value of the reference storage location and the length of the offset interval determined for the first virtual object in the object ordering, the offset interval corresponding to the first virtual object in the object ordering is determined, for example, if the offset value of the reference storage location is 0, the length of the offset interval of the first virtual object in the object ordering is a, and the offset interval corresponding to the first virtual object in the object ordering is [0, a-1] or [0, a).
[0122] Next, according to the object sorting, iterate through all objects except the first virtual object in the sorted object list. Based on the currently traversed virtual object (referred to as the current virtual object), determine the starting offset value of the current virtual object by taking the last offset value in the offset interval corresponding to the previous virtual object in the object sorting. For example, add 1 to the last offset value to get the starting offset value in the current virtual object's offset interval. Then, determine the offset interval of the current virtual object based on its starting offset value and the length of its offset interval. For example, continuing the example above, if the current virtual object is the second virtual object in the object sorting, then its starting offset value is 'a'. If the length of its offset interval is 'b', then the offset interval of the current virtual object can be determined as [a, a+b-1] or [a, a+b].
[0123] After determining the offset interval of the current virtual object according to the above process, the next virtual object in the object sorting is taken as the new current virtual object, and the above process is repeated until the offset intervals of all virtual objects in the object sorting are determined. Based on the above process, it can be guaranteed that the storage locations allocated for storing offset intervals for multiple virtual objects in the target virtual scene in the second buffer are continuous, and correspondingly, the offset intervals determined for multiple virtual objects are also continuous.
[0124] For example, such as Figure 6 As shown, each object identifier and its corresponding offset interval are stored in the first buffer according to the identifier order. That is, for objects with identifiers obj0, obj1, obj2, ..., objX (the figure only shows a schematic diagram of objects with identifiers including obj0, obj1, and obj2), there are two candidate light sources that intersect with obj0, namely light0 and light1; there are three candidate light sources that intersect with obj1, namely light1, light2, and light4; there are two candidate light sources that intersect with obj3, namely light0 and light4; the intersection of other virtual objects with light sources is not shown. If the obtained object sorting is obj0, obj1, obj2, ..., objX, that is, the first virtual object in the object sorting is obj0, the offset value corresponding to the storage position of the light source identifier of the first candidate light source whose light intersects with the virtual object obj0 in the second buffer is 0. According to the above embodiment, the offset interval corresponding to obj0 is [0,2), the offset interval corresponding to obj1 is [2,5), and the offset interval corresponding to obj2 is [5,7).
[0125] After obtaining the interval corresponding to each object identifier, the light source identifier corresponding to each object identifier can be stored in the second buffer at the storage location pointed to by the offset interval corresponding to the object identifier. Specifically, the light sources light0 and light1 intersecting obj0 can be stored at the storage locations pointed to by the offset values in the offset interval [0, 2) in the second buffer, and the offset values in the offset interval [0, 2) include 0 and 1; the light sources light1, light2, and light4 intersecting obj1 can be stored at the storage locations pointed to by the offset values in the offset interval [2, 5) in the second buffer; and the light sources light0 and light4 intersecting obj2 can be stored at the storage locations pointed to by the offset values in the offset interval [5, 7) in the second buffer. In this way, the light source identifiers of the candidate light sources intersecting the light rays of each virtual object can be integrated together.
[0126] In an implementation manner, referring to FIG. 13, Figure 7 On the basis that the first buffer is configured to store offset interval information and the second buffer is configured to store light source identifiers, the step S140 includes steps S142-S148.
[0127] In step S142, the GPU determines, based on the target instance value of the current instance to be drawn, the target light source identifier stored at the storage location pointed to by the target instance value as an offset value in the second buffer, and the target offset interval in which the target instance value is located in the first buffer.
[0128] The instance value of each instance to be drawn is the offset of the instance to be drawn relative to the first drawn instance. For example, the instance value of the first drawn instance is 0, and the instance value of the second drawn instance is 1, indicating that the offset of the second drawn instance relative to the first drawn instance is 1.
[0129] In the case where the determined offset intervals for the plurality of virtual objects are consecutive, the sum of the offset lengths of the plurality of offset intervals is the total number of instances to be drawn, which is equal to the sum of the instance numbers of all virtual objects. That is, the total number of instances to be drawn is equal to the total number of storage locations in the second buffer storing light source identifiers. Thus, the instance value of the instance to be drawn can be used as an offset value to determine the light source currently to be used and the shadow map currently to be drawn on which virtual object the light source forms a shadow, in combination with the offset interval, the object identifier, and the offset value of each storage location in the first buffer and the second buffer.
[0130] That is, the light source indicated by the target light source identifier stored in the storage location pointed by the second buffer with the target instance value as the offset value is determined as the light source needed to be used by the instance to be drawn currently. In the case that the target instance value is used as the offset value, the target instance value is in one of the multiple offset intervals stored in the first buffer, thus, the offset interval in which the target instance value is can be determined correspondingly, and the offset interval in which the target instance value is is referred to as the target offset interval.
[0131] Step S144: determining, by the GPU, the target object identifier stored in the first buffer in association with the target offset interval.
[0132] In the first buffer, different offset intervals and object identifiers associated with each offset interval are stored, and after the target offset interval is obtained, the object identifier stored in the first buffer in association with the target offset interval can be found, and the object identifier is referred to as the target object identifier.
[0133] Step S146: determining, by the GPU, the target map allocated to the target light source indicated by the target light source identifier according to the light source allocation information.
[0134] The light source allocation information includes multiple identifier pairs, one identifier pair includes one light source identifier and one map identifier, and the map indicated by the map identifier in one identifier pair is the map allocated to the light source indicated by the light source identifier in the identifier pair.
[0135] The above step S146 can be that the GPU obtains the target map identifier associated with the target light source identifier from the light source allocation information, and the map indicated by the target map identifier is the target map.
[0136] If the map identifier of a map includes the level identifier of the map layer in which the map is located and the viewport identifier of the viewport in the map layer in which the map is located, the above step S146 can also be that the GPU obtains the target map identifier associated with the target light source identifier from the light source allocation information, and the map indicated by the target map identifier is the target map; and the GPU obtains the target map based on the level identifier in the target map identifier and the viewport identifier in the target map identifier.
[0137] Step S148: drawing, by the GPU, the shadow map of the shadow formed by the target light source on the target virtual object on the target map according to the indirect drawing instruction data of the target virtual object indicated by the target object identifier and the light source information of the target light source indicated by the target light source identifier.
[0138] After the target light source forms a shadow on the target virtual object, the shadow map drawn on the target map indicates that the drawing of the current instance is completed, then the target instance value is added by 1, and the obtained value is taken as the instance value of the next instance to be drawn, then the drawing is performed according to the steps S142-S148 as above until the number of instances drawn reaches the total number of instances determined to be drawn.
[0139] By adopting the steps S142-S148, the light source to be used for drawing and the virtual object to be used are determined by separating the storage of the light source identifier and the offset interval information, combining the target instance value as an index, and combining the offset interval and the object identifier associatedly stored in the first buffer, and the offset value of each storage position in the second buffer and the light source identifier stored in each storage position, so that the ordered drawing of multiple shadow maps can be ensured, and the omission or repeated drawing can be avoided, and the orderedness of the shadow map drawing in the multi-light source and multi-virtual object scene can be ensured.
[0140] In an implementation manner, the GPU further has a third buffer and a fourth buffer, the third buffer stores the instance numbers corresponding to each virtual object, and the instance number corresponding to a virtual object is equal to the number of candidate light sources intersecting with the virtual object; the fourth buffer stores a plurality of identifier groups, an identifier group includes an object identifier and a light source identifier, and the virtual object indicated by the object identifier in an identifier group has an intersection relationship with the light source indicated by the light source identifier in the identifier group; before the step of storing, by the GPU, the offset interval corresponding to each virtual object and the object identifier of the virtual object in the first buffer in association with each other for each virtual object, the method further includes: performing, by the GPU, offset interval allocation for a plurality of virtual objects according to the instance numbers corresponding to each virtual object in the third buffer, to obtain the offset interval corresponding to each virtual object. The process of performing offset interval allocation is described above and will not be described here.
[0141] Correspondingly, the step of storing, by the GPU, the light source identifier of the candidate light source intersecting with the virtual object into the storage position indicated by the address offset in the offset interval corresponding to the virtual object in the second buffer includes: storing, by the GPU, the light source identifier in the identifier group at a usable storage position in the storage space indicated by the offset interval corresponding to the object identifier in the identifier group in the second buffer for each identifier group in the fourth buffer.
[0142] The instance number corresponding to each virtual object in the third buffer can be obtained by counting the number of candidate light sources intersecting with the virtual object in N light sources for each virtual object.
[0143] The plurality of identification groups stored in the fourth buffer can be that after the GPU completes the intersection determination of each virtual object and each light source to obtain a plurality of identification groups, the GPU respectively stores all the obtained identification groups in different storage locations of the fourth buffer. Alternatively, during the execution of the intersection determination, for each virtual object, if it is determined that the light ray of the current light source intersects with the virtual object, an identification group is obtained based on the light source identification of the current light source and the object identification of the virtual object, and the identification group is stored in the fourth buffer.
[0144] When the GPU performs offset interval allocation for the plurality of virtual objects according to the instance numbers corresponding to each virtual object in the third buffer to obtain the offset intervals corresponding to each virtual object, the offset intervals corresponding to each virtual object are disjoint, and the plurality of offset intervals obtained after the interval allocation for the plurality of virtual objects can be continuous in the third buffer. The positions of the offset intervals corresponding to each virtual object in the third buffer can be randomly arranged, or can be arranged in sequence according to the above object sorting. The actual requirements can be set.
[0145] Referring to FIG. 1, Figure 8 In an embodiment of the present application, the GPU is provided with a third buffer and a fourth buffer; and step S130 includes steps S132-S136:
[0146] Step S132: The GPU performs intersection determination on each virtual object and each light source in N light sources according to the model vertex data of each virtual object and the light source information of the N light sources, to obtain intersection determination results of each virtual object and each light source.
[0147] To improve the efficiency of the GPU in performing intersection determination on each virtual object and each light source in N light sources, a plurality of threads that can run in parallel can be used in the GPU, and each thread is used to perform intersection determination between the light rays of at least one virtual object and at least one light source. For example, each thread can be used to perform intersection determination on at least one virtual object and the light rays of N light sources respectively, to determine the candidate light sources corresponding to the virtual object. At this time, the thread allocation is performed in units of virtual objects, and the total number of threads can be equal to the total number of virtual objects, or can be less than the total number of virtual objects.
[0148] In an embodiment of the present application, before step S132 is performed, the GPU can assign a thread to each virtual object in P threads, P being an integer greater than 1 and P not exceeding the number of virtual objects; at this time, step S132 can be specifically that the GPU runs the P threads in parallel, and one thread is used to perform intersection determination between the virtual object corresponding to the thread and each of the N light sources according to the model vertex data of the virtual object and the light source information of the N light sources, to obtain the intersection determination result between the virtual object and each of the light sources.
[0149] In some examples, thread assignment can also be performed on a light source basis, that is, each thread can also be used to determine, for at least one light source, the virtual object that intersects with the light ray of the light source from the plurality of virtual objects; at this time, the total number of threads can be equal to the total number of light sources, or can be less than the total number of light sources (in this case, part of the threads can be used to determine the intersection determination between another light source and the plurality of virtual objects after determining the intersection determination between one light source and the plurality of virtual objects); each thread can also be used to determine only the intersection between one virtual object and the light ray of one light source; at this time, the number of threads can be determined according to the number of light sources and the number of virtual objects, for example, one thread can be responsible for the intersection determination between one virtual object and one light source, one thread can be responsible for the intersection determination between one virtual object and two light sources, and so on.
[0150] Step S134: determining, by the GPU, the candidate light source that intersects with the light ray and each virtual object based on the intersection determination result, and storing the object identifier of each virtual object and the light source identifier of the candidate light source corresponding to the virtual object as an identifier group in the fourth buffer.
[0151] In the process of storing the identifier group in the fourth buffer, the GPU can store the identifier group in the fourth buffer randomly, or can determine the storage position of the identifier group to be stored in the fourth buffer according to the total number of the identifier groups that have been stored, for example, the next storage position of the storage position of the last stored identifier group in the fourth buffer can be used as the storage position of the identifier group to be stored in the fourth buffer.
[0152] In an embodiment of the present application, the GPU further comprises a fifth buffer, and the fifth buffer is used to store the value of the target field; step S134 can be specifically that, for each intersection determination result, if the intersection determination result indicates that there is an intersection relationship, the GPU accumulatively adds 1 to the value of the target field in the fifth buffer; and the GPU stores the object identifier of the virtual object involved in the intersection determination result and the light source identifier of the candidate light source involved as an identifier group in the storage position indicated by the reference offset in the fourth buffer, the reference offset being the value of the target field before the accumulative addition.
[0153] For each intersection determination result, if the result indicates an intersection relationship, a thread in the GPU can perform an atomic increment operation on the value of the target field in the fifth buffer. This accumulates the value of the target field in the fifth buffer by 1. Even when multiple threads are running in parallel to determine intersection, and multiple threads attempt to increment the value of the field simultaneously, only one thread's operation will take effect at a time, thus avoiding data races and inconsistencies. Through atomic increment operations, each thread that discovers an intersection can independently and immediately update the target count without additional synchronization mechanisms (such as locks). Using atomic operations to accumulate intersection determination results effectively ensures data correctness and consistency.
[0154] Understandably, after storing all the identifier groups in the fourth buffer, the value of the target field in the fifth buffer is the total number of instances that need to be drawn.
[0155] Step S136: The GPU associates and stores the number of candidate light sources that intersect with each virtual object and the object identifier of the virtual object in the third buffer.
[0156] Alternatively, the object identifiers of each virtual object and the corresponding number of candidate light sources can be associated and randomly stored in the third buffer. Or, the object identifiers of each virtual object and the corresponding number of candidate light sources can be associated and stored in the third buffer according to the object sorting above.
[0157] In the above embodiment of running P threads in parallel in the GPU to perform intersection determination, each thread is used to determine the intersection of the virtual object with each of the N light sources based on the model vertex data of the virtual object corresponding to the thread and the light source information of N light sources, and to obtain the intersection determination result of the virtual object with each light source. Each thread is also used to count the number of candidate light sources that intersect with the light rays of the virtual object corresponding to the thread among the N light sources, and to store the counted number of candidate light sources in the third buffer after associating them with the object identifier of the corresponding virtual object.
[0158] like Figure 9 As shown, this illustrates thread 0, thread 1, and thread 2, which are P threads running in parallel on the GPU. Thread 0 is used to determine the intersection of the virtual object indicated by object identifier obj0 with the light rays from N light sources; thread 1 is used to determine the intersection of the virtual object indicated by object identifier obj1 with the light rays from N light sources; and thread 2 is used to determine the intersection of the virtual object indicated by object identifier obj2 with the light rays from N light sources.
[0159] Figure 9As shown in the above example, thread 0 performs intersection determination of the virtual object indicated by object identifier obj0 and the light ray of light source light1, and the determination result indicates that the virtual object indicated by obj0 intersects with light source light1. At this time, thread 0 performs atomic addition operation on the value of the target field in the fifth buffer, and obtains the value before the atomic addition operation as the reference offset. The value before the atomic addition operation is 2. The GPU stores the object identifier obj0 of the virtual object involved in the intersection determination result and the light source identifier light1 of the involved candidate light source as an identifier group (obj0, light1) in the fourth buffer at the storage location indicated by the reference offset of 2. Each thread is also used to count the number of candidate light sources in the N light sources that intersect with the virtual object corresponding to the thread, and stores the counted number of candidate light sources in association with the object identifier of the corresponding virtual object in the third buffer. As shown in the above example, the counted number of candidate light sources can be represented by the character "cnt". Figure 9
[0160] Embodiments of the present application also provide a shadow map rendering method, which specifically includes the following steps: one, map allocation; two, light source and virtual object culling; three, culling result integration; four, generating indirect rendering instruction data; and five, map rendering.
[0161] One, map allocation
[0162] A shadow map pool including a plurality of map layers (such as K) is created, and each map layer is regionally divided so that the final shadow map pool includes M maps. When each map layer is regionally divided, each map layer can be divided according to the same regional division manner, and each map included in the same map layer can be the same size or different sizes, as long as the division region on a single map layer does not exceed 16.
[0163] After obtaining the M maps, one map can be assigned to each light source in the target virtual scene, and light source assignment information is recorded for the map assigned to each light source. Specifically, the light source identifier of the light source and the map identifier of the corresponding map can be associated and stored to obtain the light source assignment information, wherein the map identifier of the map specifically includes the layer identifier (such as target0, target1, target2, etc.) of the map layer where the map is located and the viewport identifier (such as viewpor0, viewpor1, viewpor2, etc.) of the viewport in the map layer where the map is located. In specific embodiments, a large Texture2DArray type map can be applied as a map pool, wherein Texture2DArray is a data structure in computer graphics for storing a set of multiple 2D textures, and Texture2DArray packs multiple 2D textures into a single texture resource, and each texture layer shares the same resolution and format. A texture layer is a map layer in the present application.
[0164] II. Light source and virtual object culling
[0165] The light source and virtual object culling refers to selecting candidate light sources from the multiple light sources in which the light rays intersect the virtual objects for each virtual object in the target virtual scene.
[0166] The CPU transmits the drawing instruction, the light source information of the light source, the scene data of the target virtual scene, and the light source assignment information to the GPU, and applies multiple buffers (such as a third buffer, a fourth buffer, and a fifth buffer) at the GPU end: the fifth buffer is used to store the value of the target field; the fourth buffer is used to store the identifier group; and the third buffer is used for the instance number corresponding to each virtual object.
[0167] Specifically, multiple threads are created in the GPU, each thread is responsible for determining the intersection of a virtual object and the light rays of all light sources in the target virtual scene. If the light ray of the current light source intersects with the virtual object, an atomic add operation is performed on the value in the fifth buffer to obtain the value in the fifth buffer before the atomic add operation as an offset value for recording the rejection result. The corresponding thread of the virtual object records the object identifier of the virtual object and the light source identifier of the current light source at the storage location pointed to by the offset value in the fourth buffer. At the same time, each thread can record the number of virtual objects and light rays intersecting with the virtual object in the third buffer while determining the intersection of the corresponding virtual object and the light rays of all light sources. That is, the number of final actual rendering instances of the object. After the intersection determination of each virtual object with all light sources is completed, the total number of instances to be rendered in the target virtual scene is stored in the fifth buffer. The rejection result that does not need to be recorded in the fourth buffer is a plurality of identifier groups recorded in disorder. An identifier group includes an object identifier and a light source identifier. The virtual object indicated by the object identifier in an identifier group has an intersection relationship with the light source indicated by the light source identifier in the identifier group. The total number of instances to be rendered is the same as the number of identifier groups. The number of instances corresponding to each virtual object is stored in the third buffer. The number of instances corresponding to a virtual object is equal to the number of candidate light sources intersecting with the virtual object. The number of instances corresponding to the virtual object is stored in the third buffer in association with the object identifier of the virtual object.
[0168] III. Rejection result integration
[0169] It refers to integrating the light source identifiers of the candidate light sources intersecting with the same virtual object.
[0170] Specifically, the GPU can store the light source identifiers in the same identifier group with the same object identifier in the second buffer in a plurality of continuous storage locations according to the number of instances corresponding to the virtual object in the third buffer. Determine the offset interval corresponding to the plurality of continuous storage locations, and store each object identifier and the corresponding offset interval in the first buffer. In the GPU, a thread is allocated for each identifier group. Each thread is responsible for returning one rejection result to the correct position. Each time the object identifier associated with the minimum value in the offset interval of the identifier group corresponding to the thread is subjected to an atomic add operation to obtain an offset value after the atomic add operation, and the thread writes the light source identifiers in the corresponding identifier group to the storage location pointed to by the offset value after the atomic add operation in the second buffer, thereby arranging the unordered storage of the identifier groups in the fourth buffer to be in order.
[0171] IV. Generating indirect rendering instruction data
[0172] The GPU determines, based on the model vertex data of the virtual objects and the instance number corresponding to each virtual object, indirect rendering instruction data of each virtual object after receiving the rendering instruction. The indirect rendering instruction data includes the model vertex data and the instance number corresponding to each virtual object.
[0173] V. Drawing of the shadow map
[0174] The GPU determines, based on the target instance number of the current instance to be drawn, a target light source identifier stored in a storage location pointed to by the target instance number as an offset value in the second buffer and a target offset interval in the first buffer in which the target instance number is located; the GPU determines a target object identifier stored in the first buffer in association with the target offset interval; the GPU obtains, from the light source allocation information, a target map identifier associated with the target light source identifier, and the map indicated by the target map identifier as a target map; the GPU obtains the target map based on the level identifier in the target map identifier and the viewport identifier in the target map identifier; and the GPU draws, based on the indirect rendering instruction data of the target virtual object indicated by the target object identifier and the light source information of the target light source indicated by the target light source identifier, a shadow map of the shadow formed by the target light source on the target virtual object on the target map.
[0175] The target instance number of the current instance to be drawn can be obtained in the following manner: in the vertex shader, based on InstanceID (a system-provided variable used to identify a unique number of a virtual object being currently drawn) and InstanceOffset (a system-provided variable used to identify the number of times instance drawing has been performed on the current virtual object), specifically, based on InstanceID, the minimum value of the offset interval of the virtual object can be obtained from the first buffer, and based on InstanceOffset, the number of times instance drawing has been performed on the virtual object can be obtained, and the sum of the minimum value of the offset interval and the number of times instance drawing has been performed is taken as the target instance number of the current instance to be drawn.
[0176] In the case that the light source distribution information is known, the light source distribution information can be stored in a structure data set or a lookup table, such as the LightViewData data set in which a plurality of light source identifiers and corresponding map identifiers of the light source identifiers are stored. According to the light source distribution information, the map identifier corresponding to the light source identifier of the target light source can be obtained. The map identifier is bound to the corresponding rendering pipeline. For example, the viewport number in the map identifier can be passed to the SV_ViewportArrayIndex built in the multimedia programming interface (such as Direct X) of the GPU processor, and the target number in the map identifier can be passed to the SV_RenderTargetArrayIndex built in the multimedia programming interface of the GPU processor. The SV_ViewportArrayIndex allows the program to switch between a plurality of viewports, and the SV_RenderTargetArrayIndex is used to select a specific rendering target to write output data in the scene with multiple rendering targets (MRT, Multiple Render Targets), so as to realize the drawing of the shadow map of the shadow formed by the target light source on the virtual object in the map indicated by the map identifier by using the rendering pipeline.
[0177] Please refer to Figure 10 and Figure 11 shown, Figure 10 shows the time length and the amount of data transmitted when the map drawing method in the related art is used to draw the map of the target virtual scene with multiple light sources. Figure 11 shows the time length and the amount of data transmitted when the map drawing method of the present application is used to draw the map of the target virtual scene with multiple light sources.
[0178] In the related art, in the data preparation process of the GPU, data corresponding to each light source participating in shadow map rendering is prepared for the light source, so that when the shadow map of a large number of light sources needs to be rendered for a virtual object in a target virtual scene, each light source needs to send a rendering instruction to the GPU through the CPU to initiate the rendering of the shadow map of the light source. In the scheme of the present application, the CPU only needs to obtain the light source allocation information, prepare the light source information of each light source and the scene data of the target virtual scene, and perform a rendering instruction and related data sending operation to realize the shadow map rendering of each light source. Therefore, the time consumed by the CPU in the data preparation stage is significantly reduced, such as from 48.3 ms in the related art to 15.27 ms in the present application. The time consumed in the GPU to execute the rendering instruction to complete the map rendering is also significantly reduced, such as from 22.39 ms in the related art to 10.32 ms in the present application. Specifically, the number of channels involved in the shadow map rendering in the data preparation stage is reduced from multiple in the related art to one, and accordingly, the time consumed for data assembly related to the rendering channel in the data preparation stage is also reduced, from 13.03 ms in the related art to 9.78 ms in the present application. (The channel refers to a series of independent operations or stages in the rendering process, each stage focuses on processing a specific type of rendering task. The number of times of shadow calculation in the related art can be consistent with the number of light sources in the target virtual scene.) In the GPU rendering stage, the rendering instruction for shadow map rendering is reduced from multiple in the related art to only one, wherein the number of rendering instructions for shadow map rendering in the related art is consistent with the number of light sources in the target virtual scene.
[0179] The following continues to illustrate an exemplary structure of the shadow map rendering device provided by the embodiments of the present application implemented as a software module. In some embodiments, as shown in Figure 1 The software module in the shadow map rendering device 155 stored in the memory can include an information obtaining module 1551, an information sending module 1552, a light source determining module 1553, and a map rendering module 1554.
[0180] The information obtaining module 1551 is configured to obtain light source allocation information, the light source allocation information indicating that one map in a map pool is allocated to each of N light sources respectively, and M maps in the map pool are obtained by regionally dividing each of K map layers; K, M, and N are all integers greater than 1, and M is greater than K; the information sending module 1552 is configured to send drawing instructions, the light source allocation information, light source information of the N light sources, and scene data of a target virtual scene to a graphics processing unit (GPU); the scene data includes model vertex data of each virtual object in the target virtual scene; the light source determining module 1553 is configured to determine, by using the GPU, candidate light sources at which light rays intersect with each virtual object, from the N light sources, according to the light source information of the N light sources and the model vertex data of each virtual object; and the map drawing module 1554 is configured to draw, by using the GPU, a shadow map of a shadow formed by a candidate light source on a virtual object on a map allocated to the candidate light source, in response to the drawing instructions, based on the light source information of the candidate light source determined for the virtual object, the model vertex data of the virtual object, and the light source allocation information.
[0181] In an implementation manner, the shadow map drawing apparatus 155 further includes an information integrating module and an instruction data determining module. The information integrating module is configured to integrate, by using the GPU, light source identifiers of candidate light sources at which light rays intersect with a same virtual object, to obtain light source integration information of each virtual object; and the instruction data determining module is configured to determine, by using the GPU, indirect drawing instruction data of each virtual object, based on a corresponding instance number of the virtual object and the model vertex data of the virtual object; the corresponding instance number of a virtual object is equal to a number of candidate light sources at which light rays intersect with the virtual object. The map drawing module 1554 is further configured to draw, by using the GPU, a shadow map of a shadow formed by a candidate light source on a virtual object on a map allocated to the candidate light source for each virtual object, in response to the drawing instructions, based on the indirect drawing instruction data of the virtual object, light source information of the candidate light source indicated by a light source identifier in the light source integration information of the virtual object, and the light source allocation information.
[0182] In an embodiment, the light source integration information of a virtual object includes offset interval information and light source identifiers of candidate light sources intersecting the virtual object, the GPU is provided with a first buffer and a second buffer, the offset interval information is stored in the first buffer, and the light source identifiers are stored in the second buffer; the offset interval information includes an object identifier of the virtual object and an offset interval corresponding to the virtual object; one offset value in the offset interval points to a storage location in the second buffer; and the information integration module includes a first storage submodule and a second storage submodule; the first storage submodule is configured to, for each virtual object, store, by the GPU, the offset interval corresponding to the virtual object and the object identifier of the virtual object in the first buffer in association, the number of offset values in the offset interval corresponding to a virtual object being equal to the number of candidate light sources intersecting the virtual object; and the second storage submodule is configured to store, by the GPU, the light source identifiers of the candidate light sources intersecting the virtual object in the storage location indicated by the offset value in the offset interval corresponding to the virtual object in the second buffer, wherein different light source identifiers correspond to different offset values in the storage location in the first buffer.
[0183] In an embodiment, the GPU is further provided with a third buffer and a fourth buffer, the third buffer stores the number of instances corresponding to each virtual object, the number of instances corresponding to a virtual object being equal to the number of candidate light sources intersecting the virtual object; the fourth buffer stores a plurality of identifier groups, one identifier group including one object identifier and one light source identifier, the virtual object indicated by the object identifier in one identifier group having an intersection relationship with the light source indicated by the light source identifier in the identifier group; and the information integration module further includes an interval allocation submodule, the interval allocation submodule being configured to allocate, by the GPU, offset intervals for a plurality of virtual objects according to the number of instances corresponding to each virtual object in the third buffer, to obtain the offset interval corresponding to each virtual object; and the second storage submodule is further configured to, for each identifier group in the fourth buffer, store, by the GPU, the light source identifier in the identifier group at an available storage location in the storage space indicated by the offset interval corresponding to the object identifier in the second buffer.
[0184] In an implementation, the GPU is provided with a third buffer and a fourth buffer; the light source determining module 1553 includes an intersection determining submodule, a third storage submodule, and a fourth storage submodule. The intersection determining submodule is configured to use the GPU to determine, according to the model vertex data of each virtual object and the light source information of the N light sources, the intersection between each virtual object and each of the N light sources, to obtain the intersection determination result of each virtual object and each light source; the third storage submodule is configured to use the GPU to determine, based on the intersection determination result, the candidate light source of which the light ray intersects with each virtual object, and store the object identifier of each virtual object and the light source identifier of a candidate light source corresponding to the virtual object as an identifier group in the fourth buffer; and the fourth storage submodule is configured to use the GPU to store, in the third buffer, the number of candidate light sources of which the light ray intersects with each virtual object and the object identifier of the virtual object in association.
[0185] In an implementation, the GPU is further provided with a fifth buffer, the fifth buffer being configured to store the value of a target field; and the fourth storage submodule is further configured to, for each intersection determination result, if the intersection determination result indicates an intersection relationship, use the GPU to accumulate 1 to the value of the target field in the fifth buffer; and use the GPU to store, in the fourth buffer, the object identifier of the virtual object involved in the intersection determination result and the light source identifier of the candidate light source involved as an identifier group at the storage location indicated by the reference offset, the reference offset being the value of the target field before the accumulation of 1.
[0186] In an implementation, the shadow map drawing apparatus 155 further includes a thread allocation module configured to use the GPU to allocate one thread to each virtual object in P threads, P being an integer greater than 1 and P not exceeding the number of virtual objects; and the intersection determining submodule is further configured to use the GPU to run the P threads in parallel, one thread being configured to use the GPU to determine, according to the model vertex data of a virtual object corresponding to the thread and the light source information of the N light sources, the intersection between the virtual object and each of the N light sources, to obtain the intersection determination result of the virtual object and each light source.
[0187] In an implementation, the map drawing module 1554 includes an information determining submodule, an object identification determining submodule, a map determining submodule, and a map drawing submodule. The information determining submodule is configured to determine, by using the GPU, a target light source identification stored at a storage location in the second buffer pointed to by the target instance value as an offset value, and determine a target offset interval in which the target instance value is located in the first buffer based on a target instance value of a current instance to be drawn. The object identification determining submodule is configured to determine, by using the GPU, a target object identification stored in the first buffer in association with the target offset interval. The map determining submodule is configured to determine, by using the GPU, a target map allocated for a target light source indicated by the target light source identification according to the light source allocation information. The map drawing submodule is configured to draw, by using the GPU, a shadow map of a shadow formed by the target light source on a target virtual object on the target map according to indirect drawing instruction data of the target virtual object indicated by the target object identification and light source information of the target light source indicated by the target light source identification.
[0188] In an implementation, the light source allocation information includes a plurality of identification pairs, each identification pair including a light source identification and a map identification, and a map indicated by the map identification in each identification pair is a map allocated for a light source indicated by the light source identification in the identification pair. The map identification of a map includes a level identification of a map layer in which the map is located and a viewport identification of a viewport in the map layer in which the map is located. The map determining submodule is further configured to obtain, by using the GPU, a target map identification associated with the target light source identification from the light source allocation information, and a map indicated by the target map identification is the target map. The map determining submodule is further configured to obtain, by using the GPU, the target map based on the level identification in the target map identification and the viewport identification in the target map identification.
[0189] In an implementation, the information obtaining module 1551 includes a contribution value obtaining submodule and a map allocation submodule. The contribution value obtaining submodule is configured to obtain, based on light source information of each light source, a light contribution value of each light source. The map allocation submodule is configured to allocate, based on the light contribution value of each light source and an area of each map, a map for each light source, and the light contribution value of each light source is positively correlated with the area of the map allocated for the light source.
[0190] In an implementation, the contribution value obtaining submodule is further configured to determine, according to the light source information of each of the light sources, a coverage area of each of the light sources in a target virtual scene and a distance between each of the light sources and a virtual camera in the target virtual scene. For each of the N light sources, the light contribution value of the light source is obtained based on the distance between the light source and the virtual camera corresponding to the target virtual scene and the coverage area of the light source in the target virtual scene, where the light contribution value is negatively correlated with the distance and positively correlated with the coverage area.
[0191] The embodiment of the present application provides a computer program product, which comprises a computer program stored in a computer readable storage medium. A processor of an electronic device reads the computer program from the computer readable storage medium, and the processor executes the computer program, so that the electronic device executes the shadow map drawing method provided by the embodiment of the present application.
[0192] The embodiment of the present application provides a computer readable storage medium, which stores a computer program. When the computer program is executed by a processor, the processor executes the shadow map drawing method provided by the embodiment of the present application.
[0193] In some embodiments, the computer readable storage medium can be a memory such as FRAM, ROM, PROM, EPROM, EEPROM, flash memory, magnetic surface memory, optical disc, or CD-ROM; and can also be various devices comprising one or any combination of the above memories.
[0194] In some embodiments, the computer program can be in the form of a program, software, software module, script or code, written in any form of programming language (including a compiled or interpreted language, or a declarative or procedural language), and can be deployed in any form, including being deployed as a stand-alone program or being deployed as a module, component, subroutine or other unit suitable for use in a computing environment.
[0195] As an example, the computer program can but not necessarily correspond to a file in a file system, can be stored in a part of a file storing other programs or data, for example, in one or more scripts in a Hyper Text Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple cooperative files (for example, files storing one or more modules, subprograms or code portions).
[0196] As an example, the computer program can be deployed to execute on one electronic device, or on multiple electronic devices located in one place, or on multiple electronic devices distributed in multiple places and interconnected through a communication network.
[0197] The above is only an embodiment of the present application, and is not used to limit the protection scope of the present application. Any modification, equivalent replacement and improvement made within the spirit and scope of the present application shall be included in the protection scope of the present application.
Claims
1. A method for drawing shadow maps, characterized in that, include: Obtain light source allocation information, which indicates that a texture is allocated to each of the N light sources in the texture pool. The M textures in the texture pool are obtained by dividing each of the K texture layers into regions. K, M, and N are all integers greater than 1, M is greater than K, and N ≤ M. The system sends drawing instructions, light source allocation information, light source information of the N light sources, and scene data of the target virtual scene to the graphics processing unit (GPU); the scene data includes the model vertex data of each virtual object in the target virtual scene. The GPU determines candidate light sources from among the N light sources where the light rays intersect with the virtual objects, based on the light source information of the N light sources and the model vertex data of each virtual object. In response to the rendering command, the GPU renders a shadow map of the shadow cast by the candidate light source on the virtual object on the texture map allocated to the candidate light source, based on the light source information of the candidate light source determined for the virtual object, the model vertex data of the virtual object, and the light source allocation information.
2. The method according to claim 1, characterized in that, Before the GPU responds to the rendering command and, based on the light source information of candidate light sources determined for the virtual object, the model vertex data of the virtual object, and the light source allocation information, renders the shadow map of the shadow cast by the candidate light source on the virtual object on the texture allocated to the candidate light source, the method further includes: The GPU integrates the light source identifiers of candidate light sources that intersect with the same virtual object to obtain the integrated light source information of each virtual object; The GPU determines the indirect rendering instruction data for each virtual object based on the number of instances corresponding to each virtual object and the model vertex data of the virtual object; the number of instances corresponding to a virtual object is equal to the number of candidate light sources where the light intersects with the virtual object; The step of the GPU responding to the rendering command by rendering a shadow map of the shadow cast by the candidate light source on the virtual object on the texture allocated to the candidate light source, based on the light source information of the candidate light source determined for the virtual object, the model vertex data of the virtual object, and the light source allocation information, includes: In response to the rendering command, the GPU renders a shadow map of the shadow cast by the candidate light source on the virtual object based on the indirect rendering command data of the virtual object, the light source information of the candidate light source indicated by the light source identifier in the light source integration information of the virtual object, and the light source allocation information, on the texture allocated to the candidate light source corresponding to the virtual object.
3. The method according to claim 2, characterized in that, The light source integration information of the virtual object includes offset interval information and light source identifiers of candidate light sources whose rays intersect with the virtual object. The GPU has a first buffer and a second buffer. The offset interval information is stored in the first buffer, and the light source identifiers are stored in the second buffer. The offset interval information includes the object identifier of the virtual object and the offset interval corresponding to the virtual object. An offset value in the offset interval points to a storage location in the second buffer. The process involves the GPU integrating the light source identifiers of candidate light sources that intersect with the same virtual object to obtain integrated light source information for each virtual object, including: For each virtual object, the GPU associates the offset interval corresponding to the virtual object with the object identifier of the virtual object and stores it in the first buffer. The number of offset values in the offset interval corresponding to a virtual object is equal to the number of candidate light sources that intersect with the virtual object. The GPU stores the light source identifiers of candidate light sources whose light intersects with the virtual object in the second buffer at the storage location indicated by the offset value in the offset interval corresponding to the virtual object. The offset values corresponding to the storage locations of different light source identifiers in the first buffer are different.
4. The method according to claim 3, characterized in that, The GPU also includes a third buffer and a fourth buffer. The third buffer stores the number of instances corresponding to each virtual object. The number of instances corresponding to a virtual object is equal to the number of candidate light sources that intersect with the virtual object. The fourth buffer stores multiple identifier groups. Each identifier group includes an object identifier and a light source identifier. The virtual object indicated by the object identifier in an identifier group and the light source indicated by the light source identifier in the identifier group have an intersection relationship. Before the GPU associates the offset range corresponding to each virtual object with the object identifier of the virtual object and stores it in the first buffer, the method further includes: The GPU allocates offset intervals to multiple virtual objects based on the number of instances corresponding to each virtual object in the third buffer, thereby obtaining the offset interval corresponding to each virtual object. The step of storing the light source identifiers of candidate light sources whose rays intersect with the virtual object by the GPU into the storage location indicated by the address offset in the offset interval corresponding to the virtual object in the second buffer includes: The GPU stores the light source identifier in each identifier group in the fourth buffer at an available storage location in the storage space indicated in the second buffer, corresponding to the offset interval of the object identifier in that identifier group.
5. The method according to any one of claims 2 to 4, characterized in that, The GPU is equipped with a third buffer and a fourth buffer. The step of the GPU determining candidate light sources from among the N light sources for each virtual object to intersect with the virtual object, based on the light source information of the N light sources and the model vertex data of each virtual object, includes: The GPU uses the model vertex data of each virtual object and the light source information of the N light sources to determine the intersection between each virtual object and each of the N light sources, and obtains the intersection determination result between each virtual object and each light source. The GPU determines candidate light sources that intersect with each virtual object based on the intersection determination result, and stores the object identifier of each virtual object and the light source identifier of a candidate light source corresponding to that virtual object as an identifier group in the fourth buffer. The GPU associates and stores the number of candidate light sources that intersect with each virtual object and the object identifier of that virtual object in the third buffer.
6. The method according to claim 5, characterized in that, The GPU also includes a fifth buffer, which is used to store the value of the target field. The process involves the GPU determining candidate light sources that intersect with each virtual object based on the intersection determination result, and storing the object identifier of each virtual object and the light source identifier of a corresponding candidate light source as an identifier group in the fourth buffer, including: For each intersection determination result, if the intersection determination result indicates that there is an intersection relationship, the GPU will increment the value of the target field in the fifth buffer by 1. The GPU stores the object identifiers of the virtual objects involved in the intersection determination result and the light source identifiers of the candidate light sources involved as an identifier group in the fourth buffer at the storage location indicated by the reference offset, where the reference offset refers to the value of the target field before accumulating 1.
7. The method according to claim 5, characterized in that, Before the GPU performs intersection determination between the virtual object and each of the N light sources based on the model vertex data of the virtual object and the light source information of the N light sources, and obtains the intersection determination result between the virtual object and each light source, the method further includes: The GPU allocates one thread for each virtual object in P threads, where P is an integer greater than 1 and P does not exceed the number of virtual objects; The process involves the GPU performing intersection determination between each virtual object and each of the N light sources based on the model vertex data of each virtual object and the light source information of the N light sources, to obtain the intersection determination results between each virtual object and each light source. This includes: The P threads are run in parallel in the GPU. Each thread is used to determine the intersection between the virtual object and each of the N light sources based on the model vertex data of the virtual object corresponding to the thread and the light source information of the N light sources, so as to obtain the intersection determination result between the virtual object and each light source.
8. The method according to claim 3, characterized in that, The step of the GPU responding to the rendering command, for each virtual object, based on the indirect rendering command data of the virtual object, the light source information of the candidate light source indicated by the light source identifier in the light source integration information of the virtual object, and the light source allocation information, to render a shadow map of the shadow formed by the candidate light source on the virtual object on the texture allocated to the candidate light source corresponding to the virtual object, including: The GPU determines the target light source identifier stored as an offset value in the storage location pointed to by the second buffer based on the target instance value of the current instance to be drawn, and determines the target offset interval in the first buffer where the target instance value is located; The GPU determines the target object identifier stored in the first buffer that is associated with the target offset interval; The GPU determines the target texture allocated to the target light source indicated by the target light source identifier based on the light source allocation information. The GPU draws a shadow map on the target map, which is the shadow cast by the target light source on the target virtual object, based on the indirect drawing instruction data of the target virtual object indicated by the target object identifier and the light source information of the target light source indicated by the target light source identifier.
9. The method according to claim 8, characterized in that, The light source allocation information includes multiple identifier pairs. Each identifier pair includes a light source identifier and a texture identifier. The texture indicated by the texture identifier in an identifier pair is a texture allocated to the light source indicated by the light source identifier in the identifier pair. The texture identifier of a texture includes the layer identifier of the texture layer in which the texture is located and the viewport identifier of the viewport in which the texture is located in the texture layer. The step of the GPU determining the target texture allocated to the target light source indicated by the target light source identifier based on the light source allocation information includes: The GPU obtains a target texture identifier associated with the target light source identifier from the light source allocation information, and the texture indicated by the target texture identifier is used as the target texture. The GPU obtains the target texture based on the layer identifier and the viewport identifier in the target texture identifier.
10. The method according to claim 1, characterized in that, The acquisition of light source allocation information includes: The illumination contribution value of each light source is obtained based on the light source information of each light source; Based on the illumination contribution value of each light source and the area of each texture, a texture is assigned to each light source, and the illumination contribution value of each light source is positively correlated with the area of the texture assigned to that light source.
11. The method according to claim 10, characterized in that, The step of obtaining the illumination contribution value of each light source based on the light source information of each light source includes: Based on the light source information of each light source, determine the coverage area of each light source in the target virtual scene and the distance between each light source and the virtual camera in the target virtual scene; For each of the N light sources, the illumination contribution value of the light source is obtained based on the distance between the light source and the virtual camera corresponding to the target virtual scene and the coverage area of the light source in the target virtual scene. The illumination contribution value is negatively correlated with the distance and positively correlated with the coverage area.
12. A shadow mapping device, characterized in that, include: The information acquisition module is used to acquire light source allocation information, which indicates that a texture is allocated to each of the N light sources in the texture pool. The M textures in the texture pool are obtained by dividing each of the K texture layers into regions. K, M, and N are all integers greater than 1, M is greater than K, and N ≤ M. The information sending module is used to send drawing instructions, light source allocation information, light source information of the N light sources, and scene data of the target virtual scene to the graphics processor (GPU); the scene data includes the model vertex data of each virtual object in the target virtual scene. The light source determination module is used by the GPU to determine candidate light sources from among the N light sources for each virtual object, based on the light source information of the N light sources and the model vertex data of each virtual object; The texture drawing module is used to utilize the GPU in response to the drawing command, and based on the light source information of the candidate light source determined for the virtual object, the model vertex data of the virtual object, and the light source allocation information, draw a shadow map of the shadow formed by the candidate light source on the virtual object on the texture allocated to the candidate light source.
13. An electronic device, characterized in that, include: One or more processors; Memory; One or more computer programs, wherein the one or more computer programs are stored in the memory and configured to be executed by the one or more processors, the one or more computer programs being configured to perform the method as described in any one of claims 1-11.
14. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that can be invoked by a processor to perform the method as described in any one of claims 1-11.
15. A computer program product, comprising a computer program, characterized in that, When executed by a processor, the computer program performs the steps of the method described in any one of claims 1-11.