A shader program capable of execution on a GPU is analyzed for constant expressions. These constant expressions are replaced with references to registers or memory addresses on the GPU. A preshader is created that comprises two executable files. The first executable file contains the shader program with the each constant expression removed and replaced with a unique reference accessible by the GPU. The first file is executable at the GPU. A second file contains the removed constant expressions along with instructions to place the values generated at the associated reference. The second executable file is executable at a CPU. When the preshader is executed, an instance of the first file is executed at the GPU for each vertex or pixel that is displayed. One instance of the second file is executed at the CPU. As the preshader is executed, the constant expressions in the second file are evaluated and the resulting intermediate values are passed to each instance of the first file on the GPU.
|
1. A computer-readable storage medium comprising computer-executable instructions that when executed perform operations comprising:
analyzing a shader program to find each constant expression that evaluates to a same set of intermediate values for all instances of the shader program;
generating a first executable file corresponding to the shader program and to be executed in place of the shader program by replacing each found constant expression in the shader program with a unique reference; and
generating a second executable file as a preshader program to be executed prior to execution of the first executable file, the second executable file comprising each replaced constant expression of the shader program, the second executable file as executed prior to the first executable file evaluating each replaced constant expression in such second executable file to produce an intermediate result and placing the intermediate result in a location corresponding to the unique reference associated with each found constant expression in the first executable file, each instance of the first executable file being executed in place of the shader program and after the execution of the second executable file, each instance of the first executable file upon encountering the unique reference therein retrieving the corresponding intermediate value as previously produced by the second executable file from the location corresponding to the unique reference and employing the retrieved intermediate value, wherein each replaced constant expression need only be evaluated once by the second executable file rather than multiple times by each instance of the first executable file.
17. A computing device, comprising:
a computer-readable storage medium comprising a first portion and a second portion of computer-executable instructions;
a first processing unit coupled to the computer-readable storage medium, the first processing unit to execute the first portion, the first portion generated by finding each constant expression in a shader program that evaluates to a same set of intermediate values for all instances of the shader program and replacing each found constant expression in the shader program with a unique reference; and
a second processing unit coupled to the computer-readable storage medium, the second processing unit to execute the second portion, the second portion generated by including therein each replaced constant expression of the shader program, the second portion to execute prior to execution of the first portion and as executing evaluating each replaced constant expression in such second portion to produce an intermediate result and placing the intermediate result in a location corresponding to the unique reference associated with each found constant expression in the first portion, each instance of the first portion being executed in place of the shader program and after the execution of the second portion, each instance of the first portion upon encountering the unique reference therein retrieving the corresponding intermediate value as previously produced by the second portion from the location corresponding to the unique reference and employing the retrieved intermediate value, wherein each replaced constant expression need only be evaluated once by the second portion rather than multiple times by each instance of the first portion.
9. A computing device, comprising:
one or more processing units; and
a computer-readable storage medium coupled to the one or more processing units, the computer-readable storage medium comprising computer-executable instructions that when executed by the one or more processing units perform operations comprising:
analyzing a shader program to find each constant expression that evaluates to a same set of intermediate values for all instances of the shader program;
generating a first executable file corresponding to the shader program and to be executed in place of the shader program by replacing each found constant expression in the shader program with a unique reference; and
generating a second executable file as a preshader program to be executed prior to execution of the first executable file, the second executable file comprising each replaced constant expression of the shader program, the second executable file as executed prior to the first executable file evaluating each replaced constant expression in such second executable file to produce an intermediate result and placing the intermediate result in a location corresponding to the unique reference associated with each found constant expression in the first executable file, each instance of the first executable file being executed in place of the shader program and after the execution of the second executable file, each instance of the first executable file upon encountering the unique reference therein retrieving the corresponding intermediate value as previously produced by the second executable file from the location corresponding to the unique reference and employing the retrieved intermediate value, wherein each replaced constant expression need only be evaluated once by the second executable file rather than multiple times by each instance of the first executable file.
2. The computer-readable storage medium of
3. The computer-readable storage medium of
4. The computer-readable storage medium of
5. The computer-readable storage medium of
6. The computer-readable storage medium of
7. The computer-readable storage medium of
8. The computer-readable storage medium of
10. The computing device of
11. The computing device of
12. The computing device of
13. The computing device of
14. The computing device of
15. The computing device of
16. The computing device of
18. The computing device of
19. The computing device of
20. The computing device of
21. The computing device of
22. The computing device of
|
This application is a continuation of application Ser. No. 11/045,784, filed Jan. 28, 2005, the contents of which are herein incorporated by reference in its entirety.
The present invention is directed to systems and methods for optimizing programmable shaders in connection with computer graphics. More particularly, the present invention is related to a computer-readable storage medium comprising computer-executable instructions for removing constant expressions from shaders executing at a graphics processing unit (GPU), and instead executing the constant expressions at a computer processing unit (CPU).
Rendering and displaying three dimensional (3-D) graphics typically involves many calculations and computations. For example, to render a 3-D object, a set of coordinate points or vertices that define the object to be rendered is formed. Vertices can be joined to form polygons that define the surface of the object to be rendered and displayed. Once the vertices that define an object are formed, the vertices can be transformed from an object or model frame of reference to a world frame of reference and finally to two dimensional (2-D) coordinates that can be displayed on a flat display device, such as a monitor. Along the way, vertices may be rotated, scaled, eliminated or clipped because they fall outside of a viewable area, lit by various lighting schemes and sources, colorized, otherwise transformed, shaded and so forth. The processes involved in rendering and displaying a 3-D object can be computationally intensive and may involve a large number of vertices.
Complex 3-D objects, or portions thereof, can be represented by collections of triangles (“a mesh”) representing the approximate geometry of the 3-D object, or by a geometry map, or surface, in 2-D surface space. The mesh can be specified through the position of the vertices of the triangles. One or more texture maps can be mapped to the surface to create a textured surface according to a texture mapping process. In this regard, signals textured over a surface can be very general, and can specify any sort of intermediate result that can be input to transformation mechanism(s), such as shader procedure(s), to produce a final color and/or other values associated with a point sample.
The computer graphics industry and graphics pipelines have seen a particularly tremendous amount of growth in the last few years. For example, current generations of computer games are moving to 3-D graphics in an ever increasing and more realistic fashion. At the same time, the speed of play is driven faster and faster. This combination has fueled a genuine need for the rapid rendering of 3-D graphics in relatively inexpensive systems.
The rendering of graphics data in a computer system is a collection of resource intensive processes. The process of shading, i.e., the process of performing complex algorithms upon set(s) of specialized graphics data structures, used to determine values for certain primitives, such as color, etc. associated with the graphics data structures, exemplifies such a computation intensive and complex process. Vertex shaders and pixel shaders are examples of such shaders.
Vertex and pixel shaders are computer programs that run on a specialized computer processor known as a graphics processing unit (“GPU”). The GPU is typically introduced into a general purpose computer system on a specialized piece of graphics hardware often with its own memory. An instance of a vertex or pixel shader is executed by the GPU, often in parallel, for every vertex or pixel in a specified 3-D mesh being displayed. Shaders are typically written in either the native assembly language of the particular GPU or in a high-level shader language, for example.
Shaders typically operate on stream data. The stream data comes from either a rasterization unit or a vertex stream unit on the GPU. In addition, shaders also receive input data from the host CPU known as constants. For a given execution of a set of meshes, these constants are unchanging. Shaders are typically authored with instructions which operate only on these constants, thereby causing each instance of the shader program executing on the GPU to generate intermediate values which are identical across all instances of the shader. The GPU's ability to run shaders is limited in comparison to a CPU by the amount of instructions and resources available. Given that an instance of a shader is executed for every vertex or pixel in a specified 3-D mesh being displayed, it is a waste of GPU resources for each instance of a shader to calculate the same identical intermediate values.
Therefore, what is needed are systems and methods for moving the evaluation of constant expressions in shader programs from the GPU to the CPU.
A shader program capable of execution on a GPU is analyzed for constant expressions. These constant expressions are replaced with references to registers or memory addresses on the GPU. A preshader is created that comprises two components. The first component (the GPU program) contains the shader program with each constant expression removed and replaced with a unique reference accessible by the GPU. The GPU program is executable at the GPU. A second component (the preshader program) contains the removed constant expressions along with instructions to place the values generated at the associated references. The preshader program is executable at a CPU. When the preshader is executed, an instance of the GPU program is executed at the GPU for each vertex or pixel that is displayed. One instance of the preshader program is executed at the CPU. As the preshader is executed, the constant expressions in the preshader program are evaluated and the resulting intermediate values are passed to each instance of the GPU program at the GPU.
The foregoing summary, as well as the following detailed description of preferred embodiments, is better understood when read in conjunction with the appended drawings. For the purpose of illustrating the invention, there is shown in the drawings exemplary constructions of the invention; however, the invention is not limited to the specific methods and instrumentalities disclosed. In the drawings:
Graphics cards 155 are desirably able to modify or change the appearance of the meshes that are displayed on display 169. For example, a graphics card may be used to animate a mesh, light a mesh, or cast shadows. Programs that are commonly used to make these changes are known as shaders. Shaders typically operate on either the pixels making up the 3-D mesh before display, or directly on the vertices themselves. An instance of a shader is desirably executed for each vertex or pixel comprising the 3-D mesh. Shaders may operate on both the vertex and pixel data simultaneously. Shaders which operate on the vertex data typically modify the position of an object or mesh either through a transform or an animation, for example. Pixel shaders typically adjust the appearance of an object or mesh such as its color and the way in which the object is lit, for example.
The shaders are typically executed at the GPU 133 on stream data. The stream data desirably comprises data related to the 3-D mesh such as position of vertices, for example. The stream data may be read from the local GPU 133 memory, such as memory 143 for example. However, the stream data may also be read from CPU memory 120, for example.
Shaders may contain expressions which are constant, called constant expressions. Constant expressions are defined as those expressions that evaluate to the same set of intermediate values for all instances of a shader regardless of the pixel or vertex associated with that instance of the shader. Because each instance of a shader separately evaluates these constant expressions even though the evaluation will produce the same intermediate results, there is a large number of GPU cycles spent reevaluating the constant expressions.
Preshaders avoid this recalculation by moving the calculation of the constant expressions from the GPU 133 to the CPU 121. A specialized compiler, as described further with respect to
The preshader program desirably comprises the removed constant expressions. In addition, CPU 121 executable instructions are generated for each constant expression. The instructions provide that when the associated constant expression is evaluated the calculated intermediate results are desirably saved, or otherwise made available, at the memory location substituted for that constant expression in the GPU program. The preshader program is desirably executable on the CPU 121 through a GPU 133 emulator, for example. In addition, the preshader program may be directly executable on the CPU 121 through direct compilation of the preshader program into the native format of the CPU 121, for example. The preshader program may also be executable on the graphics card 155 independent of the execution of the GPU program at a specialized processor on the graphics card 155 (not shown).
When a preshader is selected for execution on a 3-D mesh being displayed on display 169, both the GPU program and the preshader program are desirably executed. An instance of the GPU program of the preshader is desirably executed at the GPU 133 for each pixel or vertex that comprises the displayed 3-D mesh. Because constant expressions evaluate to the same intermediate values for all instances of the GPU program, only one instance of the preshader program is desirably executed at the CPU 121, for example. As intermediate values are generated through the execution of the preshader program they are saved or made available to the GPU at the location specified in preshader program, for example memory 143. As each instance of the GPU program is executed at the GPU, the pre-calculated intermediate values are retrieved from memory or registers and incorporated into each execution of the GPU program.
At 210, the shader program has been selected to be made into a preshader. As described with respect to
In each shader there may exist constant expressions. A constant expression has the property that no matter which element of a stream the shader is processing, the constant expression will evaluate to the same intermediate value. These constant expressions are based on input values which are known to not come from a stream element, or other changeable resource. Because streams can have many thousands of elements, it is desirable to move the evaluation the constant expressions from each of the instances of the shader to a general purpose CPU.
At 220, the shader is processed into a preshader by a compiler. The shader may be written in a programming language specific to the GPU or in a high level shader programming language, for example. A specialized compiler desirably parses the shader program looking for constant expressions. Constant expressions located in the shader program are desirably replaced by references. Each constant expression is desirably replaced by a unique reference that is accessible to the GPU. The reference may correspond to a register on the GPU, or to a portion of memory on the GPU, for example. The reference may also be located at the general purpose computer, as long as the location on the general purpose computer is readable by the GPU. Any system, method, or technique for assigning references may be used. The shader with the constant expressions removed is the GPU program of the preshader.
At 240, a preshader program is generated comprising each of the removed constant expressions. The new program desirably comprises the removed expressions in addition to new CPU executable instructions to deposit any calculated intermediate results at the associated reference indicated in the GPU program. The new file is desirably executable directly on the CPU, or through a GPU emulator or virtual machine on a general purpose computer, for example.
At 250, the GPU program and the preshader program are associated together such that when the preshader is selected for execution on a particular 3-D mesh, an instance of the GPU program is executed on the GPU for each vertex or pixel comprising the 3-D mesh, and a single instance of the preshader program is executed at the CPU. By executing each constant expression desirably only once at the CPU for all instances of the preshader, considerable GPU cycles are saved.
At 310, a preshader is desirably selected for execution. As described previously with respect to
At 330, the GPU program and the preshader program desirably begin executing. Prior to execution, the GPU program is desirably loaded into the memory of the GPU, and the preshader program is desirably loaded into the memory of the CPU. Any system, method or technique known in the art for loading a program into memory can be used. The preshader program desirably begins executing at any time after the constant data becomes available, but desirably before the execution of the GPU program. Because the GPU program depends on the intermediate values generated by the preshader program, the intermediate values are desirably made available before the GPU program begins to execute.
As described with respect to
At 340, the constants expressions comprised in the preshader program are evaluated and the results are desirably posted to the associated reference. As described with respect to
At 350, each instance of the GPU program executing at the GPU desirably accesses the stored intermediate values. As each instance of the GPU program executing at the GPU processes the stream data, a reference is desirably encountered in the GPU program code. This reference desirably corresponds to one of the constant expressions that was removed from the shader. This constant expression was desirably evaluated at the CPU resulting in an intermediate value that was desirably saved to the location specified by the reference. Each instance of the GPU program executing at the GPU desirably retrieves the stored intermediate values and desirably continues to process the stream data using the retrieved intermediate values.
The analyzer 410 receives a shader program and generates a preshader program from the received shader program. The shader program is used to modify or affect the appearance of a 3-D mesh displayed by a GPU. An instance of the shader program is executed by the GPU for each vertex or pixel comprising the displayed 3-D mesh. The shader may comprise several constant expressions. The analyzer 410 desirably analyzes the shader program for constant expressions and replaces each constant expression with a reference to a unique memory address. The analyzer 410 desirably generates the preshader in two parts or programs. A first part comprises the original shader program with the constant expressions removed and replaced with memory references and is desirably executable on the GPU. The second part comprises the removed constant expressions and is executable by the GPU virtual machine 420, for example. However, the second part may also be made directly executable directly on the CPU. The analyzer 410 can be any implemented using any suitable system, method or technique known in the art for identifying and removing constant expressions. The analyzer 410 can be implemented using software, hardware, or a combination of both.
The GPU virtual machine 420 provides an execution environment for the second part of the preshader. By executing each constant expression only once at the GPU virtual machine 420 versus multiple times for each instance of the shader, considerable GPU cycles are saved for each instance of the preshader executing at the GPU. The GPU virtual machine 420 can be any implemented using any suitable system, method or technique known in the art for emulating GPU hardware on a general purpose CPU. The GPU virtual machine 420 can be implemented using software, hardware, or a combination of both.
The infrastructure 430 desirably connects each instance of the first part of the preshader executing at the GPU with the preshader program of the preshader executing at the GPU virtual machine 420, or directly on the CPU, for example. While the preshader technique moves the execution of constant expression from the GPU to the CPU, the instances of the preshader executing on the GPU still depend on the intermediate values generated by the GPU virtual machine 420, or generated directly by the CPU, for example. The infrastructure 430 desirably allows the instances of the preshader executing at the GPU to communicate with and receive intermediate values from the instance of the preshader executing at the CPU. The infrastructure 430 can be implemented using software, hardware, or a combination of both.
Exemplary Computing Environment
The invention is operational with numerous other general purpose or special purpose computing system environments or configurations. Examples of well known computing systems, environments, and/or configurations that may be suitable for use with the invention include, but are not limited to, personal computers, server computers, hand-held or laptop devices, multiprocessor systems, microprocessor-based systems, set top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, distributed computing environments that include any of the above systems or devices, and the like.
The invention may be described in the general context of computer-executable instructions, such as program modules, being executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform particular tasks or implement particular abstract data types. The invention may also be practiced in distributed computing environments where tasks are performed by remote processing devices that are linked through a communications network or other data transmission medium. In a distributed computing environment, program modules and other data may be located in both local and remote computer storage media including memory storage devices.
With reference to
Computer 510 typically includes a variety of computer readable media. Computer readable media can be any available media that can be accessed by computer 510 and includes both volatile and non-volatile media, removable and non-removable media. By way of example, and not limitation, computer readable media may comprise computer storage media and communication media. Computer storage media includes both volatile and non-volatile, removable and non-removable media implemented in any method or technology for storage of information such as computer readable instructions, data structures, program modules or other data. Computer storage media includes, but is not limited to, RAM, ROM, EEPROM, flash memory or other memory technology, CD-ROM, digital versatile disks (DVD) or other optical disk storage, magnetic cassettes, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other medium which can be used to store the desired information and which can accessed by computer 510. Communication media typically embodies computer readable instructions, data structures, program modules or other data in a modulated data signal such as a carrier wave or other transport mechanism and includes any information delivery media. The term “modulated data signal” means a signal that has one or more of its characteristics set or changed in such a manner as to encode information in the signal. By way of example, and not limitation, communication media includes wired media such as a wired network or direct-wired connection, and wireless media such as acoustic, RF, infrared and other wireless media. Combinations of any of the above should also be included within the scope of computer readable media.
The system memory 530 includes computer storage media in the form of volatile and/or non-volatile memory such as ROM 531 and RAM 532. A basic input/output system 533 (BIOS), containing the basic routines that help to transfer information between elements within computer 510, such as during start-up, is typically stored in ROM 531. RAM 532 typically contains data and/or program modules that are immediately accessible to and/or presently being operated on by processing unit 520. By way of example, and not limitation,
The computer 510 may also include other removable/non-removable, volatile/non-volatile computer storage media. By way of example only,
The drives and their associated computer storage media provide storage of computer readable instructions, data structures, program modules and other data for the computer 510. In
The computer 510 may operate in a networked environment using logical connections to one or more remote computers, such as a remote computer 580. The remote computer 580 may be a personal computer, a server, a router, a network PC, a peer device or other common network node, and typically includes many or all of the elements described above relative to the computer 510, although only a memory storage device 581 has been illustrated in
When used in a LAN networking environment, the computer 510 is connected to the LAN 571 through a network interface or adapter 570. When used in a WAN networking environment, the computer 510 typically includes a modem 572 or other means for establishing communications over the WAN 573, such as the internet. The modem 572, which may be internal or external, may be connected to the system bus 521 via the user input interface 560, or other appropriate mechanism. In a networked environment, program modules depicted relative to the computer 510, or portions thereof, may be stored in the remote memory storage device. By way of example, and not limitation,
As mentioned above, while exemplary embodiments of the present invention have been described in connection with various computing devices, the underlying concepts may be applied to any computing device or system.
The various techniques described herein may be implemented in connection with hardware or software or, where appropriate, with a combination of both. Thus, the methods and apparatus of the present invention, or certain aspects or portions thereof, may take the form of program code (i.e., instructions) embodied in tangible media, such as floppy diskettes, CD-ROMs, hard drives, or any other machine-readable storage medium, wherein, when the program code is loaded into and executed by a machine, such as a computer, the machine becomes an apparatus for practicing the invention. In the case of program code execution on programmable computers, the computing device will generally include a processor, a storage medium readable by the processor (including volatile and non-volatile memory and/or storage elements), at least one input device, and at least one output device. The program(s) can be implemented in assembly or machine language, if desired. In any case, the language may be a compiled or interpreted language, and combined with hardware implementations.
The methods and apparatus of the present invention may also be practiced via communications embodied in the form of program code that is transmitted over some transmission medium, such as over electrical wiring or cabling, through fiber optics, or via any other form of transmission, wherein, when the program code is received and loaded into and executed by a machine, such as an EPROM, a gate array, a programmable logic device (PLD), a client computer, or the like, the machine becomes an apparatus for practicing the invention. When implemented on a general-purpose processor, the program code combines with the processor to provide a unique apparatus that operates to invoke the functionality of the present invention. Additionally, any storage techniques used in connection with the present invention may invariably be a combination of hardware and software.
While the present invention has been described in connection with the preferred embodiments of the various figures, it is to be understood that other similar embodiments may be used or modifications and additions may be made to the described embodiments for performing the same function of the present invention without deviating therefrom. Therefore, the present invention should not be limited to any single embodiment, but rather should be construed in breadth and scope in accordance with the appended claims.
Peeper, Craig C., Aronson, David F., Baker, Daniel K., McQuade, Loren
Patent | Priority | Assignee | Title |
8941670, | Jan 17 2012 | Microsoft Technology Licensing, LLC | Para-virtualized high-performance computing and GDI acceleration |
Patent | Priority | Assignee | Title |
20060152509, |
Executed on | Assignor | Assignee | Conveyance | Frame | Reel | Doc |
Jul 08 2008 | Microsoft Corporation | (assignment on the face of the patent) | / | |||
Oct 14 2014 | Microsoft Corporation | Microsoft Technology Licensing, LLC | ASSIGNMENT OF ASSIGNORS INTEREST SEE DOCUMENT FOR DETAILS | 034564 | /0001 |
Date | Maintenance Fee Events |
Mar 18 2013 | M1551: Payment of Maintenance Fee, 4th Year, Large Entity. |
Mar 29 2013 | ASPN: Payor Number Assigned. |
May 04 2017 | M1552: Payment of Maintenance Fee, 8th Year, Large Entity. |
Jul 05 2021 | REM: Maintenance Fee Reminder Mailed. |
Dec 20 2021 | EXP: Patent Expired for Failure to Pay Maintenance Fees. |
Date | Maintenance Schedule |
Nov 17 2012 | 4 years fee payment window open |
May 17 2013 | 6 months grace period start (w surcharge) |
Nov 17 2013 | patent expiry (for year 4) |
Nov 17 2015 | 2 years to revive unintentionally abandoned end. (for year 4) |
Nov 17 2016 | 8 years fee payment window open |
May 17 2017 | 6 months grace period start (w surcharge) |
Nov 17 2017 | patent expiry (for year 8) |
Nov 17 2019 | 2 years to revive unintentionally abandoned end. (for year 8) |
Nov 17 2020 | 12 years fee payment window open |
May 17 2021 | 6 months grace period start (w surcharge) |
Nov 17 2021 | patent expiry (for year 12) |
Nov 17 2023 | 2 years to revive unintentionally abandoned end. (for year 12) |