Thứ Ba, 18 tháng 3, 2014

Tài liệu HLSL and Pixel Shaders for XAML Developers doc


LINK DOWNLOAD MIỄN PHÍ TÀI LIỆU "Tài liệu HLSL and Pixel Shaders for XAML Developers doc": http://123doc.vn/document/1037132-tai-lieu-hlsl-and-pixel-shaders-for-xaml-developers-doc.htm


Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
1. Shader 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Why Should XAML Developers Learn HLSL? 1
The Tale of the Shader 2
HLSL and DirectX 3
Understanding the Graphics Pipeline 5
XAML and Shaders 7
Summary 9
2. Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Setting Up Your Development Computer 11
Silverlight Development 11
WPF Development 12
Expression Blend 4 12
Choosing a Shader Compiler 12
DirectX Compiler 12
WPF Build Task 13
Shazzam Shader Editor 13
Other Tools to Consider 13
FX Composer 13
NShader 14
Visual Studio 2012 14
A First Shader Project 15
Using Prebuilt Effects 16
Adding Effects 19
Debrief 22
Custom Shader 22
Create a Shader Algorithm 23
iii
Compile the HLSL Code 24
Add to Visual Studio XAML Project 24
Create a .NET Wrapper Class 25
Compile the Project 26
Instantiate the Shader 27
Summary 28
3. Commonplace Pixel Shaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
A Word About Pseudocode 30
Our Sample Image 30
Color Modification 30
Common Techniques 31
Black, White, and Gray 32
Color Replacement 34
Color Enhancement and Correction 34
Distinctive Effects 36
Distortion and Displacement 37
Magnify 38
Embossed 39
Testing Distortion Effects 40
Other Displacement Effects 42
Blurs 42
Motion Blur 43
Zoom Blur 43
Sharpening 43
Generative Effects 43
Multiple Inputs 44
Sampler Transition 44
Texture Map 46
Color Combination 47
Photoshop Blend Modes 49
Darken Modes 49
Lighten Modes 51
Contrast Modes 52
Comparative Modes 52
Other Modes 52
Blend Modes in HLSL 53
Practical Uses for Shader Effects 53
Motion Blur in Moving Parts 54
Blurs to Emphasize UI Focus 54
Desaturate for Emphasis 55
Summary 55
iv | Table of Contents
4. How WPF and Silverlight Use Shaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Framework Effects 57
BlurEffect 58
DropShadowEffect 59
Nested Effects 59
Multiple Effects on One Element 59
Expression Blend Effects 60
Using a Blend Effect 61
Custom Effects 63
Creating a Custom ShaderEffect 63
Working with Samplers 65
Multi-Input Shaders 72
Understanding Sampler Size 73
Creating Parameterized Effects 75
Padding 77
Effect Mapping 78
Summary 83
5.
Using Expression Blend and Visual Studio with Shaders . . . . . . . . . . . . . . . . . . . . . . 85
Creating a Project 85
New Project 86
Adding Assets 87
Adding Effects 88
Setting Effect Properties 90
Visual Studio Editor 90
Using Custom Effects in Blend 92
Multi-Input Effects 94
Transition Effects in Blend 98
Create a State Group 98
Add the States 99
Set the Properties 99
Adding a Transition Effect 100
Summary 102
6. Using the Shazzam Shader Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
UI Tour 103
Create a Custom Shader 103
Exploring the HLSL Code Editor 105
Compiling and Testing the Shader 106
Editor Status Bar 109
Exploring the Generated Code 109
Changing Options 110
Working with HLSL Parameters 112
Table of Contents | v
Testing Parameters 113
Default Values for Parameters 114
Shazzam Tags 115
Input Parameter Types 117
Influencing the .NET Type 117
Multi-Input Shaders 119
Shader Loader 121
More Shazzam Tools 123
Copying the Files into a Visual Studio Project 123
Summary 124
7. HLSL in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Parallel Processing in Shaders 125
Parallelism and the GPU 126
One-Pixel-at-a-Time Algorithm 126
The Relationship Between DirectX and the GPU 127
Understanding Registers 127
Basic Structure of an HLSL Program 128
User-Defined Functions 129
Semantics 130
Data Types 132
Type Casting 138
Local Variables 139
Shader Inputs 139
TEXCOORD Input 139
Global Variables 140
Global Inputs 140
Texture and Sampler Input 141
Sampler Declarations 142
Other Code Constructs 143
Flow Control—Branching 143
Flow Control—Loops 144
Operators 144
Built-in Functions 145
Texture Functions 146
Math Functions 146
Useful Built-in Functions 147
Summary 147
8. The Zen of Shader Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Zen of Pixel Programming 149
Sampling a Pixel Color 149
Sampling a Color from Another Texture 150
vi | Table of Contents
Colors 151
Color Channel Manipulation 151
Conditional Colors 152
Replace a Color 153
Other Color Modifier Examples 154
Coordinates 158
Distortion and Displacement Effects 165
Random Values 170
Noise Textures 172
Perlin Noise in Shaders 175
Summary 179
A.
Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
B.
Shazzam Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Table of Contents | vii

Preface
Pixel Shaders are one of the more powerful graphic tools available for XAML program-
mers. 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 documen-
tation 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
ix
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 investi-
gated 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 de-
veloper. 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 Shader
Effect 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.
x | Preface
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.
This icon signifies a tip, suggestion, or general note.
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
Preface | xi
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 Devel-
opers 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 permissions@oreilly.com.
Safari® Books Online
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 cre-
ative 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 organi-
zations, government agencies, and individuals. Subscribers have access to thousands
of books, training videos, and prepublication manuscripts in one fully searchable da-
tabase 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 Tech-
nology, 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)
xii | Preface

Không có nhận xét nào:

Đăng nhận xét