Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
HLSL Development Cookbook
HLSL Development Cookbook

HLSL Development Cookbook: Implement stunning 3D rendering techniques using the power of HLSL and DirectX 11

eBook
$9.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

HLSL Development Cookbook

Chapter 2. Deferred Shading

In this chapter we will cover:

  • GBuffer generation

  • GBuffer unpacking

  • Directional light

  • Point light

  • Capsule light

  • Spot light

Introduction


In the previous chapter, we mentioned that forward lighting has performance limitations. If you recall the basic algorithm for forward lighting, we potentially need to draw every mesh once for each light source. For a scene with N light sources and M meshes, we may need N times M draw calls. This means that every additional mesh is going to add up to N new draw calls and each new light source will add up to M new draw calls. Those numbers add up very quickly and pose a major limitation on the amount of elements you can render in an interactive frame rate.

Over the years, as graphic cards' memory size increased and GPUs commonly supported rendering to multiple render targets (MRT), a couple of solutions to the performance problems were developed. At their core, all the solutions work on the same principle – separate the scene rendering from the lighting. This means that when using those new methods, a scene with M meshes and N light sources is going to only take M + N draw calls...

GBuffer generation


Unlike forward lighting, where we just started rendering the scene with the lights, deferred shading requires the GBuffer to be generated before any light related calculation can take place.

Choosing the structure of the GBuffer is a very important decision. The bare minimum you can get away with is storing depth, base color, normal, specular power, and specular intensity. For this configuration, you will need between three to four render targets.

Storing depth and base color is straight forward. Use a D24S8 render target for the depth and stencil. For base color we will be using an A8R8G8B8 render target as the base color values are usually sampled from 8 bit per-pixel texture. The alpha channel of the color target can be used to store the specular intensity. Storing normals on the other hand is not that simple and requires some planning. To keep this section relatively short, we are only going to cover three storage methods that have been used in popular AAA games.

Tip

Note...

GBuffer unpacking


Before any light related work can be accomplished, all the data stored in the GBuffer has to be unpacked back into a format that we can use with the lighting code presented in the forward lighting recipes.

You may have noticed one thing missing from our GBuffer and that is the world space position needed for lighting. Although we could store per-pixel world positions in our GBuffer, those positions are implicitly stored already in the depth buffer as non-linear depth. Together with the projection parameters, we are going to reconstruct the linear depth of each pixel (world distance from the camera) as part of the GBuffer unpacking process. The linear depth will later be used together with the inversed view matrix to reconstruct the world position of each pixel.

Getting ready

While unpacking the GBuffer, you always want to use point sampling when fetching data from the GBuffer texture views. You will need to set one point sampler and the four shader resource views for the pixel...

Directional light


Now that we know how to decode the GBuffer, we can use it to calculate directional lighting. Unlike forward rendering, where we rendered the scene in order to trigger the pixel shader and do the light calculations, the whole point using a deferred approach is to avoid rendering the scene while doing the lighting. In order to trigger the pixel shader we will be rendering a mesh with a volume that encapsulates the GBuffer pixels affected by the light source. Directional light affects all the pixels in the scene, so we can render a full screen quad to trigger the pixel shader for every pixel in the GBuffer.

Getting ready…

Back in the old days of Direct3D 9 you would need to prepare a vertex buffer to render a full screen quad. Fortunately, this is a thing of the past. With DirectX11 (as well as DirectX10) it is very easy to render a full screen quad with null buffers and sort out the values in the vertex shader. You will still need a constant buffer to store the same light information...

Point light


Compared to the directional light, point lights may only cover parts of the screen. In addition, it may be partially or fully occluded by the scene as viewed when generating the GBuffer. Fortunately, all we need to represent the light source volume is to render the front of a sphere located at the point light's center position and scale to its range.

Representing the light source as its volume makes sense when you think about the portion of the scene encapsulated by this volume. In the point lights case, the portion of the scene that gets lit is going to be inside this sphere volume. Rendering the volume will trigger the pixel shader for all pixels that are inside the volume, but could also trigger the pixel shader for pixels that are in front or behind the volume. Those pixels that are outside the volume will either get culled by the depth test or get fully attenuated so they won't affect the final image.

In the DirectX9 days you would normally use a pre-loaded mesh to represent...

Capsule light


Capsule light volume is made of a sphere divided into two and extruded along the capsules segment to form the cylindrical part of the capsule. As with the point light volume, we will use the tessellator to generate the capsule volume, only this time we will expend two control points instead of one. Each one of these points will eventually get turned into a half sphere attached to half of the capsules cylindrical part. The two capsule halves will connect at the center of the capsule.

Getting ready…

Our capsule will be scaled by two values, a range and a segment length. Those are the exact same values we used for the forward capsule light. Unfortunately, we can't use a matrix to scale the capsule because we need to scale the segment and the range separately. Therefore, we will set the capsule lights range, segment length, and a transformation to projected space to the Domain shader. The matrix will combine the capsule rotation and translation with the view and projection matrix...

Spot light


Spot light volume is represented by a cone with a rounded base. We can use same technique used for generating the capsule light half capsule volume and shape the result as the spot lights cone. As this requires only half of the capsule, we will only need to draw a single vertex.

Getting ready

As with the point and capsule lights, we will need all the GBuffer related values along with the pixel shader constants we used for the light properties in the forward spot light recipe.

Unlike the capsule light volume, the spot light volume can be scaled by a matrix, so we won't need to pass any scaling parameters to the Domain shader. The following illustration shows how the pretransform cone will look like:

We can use the following formula to calculate the matrix X and Y scale value:

Where α is the cone's outer angle LightRange is the spot light's range. For the Z component we just use the light range value.

The light cone tip will be at coordinate origin, so the rotation and translation parts...

Left arrow icon Right arrow icon

Key benefits

  • Discover powerful rendering techniques utilizing HLSL and DirectX 11
  • Augment the visual impact of your projects whilst taking full advantage of the latest GPUs
  • Experience the practical side of 3D rendering with a comprehensive set of excellent demonstrations

Description

3D graphics are becoming increasingly more realistic and sophisticated as the power of modern hardware improves. The High Level Shader Language (HLSL) allows you to harness the power of shaders within DirectX 11, so that you can push the boundaries of 3D rendering like never before.HLSL Development Cookbook will provide you with a series of essential recipes to help you make the most out of different rendering techniques used within games and simulations using the DirectX 11 API.This book is specifically designed to help build your understanding via practical example. This essential Cookbook has coverage ranging from industry-standard lighting techniques to more specialist post-processing implementations such as bloom and tone mapping. Explained in a clear yet concise manner, each recipe is also accompanied by superb examples with full documentation so that you can harness the power of HLSL for your own individual requirements.

Who is this book for?

If you have some basic Direct3D knowledge and want to give your work some additional visual impact by utilizing advanced rendering techniques, then this book is for you. It is also ideal for those seeking to make the transition from DirectX 9 to DirectX 11, and those who want to implement powerful shaders with the High Level Shader Language (HLSL).

What you will learn

  • Learn about different light sources in both forward and deferred implementations
  • Take your dynamic lighting to the next level with shadow map support
  • Discover a range of post processing effects like bloom and tone mapping
  • Implement 3D elements such as decals, rain, and more
  • Harness various screen space techniques that help augment the visual quality of your work

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 13, 2013
Length: 224 pages
Edition : 1st
Language : English
ISBN-13 : 9781849694216
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jun 13, 2013
Length: 224 pages
Edition : 1st
Language : English
ISBN-13 : 9781849694216
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 154.97
HLSL Development Cookbook
$54.99
GLSL Essentials
$38.99
Direct3D Rendering Cookbook
$60.99
Total $ 154.97 Stars icon
Banner background image

Table of Contents

6 Chapters
Forward Lighting Chevron down icon Chevron up icon
Deferred Shading Chevron down icon Chevron up icon
Shadow Mapping Chevron down icon Chevron up icon
Postprocessing Chevron down icon Chevron up icon
Screen Space Effects Chevron down icon Chevron up icon
Environment Effects Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(14 Ratings)
5 star 35.7%
4 star 28.6%
3 star 21.4%
2 star 7.1%
1 star 7.1%
Filter icon Filter
Top Reviews

Filter reviews by




The Saint Aug 18, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
HLSL Development Cookbook In the early 1990's I was responsible for leading the effort to create the DirectX API's and Direct3D at Microsoft. As we all know the technology was tremendously successful and widely adopted, as an unfortunate consequence of that success I was promoted and condemned to many years in management. 18 years later, I finally have the time and opportunity to get back to programming and I find myself in the embarrassing position of having to re-learn the technology I helped to pioneer after many generations of advancement since I last worked with it personally. I've been reading a lot of technical books lately to re-acquaint myself with the modern Direct3D API and I found this book REALLY helpful. Since the 1990's 3D game engine design has evolved tremendously and become an incredibly sophisticated business. I'm very fortunate to have a great foundation to build on but I'm constantly amazed at how advanced and complex the technology has become since my early days in the field. I do not envy the difficulties that new engineers to the field face when trying to master such amazingly sophisticated technology for the first time starting from scratch.I've read several great introductory books on DirectX 11 programming and some great books that explain the overall architecture of the modern Direct3D API's but I found myself frequently bewildered by how all of these powerful features and API's are used in concert to build complete modern game engines until I ran across the HLSL Development Cookbook. This book really put it all together for me in a practical way. I already know C/C++, I already know how to program in Windows and I'm already familiar with DirectX architecture and API's. I just needed to understand how an advanced 3D game engine is constructed using these tools which, this book did a fantastic job of. It is very clearly and concisely written by somebody who obviously had years of experience in the area and understood all of the nuances and shortcomings of various approaches to 3D that have led them to a clear and focused understanding of how to balance graphic realism with performance and coding productivity.The first thing I do when I buy these books is install the sample code and see if it builds and works, because if the sample code doesn't work then the book is often useless to me. I usually end up spending more time and learn more from working with code samples than reading the actual text. In this case, the sample code installed and built correctly on my machine immediately in all build modes using Visual Studio 2010 which it was authored for. When I subsequently opened the samples in Visual Studio 2012 they migrated flawlessly. The samples are great, they are a complete logical progression of all of the fundamental "recipes" one needs to build a reasonably advanced modern 3D game engine. The text does a great job of walking the reader through the examples and explaining WHY they were written that way, which is what I found that I needed most. Here's an example of a paragraph from the book that did wonders to shed light on why a particular problem was solved a certain way in Direct3D when it might seem that there are many reasonable approaches that SHOULD work."Rendering the bokeh highlights uses an interesting feature introduced in DirectX10 which lets us issue a Draw call without knowing how many vertices or primitives we want to draw. DirectX10 lets us issue this kind of Draw call for a vertex buffer that got streamed out and DirectX11 added support for buffers which we use in this recipe. This allows us to issue a Draw call with an amount of primitives equal to the amount of highlights we added to the buffer."Clearly the Direct3D API had evolved a lot over the years to cope with progressively more advanced demands and new hardware capabilities. Somebody NEW to the API may not understand the need for certain features without a context provided by a seasoned Direct3D veteran who had first-hand experience with why the API had evolved a certain way. It helped a lot.This book is not an introduction to DirectX, Windows programming or C/C++, it assumes that the reader already knows or is familiar with that stuff. It gets straight down to showing you exactly how it's done. The samples are high quality and did a great job of helping me put it all together. It was a great addition to my library.
Amazon Verified review Amazon
Chang L. Aug 01, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
HLSL Development Cookbook is for professional game developers in advanced shader programming. Beginning game developers can also learn the basic physics in shader from the book. Developers can apply the techniques and codes described in the book to their real games.To design a HLSL shader, a developer needs to understand math, physics, and HLSL syntax, type, as well as function. To understand a HLSL algorithm in physics, a developer needs to understand the light and its effects.This book was well organized. Author paid attention for readers to understand the "know-how" of HLSL shader. One of the most helpful section of the book is "How it works ...", in which the physics and algorithms of HLSL shader were described. To better understand HLSL codes, readers can read this section first, then read section "How to do it ...".Chapter 1 to 3 are about pre-processing. Chapter 4 and 5 are about post-processing. Chapter 6 is about environment processing. Chapters of pre-processing are focused on lighting from basic forward lighting to shadows.In addition to the introduction of ambient, directional, point, spot, and capsule light, the first chapter also introduced projected texture for point and spot light. That is a technique to represent complex light sources by intensity/color patterns. Similar to per-pixel lighting technique, texture lighting may generate wonderful unrealistic lighting effects. HLSL shaders are powerful expression of these effects.Deferred Shading introduced in Chapter 2 is an optimized method for computing of multiple light sources and objects. This Chapter introduced GBuffer method developed in DirectX 11. Hull, Domain, and Geometry shaders in DirectX 11 are also explained in Chapter 2.By adding attenuation in Blinn-Phong light model, shadow mapping technique was introduced in Chapter 3. Author discussed PCF, a shadow-casting technique that is based on projected textures in Chapter 1.Postprocessing is an interesting topic in Chapter 4. HDR, Bloom, DOF and Bokeh have been discussed. One of the most interested solution of HDR is to apply 16-bit float format for image channel and tone mapping by compute shader. The detail discussion of downscale in DirectX 11 is very helpful for HDR programming with HLSL shader. HDR technique has been widely used in modern game programming and will be helpful for new generation console - XBOX One - programming.Chapter 5 discussed screen space ambient, occlusion, sun rays, reflections, and lens flare. An interesting topic is to simulate the lens flare of camera lens that shows a series of colorful shapes around the scene. Movie makers have applied lens flare to generate artistic effects.Chapter 6 discussed dynamic mesh generation, particle system, and depth-based rendering. Particle system can be used to simulate fire and rain. UAV (Unordered Access View) buffer of DirectX 11 was used for buffer read and write in rain simulation.Sample codes of the book are easy to compile and build under Visual Studio 2010/2012 for Windows Desktop. Developers can understand the C++ codes of DirectX 11 and HLSL codes from these samples.
Amazon Verified review Amazon
John Mabbott Sep 17, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Given the value that this book provides at the price, I think many DX11 developers should consider it a must-buy. Doron has leveraged his experience working on titles such as GTA V and Max Payne 3 to publish a comprehensive set of lighting, shadowing and post-processing effects that can give your game a professional look.I am most of the way through incorporating the recipes from the book into my engine, and now have robust forward and deferred rendering paths, complete with shadowing and some effects. I would say that I wouldn't have got here without the firm foundation provided by Frank Luna's excellent DX11 book, but I'd say that Doron's work have elevated the look of my game from reasonable to something that looks passably good.There are a few others things to mention that drove my rating of 5 stars:1) All source compiled first time without errors on the latest free MS compiler (Visual Studio Express 2013 for Desktop)2) Pre-compiled binaries were included with the source distribution to allow for samples to be run immediately.3) I emailed the author a question about soft shadows for omnidirectional point lights and got a very prompt and helpful answer that pointed me in the right direction.4) The author makes full use of the latest capabilities of DX11 - hull, domain and compute shaders are expertly applied to take advantage of the latest hardware capability that DX11 exposes.As for cons, I noted that the other reviews mention spelling and grammatical errors in the book. There are a number of these, however this did not detract from my ability to determine the author's intent at all, in other words it was obvious what was trying to be communicated where these mistakes were made. This certainly wasn't enough in my mind to drop the rating by a star, and the value provided by the techniques exhibited is so high.
Amazon Verified review Amazon
Michael Schuld Sep 16, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book tends to be pretty source heavy (HLSL source that is) with very little C++ or DirectX code being shown, so it is definitely recommended to at least have a basic grasp of setting up a simple DirectX 11 application works before tackling the content. On the bright side though, all of the HLSL source is explained in great detail including many diagrams to help get across more of the complicated concepts that aren't necessarily obvious just from reading the code itself. One thing I really got from these descriptions and diagrams was a good explanation of how the half angle optimization for Blinn Specular works. I always knew how to write the code for this technique but never really understood the real reasoning behind why it works until now. Each "recipe" builds on top of the last one, so going through the whole chapter one sample at a time is a great way to build your knowledge of all the standard lighting types and shaders normally used.Once you have a firm grasp of the different shading models and techniques for each light type, Chapter 2 will take you into the much more complicated but also more high performance world of Deferred Shading. Using the new Hull and Domain shader functionality provided by DirectX 11, much of the hard work of optimizing the steps of Deferred Shading are easily handled. All of the complexities of basic Hull and Domain shaders are explained while developing the model, and each light type from the first chapter is then implemented in a single flexible shader that can eventually handle multiple lights (up to 4) in a single rendering pass.After Deferred Shading, the much more advanced and complex techniques of shadow maps, post processing, screen space effects like ambient occlusion and "god rays", and environmental effects like fog and decals are discussed in depth. There is too much content to go in to in a simple review, so why don't you go pick up a copy and see for yourself how great the recipes in this title are and how much you can learn from it! I know I'll be using the techniques in future development.
Amazon Verified review Amazon
Patrik Van Ostaeyen Feb 05, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great to-the-point book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.