DirectX Programming #4. Kang, Seongtae Computer Graphics, 2009 Spring

Similar documents
Shaders Part II. Traditional Rendering Pipeline

The Effects Framework. October 31, Composed of the following components:

Shader Series Primer: Fundamentals of the Programmable Pipeline in XNA Game Studio Express

Lab 3 Shadow Mapping. Giuseppe Maggiore

Shader Programming CgFX, OpenGL 2.0. Michael Haller 2003

Shaders (some slides taken from David M. course)

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

Basics of GPU-Based Programming

Sébastien Dominé, NVIDIA. CgFX

CS GPU and GPGPU Programming Lecture 7: Shading and Compute APIs 1. Markus Hadwiger, KAUST

The Application Stage. The Game Loop, Resource Management and Renderer Design

Shaders. Slide credit to Prof. Zwicker

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

Programmable Graphics Hardware

Shaders in Eve Online Páll Ragnar Pálsson

Graphics Hardware. Instructor Stephen J. Guy

Technical Report. Mesh Instancing

CS195V Week 9. GPU Architecture and Other Shading Languages

12.2 Programmable Graphics Hardware

Could you make the XNA functions yourself?

DirectX 11 First Elements

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

!" "!#$!"%!&% ' ( )) * ) )+( ( ))+ ))(, ) ( - +,./ )) ) ) $( -( 1), , ) ( -)) ') 5 ' +) 67 4 ) ' 8, ( ) ( 9 :! ; $) ) ; 5<4,4$6 ( =) >

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

COMP371 COMPUTER GRAPHICS

Supplement to Lecture 22

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

Tutorial on GPU Programming #2. Joong-Youn Lee Supercomputing Center, KISTI

Kang, Seong-tae Computer Graphics, 2007 Spring. Texture mapping Texture mapping on Direct3D. CGLab

Cg Toolkit. Cg 1.3 Release Notes. December 2004

Programmable Graphics Hardware

Cg: A system for programming graphics hardware in a C-like language

CS GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

CS GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

Designing a Modern GPU Interface

Lecture 2. Shaders, GLSL and GPGPU

Introduction to Shaders.

DirectX Programming #1. Hyunna Lee Computer Graphics, 2010 Spring

Graphics Hardware. Computer Graphics COMP 770 (236) Spring Instructor: Brandon Lloyd 2/26/07 1

Sung-Eui Yoon ( 윤성의 )

Cg 2.0. Mark Kilgard

Cg Toolkit. Cg 2.2 February 2010 Release Notes

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Advanced Rendering & Shading

Shader Programming and Graphics Hardware

OpenGL ES 2.0 : Start Developing Now. Dan Ginsburg Advanced Micro Devices, Inc.

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

GPU Memory Model. Adapted from:

How to Work on Next Gen Effects Now: Bridging DX10 and DX9. Guennadi Riguer ATI Technologies

Lecture 14 of 41. Surface Detail 5 of 5: Shading Languages OGLSL, Direct3D Shading

Cg Toolkit. Cg 2.1 October 2008 Release Notes

Programming Graphics Hardware

Graphics Hardware, Graphics APIs, and Computation on GPUs. Mark Segal

General Purpose computation on GPUs. Liangjun Zhang 2/23/2005

Programmable GPUS. Last Time? Reading for Today. Homework 4. Planar Shadows Projective Texture Shadows Shadow Maps Shadow Volumes

Shader Programming 1. Examples. Vertex displacement mapping. Daniel Wesslén 1. Post-processing, animated procedural textures

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

CIS 536/636 Introduction to Computer Graphics. Kansas State University. CIS 536/636 Introduction to Computer Graphics

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

Cg Toolkit. Cg 2.2 April 2009 Release Notes

Programming shaders & GPUs Christian Miller CS Fall 2011

Evolution of GPUs Chris Seitz

Beginning Direct3D Game Programming: 1. The History of Direct3D Graphics

Cg Toolkit. Cg 2.0 May 2008 Release Notes

CS 179: GPU Programming

GPU Programmable Graphics Pipeline

Lecture 14 of 41. Surface Detail 5 of 5: Shading Languages OGLSL, Direct3D Shading

CS GPU and GPGPU Programming Lecture 2: Introduction; GPU Architecture 1. Markus Hadwiger, KAUST

Cg Toolkit. Cg 2.0 January 2008 Release Notes

Cg Toolkit. Cg 2.1 beta August 2008 Release Notes

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

Rendering Grass with Instancing in DirectX* 10

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

Parallelizing Graphics Pipeline Execution (+ Basics of Characterizing a Rendering Workload)

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

CS427 Multicore Architecture and Parallel Computing

Rendering Subdivision Surfaces Efficiently on the GPU

Lecture 13: OpenGL Shading Language (GLSL)

Introduction to Programmable GPUs CPSC 314. Real Time Graphics

Computation of Filtered Back Projection on Graphics Cards

DirectX10 Effects. Sarah Tariq

DirectX10 Effects and Performance. Bryan Dudash

Hardware Accelerated Volume Visualization. Leonid I. Dimitrov & Milos Sramek GMI Austrian Academy of Sciences

Using D3D10 Now. Kevin Myers NVIDIA

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

CG Programming: 3D Texturing

CPSC 436D Video Game Programming

Summed-Area Tables. And Their Application to Dynamic Glossy Environment Reflections Thorsten Scheuermann 3D Application Research Group

GpuPy: Accelerating NumPy With a GPU

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Creating Real Shaders in FX Composer. Kevin Bjorke NVIDIA Corporation

Compute Shaders. Christian Hafner. Institute of Computer Graphics and Algorithms Vienna University of Technology

Grafica Computazionale: Lezione 30. Grafica Computazionale. Hiding complexity... ;) Introduction to OpenGL. lezione30 Introduction to OpenGL

Transcription:

DirectX Programming #4 Kang, Seongtae Computer Graphics, 2009 Spring

Programmable Shader For recent hardwares, vertex and pixel processing stage of the graphics pipeline is programmable Programmable Vertex Shader Programmable Pixel Shader Geometry processing also programmable since D3D10

Programming for Shader In early stage, hardware-dependant mnemonic language is used to program for shader Shader Assembler Currently, C-like shading language is widely used Cg, HLSL, GLSL General programming platform to utilize GPU s computing power for general purpose is being introduced lately NVIDIA CUDA OpenCL

Brief History on Programmable Shader Pixar RenderMan Interface Specification (1988) Concept of Shader Language(SL) is introduced Required special-purpose hardwares and used for studio works Luxo Jr. (SIGGRAPH, 1986)

Brief History on Programmable Shader Programmable shader for consumer hardware Microsoft DirectX 8 (Shader Model 1.1) Shader assembler is introduced for vertex and pixel shader programmability

Brief History on Programmable Shader NVIDIA Cg (1998) Introduced to program NVIDIA s programmable GPU C-like grammar with reserved keywords // input vertex struct VertIn { float4 pos : POSITION; float4 color : COLOR0; }; // output vertex struct VertOut { float4 pos : POSITION; float4 color : COLOR0; }; // vertex shader main entry VertOut main(vertin IN, uniform float4x4 modelviewproj) { VertOut OUT; OUT.pos = mul(modelviewproj, IN.pos); // calculate output coords OUT.color = IN.color; // copy input color to output OUT.color.z = 1.0f; // blue component of color = 1.0f return OUT; }

Brief History on Programmable Shader Microsoft High-Level Shading Language (HLSL) Introduced with Microsoft DirectX 9 (Shader Model 2) Developed alongside NVIDIA Cg Very similar to Cg struct VS_OUTPUT0 { float4 Pos : POSITION; // position float4 ObjSliceTex : TEXCOORD0; // Scaled Front slice Texture Coordinate float4 OutInterTex : TEXCOORD2; }; struct PS_OUTPUT0 { float4 Color : COLOR0; float Depth : DEPTH; }; VS_OUTPUT0 EmptySkipVShader( float4 inpos : POSITION, float4 inslicetex : TEXCOORD0) { VS_OUTPUT0 Out = (VS_OUTPUT0) 0; float4 ttt = inpos + sign(isfrontface)*facenormal; Out.Pos = mul( ttt, objecttransmatrix ); // transform vertices by objecttransmatrix matrix Out.ObjSliceTex = inslicetex; Out.OutInterTex = Out.Pos; return Out; }

Shader Model Programmability of the shader strongly rely on hardware implementations Distinguished by some Generations No de jure standards for shader model Microsoft s shader model, which inherited NVIDIA model, is de facto standard

Microsoft Shader Model Shader Model 1.1 Introduced in DirectX 8 Shader Assembler Shader Model 2 DirectX 9.0 HLSL introduced No flow control

Microsoft Shader Model Shader Model 3 DirectX 9.0c identical to SM2 HLSL grammar Dynamic flow control (conditional branch, loop ) Shader Model 4 Direct3D 10 Renewal of shader language : HLSL4 object and template based grammar Programmable geometry processing stage Full flow control and general-purpose features

HLSL on Direct3D 9 Pass A unit for custom-tailored pipeline stage Only one pass can be executed for one GPU Consists of entry functions for VS/PS, and render states Technique Made up of one or more passes

HLSL on Direct3D 9 Effect Contains one or more techniques Global variables and type definitions Format Text source Binary object You can compile an effect source file to the binary object on the command line using fx compiler tool (fxc.exe) Location External file (usually.fx for source,.obj for binary) Included resource

Structure of a HLSL Effect File Variable Declaration Type Declaration Functions Technique and pass description

HLSL : Variable Declaration Global variables for the effect Constants These variables can be assigned from CPU-side float4x4 g_mworld; float4x4 g_mview; float4x4 g_mproj; float4 g_vlight = float4( 0.0f, 0.0f, -10.0f, 1.0f ); float4 g_vlightcolor = float4( 1.0f, 1.0f, 1.0f, 1.0f ); texture g_txscene; float4 Diffuse;

HLSL : Type Declaration Custom structures Render state description set Texture sampler struct VS_OUTPUT0 { float4 Pos : POSITION; float4 Diffuse : COLOR0; float2 Tex0 : TEXCOORD0; }; sampler2d g_samscene = sampler_state { Texture = <g_txscene>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; };

HLSL : Type Declaration Custom structures Render state description set Texture sampler struct VS_OUTPUT0 { float4 Pos : POSITION; float4 Diffuse : COLOR0; float2 Tex0 : TEXCOORD0; }; sampler2d g_samscene = sampler_state { Texture = <g_txscene>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; };

HLSL : Functions Limitations for VS and PS function are not identical! Until SM 3 VS_OUTPUT0 VertScene( float4 vpos : POSITION, float3 vnormal : NORMAL, float2 vtex0 : TEXCOORD0) { VS_OUTPUT out; } out.pos = mul( vpos, g_mworld ); out.pos = mul( out.pos, g_mview ); out.pos = mul( out.pos, g_mproj ); float4 wpos = mul( vpos, g_mworld ); wpos = mul( wpos, g_mview ); float3 N = mul( vnormal, (float3x3)g_mworld ); N = normalize( mul( N, (float3x3)g_mview ) ); float3 InvL = g_vlight - wpos; InvL = normalize( InvL ); oout.diffuse = saturate( dot( N, InvL ) ) * Diffuse * g_vlightcolor; out.tex0 = vtex0; return out; float4 PixScene( float4 Diffuse : COLOR0, float2 Tex0 : TEXCOORD0 ) : COLOR0 { return tex2d( g_samscene, Tex0 ) * Diffuse; }

HLSL : Technique and Pass Description NULL for VertexShader/PixelShader means fixed pipeline behavior technique MyRenderTech { pass P0 { VertexShader = compile vs_2_0 VertScene(); PixelShader = compile ps_2_0 PixScene(); CullMode = NONE; AlphaBlendEnable = FALSE; } }

Creating an Effect From external file Either a source text or compiled binary can be used HRESULT D3DXCreateEffectFromFile(LPDIRECT3DDEVICE9 pdevice, LPCTSTR psrcfile, CONST D3DXMACRO * pdefines, LPD3DXINCLUDE pinclude, DWORD Flags, LPD3DXEFFECTPOOL ppool, LPD3DXEFFECT *ppeffect, LPD3DXBUFFER *ppcompilationerrors); psrcfile : effect file name Flags : several flags such as compilation options ppeffect : a pointer to a buffer containing the compiled effect ID3DXEffect* m_peffect; D3DXCreateEffectFromFile( pd3ddevice, str, NULL, NULL, NULL, NULL, &m_peffect, NULL );

Binding Variables Set<Type> method series of effect class Primitive types are passed by value Struct types are passed by pointer Textures are bound by texture object pointer D3DXMATRIX matworld, matview, matview; IDirect3DTexture9 *mytex; : : m_peffect->setmatrix( g_mworld, &matworld); // struct type m_peffect->setmatrix( g_mview, &matview); m_peffect->setfloat( Diffuse, 0.6f); // primitive type m_peffect->settexture( g_txscene, mytex); // texture

Using Effects on Rendering Begin an effect Set a technique Begin a pass Render End a pass End an effect m_peffect->begin(&npasses, 0 ); // # of passes for this technique will be returned to npasses int ntechnique=pterrainpainter->gettechniquebyname( MyRenderTech ); pterrainpainter->settechnique(ntechnique); // using MyRenderTech technique m_peffect->beginpass(0); // using pass 0 m_pd3ddevice->drawprimitive( D3DPT_TRIANGLESTRIP, 0, linecnt); // rendering code m_peffect->endpass(); m_peffect->end(); // Begin s 2 nd param 0 triggers restoration // of previous render state when an effect ends

Next Class @ 301-314 Practice on programming DirectX and HLSL application using Visual C++ on Win32 platform Basic sample code will be supplied Direct3D initialization Vertex buffer creation Drawing some polygon We will code in next class Effect creation and binding Effect file coding in HLSL Basic fixed pipeline Phong shading DirectX homework will be assigned

Practice Assignments Prepare for HLSL coding See HLSL contents on the DirectX document Analyse the sample code Will be uploaded on homepage this week