CN115454438A - Method for converting HLSL shader source code into GLSL shader source code based on comparison table - Google Patents

Method for converting HLSL shader source code into GLSL shader source code based on comparison table Download PDF

Info

Publication number
CN115454438A
CN115454438A CN202210937137.4A CN202210937137A CN115454438A CN 115454438 A CN115454438 A CN 115454438A CN 202210937137 A CN202210937137 A CN 202210937137A CN 115454438 A CN115454438 A CN 115454438A
Authority
CN
China
Prior art keywords
glsl
hlsl
comparison table
functions
source codes
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
CN202210937137.4A
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.)
CETC 28 Research Institute
Original Assignee
CETC 28 Research Institute
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 CETC 28 Research Institute filed Critical CETC 28 Research Institute
Priority to CN202210937137.4A priority Critical patent/CN115454438A/en
Publication of CN115454438A publication Critical patent/CN115454438A/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
    • G06F8/42Syntactic analysis
    • G06F8/425Lexical analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3604Software analysis for verifying properties of programs
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/43Checking; Contextual analysis
    • G06F8/436Semantic checking

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computational Linguistics (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The invention discloses a method for converting HLSL shader source codes into GLSL shader source codes based on a comparison table, which comprises the following steps of 1, obtaining the existing HLSL shader source codes and the converted GLSL shader source codes, and generating the comparison table; and 2, converting the HLSL shader source codes into GLSL shader source codes based on the comparison table. The method automatically converts the source codes of the vertexes and the pixel shaders in the HLSL into the source codes of the vertexes and the fragment shaders in the GLSL based on the comparison table under a domestic platform, converts the found HLSL keywords into the GLSL keywords by scanning the HLSL source codes according to the established comparison table of the HLSL language vertexes and the pixel shaders as well as the keywords of the GLSL language vertexes and the fragment shaders, and supplements the syntactic structures of the shaders under the GLSL, so that the functions of automatically translating the vertexes and the pixel shader algorithms and the processing functions into the vertexes and the fragment shaders are realized.

Description

Method for converting HLSL shader source code into GLSL shader source code based on comparison table
Technical Field
The invention belongs to the technical field of computer software development, and particularly relates to a method for converting HLSL shader source codes into GLSL shader source codes based on a comparison table.
Background
Vertex and pixel shaders of HLSL (High Level Shading Language) can be converted into GLSL (Open Graphics Library shader Language) through a cross compiler, a latest DirectX program cannot be directly run under a domestic software and hardware platform and needs to be converted into an OpenGL program, and in the face of newly generated increasingly complex HLSL source code cross compiler conversion, conversion often fails, so that a simple and general tool is needed to realize conversion of the HLSL Language vertex and the pixel shaders into GLSL Language vertices and fragment shaders. The method of transforming the cross compiler is difficult to realize on domestic CPUs and operating systems with various architectures.
Disclosure of Invention
The invention aims to: the present invention provides a method for converting HLSL shader source code into GLSL shader source code based on a look-up table, aiming at the defects of the prior art.
In order to solve the above technical problem, the present invention discloses a method for converting HLSL shader source codes into GLSL shader source codes based on a lookup table, comprising the following steps:
step 1, acquiring the source code of an existing HLSL shader and the converted GLSL shader, and generating a comparison table;
and 2, converting the HLSL shader source codes into GLSL shader source codes based on the comparison table.
Further, the HLSL shader source code in step 1 includes HLSL vertex and pixel shader source code of DirectX under the Windows platform, and the GLSL shader source code includes GLSL vertex and fragment shader source code, and step 1 includes:
step 1.1, acquiring existing HLSL vertex and pixel shader source codes and converted GLSL vertex and fragment shader source codes in a cross compiling mode or a manual conversion mode to form a data set, and dividing the data set into training data and inspection data;
step 1.2, separating basic items including variables, data types, data, comments, operators and functions from HLSL vertexes and pixel shader source codes in training data through an HLSL compiler to generate a comparison table HLSL end;
step 1.3, separating basic items including variables, data types, data, comments, operators and functions from GLSL vertexes and fragment shader source codes in training data through a GLSL compiler to generate a comparison table GLSL end;
step 1.4, aligning the HLSL end of the comparison table with the GLSL end of the comparison table, and writing HLSL and GLSL alignment items into the comparison table if no alignment content exists in the comparison table;
step 1.5, repeating the steps 1.2 to 1.4, traversing HLSL vertexes and pixel shader source codes in the training data and correspondingly converted GLSL vertexes and fragment shader source codes, and obtaining a comparison table written in HLSL and GLSL alignment items;
step 1.6, the comparison table obtained in step 1.5 is used to convert the HLSL vertex and the pixel shader source codes in the inspection data into GLSL vertex and fragment shader source codes, and the GLSL vertex and the fragment shader source codes are compared with the GLSL vertex and the fragment shader source codes in the inspection data, if only naming difference exists, the comparison table meets the requirement, otherwise, step 1.1 is continuously executed.
Further, the HLSL compiler in step 1.2 and the GLSL compiler in step 1.3 both include a scanner, a lexical analyzer, a syntax analyzer, and a semantic analyzer, where the scanner is configured to scan the shader source code to obtain a character stream; the lexical analyzer is used for analyzing the character stream through a finite automaton and identifying lexical signs; the syntax analyzer is used for analyzing the lexical signs from top to bottom and constructing an abstract syntax tree by recursive descent; the semantic analyzer is used for checking whether variables and functions in the syntax tree are defined before use and whether types are compatible during assignment, so that the correctness of the syntax tree is ensured, and a comparison table HLSL end and a comparison table GLSL end are generated.
Further, aligning the table HLSL terminal and the table GLSL terminal in step 1.4 includes aligning according to comments, variables, data types, operators, and functions,
wherein, the annotation is directly quoted and does not need to be written into a comparison table; the variables comprise custom variables and internal variables, the custom variables are directly quoted without being written into a comparison table; the internal variables comprise the same internal variables and different internal variables of HLSL and GLSL, and are written into the comparison table; the data types comprise the same data type and different data types of the HLSL and the GLSL, and are written into the comparison table; operators comprise the same operator and different operators of HLSL and GLSL, and are written into the comparison table; the functions comprise the same functions, different functions, functions needing to be realized again and calls, the same and different functions are written into the comparison table, the functions needing to be realized again are realized again according to the language rules of the GLSL vertex and the fragment shader, the calls are packaged again according to the languages of the GLSL vertex and the fragment shader, and the functions before and after realization and the calls before and after packaging are written into the comparison table.
Further, aligning the HLSL end and the GLSL end of the comparison table in step 1.4 further includes aligning in a custom type, where the custom type includes items that cannot be included in annotations, variables, data types, operators, and functions, the alignment in the custom type is re-implemented according to the linguistic rules of the GLSL vertex and the fragment shader, and the items before and after implementation are expanded and stored in the comparison table.
Further, step 2 comprises:
step 2.1, separating basic items including variables, data types, data, comments, operators and functions by the HLSL compiler to HLSL vertex and pixel shader source codes to be converted to generate a first syntax tree;
step 2.2, based on a comparison table, converting each item in the first syntax tree into corresponding items in a GLSL vertex and a fragment shader;
and 2.3, filling up missing language structures according to the language formats of the GLSL vertexes and the fragment shaders, packaging the converted items through a main function, and adding GLSL version information to form complete vertexes and fragment shader source codes. Therefore, HLSL vertexes and pixel shaders of DirectX under the Windows platform are converted into GLSL vertexes and fragment shaders of OpenGL, and the fragment shaders can be called under a domestic platform.
In the prior art, when conversion is performed based on a cross compiler, a conversion rule is defined in the compiler, if the compiler cannot operate under the condition that a domestic platform marine light processor is an x86 architecture, a Feiteng processor is an ARM architecture, a LoongArch architecture is a loongArch latest processor, and an Shenwei processor is an alpha architecture, conversion cannot be realized, but the method separates a conversion program from the conversion rule, the conversion program in the step 2 can be reconstructed aiming at different architectures, and the conversion rule (generating a comparison table) in the step 1 can be reused.
Further, step 2.2 comprises:
step 2.2.1, the annotation content is not changed and is directly used in the GLSL vertex and the fragment shader source code;
step 2.2.2, aiming at the variable, the self-defined variable name is directly used in the GLSL vertex and the fragment shader without changing, and the internal variable is replaced according to the comparison table;
step 2.2.3, aiming at the data types, reserving the same data types according to the comparison table, and replacing different data types according to the corresponding relation;
step 2.2.4, aiming at operators, keeping the same operator according to the comparison table, and replacing different operators according to the corresponding relation;
and 2.2.5, aiming at the functions, reserving the same functions according to the comparison table, replacing the functions with different names and the same functions according to the corresponding relation, replacing the functions which are re-realized and packaged in the comparison table according to the corresponding relation, and re-realizing and packaging the functions and the calls which are not referred according to the language rules of the GLSL vertex and the fragment shader.
Further, step 2.2 further comprises:
and 2.2.6, replacing the self-defined types existing in the comparison table according to the corresponding relation aiming at the items which can not be classified into the comments, the variables, the data types, the operators and the functions, and re-realizing the self-defined types which do not exist in the comparison table according to the language rules of the GLSL vertexes and the fragment shader.
Further, step 2.2.5 is followed by re-implementing and encapsulating the non-referential functions and calls according to the language rules of the GLSL vertex and fragment shader, and then updating the pre-and post-implemented functions and the pre-and post-encapsulated calls to the look-up table.
Further, step 2.2.6 is followed by updating the items before and after the realization of the custom type that does not exist in the lookup table into the lookup table after the re-realization according to the language rules of the GLSL vertex and the fragment shader.
Has the beneficial effects that: compared with the prior art, the method adjusts and replaces the keywords according to the comparison table, and then completes the missing structure of the source code to form the complete source code of the vertex and the fragment shader, and has flexibility and expansibility. For codes which cannot be correctly converted by a cross compiler, the expansion of a conversion rule of a comparison table is adapted to the emerging situation, and meanwhile, because the logic of a completely controllable program of a source code is clear, code inspection and function debugging can be conveniently carried out, so that the application of the tool under various domestic platforms is realized:
the compiler and the reference table calling code can be reconstructed according to the instruction set of different domestic platform architectures, so that the support of a heterogeneous platform is realized;
the comparison table is stored in a single file form, when the comparison table needs to be upgraded, the program part does not need to be recompiled, and only the original comparison table file needs to be replaced;
aiming at the development of DirectX and OpenGL in the future, the functions can be expanded and a new data format can be added on the basis of the existing program and a comparison table, and the requirements of upgrading and updating can be met.
Drawings
The foregoing and/or other advantages of the invention will become further apparent from the following detailed description of the invention when taken in conjunction with the accompanying drawings.
Fig. 1 is a schematic diagram of source code conversion in a method for converting HLSL shader source code into GLSL shader source code based on a lookup table according to an embodiment of the present disclosure.
FIG. 2 is a schematic diagram illustrating a process of generating a lookup table in a method for converting HLSL shader source codes to GLSL shader source codes based on the lookup table according to an embodiment of the present disclosure.
FIG. 3 is a flowchart illustrating a method for converting HLSL shader source code to GLSL shader source code based on a look-up table according to an embodiment of the present disclosure.
Detailed Description
Embodiments of the present invention will be described below with reference to the accompanying drawings.
The method for converting HLSL shader source codes into GLSL shader source codes based on the comparison table utilizes HLSL vertexes and pixel shader codes of DirectX under a Windows platform to automatically convert the HLSL vertexes and the pixel shader codes into OpenGL GLSL shaders supported by a home platform, so that the DirectX shaders can be conveniently transplanted to the home platform for use, and the method can be applied to a code fast migration scene. As shown in fig. 3, the domestic processor has various architectures, including an x86 architecture for a glaucomatous processor, an ARM architecture for a soar processor, a loongson architecture for a LoongArch processor, an alpha architecture for a hawser processor, and an ARM architecture for a spread-green processor, and the operating systems matched with the domestic processor include a kylin operating system, a red flag operating system, a systemingx operating system, etc., and it is difficult for the operating systems to directly use the DirectX HLSL shader, which requires to convert the HLSL into an OpenGL GLSL shader commonly supported by the domestic platform.
In the prior art, a conventional method is to convert HLSL vertex and pixel shader codes into GLSL vertex and fragment shader codes by using a cross compiler, but development of the cross compiler needs a deep programming power base and deep understanding of the compilation principle, and the HLSL vertex and pixel shader codes and the GLSL vertex and fragment shader codes are comprehensively known, so that maintenance of one cross compiler is difficult, and a cross compiler with HLSL to GLSL functions has not been updated and maintained for years, so that the cross compiler is difficult to implement on domestic CPUs and operating systems with various architectures by modifying the cross compiler. The method for converting HLSL shader source codes into GLSL shader source codes based on the comparison table, which is provided by the embodiment of the application, simplifies the workload of development and maintenance by updating the comparison table.
The embodiment of the application discloses a method for converting HLSL shader source codes into GLSL shader source codes based on a comparison table, which comprises the following steps:
step 1, acquiring the existing HLSL shader source code and the converted GLSL shader source code, and generating a comparison table;
the HLSL shader source codes comprise HLSL vertices and pixel shader source codes, and the GLSL shader source codes comprise GLSL vertices and fragment shader source codes; as shown in fig. 2, generating the look-up table includes the following steps:
step 1.1, acquiring existing HLSL vertex and pixel shader source codes and converted GLSL vertex and fragment shader source codes in a cross compiling mode or a manual conversion mode to form a data set, dividing the data set into training data and inspection data, wherein the dividing ratio can be set to be 8;
step 1.2, separating basic items including variables, data types, data, comments, operators and functions from HLSL compilers of HLSL vertexes and pixel shader source codes in training data to generate a comparison table HLSL end;
step 1.3, separating basic items including variables, data types, data, annotations, operators and functions from GLSL (generic notation language) vertex and fragment shader source codes in training data through a GLSL compiler to generate a reference table GLSL end;
the HLSL compiler in step 1.2 and the GLSL compiler in step 1.3 both include a scanner, a lexical analyzer, a syntax analyzer, and a semantic analyzer, where the scanner is configured to scan a shader source code to obtain a character stream; the lexical analyzer is used for analyzing the character stream through a finite automaton and identifying lexical signs; taking C language as an example, namely a scanf statement, a character stream is obtained line by line, and an assignment statement "var2= var1+100 is used; for example, ' v ' a ' r '2' is read continuously until the symbol ' = ' is met, and the same principle is used to obtain ' var1 ', ' plus ', ' 100 '; "isolexical notation; the syntax analyzer is used for analyzing lexical signs from top to bottom and constructing an abstract syntax tree by recursion descending; and the semantic analyzer is used for checking whether variables and functions in the syntax tree are defined before use and whether types are compatible during assignment so as to ensure the correctness of the syntax tree, thereby generating a comparison table HLSL end and a comparison table GLSL end.
The scanner, lexical analyzer, syntax analyzer and semantic analyzer in the HLSL compiler and the GLSL compiler can be specifically realized according to different instructions of an x86 architecture, an ARM architecture, a LoongArch architecture and an alpha architecture, so that the requirements for use under different domestic platforms are met.
Step 1.4, aligning the HLSL end of the comparison table with the GLSL end of the comparison table, and writing HLSL and GLSL alignment items into the comparison table if no alignment content exists in the comparison table;
since most conversions can correspond one to one, aligning the HLSL end and the GLSL end of the comparison table in a line-by-line mode of a syntax tree comprises aligning according to comments, variables, data types, operators and functions, wherein the comments are directly quoted and do not need to be written into the comparison table;
the variables comprise custom variables and internal variables, and the custom variables are directly quoted without being written into a comparison table; the internal variables comprise the same internal variables and different internal variables of HLSL and GLSL, and are written into the comparison table; internal variables SV _ Position, PSIZE, SV _ Target [ n ], such as HLSL, correspond to internal variables gl _ Position, gl _ PointSize, gl _ FragColor, gl _ FragData [ n ] in GLSL.
The data types comprise the same data type and different data types of the HLSL and the GLSL, and are written into the comparison table; for example, the data types, pool, int, float, etc. are the same in HLSL and GLSL, and float2 and float3, etc. in HLSL correspond to vec2 and vec3, etc. in GLSL.
Operators comprise the same operator and different operators of HLSL and GLSL, and are written into the comparison table;
the functions comprise the same functions, different functions, functions which need to be realized again and calls of the HLSL and the GLSL, the same and different functions are written into the comparison table, for example, the functions abs, cos and the like are the same in the HLSL and the GLSL, and the functions with different usages, such as pow, exp, log and the like in the HLSL obtain the functions in the corresponding GLSL according to the contents of the HLSL end of the comparison table and the GLSL end of the comparison table; and re-realizing the functions to be re-realized according to the language rules of the GLSL vertexes and the fragment shaders, calling and re-packaging the functions according to the languages of the GLSL vertexes and the fragment shaders, and writing the functions before and after realization and the calls before and after packaging into the comparison table.
The alignment of the HLSL end of the comparison table and the GLSL end of the comparison table also comprises the alignment of a custom type, the custom type comprises items which cannot be classified into comments, variables, data types, operators and functions, the alignment of the custom type is realized again according to the language rules of the GLSL vertex and the fragment shader, and the items before and after the realization are expanded and stored in the comparison table.
When the comparison table is generated, the conversion of the variable, the data type, the operator, the function and the custom type may be written into the same comparison table, or may be written into the variable comparison table, the data type comparison table, the operator comparison table, the function comparison table and the custom comparison table, respectively.
Step 1.5, repeating steps 1.2 to 1.4, traversing HLSL vertexes and pixel shader source codes and GLSL vertexes and fragment shader source codes after corresponding conversion in the training data, and obtaining a comparison table written in HLSL and GLSL alignment items;
and step 1.6, converting HLSL vertexes and pixel shader source codes in the inspection data into GLSL vertexes and fragment shader source codes by using the comparison table obtained in the step 1.5, comparing the GLSL vertexes and the fragment shader source codes with the GLSL vertexes and the fragment shader source codes in the inspection data, if only naming difference exists, meeting the requirements of the comparison table, and if not, continuing to execute the step 1.1.
Step 2, converting the HLSL shader source code into the GLSL shader source code based on the lookup table, as shown in fig. 1, including the following steps:
step 2.1, separating basic items including variables, data types, data, comments, operators and functions by the HLSL compiler to HLSL vertexes and pixel shader source codes to be converted to generate a first syntax tree;
step 2.2, based on the comparison table, converting each item (keyword) in the first syntax tree into corresponding items in a GLSL vertex and a fragment shader, and realizing in-situ replacement or format adjustment; the method specifically comprises the following steps:
step 2.2.1, the annotation content is not changed and is directly used in the GLSL vertex and the fragment shader source code;
step 2.2.2, aiming at the variable, the self-defined variable name is directly used in the GLSL vertex and the fragment shader without changing, and the internal variable is replaced according to the comparison table; for example, internal variables SV _ Position, PSIZE, SV _ Target [ n ], etc. of HLSL can be directly converted into gl _ Position, gl _ PointSize, gl _ FragColor, gl _ FragData [ n ] in GLSL according to the look-up table.
Step 2.2.3, aiming at the data types, reserving the same data types according to the comparison table, and replacing different data types according to the corresponding relation; for example, if the data types are the same, flow 2, flow 3, etc. in HLSL are directly retained, and according to the comparison table, vec2, vec3, etc. in GLSL are replaced.
Step 2.2.4, aiming at operators, keeping the same operator according to the comparison table, and replacing different operators according to the corresponding relation;
step 2.2.5, aiming at the functions, reserving the same functions according to the comparison table, replacing the functions with different names and the same functions according to the corresponding relation, for example, directly referring to the functions with the same usage such as the functions abs and cos, and replacing the functions with the usage in the target language according to the comparison table for the functions with different usage such as pow, exp and log; and replacing the functions which are re-realized and packaged calls which exist in the comparison table according to the corresponding relation, and re-realizing and packaging the functions and the calls which do not have reference according to the language rules of the GLSL vertex and the fragment shader.
And 2.2.6, replacing the self-defined types existing in the comparison table according to the corresponding relation aiming at the items which can not be classified into the comments, the variables, the data types, the operators and the functions, and re-realizing the self-defined types which do not exist in the comparison table according to the language rules of the GLSL vertex and the fragment shader.
Step 2.2.5 is followed by further including re-implementing and encapsulating the non-referenced functions and calls according to the linguistic rules of the GLSL vertex and fragment shader, and updating the functions before and after implementation and the calls before and after encapsulation to the look-up table.
Step 2.2.6 is followed by updating the items before and after the realization of the custom type which does not exist in the comparison table into the comparison table after the realization of the custom type according to the language rules of the GLSL vertex and the fragment shader.
And 2.3, filling up missing language structures according to the language formats of the GLSL vertexes and the fragment shaders, packaging the converted items through a main function, and adding GLSL version information to form complete vertex and fragment shader source codes.
In a specific implementation, the present application provides a computer storage medium and a corresponding data processing unit, where the computer storage medium is capable of storing a computer program, and the computer program, when executed by the data processing unit, may run the inventive content of the method for converting HLSL shader source codes into GLSL shader source codes based on a lookup table, and some or all of the steps in each embodiment. The storage medium may be a magnetic disk, an optical disk, a read-only memory (ROM), a Random Access Memory (RAM), or the like.
It is obvious to those skilled in the art that the technical solutions in the embodiments of the present invention can be implemented by means of a computer program and its corresponding general-purpose hardware platform. Based on such understanding, the technical solutions in the embodiments of the present invention may be embodied in the form of a computer program, that is, a software product, which may be stored in a storage medium and includes several instructions to enable a device (which may be a personal computer, a server, a single chip, an MUU, or a network device) including a data processing unit to execute the method in each embodiment or some parts of the embodiments of the present invention.
The present invention provides a method for converting HLSL shader source code into GLSL shader source code based on a lookup table, and many methods and ways for implementing the method, and the above description is only a specific embodiment of the present invention, and it should be noted that, for those skilled in the art, many modifications and amendments can be made without departing from the principle of the present invention, and these modifications and amendments should be considered as the protection scope of the present invention. All the components not specified in the present embodiment can be realized by the prior art.

Claims (10)

1. A method for converting HLSL shader source code into GLSL shader source code based on a look-up table, comprising the steps of:
step 1, acquiring the existing HLSL shader source code and the converted GLSL shader source code, and generating a comparison table;
and 2, converting the HLSL shader source codes into GLSL shader source codes based on the comparison table.
2. The method according to claim 1, wherein the HLSL shader source code in step 1 comprises HLSL vertex and pixel shader source codes, and the GLSL shader source code comprises GLSL vertex and fragment shader source codes, and step 1 comprises:
step 1.1, acquiring existing HLSL vertex and pixel shader source codes and converted GLSL vertex and fragment shader source codes in a cross compiling mode or a manual conversion mode to form a data set, and dividing the data set into training data and inspection data;
step 1.2, separating basic items including variables, data types, data, comments, operators and functions from HLSL vertexes and pixel shader source codes in training data through an HLSL compiler to generate a comparison table HLSL end;
step 1.3, separating basic items including variables, data types, data, comments, operators and functions from GLSL (generic language/fragment shader) vertex and fragment shader source codes in training data by a GLSL compiler to generate a reference table GLSL end;
step 1.4, aligning the HLSL end of the comparison table with the GLSL end of the comparison table, and writing HLSL and GLSL alignment items into the comparison table if no alignment content exists in the comparison table;
step 1.5, repeating steps 1.2 to 1.4, traversing HLSL vertexes and pixel shader source codes and GLSL vertexes and fragment shader source codes after corresponding conversion in the training data, and obtaining a comparison table written in HLSL and GLSL alignment items;
and step 1.6, converting HLSL vertexes and pixel shader source codes in the inspection data into GLSL vertexes and fragment shader source codes by using the comparison table obtained in the step 1.5, comparing the GLSL vertexes and the fragment shader source codes with the GLSL vertexes and the fragment shader source codes in the inspection data, if only naming difference exists, meeting the requirements of the comparison table, and if not, continuing to execute the step 1.1.
3. The method according to claim 2, wherein the HLSL compiler in step 1.2 and the GLSL compiler in step 1.3 each comprise a scanner, a lexical analyzer, a syntax analyzer and a semantic analyzer, wherein the scanner is configured to scan the source code of the shader to obtain a character stream, and the lexical analyzer is configured to parse the character stream through finite automata and recognize lexical signs; the syntax analyzer is used for analyzing the lexical signs from top to bottom and constructing an abstract syntax tree by recursive descent; and the semantic analyzer is used for checking whether variables and functions in the syntax tree are defined before use and whether types are compatible during assignment so as to ensure the correctness of the syntax tree, thereby generating a comparison table HLSL end and a comparison table GLSL end.
4. The method according to claim 3, wherein the step 1.4 of aligning the HLSL end of the lookup table with the GLSL end of the lookup table comprises aligning according to comments, variables, data types, operators and functions,
wherein, the annotation is directly referred to without being written into a comparison table; the variables comprise custom variables and internal variables, and the custom variables are directly quoted without being written into a comparison table; the internal variables comprise the same internal variables and different internal variables of HLSL and GLSL, and are written into the comparison table; the data types comprise the same data type and different data types of HLSL and GLSL, and are written into the comparison table; operators including the same operator and different operator of HLSL and GLSL are written into the comparison table; the functions comprise the same functions, different functions, functions needing to be realized again and calls, the same and different functions are written into the comparison table, the functions needing to be realized again are realized again according to the language rules of the GLSL vertex and the fragment shader, the calls are packaged again according to the languages of the GLSL vertex and the fragment shader, and the functions before and after realization and the calls before and after packaging are written into the comparison table.
5. The method according to claim 4, wherein aligning the HLSL end of the lookup table with the GLSL end of the lookup table in step 1.4 further comprises aligning the custom type, wherein the custom type comprises items that cannot be classified into comments, variables, data types, operators, and functions, the aligning of the custom type is re-implemented according to the linguistic rules of the GLSL vertex and the fragment shader, and the items before and after the implementing are expanded and stored in the lookup table.
6. The method according to claim 5, wherein step 2 comprises:
step 2.1, separating basic items including variables, data types, data, comments, operators and functions by the HLSL compiler to HLSL vertexes and pixel shader source codes to be converted to generate a first syntax tree;
step 2.2, based on a comparison table, converting each item in the first syntax tree into corresponding items in a GLSL vertex and a fragment shader;
and 2.3, filling up missing language structures according to the language formats of the GLSL vertexes and the fragment shaders, packaging the converted items through a main function, and adding GLSL version information to form complete vertexes and fragment shader source codes.
7. The method according to claim 6, wherein step 2.2 comprises:
step 2.2.1, the annotation content is not changed and is directly used in the GLSL vertex and the fragment shader source code;
step 2.2.2, aiming at the variable, the self-defined variable name is directly used in the GLSL vertex and the fragment shader without changing, and the internal variable is replaced according to the comparison table;
step 2.2.3, aiming at the data types, reserving the same data types according to the comparison table, and replacing different data types according to the corresponding relation;
step 2.2.4, aiming at operators, keeping the same operator according to the comparison table, and replacing different operators according to the corresponding relation;
and 2.2.5, aiming at the functions, reserving the same functions according to the comparison table, replacing the functions with different names and the same functions according to the corresponding relation, replacing the functions which are re-realized and packaged in the comparison table according to the corresponding relation, and re-realizing and packaging the functions and the calls which are not referred according to the language rules of the GLSL vertex and the fragment shader.
8. The method according to claim 7, wherein step 2.2 further comprises:
and 2.2.6, replacing the self-defined types existing in the comparison table according to the corresponding relation aiming at the items which can not be classified into the comments, the variables, the data types, the operators and the functions, and re-realizing the self-defined types which do not exist in the comparison table according to the language rules of the GLSL vertex and the fragment shader.
9. The method according to claim 8, wherein step 2.2.5 is followed by updating the look-up table with the pre-and post-realization functions and the pre-and post-package calls after re-realization and packaging of the non-referential functions and calls according to the linguistic rules of the GLSL vertex and fragment shader.
10. The method according to claim 8, wherein step 2.2.6 is followed by updating the lookup table with entries before and after the re-implementation of the custom type that does not exist in the lookup table according to the linguistic rules of the GLSL vertex and fragment shader.
CN202210937137.4A 2022-08-05 2022-08-05 Method for converting HLSL shader source code into GLSL shader source code based on comparison table Pending CN115454438A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210937137.4A CN115454438A (en) 2022-08-05 2022-08-05 Method for converting HLSL shader source code into GLSL shader source code based on comparison table

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210937137.4A CN115454438A (en) 2022-08-05 2022-08-05 Method for converting HLSL shader source code into GLSL shader source code based on comparison table

Publications (1)

Publication Number Publication Date
CN115454438A true CN115454438A (en) 2022-12-09

Family

ID=84297502

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210937137.4A Pending CN115454438A (en) 2022-08-05 2022-08-05 Method for converting HLSL shader source code into GLSL shader source code based on comparison table

Country Status (1)

Country Link
CN (1) CN115454438A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116661800A (en) * 2023-06-06 2023-08-29 广州正是网络科技有限公司 Shader cross compiling method, system and storage medium based on HLSL language

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116661800A (en) * 2023-06-06 2023-08-29 广州正是网络科技有限公司 Shader cross compiling method, system and storage medium based on HLSL language

Similar Documents

Publication Publication Date Title
US6378126B2 (en) Compilation of embedded language statements in a source code program
EP0789875B1 (en) Method of translating source code from one high-level computer language to another
US5339419A (en) ANDF compiler using the HPcode-plus compiler intermediate language
EP0464525B1 (en) Architecture neutral distribution format (ANDF) producer using a compiler intermediate language
US5459868A (en) Interpretive object-oriented facility which can access pre-compiled classes
US6317871B1 (en) System for ensuring the accuracy of file structures in a source-to-source computer program translator
EP0464526B1 (en) Architecture neutral distribution format (ANDF) installer using a compiler intermediate language
US5307499A (en) Interpretive object-oriented facility which can access pre-compiled classes
US9086931B2 (en) System for translating diverse programming languages
US20040103405A1 (en) System for translating programming languages
US20130152061A1 (en) Full fidelity parse tree for programming language processing
US11740881B2 (en) Method for implementing compiled embedded Python
US6138272A (en) GDMO translator, method of GDMO translation, and recording medium containing program for GDMO translator
EP0520708B1 (en) Method and apparatus for converting high level form abstract syntaxes into an intermediate form
CN114756213B (en) Automatic code generation method and device for intelligent control system
US6381736B1 (en) Method for compile-time type-checking of arguments for externally-specified format strings
CN115454438A (en) Method for converting HLSL shader source code into GLSL shader source code based on comparison table
CN115756451A (en) Method, device, equipment and storage medium for reusing multi-project code file
US8935657B2 (en) Model-to-model transformation by kind
CN110347416B (en) Script updating method and device
CN110018816B (en) Virtual machine control system based on C/C + + compiling system and control method thereof
CN113687833A (en) Hybrid compiling method, hybrid compiling system, compiler, and storage medium
JP3266097B2 (en) Automatic reentrant method and system for non-reentrant program
Kramer et al. Reflection in attribute grammars
CN117270962B (en) Method and device for processing coloring language, storage medium and electronic equipment

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