Home > Articles > Programming > Graphic Programming

Real-Time 3D Rendering with DirectX and HLSL: Hello, Shaders!

  • Print
  • + Share This
This chapter is from the book

In this chapter, you write your first shaders. We introduce HLSL syntax, the FX file format, data structures, and more. By the end of this chapter, you’ll have a base from which to launch the rest of your exploration into graphics programming.

Your First Shader

You might recognize the canonical programming example “Hello, World!” as a first program written in a new language and whose output is this simple line of text. We follow this time-honored tradition with the shader equivalent “Hello, Shaders!”—this time, your output is an object rendered in a solid color.

To begin, launch NVIDIA FX Composer and create a new project. Open the Assets panel, right-click on the Materials icon, and choose Add Material from New Effect. Then choose HLSL FX from the Add Effect dialog box (see Figure 4.1).

Figure 4.1

Figure 4.1 NVIDIA FX Composer Add Effect dialog box.

In the next dialog box, select the Empty template and name your effect HelloShaders.fx (see Figure 4.2).

Figure 4.2

Figure 4.2 NVIDIA FX Composer Select HLSL FX Template dialog box.

Click Finish in the final dialog box of the Effect Wizard to complete the process. If all went well, you should see your HelloShaders.fx file displayed in the Editor panel and associated HelloShaders and HelloShaders_Material objects listed in the Assets panel. Notice that the Empty effect template isn’t empty after all—NVIDIA FX Composer has stubbed out a bit of code for you. This code is actually close to what you want in your first shader, but it’s written for DirectX 9, so delete this code and replace it with the contents of Listing 4.1. Then we walk through this code step by step.

Listing 4.1 HelloShaders.fx

cbuffer CBufferPerObject

{

    float4x4 WorldViewProjection : WORLDVIEWPROJECTION;

}



RasterizerState DisableCulling

{

    CullMode = NONE;

};



float4 vertex_shader(float3 objectPosition : POSITION) : SV_Position

{

    return mul(float4(objectPosition, 1), WorldViewProjection);

}



float4 pixel_shader() : SV_Target

{

    return float4(1, 0, 0, 1);

}



technique10 main10

{

    pass p0

    {

        SetVertexShader(CompileShader(vs_4_0, vertex_shader()));

        SetGeometryShader(NULL);

        SetPixelShader(CompileShader(ps_4_0, pixel_shader()));



        SetRasterizerState(DisableCulling);

    }

}

Effect Files

Direct3D pipeline stages can be programmed through separately compiled shaders. For instance, you can house a vertex shader in one file (commonly with the extension .hlsl) and a pixel shader in a different file. Under this configuration, each file must contain exactly one shader. By contrast, HLSL Effect files enable you to combine multiple shaders, support functions, and render states into a single file. This is the file format we use throughout this text, and Listing 4.1 uses it.

Constant Buffers

At the top of your HelloShaders.fx file, you find a block of code starting with cbuffer. This denotes a constant buffer, whose purpose is to organize one or more shader constants. A shader constant is input the CPU sends to a shader, which remains constant for all the primitives processed by a single draw call. Put another way, cbuffers hold variables, “constant variables.” They’re constant from the perspective of the GPU while processing the primitives of a draw call yet variable from the perspective of the CPU from one draw call to the next.

In your HelloShaders.fx file, you have just one cbuffer containing only one shader constant, WorldViewProjection, of type float4×4. This is a C-style variable declaration in which the data type is a 4×4 matrix of single-precision floating-point values. This particular variable (WorldViewProjection) represents the concatenated World-View-Projection matrix specific to each object. Recall from Chapter 2, “A 3D/Math Primer,” that this matrix transforms your vertices from object space, to world space, to view space, to homogeneous space, in a single transformation. You could pass the World, View, and Projection matrices into the effect separately and then perform three different transforms to produce the same result. But unless you have a specific reason to do so, sending less data as input and performing fewer shader instructions is the better option.

Note the text WORLDVIEWPROJECTION following the colon in the variable declaration. This is known as a semantic and is a hint to the CPU-side application about the intended use of the variable. Semantics relieve the application developer from a priori knowledge of the names of shader constants. In this example, you could have named your float4×4 variable WVP or WorldViewProj without any impact to the CPU side because it can access the variable through the WORLDVIEWPROJECTION semantic instead of through its name. A variety of common semantics exist, all of which are optional for shader constants. However, in the context of NVIDIA FX Composer, the WORLDVIEWPROJECTION semantic is not optional; it must be associated with a shader constant for your effect to receive updates to the concatenated WVP matrix each frame.

Render States

Shaders can’t define the behaviors of the nonprogrammable stages of the Direct3D pipeline, but you can customize them through render state objects. For example, the rasterizer stage is customized through a RasterizerState object. A variety of rasterizer state options exist, although I defer them to future chapters. For now, note the RasterizerState object DisableCulling (see Listing 4.2).

Listing 4.2 RasterizerState declaration from HelloShaders.fx

RasterizerState DisableCulling

{

    CullMode = NONE;

};

We briefly discussed vertex winding order and backface culling in Chapter 3, “Tools of the Trade.” By default, DirectX considers vertices presented counter-clockwise (with respect to the camera) to be back-facing and does not draw them. However, the default models included with NVIDIA FX Composer (the Sphere, Teapot, Torus, and Plane) are wound in the opposite direction. Without modifying or disabling the culling mode, Direct3D would cull what we would consider front-facing triangles. Therefore, for your work within NVIDIA FX Composer, just disable culling by specifying CullMode = NONE.

The Vertex Shader

The next HelloShaders code to analyze is the vertex shader, reproduced in Listing 4.3.

Listing 4.3 The vertex shader from HelloShaders.fx

float4 vertex_shader(float3 objectPosition : POSITION) : SV_Position

{

    return mul(float4(objectPosition, 1), WorldViewProjection);

}

This code resembles a C-style function, but with some key differences. First, note the work the vertex shader is accomplishing. Each vertex comes into the shader in object space, and the WorldViewProjection matrix transforms it into homogeneous clip space. In general, this is the least amount of work a vertex shader performs.

The input into your vertex shader is a float3, an HLSL data type for storing three single-precision floating-point values—it’s named objectPosition to denote its coordinate space. Notice the POSITION semantic associated with the objectPosition parameter. It indicates that the variable is holding a vertex position. This is conceptually similar to the semantics used for shader constants, to convey the intended use of the parameter. However, semantics are also used to link shader inputs and outputs between shader stages (for example, between the input-assembler stage and the vertex shader stage) and are therefore required for such variables. At a minimum, the vertex shader must accept a variable with the POSITION semantic and must return a variable with the SV_Position semantic.

Within the body of your vertex shader, you’re calling the HLSL intrinsic function mul. This performs a matrix multiplication between the two arguments. If the first argument is a vector, it’s treated as a row vector (with a row-major matrix as the second argument). Conversely, if the first argument is a matrix, it’s treated as a column major matrix, with a column-vector as the second argument. We use row-major matrices for most of our transformations, so we use the form mul(vector, matrix).

Notice that, for the first argument of the mul function, you are constructing a float4 out of the objectPosition (a float3) and the number 1. This is required because the number of columns in the vector must match the number of rows in the matrix. Because the vector you’re transforming is a position, you hard-code the fourth float (the w member) to 1. Had the vector represented a direction, the w component would be set to 0.

The Pixel Shader

As with the vertex shader, the HelloShader pixel shader is just one line of code (see Listing 4.4).

Listing 4.4 The pixel shader from HelloShaders.fx

float4 pixel_shader() : SV_Target

{

    return float4(1, 0, 0, 1);

}

The return value of this shader is a float4 and is assigned the SV_Target semantic. This indicates that the output will be stored in the render target bound to the output-merger stage. Typically, that render target is a texture that is mapped to the screen and is known as the back buffer. This name comes from a technique called double buffering, in which two buffers are employed to reduce tearing, and other artifacts, produced when pixels from two (or more) frames are displayed simultaneously. Instead, all output is rendered to a back buffer while the actual video device displays a front buffer. When rendering is complete, the two buffers are swapped so that the newly rendered frame displays. Swapping is commonly done to coincide with the refresh cycle of the monitor—again, to avoid artifacts.

The output of your pixel shader is a 32-bit color, with 8-bit channels for Red, Green, Blue, and Alpha (RGBA). All values are supplied in floating-point format, where the range [0.0, 1.0] maps to integer range [0, 255]. In this example, you’re supplying the value 1 to the red channel, meaning that every pixel rendered will be solid red. You are not employing color blending, so the alpha channel has no impact. If you were using color blending, an alpha value of 1 would indicate a fully opaque pixel. We discuss color blending in more detail in Chapter 8, “Gleaming the Cube.”

Techniques

The last section of the HelloShaders effect is the technique that brings the pieces together (see Listing 4.5).

Listing 4.5 The technique from HelloShaders.fx

technique10 main10

{

    pass p0

    {

        SetVertexShader(CompileShader(vs_4_0, vertex_shader()));

        SetGeometryShader(NULL);

        SetPixelShader(CompileShader(ps_4_0, pixel_shader()));



        SetRasterizerState(DisableCulling);

    }

}

A technique implements a specific rendering sequence through a set of effect passes. Each pass sets render states and associates your shaders with their corresponding pipeline stages. In the HelloShaders example, you have just one technique (named main10) with just one pass (named p0). However, effects can contain any number of techniques, and each technique can contain any number of passes. For now, all your techniques contain a single pass. We discuss techniques with multiple passes in Part IV, “Intermediate-Level Rendering Topics.”

Note the keyword technique10 in this example. This keyword denotes a Direct3D 10 technique, versus DirectX 9 techniques, which have no version suffix. Direct3D 11 techniques use the keyword technique11. Unfortunately, the current version of NVIDIA FX Composer does not support Direct3D 11. But you won’t be using any Direct3D 11–specific features at the beginning of your exploration of shader authoring, so this isn’t a show stopper. We start using Direct3D 11 techniques in Part III, “Rendering with DirectX.”

Also notice the arguments vs_4_0 and ps_4_0 within the SetVertexShader and SetPixelShader statements. These values identify the shader profiles to use when compiling the shaders specified in the second arguments of the CompileShader calls. Shader profiles are analogous to shader models, which define the capabilities of the graphics system that are required to support the corresponding shaders. As of this writing, there have been five major (and several minor) shader model revisions; the latest is shader model 5. Each shader model has extended the functionality of the previous revision in a variety of ways. Generally, however, the potential sophistication of shaders has increased with each new shader model. Direct3D 10 introduced shader model 4, which we use for all Direct3D 10 techniques. Shader model 5 was introduced with Direct3D 11, and we use that shader model for all Direct3D 11 techniques.

Hello, Shaders! Output

You’re now ready to visualize the output of the HelloShaders effect. To do so, you first need to build your effect through the Build, Rebuild All or Build, Compile HelloShaders.fx menu commands. Alternately, you can use the shortcut keys F6 (Rebuild All) or Ctrl+F7 (Compile Selected Effect). Be sure you do this after any changes you make to your code.

Next, ensure that you are using the Direct3D 10 rendering API by choosing it from the drop-down menu in the main toolbar (it’s the right-most toolbar item, and it likely defaults to Direct3D 9). Now open the Render panel within NVIDIA FX Composer. Its default placement is in the lower-right corner. Create a sphere in the Render panel by choosing Create, Sphere from the main menu or by clicking the Sphere icon in the toolbar. Finally, drag and drop your HelloShaders_Material from either the Materials panel or the Assets panel onto the sphere in the Render panel. You should see an image similar to Figure 4.3.

Figure 4.3

Figure 4.3 HellShaders.fx applied to a sphere in the NVIDIA FX Composer Render panel.

This might be a bit anti-climactic, given the effort to get here, but you’ve actually accomplished quite a lot! Take a few minutes to experiment with the output of this shader. Modify the RGB channels within the pixel shader to get a feel for what’s happening.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020