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
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
HLSL Development Cookbook
HLSL Development Cookbook

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

eBook
€28.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781849694209
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 116.97
Direct3D Rendering Cookbook
€45.99
GLSL Essentials
€28.99
HLSL Development Cookbook
€41.99
Total 116.97 Stars icon

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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.