Preface

Pixel Shaders are one of the more powerful graphic tools available for XAML programmers. I first encountered them in the Windows Presentation Foundation 3.5 SP1 release and was completely smitten. It didn’t take long to learn that I could create custom shaders, commonly called Effects in WPF, and add them to my projects. Excited by the prospect, I started my research and soon learned that shaders are written in a language called High Level Shader Language (HLSL). I like programming challenges and learning new technologies, so I set off to learn more about custom shaders. I downloaded the DirectX SDK, opened the documentation, and started digging through the pages.

It was at this point that I ran into the wall of uncertainty.

Shader development is very different from working in XAML element trees, and bears little resemblance to traditional .NET programming. The overall mindset is different: the language looks similar to C but uses a quaint semantic syntax and the documentation is sparse and confusing. I found myself writing shader code and hesitantly testing the output, never quite sure what would show up onscreen.

I learned later that I’m not alone in feeling unsure when writing HLSL algorithms. I talked to countless Silverlight developers who echoed my feelings of doubt when faced with the prospect of writing custom shaders. I was reassured to learn, while attending a daylong game workshop, that many game development shops have dedicated shader developers who specialize in mastering this arcane craft. The reason is clear: it takes a different mindset to understand shaders and some developers are better suited to the skills needed to write successful effects.

As a result, I started designing tools and tutorials to simplify the shader learning process for XAML developers. I didn’t know it at the time, but that road was to lead me to O’Reilly and the writing of this book.

Who This Book Is For

This book is aimed squarely at the Silverlight, WPF, and XAML developer crowd. It’s meant to be a reliable introduction to the eccentric world of GPU programming. I assume that you are familiar with .NET and have passing knowledge of XAML. I’m not teaching programming or XAML, so having some measure of programming experience is essential before you read this book. For this book, I’ll use C# for the .NET code but all examples are easily translated to Visual Basic.

No previous Direct3D or HLSL knowledge is required.

What You Need to Use This Book

For developing custom shaders, you need only a text editor and an HLSL compiler. The compiler is included in the DirectX SDK, which is available at http://msdn.microsoft.com/directx/. To build a .NET wrapper for the custom shader, it’s convenient to use Visual Studio 2010 and .NET 4.0. Expression Blend 4 provides a delightful designer tool that makes it easy to drag shaders onto your UI elements and preview the results in real time.

I’ve written a free utility called Shazzam Shader Editor, which greatly simplifies writing, compiling, and testing shaders. I use Shazzam extensively throughout this book and I encourage you to download a copy from http://shazzam-tool.com and follow along with the book examples. If you install Shazzam, you don’t need to install the massive DirectX SDK. Furthermore, Shazzam contains all the sample shaders discussed in this book.

Contents of This Book

Here is what each chapter of the book will cover.

Chapter 1: This chapter introduces the basics of shader programming, including a look into the early history of 3D programming.

Chapter 2: The first exploratory steps to creating a XAML specific shader are investigated in this chapter. It provides a quick tour of the shader features covered in the following chapters.

Chapter 3: There are a bewildering number of shader types available to the effect developer. This chapter highlights the common shader types, breaking them down into sensible categories.

Chapter 4: This chapter examines in detail the working parts of the XAML ShaderEffect class. It shows how the HLSL and XAML code work in conjunction to create effects that utilize the power of XAML applications.

Chapter 5: Visual Studio and Expression Blend are the premium Microsoft developer and designer tools. This chapter explores the shader specific tools available in these powerful IDEs.

Chapter 6: Shazzam is a free standalone shader editor that contains all the tools you need to compile, test, and visualize your custom pixel shaders. This chapter shows how to use this interesting tool.

Chapter 7: Writing a shader effect requires learning the HLSL programming language. This chapter tours the important features and syntax rules of this quirky DirectX dialect.

Chapter 8: This chapter tours a broad range of topics, gleaned from real world effect development, and shows how to take the knowledge from the previous chapters and write effective shaders.

Appendix A: A list of resources for learning more about pixel shader development.

Appendix B: A reference appendix containing descriptions of the Shazzam shader tags.

Conventions Used in This Book

The following typographical conventions are used in this book:

Plain text

Indicates menu titles, menu options, menu buttons, and keyboard accelerators (such as Alt and Ctrl).

Italic

Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities.

Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files, or the output from commands.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values.

Note

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “HLSL and Pixel Shaders for XAML Developers by Walt Ritscher (O’Reilly). Copyright 2011 Walt Ritscher, 978-1-449-31984-7.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online (www.safaribooksonline.com) is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://oreil.ly/hlsl_px_shaders

To comment or ask technical questions about this book, send email to:

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

No author can complete a book without the help of other people. I’d like to thank:

  • The O’Reilly Media Team: The publishing industry is changing rapidly; the disruptive vortex known as the Internet is relentlessly crushing the old print empires. O’Reilly seems undaunted by the sinkholes opening in their neighborhood; they keep finding innovative ways to stay relevant. A book like mine, small in pagecount, targeting a niche topic, would never have been published five years ago. However, O’Reilly is embracing the eBook market and rethinking what it means to publish a book in this era. Thanks for inviting me to the O’Reilly author circle.

  • My editor, Rachel Roumeliotis, gets kudos for listening to my original book pitch and advocating the project within the shadowy walls of the O’Reilly castle. Little did she know that once we had the green light she’d get an endless stream of email queries as I learned the idiosyncrasies of the O’Reilly book production process. Rachel, the next book will be done sooner, I promise!

  • I knew that finding a tech reviewer for this project would be a challenge. The number of XAML experts that also know HLSL is a very short list. René Schulte (http://kodierer.blogspot.com/) is one of that elite crew and he signed on as tech editor the moment he heard about the project. He’s a busy guy, a productive writer in his own right, and he has an impressive list of Silverlight and phone applications to his credit. But that didn’t stop him from offering pointed criticism when it was needed. This could easily have been his book: his knowledge of computer vision, bitmaps, shaders, and the entire pixel pipeline is top-notch. Thanks for keeping me on track, René.

  • The Review team. Jeremiah Morrill read an early draft of the first chapter. His enthusiastic comments were a huge motivation for me to finish the book. David Kelley reviewed the first half of the book and immediately replied with constructive feedback. Thanks to both of you, for helping me see my manuscript with fresh eyes.

  • My wife, Amy, is a writer. She finished two drafts of her novel (eighty thousand words!) while I slowly worked my way to the conclusion of this techie novella. She did this while ferrying our daughter to endless vocal rehearsals and play practices. We had many discussions about writing; during the production of this book, she was always ready to listen to my writing questions and offer constructive advice. Amy, you are a truly one of a kind, a real jewel!

  • I’m grateful to the following photographers for releasing their beautiful images with a creative commons license: Richard Taylor, Mike Keeling, and SolStock.

  • Cory Plotts graciously submitted his Photoshop blend mode shaders to the project. You can learn more about them at his blog (http://www.cplotts.com/2009/06/16/blend-modes-part-i/).

  • I'm sure many readers are familiar with Pete Brown. He's highly visible to the XAML community, constantly sharing his UI and XAML knowledge with the world. Pete is also a successful book author. My advice: buy anything he writes. His books are full of useful information, his prose laced with humorous asides and geek-related commentary. I am thrilled to have Pete write the forward to this book.

  • Finally, I’d like to thank my readers. You were always at the back of my mind, as I wrestled with the best way to explain the HLSL technology in each chapter.

Get HLSL and Pixel Shaders for XAML Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.