CN114527984A - Shader generation method and device, player and storage medium - Google Patents

Shader generation method and device, player and storage medium Download PDF

Info

Publication number
CN114527984A
CN114527984A CN202011322221.2A CN202011322221A CN114527984A CN 114527984 A CN114527984 A CN 114527984A CN 202011322221 A CN202011322221 A CN 202011322221A CN 114527984 A CN114527984 A CN 114527984A
Authority
CN
China
Prior art keywords
shader
result file
precompiled
source code
component
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202011322221.2A
Other languages
Chinese (zh)
Inventor
黄宋悦
张欣
陈杰
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Hangzhou Hikvision Digital Technology Co Ltd
Original Assignee
Hangzhou Hikvision Digital Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Hangzhou Hikvision Digital Technology Co Ltd filed Critical Hangzhou Hikvision Digital Technology Co Ltd
Priority to CN202011322221.2A priority Critical patent/CN114527984A/en
Publication of CN114527984A publication Critical patent/CN114527984A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The embodiment of the application provides a generation method, a device, a player and a storage medium of a shader, wherein the method is applied to a precompilation assembly and comprises the following steps: acquiring a shader source code; and pre-compiling the shader source code to obtain a pre-compiled result file, so that the rendering component reads the pre-compiled result file under the condition of obtaining a rendering instruction, and generates a shader based on the pre-compiled result file. Because the precompilation component can obtain the shader source code in advance and then precompilation is carried out on the shader source code to obtain the precompilation result file for standby, the rendering component can directly read the precompilation result file and generate the shader based on the precompilation result file, and the shader source code does not need to be compiled when the image rendering task needs to be processed, the initialization time of the shader is greatly shortened, the starting time of the rendering engine is shortened, and the whole time needed for processing the image rendering task can be shortened.

Description

Shader generation method and device, player and storage medium
Technical Field
The present application relates to the field of computer graphics technologies, and in particular, to a method and an apparatus for generating a shader, a player, and a storage medium.
Background
Shaders are components used in rendering engines to implement image rendering, replacing programmable programs of fixed rendering pipelines. The source code formats of shaders in different rendering engines are different, for example, Glsl, Hlsl, cuda, etc., but all the shaders need to be compiled into binary code for GPU (Graphics Processing Unit) to execute.
In the current image rendering process based on a rendering engine, source codes of a manually written shader are stored in a database in a character string mode, when an image rendering task needs to be processed, the source codes of the shader are read, then the source codes of the shader are compiled to obtain a compiling result file, the compiling result file is input into the rendering engine to generate the shader, and the shader is adopted to complete the image rendering task.
In the image rendering process, it takes a long time to compile the source code of the shader, which may cause the initialization time of the shader to be too long, and affect the start time of the rendering engine, thereby resulting in a long overall time required for processing the image rendering task.
Disclosure of Invention
Embodiments of the present application provide a method and an apparatus for generating a shader, a player, and a storage medium, so as to shorten initialization time of the shader and shorten overall time required for processing an image rendering task. The specific technical scheme is as follows:
in a first aspect, an embodiment of the present application provides a method for generating a shader, which is applied to a precompiled component, and the method includes:
acquiring a shader source code;
and pre-compiling the shader source code to obtain a pre-compiled result file, so that the rendering component reads the pre-compiled result file under the condition of obtaining a rendering instruction, and generates a shader based on the pre-compiled result file.
Optionally, the step of performing precompilation on the shader source code to obtain a precompilation result file includes:
determining whether an operating platform corresponding to the shader source code is the same as an operating platform of the precompiled component;
under the condition that an operation platform corresponding to the shader source code is different from an operation platform of the pre-compiled assembly, converting the shader source code to obtain a shader code in a target format, wherein the target format is a shader language format of the operation platform of the pre-compiled assembly;
and pre-compiling the shader codes in the target format to obtain a pre-compiling result file.
Optionally, the step of converting the shader source code to obtain a shader code in a target format includes:
analyzing the shader source codes, and determining nouns and expressions included by the shader source codes;
acquiring a first shader language name library of an operation platform corresponding to the shader source code and a second shader language name library of the operation platform of the pre-compiling component;
replacing nouns in the shader source code with corresponding nouns in the second shader language noun library based on the first shader language noun library and the second shader language noun library;
and converting the expression in the shader source code based on the shader language format of the running platform of the precompiled component to obtain the shader code in the target format.
Optionally, after the step of performing precompilation on the shader source code to obtain a precompilation result file, the method further includes:
and compressing the precompiled result file under the condition that the size of the precompiled result file reaches a preset threshold value to obtain a compressed result file.
Optionally, after the step of performing precompilation on the shader source code to obtain a precompilation result file, the method further includes:
encrypting the shader source code to obtain an encrypted source code;
and adding the encrypted source code into the pre-compiling result file.
Optionally, the precompiled result file includes a file header, an index table, and a shader table, where:
the file header comprises the size of the file header; the index table comprises the starting position identification of the shader binary code in the precompiled result file; the shader table includes the shader binaries and the shader binaries size, where the shader binaries are precompiled from the shader source code.
In a second aspect, an embodiment of the present application provides a method for generating a shader, which is applied to a rendering component, and the method includes:
under the condition that a rendering instruction is obtained, reading a pre-compiling result file, wherein the pre-compiling result file is obtained by pre-compiling a pre-compiling component to a color mapper source code in advance;
and generating a shader based on the precompiled result file.
Optionally, the step of generating a shader based on the precompiled result file includes:
analyzing the precompiled result file to obtain a shader binary code included in the precompiled result file, wherein the shader binary code is obtained by precompiling the shader source code by the precompiled component;
inputting the shader binary code into a rendering engine interface to cause a rendering engine to generate a shader from the shader binary code.
Optionally, the precompiled result file includes a file header, an index table, and a shader table, where the file header includes a size of the file header; the index table comprises a start position identification of the shader binary code in the precompiled result file; the shader table includes the shader binaries and the shader binaries sizes;
the step of analyzing the precompiled result file to obtain the shader binary code included in the precompiled result file includes:
analyzing the pre-compiling result file to obtain the size of the file header;
determining the position of the index table based on the size of the file header, and reading the starting position identification of the shader binary code in the precompiled result file from the position of the index table;
determining a starting position of a shader binary code of a target shader in the precompiled result file based on a starting position identification of the shader binary code in the precompiled result file, and acquiring the size of the target shader binary code from the shader table, wherein the target shader is a shader to which the rendering instruction is directed;
starting from the starting position, shader binaries of the target shader binary size are read.
Optionally, before the step of generating a shader based on the precompiled result file, the method further includes:
decompressing the precompiled result file under the condition that the precompiled result file is a compressed file to obtain a decompressed result file;
the step of generating a shader based on the precompiled result file includes:
and generating a shader based on the decompressed result file.
Optionally, the method further includes:
and under the condition that the precompiled result file comprises the encrypted source code, decrypting the encrypted source code to obtain the shader source code.
In a third aspect, an embodiment of the present application provides a shader generation apparatus, which is applied to a precompiled component, and the apparatus includes:
the source code acquisition module is used for acquiring shader source codes;
and the precompilation module is used for precompilating the shader source code to obtain a precompilation result file, so that the rendering component reads the precompilation result file under the condition of obtaining the rendering instruction, and generates a shader based on the precompilation result file.
In a fourth aspect, an embodiment of the present application provides a shader generation apparatus, which is applied to a rendering component, and the apparatus includes:
the file reading module is used for reading a pre-compiling result file under the condition of obtaining the rendering instruction, wherein the pre-compiling result file is obtained by pre-compiling a pre-compiling component to a color machine source code in advance;
and the shader generating module is used for generating a shader based on the precompiled result file.
In a fifth aspect, an embodiment of the present application provides a player, where the player includes a precompilation component and a rendering component, where:
the precompilation component is configured to perform the method steps of any of the above first aspects;
the rendering component is configured to perform the method steps of any of the above second aspects.
In a sixth aspect, this embodiment provides a computer-readable storage medium, where a computer program is stored in the computer-readable storage medium, and the computer program, when executed by a processor, implements the method steps of any of the first aspect or the second aspect.
In the scheme provided by the embodiment of the application, the precompilation component can obtain the shader source code, and then precompilation is performed on the shader source code to obtain the precompilation result file, so that the rendering component reads the precompilation result file under the condition of obtaining the rendering instruction, and generates the shader based on the precompilation result file. The pre-compiling component can pre-acquire the shader source code and then pre-compile the shader source code to obtain the pre-compiling result file for standby, so that the rendering component can directly read the pre-compiling result file under the condition of acquiring the rendering instruction and generate the shader based on the pre-compiling result file, and the shader source code does not need to be compiled when an image rendering task needs to be processed, so that the initialization time of the shader is greatly shortened, the starting time of a rendering engine is shortened, and the overall time needed for processing the image rendering task can be shortened. Of course, not all advantages described above need to be achieved at the same time in the practice of any one product or method of the present application.
Drawings
In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present application, and for those skilled in the art, other drawings can be obtained according to the drawings without creative efforts.
Fig. 1 is a flowchart of a first shader generation method according to an embodiment of the present disclosure;
FIG. 2 is a diagram illustrating a generation method of the shader shown in FIG. 1;
FIG. 3 is a flow chart of a source code conversion method according to the embodiment shown in FIG. 1;
FIG. 4 is a schematic diagram illustrating a structure of a precompiled result file according to the embodiment shown in FIG. 1;
FIG. 5(a) is a schematic diagram of a structure of a file header in the embodiment shown in FIG. 4;
FIG. 5(b) is a diagram illustrating a structure of an index table in the embodiment shown in FIG. 4;
FIG. 5(c) is a block diagram of a shader table in the embodiment shown in FIG. 4;
FIG. 6 is a flowchart illustrating a second method for generating a shader according to an embodiment of the present disclosure;
FIG. 7 is a flowchart illustrating a method for parsing a pre-compiled result file according to the embodiment shown in FIG. 6;
fig. 8 is a schematic structural diagram of a generating apparatus of a first shader according to an embodiment of the present disclosure;
fig. 9 is a schematic structural diagram of a second generation apparatus for a shader according to an embodiment of the present disclosure;
fig. 10 is a schematic structural diagram of a player according to an embodiment of the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
In order to shorten initialization time of a shader and improve processing efficiency of an image rendering task, embodiments of the present application provide a generation method and apparatus of a shader, a player, and a computer-readable storage medium.
First, a method for generating a shader according to a first embodiment of the present application is described below. The first method for generating a shader provided in the embodiment of the present application may be applied to a pre-compiling component preset in an electronic device, where the pre-compiling component is used to pre-compile a shader source code.
As shown in fig. 1, a method for generating a shader is applied to a precompiled component, and the method includes:
s101, obtaining shader source codes;
and S102, performing precompilation on the shader source code to obtain a precompilation result file, so that the rendering component reads the precompilation result file under the condition that the rendering instruction is obtained, and generating a shader based on the precompilation result file.
As can be seen, in the scheme provided in the embodiment of the present application, the precompilation component may obtain a shader source code, and then perform precompilation on the shader source code to obtain a precompilation result file, so that the rendering component reads the precompilation result file and generates a shader based on the precompilation result file when obtaining the rendering instruction. The pre-compiling component can pre-acquire the shader source code and then pre-compile the shader source code to obtain the pre-compiling result file for standby, so that the rendering component can directly read the pre-compiling result file under the condition of acquiring the rendering instruction and generate the shader based on the pre-compiling result file, and the shader source code does not need to be compiled when an image rendering task needs to be processed, so that the initialization time of the shader is greatly shortened, the starting time of a rendering engine is shortened, and the overall time needed for processing the image rendering task can be shortened.
The precompilation component is a component which is disposed in an electronic device and is used for precompiling the shader source code, and the electronic device can be a player. After the shader source code is written by a developer, the shader source code can be stored into a file in a character string mode and stored into a platform directory of an operation platform of the pre-compiling component. Thus, in step S101, the precompilation component can obtain the shader source code.
Furthermore, the precompilation component may perform the step S102, namely precompilating the shader source code to obtain a precompilation result file. In one embodiment, the precompilation component may convert the shader source code into binary code to obtain a precompilation result file, and store the precompilation result file.
When the rendering component acquires the rendering instruction, it is indicated that the image rendering task needs to be processed at this time, and then the rendering component may read the compiled precompiled result file, and generate a shader based on the precompiled result file, and the image rendering task may be completed by using the shader.
Therefore, when the rendering component acquires the rendering instruction, the precompiled result file can be directly read, the shader is generated based on the precompiled result file, and the shader source code does not need to be compiled when the image rendering task needs to be processed, so that the initialization time of the shader is greatly shortened, the starting time of the rendering engine is shortened, and the overall time required for processing the image rendering task can be shortened.
Since the shader language used by the developer to write the shader source code may be different from the shader language corresponding to the operating platform of the pre-compiled component, the pre-compiled component cannot compile the shader source code. For example, the Hlsl shader language only has the D3D (Direct3D) rendering engine of the windows platform capable of recognizing compilation, and opengl (Open Graphics Library) in the embedded ARM (advanced RISC machines) platform can compile shader code written by running the Glsl shader language, so that the precompiled component of the embedded ARM platform cannot compile shader source code written by Hlsl.
In order to solve the problem of the cross-platform application, as an implementation manner of the embodiment of the present application, the step of performing precompilation on the shader source code to obtain a precompilation result file may include:
determining whether an operating platform corresponding to the shader source code is the same as an operating platform of the precompiled component; under the condition that the running platform corresponding to the shader source code is different from the running platform of the pre-compiled assembly, converting the shader source code to obtain a shader code in a target format; and pre-compiling the shader codes in the target format to obtain a pre-compiling result file.
After the precompilation component obtains the shader source code, in order to ensure that precompilation can be completed smoothly, whether the running platform corresponding to the shader source code is the same as the running platform of the precompilation component or not can be determined. In one embodiment, the shader source code may include an identifier of a corresponding operating platform, so that the precompilation component may parse the shader source code to obtain the identifier, and determine the operating platform corresponding to the shader source code according to the identifier. Then comparing with the self running platform to determine whether the two are the same.
If the running platform corresponding to the shader source code is the same as the running platform of the precompiled component, which indicates that the precompiled component can compile the shader source code, the steps of precompiling the shader source code to obtain a precompiled result file can be continuously executed.
If the running platform corresponding to the shader source code is different from the running platform of the pre-compiling component, which indicates that the pre-compiling component cannot compile the shader source code, the pre-compiling component can convert the shader source code to obtain the shader code in the target format. The target format is a shader language format of an operation platform of the precompiled component. That is, the precompilation component can convert the shader source code to obtain the shader code in the shader language format that can be compiled by itself.
After the shader code of the target format is obtained, the precompilation component can perform precompilation on the shader code of the target format to obtain a precompilation result file.
As shown in fig. 2, the precompilation component may run on multiple platforms, and when each platform runs, shader source codes (source codes) written by various shader languages (e.g., Glsl, cuda, CG, Hlsl, and Gpul) may be translated to shader codes in a target format corresponding to the platform where the platform is located, and the shader codes in the target format may be compiled to generate a precompilation result file. When image rendering is needed, the rendering component in the GPU can acquire the precompiled result file, a shader is generated, and an application program loads the shader to execute an image rendering task, so that a rendered image is output.
As can be seen, in this embodiment, the precompilation component may determine whether the running platform corresponding to the shader source code is the same as the running platform of the precompilation component, and if the running platform is different from the running platform corresponding to the shader source code, convert the shader source code to obtain the shader code in the target format, and then precompile the shader code in the target format to obtain the precompilation result file. Therefore, even if the shader language used by a developer for writing the shader source code is different from the shader language corresponding to the running platform of the pre-compiling component, the pre-compiling component can still be compiled to obtain a pre-compiling result file, and the purpose of cross-platform application is achieved. And the precompilation component can run on a plurality of platforms and output a precompilation result file in a shader language format of the corresponding platform, so that the rendering component of the corresponding platform generates a shader to complete an image rendering task.
As an implementation manner of the embodiment of the present application, as shown in fig. 3, the step of converting the shader source code to obtain a shader code in a target format may include:
s301, analyzing the shader source code, and determining a noun and an expression included in the shader source code;
although the various shader languages are different, the shader source code has a common characteristic that the shader source code is composed of various nouns and expressions, and in order to perform translation conversion on the shader source code, the precompilation component may parse the shader source code and determine the nouns and expressions included in the shader source code.
S302, acquiring a first shader language name library of the running platform corresponding to the shader source code and a second shader language name library of the running platform of the pre-compiling component;
in order to determine the nouns included in the shader source code in the shader language of the running platform of the precompiled component, the precompiled component may obtain a first shader language noun library of the running platform corresponding to the shader source code and a second shader language noun library of the running platform of the precompiled component.
The first shader language name library comprises each name in the shader language of the shader source code, and the second shader language name library comprises each name in the shader language of the running platform of the precompiled component.
S303, replacing nouns in the shader source code with corresponding nouns in the second shader language name library based on the first shader language name library and the second shader language name library;
then, the precompilation component can replace the nouns in the shader source code with corresponding nouns in the second shader language noun library based on the first shader language noun library and the second shader language noun library. As an embodiment, the precompilation component may search each noun corresponding to a noun included in the shader source code from the second shader language noun library according to a corresponding relationship between each noun in the first shader language noun library and the second shader language noun library.
And then the nouns in the shader source codes are replaced by the corresponding nouns in the searched second shader language noun library, so that the replacement of the nouns is completed.
S304, based on the shader language format of the running platform of the precompiled component, the expressions in the shader source codes are converted to obtain shader codes in a target format.
Because various shader languages are different from each other in specific writing formats for expressing the same target expression, the precompiled component can also convert the expression in the shader source code based on the shader language format of the running platform of the precompiled component. Thus, the source code after the noun and expression conversion is the shader language format code that conforms to the running platform of the precompiled component, i.e. the shader code in the target format. Furthermore, the pre-compiling component can successfully compile the shader code.
As can be seen, in this embodiment, the precompilation component may parse the shader source code, determine nouns and expressions included in the shader source code, obtain a first shader language noun library of the operating platform corresponding to the shader source code and a second shader language noun library of the operating platform of the precompilation component, replace nouns in the shader source code with corresponding nouns in the second shader language noun library based on the first shader language noun library and the second shader language noun library, and further convert the expressions in the shader source code based on the shader language format of the operating platform of the precompilation component to obtain the shader code in the target format. Therefore, codes conforming to the shader language format of the running platform of the precompiled component can be obtained, and compiling of the shader codes can be smoothly carried out.
As an implementation manner of the embodiment of the present application, after the step of performing precompilation on the shader source code to obtain a precompilation result file, the method may further include:
and compressing the precompiled result file under the condition that the size of the precompiled result file reaches a preset threshold value to obtain a compressed result file.
The problem that the rendering assembly reads the pre-compilation result file slowly and the like is caused by the fact that the size of the pre-compilation result file is too large, so that the pre-compilation assembly can compress the pre-compilation result file under the condition that the size of the pre-compilation result file reaches a preset threshold value to obtain a compressed result file. The specific compression method is not specifically limited, and may be zlib, WinRAR, or the like, for example.
The preset threshold may be set according to factors such as the size of a storage space of a disk, the reading performance of a memory or a register, and the image rendering efficiency, and is not specifically limited herein. For example, it may be 100M, 200M, 500M, etc.
Therefore, in this embodiment, under the condition that the size of the precompiled result file reaches the preset threshold, the precompiled component may compress the precompiled result file to obtain a compressed result file, so that the occupation of the storage space may be reduced, and the speed of reading the precompiled result file by the rendering component may be increased.
As an implementation manner of the embodiment of the present application, after the step of performing precompilation on the shader source code to obtain a precompilation result file, the method may further include:
encrypting the shader source code to obtain an encrypted source code; and adding the encrypted source code into the pre-compiling result file.
Shader source code may need to be used in some cases, for example, when problems with the shader are being troubleshot, the shader source code needs to be viewed, and thus needs to be added to the pre-compiled result file. In this case, in order to ensure data security of the shader source code, the precompilation component may encrypt the shader source code to obtain an encrypted source code, and then add the encrypted source code to the precompilation result file.
The specific way of encrypting the source code of the color device may be any Encryption processing way in the field of Data Encryption processing, for example, AES (Advanced Encryption Standard), DES (Data Encryption Standard), DSA (Digital Signature Algorithm), etc., and is not limited herein.
As can be seen, in this embodiment, the pre-compiling component may perform encryption processing on the shader source code to obtain an encrypted source code, and then add the encrypted source code to the pre-compiling result file. Therefore, the requirement that the shader source code possibly needs to be used under certain conditions can be met, and meanwhile, the data security of the shader source code can be ensured, and the confidentiality is high.
As an implementation manner of the embodiment of the present application, as shown in fig. 4, the precompiled result file includes a header 410, an index table 420, and a shader table 430, where:
the file header 410 includes a size of the file header (hesder _ size); the index table 420 includes an identification of the starting location of the shader binary in the precompiled result file; the shader table 430 includes the shader binaries and the shader binaries size.
The file header 410 may include a size of the file header, and may further include a running platform type (engine _ type) of the precompiled component, a version (version) of the precompiled result file, a size (file _ size) of the precompiled result file, an Index number (Index _ count) in the Index table, related information (platform _ info) of the running platform, and the like, where a specific format may be as shown in fig. 5 (a).
The index table 420 may include a start position identifier (Positionl) of the precompiled shader binary in the precompiled result file. In order to enable the rendering component to accurately read the shader binary from the precompiled result file, a start position identifier may be included in the index table 420 of the precompiled result file for the start position of the shader binary in the precompiled result file. The shader binary code is obtained by precompiling a shader source code.
Since multiple shaders are generally required to complete an image rendering task, a precompiled result file compiled by the precompiling component may include shader binary codes corresponding to the multiple shaders, and in order to accurately identify the start positions of the respective shader binary codes in the precompiled result file, the Index table 420 may further include multiple indexes (indexes), each Index corresponding to a shader binary code of one shader. The specific format may be as shown in fig. 5 (b).
The shader table 430 is mainly used to store shader binary codes (Program contents) and some parameters required for generating shaders, and the parameters required for generating shaders vary according to different precompiled component operating platforms. Taking the shader language of the runtime platform as OpenGL as an example, the format of the shader table 430 can be as shown in fig. 5 (c). May include storage format (Program format), shader binary size (Program size), shader binary, and the like.
In one embodiment, if the shader Source code is needed in the subsequent shader usage, the precompiled component may put the encrypted Source code (Source code After encryption) into the shader table 430, for example, the shader table 430 shown in fig. 5(c), and then may put the shader binary code.
The precompiled result file may include shader binaries corresponding to a plurality of shaders, and thus may include a plurality of shader tables, each corresponding to a shader, for storing the shader binaries of the shader and some parameters required for generating the shader.
As can be seen, in this embodiment, the precompiled result file may include a header, an index table and a shader table, where the header includes a size of the header, the index table includes a start position identifier of a shader binary in the precompiled result file, and the shader table includes the shader binary and a size of the shader binary. The precompiled result file can completely save the compiled result of the shader source codes and can simultaneously save the compiled results of a plurality of shader source codes. The rendering component can subsequently read information such as a shader binary code corresponding to a needed generated shader from the pre-compiling result file according to the file header, the index table and the shader table, and can smoothly generate the shader to complete an image rendering task.
Corresponding to the above first method for generating a shader, an embodiment of the present application further provides a second method for generating a shader. A second method for generating a shader is described below. The second generation method of the shader provided in the embodiment of the present application may be applied to a rendering component in an electronic device, where the rendering component is used to generate the shader.
As shown in fig. 6, a method for generating a shader is applied to a rendering component, and the method includes:
s601, reading a pre-compiling result file under the condition of obtaining a rendering instruction;
the precompiled result file is obtained by precompiling the colorator source code in advance by the precompiling component.
S602, generating a shader based on the precompiled result file.
As can be seen, in the scheme provided in the embodiment of the present application, the rendering component may read the pre-compilation result file when the rendering instruction is obtained, where the pre-compilation result file is obtained by pre-compiling the pre-compilation component with respect to the shader source code in advance, and then the rendering component may generate the shader based on the pre-compilation result file. The pre-compiling component can pre-acquire the shader source code and then pre-compile the shader source code to obtain the pre-compiling result file for standby, so that the rendering component can directly read the pre-compiling result file under the condition of acquiring the rendering instruction and generate the shader based on the pre-compiling result file, and the shader source code does not need to be compiled when an image rendering task needs to be processed, so that the initialization time of the shader is greatly shortened, the starting time of a rendering engine is shortened, and the overall time needed for processing the image rendering task can be shortened.
In the step S601, for the case that the rendering component acquires the rendering instruction, it is described that the image rendering task needs to be processed at this time, and then the rendering component may read the compiled precompiled result file, and in the step S602, a shader is generated based on the precompiled result file, and the image rendering task may be completed by using the shader.
The pre-compilation result file is obtained by pre-compiling the shader source code in advance by the pre-compilation component, that is, the pre-compilation result file can be obtained by the pre-compilation component by using any embodiment of the above generation method of the first shader.
As an implementation manner of the embodiment of the present application, the step of generating a shader based on the precompiled result file may include:
analyzing the precompiled result file to obtain a shader binary code included in the precompiled result file; inputting the shader binary code into a rendering engine interface to cause a rendering engine to generate a shader from the shader binary code.
The shader binary code is obtained by precompiling the shader source code by the precompiling component. After the rendering component reads the precompiled result file, the precompiled result file can be analyzed to obtain the shader binary codes included in the precompiled result file, and then the shader binary codes are input into a rendering engine interface, so that the rendering engine can generate corresponding shaders according to the shader binary codes, and an image rendering task is completed by using the shaders.
In one embodiment, if the rendering component does not parse out shader binaries from the precompiled result file, error hint information may be output. The abnormal condition that the shader generates errors is reminded to occur to the manager, so that the manager can process the errors conveniently.
As can be seen, in this embodiment, the rendering component may parse the precompiled result file to obtain the shader binary code included in the precompiled result file, and then input the shader binary code into the rendering engine interface, so that the rendering engine generates the shader according to the shader binary code. Therefore, the shader can be conveniently and quickly generated, and the processing efficiency of the image rendering task is improved.
As an implementation manner of the embodiment of the present application, the precompiled result file may include a file header, an index table, and a shader table, where the file header includes a size of the file header; the index table includes a start position identification of the shader binary in the precompiled result file; the shader table includes the shader binaries and the shader binaries sizes.
Since the specific format of the precompiled result file has been introduced in the embodiment of the first shader generation method, it is not described herein again.
As shown in fig. 7, the step of parsing the precompiled result file to obtain the shader binary code included in the precompiled result file may include:
s701, analyzing the pre-compiling result file to obtain the size of the file header;
s702, determining the position of the index table based on the size of the file header, and reading the starting position identifier of the shader binary code in the precompiled result file from the position of the index table;
in order to accurately read the required shader binary code from the precompiled result file, the rendering component may parse the precompiled result file to obtain the size of the file header. Since the file header is followed by the index table, the rendering component can determine the position of the index table based on the size of the file header, the end position of the file header is the position of the index table, and further, the rendering component can read the start position identifier of the shader binary code in the precompiled result file from the position of the index table.
The specific way of parsing the precompiled result file may be any file parsing way in the field of file parsing, as long as the size of the file header can be obtained, and is not specifically limited and described herein.
S703, determining the starting position of the shader binary code of the target shader in the precompiled result file based on the starting position identification of the shader binary code in the precompiled result file, and acquiring the size of the target shader binary code from the shader table;
the precompiled result file generally includes shader binary codes corresponding to a plurality of shaders, that is, the precompiled result file mainly includes a header and the shader binary codes corresponding to the respective shaders. The index table of the precompiled result file may include a start position identifier for the start position of the shader binary in the precompiled result file.
After determining the starting position of the shader binary code of the target shader in the precompiled result file, the shader binary code of the target shader can be read from the precompiled result file as long as the size of the shader binary code is known, and therefore, the rendering component can obtain the size of the target shader binary code from the shader table corresponding to the target shader. The target shader is a shader targeted by the rendering instruction, that is, a shader that needs to be generated currently. It is reasonable that there may be one or more target shaders.
S704, starting from the starting position, reading the shader binary code of the size of the target shader binary code.
After determining the starting position and size of the shader binary code of the target shader in the precompiled result file, the rendering component can read the shader binary code of the size of the target shader binary code from the starting position in the precompiled result file, wherein the shader binary code is the binary code corresponding to the target shader.
In one embodiment, the rendering component may also read other information included in the shader table of the target shader, such as encrypted source code, and the like.
As can be seen, in this embodiment, in the case that the precompiled result file includes a file header, an index table, and a shader table, the rendering component can accurately read the contents, such as the shader binary code, corresponding to the shader to be generated, so as to accurately and quickly generate the shader.
As an implementation manner of the embodiment of the present application, before the step of generating a shader based on the precompiled result file, the method may further include:
and under the condition that the precompiled result file is a compressed file, decompressing the precompiled result file to obtain a decompressed result file.
If the precompiled result file is a compressed file, the rendering component can decompress the precompiled result file according to the compression mode of the precompiled result file to obtain a decompressed result file.
Accordingly, the step of generating a shader based on the precompiled result file may include:
and generating a shader based on the decompressed result file.
Further, the rendering component may generate a shader based on the decompressed result file.
As an implementation manner of the embodiment of the present application, the method may further include:
and under the condition that the precompiled result file comprises the encrypted source code, decrypting the encrypted source code to obtain the shader source code.
If the rendering component reads the encrypted source code from the precompiled result file, the rendering component can decrypt the encrypted source code according to the encryption mode of the shader source code to obtain the shader source code, and then the shader source code can be used when needed.
Corresponding to the first method for generating a shader, an embodiment of the present application further provides a device for generating a shader. The following describes a first shader generation apparatus provided in an embodiment of the present application. The first method for generating a shader provided in the embodiment of the present application may be applied to a preset precompilation component in an electronic device, where the precompilation component is used to precompile a shader source code.
As shown in fig. 8, a shader generation apparatus applied to a precompiled component includes:
a source code obtaining module 810, configured to obtain shader source codes;
the precompilation module 820 is configured to precompile the shader source code to obtain a precompilation result file, so that the rendering component reads the precompilation result file when acquiring the rendering instruction, and generates a shader based on the precompilation result file.
As can be seen, in the scheme provided in the embodiment of the present application, the precompilation component may obtain a shader source code, and then precompilation is performed on the shader source code to obtain a precompilation result file, so that the rendering component reads the precompilation result file when obtaining the rendering instruction, and generates a shader based on the precompilation result file. The pre-compiling component can pre-acquire the shader source code and then pre-compile the shader source code to obtain the pre-compiling result file for standby, so that the rendering component can directly read the pre-compiling result file under the condition of acquiring the rendering instruction and generate the shader based on the pre-compiling result file, and the shader source code does not need to be compiled when an image rendering task needs to be processed, so that the initialization time of the shader is greatly shortened, the starting time of a rendering engine is shortened, and the overall time needed for processing the image rendering task can be shortened.
As an implementation manner of the embodiment of the present application, the precompilation module 820 may include:
the platform determination unit is used for determining whether an operation platform corresponding to the shader source code is the same as an operation platform of the precompiled component or not before the shader source code is precompiled to obtain a precompiled result file;
the interpretation translation unit is used for converting the shader source code under the condition that an operation platform corresponding to the shader source code is different from an operation platform of the precompiled component to obtain a shader code in a target format;
wherein the target format is a shader language format of an operating platform of the precompiled component.
And the precompilation unit is used for precompilating the shader codes in the target format to obtain a precompilation result file.
As an implementation manner of the embodiment of the present application, the interpretation translation unit may include:
the code analysis subunit is used for analyzing the shader source code and determining nouns and expressions included by the shader source code;
a name library obtaining subunit, configured to obtain a first shader language name library of the running platform corresponding to the shader source code and a second shader language name library of the running platform of the pre-compiled component;
a noun replacement subunit, configured to replace a noun in the shader source code with a corresponding noun in the second shader language noun library based on the first shader language noun library and the second shader language noun library;
and the expression conversion subunit is used for converting the expressions in the shader source codes based on the shader language format of the running platform of the precompiled component to obtain shader codes in a target format.
As an implementation manner of the embodiment of the present application, the apparatus may further include:
and the compression processing module is used for compressing the precompiled result file to obtain a compressed result file under the condition that the size of the precompiled result file reaches a preset threshold value after the precompiled source code of the shader is precompiled to obtain the precompiled result file.
As an implementation manner of the embodiment of the present application, the apparatus may further include:
the encryption processing module is used for carrying out encryption processing on the shader source code after the shader source code is subjected to precompilation to obtain a precompilation result file, so as to obtain an encrypted source code;
and the code adding module is used for adding the encrypted source code into the pre-compiling result file.
As an implementation manner of the embodiment of the present application, the precompiled result file may include a file header, an index table, and a shader table, where:
the file header comprises the size of the file header; the index table comprises the starting position identification of the shader binary code in the precompiled result file; the shader table includes the shader binaries and the shader binaries sizes.
Wherein the shader binary code is pre-compiled from the shader source code.
Corresponding to the second method for generating a shader, an embodiment of the present application further provides another device for generating a shader. The following describes a second shader generation apparatus provided in the embodiments of the present application. The second generation method of the shader provided by the embodiment of the present application can be applied to a rendering component in an electronic device, where the rendering component is used to generate the shader.
As shown in fig. 9, a shader generating apparatus applied to a rendering component includes:
a file reading module 910, configured to read a pre-compilation result file when a rendering instruction is obtained;
the precompiled result file is obtained by precompiling the colorator source code in advance by the precompiling component.
And a shader generating module 920, configured to generate a shader based on the precompiled result file.
As can be seen, in the scheme provided in the embodiment of the present application, the rendering component may read the pre-compilation result file when the rendering instruction is obtained, where the pre-compilation result file is obtained by pre-compiling the pre-compilation component with respect to the shader source code in advance, and then the rendering component may generate the shader based on the pre-compilation result file. The pre-compiling component can pre-acquire the shader source code and then pre-compile the shader source code to obtain the pre-compiling result file for standby, so that the rendering component can directly read the pre-compiling result file under the condition of acquiring the rendering instruction and generate the shader based on the pre-compiling result file, and the shader source code does not need to be compiled when an image rendering task needs to be processed, so that the initialization time of the shader is greatly shortened, the starting time of a rendering engine is shortened, and the overall time needed for processing the image rendering task can be shortened.
As an implementation manner of the embodiment of the present application, the shader generating module 920 may include:
the file analysis unit is used for analyzing the precompiled result file to obtain a shader binary code included in the precompiled result file;
wherein the shader binary code is obtained by precompiling the shader source code by the precompiling component.
A first shader generation unit, configured to input the shader binary code into a rendering engine interface, so that the rendering engine generates a shader according to the shader binary code.
As an implementation manner of the embodiment of the present application, the precompiled result file may include a file header, an index table, and a shader table, where the file header includes a size of the file header; the index table comprises a start position identification of the shader binary code in the precompiled result file; the shader table includes the shader binaries and the shader binaries sizes;
the file parsing unit may include:
the identifier obtaining subunit is configured to parse the pre-compilation result file to obtain the size of the file header;
a position identifier obtaining subunit, configured to determine a position of the index table based on the size of the file header, and read a start position identifier of the shader binary code in the precompiled result file from the position of the index table;
a position determining subunit, configured to determine, based on the start position identifier of the shader binary in the precompiled result file, a start position and an end position of the shader binary of a target shader in the precompiled result file, and obtain a size of the target shader binary from the shader table;
wherein the target shader is a shader to which the rendering instruction is directed.
A code reading subunit, configured to read shader binaries of the target shader binary size starting from the starting position.
As an implementation manner of the embodiment of the present application, the apparatus may further include:
the decompression processing module is used for decompressing the precompiled result file to obtain a decompressed result file under the condition that the precompiled result file is a compressed file before the shader is generated on the basis of the precompiled result file;
the shader generation module 920 may include:
and the second shader generating unit is used for generating a shader based on the decompressed result file.
As an implementation manner of the embodiment of the present application, the apparatus may further include:
and the decryption processing module is used for decrypting the encrypted source code under the condition that the precompiled result file comprises the encrypted source code to obtain the shader source code.
An embodiment of the present application further provides a player, as shown in fig. 10, the player includes a precompilation component 1010 and a rendering component 1020, where:
the precompilation component 1010 is configured to perform the first shader generation method according to any of the above embodiments.
The rendering component 1020 is configured to perform the second shader generation method steps according to any one of the embodiments.
Therefore, in the scheme provided by the embodiment of the application, the precompilation component in the player can obtain the shader source code, and then precompilation is performed on the shader source code to obtain a precompilation result file. And under the condition that the rendering component in the player acquires the rendering instruction, reading the precompiled result file and generating a shader based on the precompiled result file. The pre-compiling component can pre-acquire the shader source code and then pre-compile the shader source code to obtain the pre-compiling result file for standby, so that the rendering component can directly read the pre-compiling result file under the condition of acquiring the rendering instruction and generate the shader based on the pre-compiling result file, and the shader source code does not need to be compiled when an image rendering task needs to be processed, so that the initialization time of the shader is greatly shortened, the starting time of a rendering engine is shortened, and the overall time needed for processing the image rendering task can be shortened.
An embodiment of the present application further provides a computer-readable storage medium, where a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, the steps of the first shader generation method according to any of the above embodiments or the steps of the second shader generation method according to any of the above embodiments are implemented.
Therefore, in the solution provided in the embodiment of the present application, when the computer program is executed by the processor, the precompilation component may obtain the shader source code, and then perform precompilation on the shader source code to obtain the precompilation result file. And under the condition that the rendering component acquires the rendering instruction, reading the precompiled result file and generating a shader based on the precompiled result file. The pre-compiling component can pre-acquire the shader source code and then pre-compile the shader source code to obtain the pre-compiling result file for standby, so that the rendering component can directly read the pre-compiling result file under the condition of acquiring the rendering instruction and generate the shader based on the pre-compiling result file, and the shader source code does not need to be compiled when an image rendering task needs to be processed, so that the initialization time of the shader is greatly shortened, the starting time of a rendering engine is shortened, and the overall time needed for processing the image rendering task can be shortened.
It should be noted that, for the above-mentioned embodiments of the apparatus, the player and the computer-readable storage medium, since they are basically similar to the method embodiments, the description is relatively simple, and for the relevant points, reference may be made to the partial description of the method embodiments.
It is further noted that, herein, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in the process, method, article, or apparatus that comprises the element.
All the embodiments in the present specification are described in a relevant manner, and the same and similar components in all the embodiments are referred to each other, and each embodiment focuses on the differences from the other embodiments.
The above description is only for the preferred embodiment of the present application, and is not intended to limit the scope of the present application. Any modification, equivalent replacement, improvement and the like made within the spirit and principle of the present application are included in the protection scope of the present application.

Claims (14)

1. A method for generating a shader, the method being applied to a precompiled component, the method comprising:
acquiring a shader source code;
and pre-compiling the shader source code to obtain a pre-compiled result file, so that the rendering component reads the pre-compiled result file under the condition of obtaining a rendering instruction, and generates a shader based on the pre-compiled result file.
2. The method of claim 1, wherein the step of precompiling the shader source code to obtain a precompiled result file comprises:
determining whether an operating platform corresponding to the shader source code is the same as an operating platform of the precompiled component;
under the condition that an operation platform corresponding to the shader source code is different from an operation platform of the pre-compiled assembly, converting the shader source code to obtain a shader code in a target format, wherein the target format is a shader language format of the operation platform of the pre-compiled assembly;
and pre-compiling the shader codes in the target format to obtain a pre-compiling result file.
3. The method of claim 2, wherein the step of converting the shader source code to obtain shader code in a target format comprises:
analyzing the shader source codes, and determining nouns and expressions included by the shader source codes;
acquiring a first shader language name library of an operation platform corresponding to the shader source code and a second shader language name library of the operation platform of the pre-compiling component;
replacing nouns in the shader source code with corresponding nouns in the second shader language noun library based on the first shader language noun library and the second shader language noun library;
and converting the expression in the shader source code based on the shader language format of the running platform of the precompiled component to obtain the shader code in the target format.
4. The method of claim 1, wherein after the step of precompiling the shader source code to obtain a precompiled result file, the method further comprises:
and compressing the precompiled result file under the condition that the size of the precompiled result file reaches a preset threshold value to obtain a compressed result file.
5. The method of claim 1, wherein after the step of precompiling the shader source code to obtain a precompiled result file, the method further comprises:
encrypting the shader source code to obtain an encrypted source code;
and adding the encrypted source code into the pre-compiling result file.
6. The method of any of claims 1-5, wherein the precompiled results file includes a header, an index table, and a shader table, wherein:
the file header comprises the size of the file header; the index table comprises the starting position identification of the shader binary code in the precompiled result file; the shader table includes the shader binaries and the shader binaries size, where the shader binaries are precompiled from the shader source code.
7. A method for generating a shader, the method being applied to a rendering component, the method comprising:
under the condition that a rendering instruction is obtained, reading a pre-compiling result file, wherein the pre-compiling result file is obtained by pre-compiling a pre-compiling component to a color mapper source code in advance;
and generating a shader based on the precompiled result file.
8. The method of claim 7, wherein the step of generating a shader based on the precompiled result file comprises:
analyzing the precompiled result file to obtain a shader binary code included in the precompiled result file, wherein the shader binary code is obtained by precompiling the shader source code by the precompiled component;
inputting the shader binary code into a rendering engine interface to cause a rendering engine to generate a shader from the shader binary code.
9. The method of claim 8, wherein the precompiled results file comprises a header, an index table, and a shader table, wherein the header comprises a size of the header; the index table comprises a start position identification of the shader binary code in the precompiled result file; the shader table includes the shader binaries and the shader binaries sizes;
the step of analyzing the precompiled result file to obtain the shader binary code included in the precompiled result file includes:
analyzing the pre-compiling result file to obtain the size of the file header;
determining the position of the index table based on the size of the file header, and reading the starting position identification of the shader binary code in the precompiled result file from the position of the index table;
determining a starting position of a shader binary code of a target shader in the precompiled result file based on a starting position identification of the shader binary code in the precompiled result file, and acquiring the size of the target shader binary code from the shader table, wherein the target shader is a shader to which the rendering instruction is directed;
reading, starting from the starting position, shader binaries of the target shader binary size.
10. The method of claim 7, wherein prior to the step of generating a shader based on the precompiled result file, the method further comprises:
decompressing the precompiled result file under the condition that the precompiled result file is a compressed file to obtain a decompressed result file;
the step of generating a shader based on the precompiled result file includes:
and generating a shader based on the decompressed result file.
11. The method of any one of claims 7-10, further comprising:
and under the condition that the precompiled result file comprises the encrypted source code, decrypting the encrypted source code to obtain the shader source code.
12. A shader generation apparatus, applied to a precompiled component, the apparatus comprising:
the source code acquisition module is used for acquiring shader source codes;
and the precompilation module is used for precompilating the shader source code to obtain a precompilation result file, so that the rendering component reads the precompilation result file under the condition of obtaining the rendering instruction, and generates a shader based on the precompilation result file.
13. A shader generation apparatus, for application to a rendering component, the apparatus comprising:
the file reading module is used for reading a pre-compiling result file under the condition of obtaining the rendering instruction, wherein the pre-compiling result file is obtained by pre-compiling a pre-compiling component to a color machine source code in advance;
and the shader generating module is used for generating a shader based on the precompiled result file.
14. A player, characterized in that the player comprises a precompilation component and a rendering component, wherein:
the precompilation component for performing the method steps of any of claims 1-6;
the rendering component for performing the method steps of any of claims 7-11.
CN202011322221.2A 2020-11-23 2020-11-23 Shader generation method and device, player and storage medium Pending CN114527984A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202011322221.2A CN114527984A (en) 2020-11-23 2020-11-23 Shader generation method and device, player and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202011322221.2A CN114527984A (en) 2020-11-23 2020-11-23 Shader generation method and device, player and storage medium

Publications (1)

Publication Number Publication Date
CN114527984A true CN114527984A (en) 2022-05-24

Family

ID=81618663

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202011322221.2A Pending CN114527984A (en) 2020-11-23 2020-11-23 Shader generation method and device, player and storage medium

Country Status (1)

Country Link
CN (1) CN114527984A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117152320A (en) * 2023-02-15 2023-12-01 荣耀终端有限公司 Image processing method and electronic device

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117152320A (en) * 2023-02-15 2023-12-01 荣耀终端有限公司 Image processing method and electronic device

Similar Documents

Publication Publication Date Title
US9607160B2 (en) Method and apparatus for providing string encryption and decryption in program files
US8276130B2 (en) Method and compiler of compiling a program
CN112015430A (en) JavaScript code translation method and device, computer equipment and storage medium
CN111176717B (en) Method and device for generating installation package and electronic equipment
CN112597454A (en) Code obfuscation method, code operation method, device, medium, and apparatus
CN106547782B (en) Log information acquisition method and device
CN110059456B (en) Code protection method, code protection device, storage medium and electronic equipment
CN109948308A (en) Code security guard method, device, electronic equipment and computer readable storage medium
CN106796525A (en) On-demand loading dynamic script language codes are used with reducing internal memory
US20200089477A1 (en) Program optimization by converting code portions to directly reference internal data representations
CN105446704A (en) Shader parse method and device
US8677149B2 (en) Method and system for protecting intellectual property in software
CN111078279A (en) Processing method, device and equipment of byte code file and storage medium
CN114527984A (en) Shader generation method and device, player and storage medium
CN110347416B (en) Script updating method and device
CN111782239A (en) Software packaging and source code version information acquisition method, device and storage medium
CN108664796B (en) So file protection method and device
CN108153530B (en) Byte code optimization method and device, storage medium, processor and terminal
CN113721928B (en) Binary analysis-based dynamic library clipping method
CN112953721B (en) IPA file analysis method, IPA file analysis device, IPA file analysis equipment and storage medium
CN114610364A (en) Application program updating method, application program developing method, application program updating device, application program developing device and computer equipment
CN114816435A (en) Software development method based on reverse technology
CN113420569A (en) Code translation method, device, equipment and storage medium
CN111241191A (en) Database synchronization method and device
US20140351799A1 (en) Binary interface instrumentation

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination