HLSL一门2个小时就能学会,但需要很多年才能精通的语言。以前一直都没怎么用#define,今天看了xna内建effect的源码,才发现原来hlsl中的宏如此强大,并且见到了最疯狂的寄存器绑定,废话不多说,直接上代码吧:)
hlsl macro
#ifdef SM4
// Macros for targetting shader model 4.0 (DX11/10)
#define BEGIN_CONSTANTS cbuffer Parameters : register(b0) {
#define MATRIX_CONSTANTS }; cbuffer ProjectionMatrix : register(b1) {
#define END_CONSTANTS };
#define _vs(r)
#define _ps(r)
#define _cb(r) : packoffset(r)
#define DECLARE_TEXTURE(Name, index) \
Texture2D<float4> Name : register(t##index); \
sampler Name##Sampler : register(s##index)
#define DECLARE_CUBEMAP(Name, index) \
TextureCube<float4> Name : register(t##index); \
sampler Name##Sampler : register(s##index)
#define SAMPLE_TEXTURE(Name, texCoord) Name.Sample(Name##Sampler, texCoord)
#define SAMPLE_CUBEMAP(Name, texCoord) Name.Sample(Name##Sampler, texCoord)
#else
// Macros for targetting shader model 2.0/3.0 (DX9)
#define BEGIN_CONSTANTS
#define MATRIX_CONSTANTS
#define END_CONSTANTS
#define _vs(r) : register(vs, r)
#define _ps(r) : register(ps, r)
#define _cb(r)
#define DECLARE_TEXTURE(Name, index) \
texture2D Name; \
sampler Name##Sampler : register(s##index) = sampler_state { Texture = (Name); };
#define DECLARE_CUBEMAP(Name, index) \
textureCUBE Name; \
sampler Name##Sampler : register(s##index) = sampler_state { Texture = (Name); };
#define SAMPLE_TEXTURE(Name, texCoord) tex2D(Name##Sampler, texCoord)
#define SAMPLE_CUBEMAP(Name, texCoord) texCUBE(Name##Sampler, texCoord)
#endif
// Macros for targetting shader model 4.0 (DX11/10)
#define BEGIN_CONSTANTS cbuffer Parameters : register(b0) {
#define MATRIX_CONSTANTS }; cbuffer ProjectionMatrix : register(b1) {
#define END_CONSTANTS };
#define _vs(r)
#define _ps(r)
#define _cb(r) : packoffset(r)
#define DECLARE_TEXTURE(Name, index) \
Texture2D<float4> Name : register(t##index); \
sampler Name##Sampler : register(s##index)
#define DECLARE_CUBEMAP(Name, index) \
TextureCube<float4> Name : register(t##index); \
sampler Name##Sampler : register(s##index)
#define SAMPLE_TEXTURE(Name, texCoord) Name.Sample(Name##Sampler, texCoord)
#define SAMPLE_CUBEMAP(Name, texCoord) Name.Sample(Name##Sampler, texCoord)
#else
// Macros for targetting shader model 2.0/3.0 (DX9)
#define BEGIN_CONSTANTS
#define MATRIX_CONSTANTS
#define END_CONSTANTS
#define _vs(r) : register(vs, r)
#define _ps(r) : register(ps, r)
#define _cb(r)
#define DECLARE_TEXTURE(Name, index) \
texture2D Name; \
sampler Name##Sampler : register(s##index) = sampler_state { Texture = (Name); };
#define DECLARE_CUBEMAP(Name, index) \
textureCUBE Name; \
sampler Name##Sampler : register(s##index) = sampler_state { Texture = (Name); };
#define SAMPLE_TEXTURE(Name, texCoord) tex2D(Name##Sampler, texCoord)
#define SAMPLE_CUBEMAP(Name, texCoord) texCUBE(Name##Sampler, texCoord)
#endif
hlsl constant define
#include "Macros.fxh"
DECLARE_TEXTURE(Texture, 0);
BEGIN_CONSTANTS
float4 DiffuseColor _vs(c0) _ps(c1) _cb(c0);
float3 EmissiveColor _vs(c1) _ps(c2) _cb(c1);
float3 SpecularColor _vs(c2) _ps(c3) _cb(c2);
float SpecularPower _vs(c3) _ps(c4) _cb(c2.w);
float3 DirLight0Direction _vs(c4) _ps(c5) _cb(c3);
float3 DirLight0DiffuseColor _vs(c5) _ps(c6) _cb(c4);
float3 DirLight0SpecularColor _vs(c6) _ps(c7) _cb(c5);
float3 DirLight1Direction _vs(c7) _ps(c8) _cb(c6);
float3 DirLight1DiffuseColor _vs(c8) _ps(c9) _cb(c7);
float3 DirLight1SpecularColor _vs(c9) _ps(c10) _cb(c8);
float3 DirLight2Direction _vs(c10) _ps(c11) _cb(c9);
float3 DirLight2DiffuseColor _vs(c11) _ps(c12) _cb(c10);
float3 DirLight2SpecularColor _vs(c12) _ps(c13) _cb(c11);
float3 EyePosition _vs(c13) _ps(c14) _cb(c12);
float3 FogColor _ps(c0) _cb(c13);
float4 FogVector _vs(c14) _cb(c14);
float4x4 World _vs(c19) _cb(c15);
float3x3 WorldInverseTranspose _vs(c23) _cb(c19);
MATRIX_CONSTANTS
float4x4 WorldViewProj _vs(c15) _cb(c0);
END_CONSTANTS
DECLARE_TEXTURE(Texture, 0);
BEGIN_CONSTANTS
float4 DiffuseColor _vs(c0) _ps(c1) _cb(c0);
float3 EmissiveColor _vs(c1) _ps(c2) _cb(c1);
float3 SpecularColor _vs(c2) _ps(c3) _cb(c2);
float SpecularPower _vs(c3) _ps(c4) _cb(c2.w);
float3 DirLight0Direction _vs(c4) _ps(c5) _cb(c3);
float3 DirLight0DiffuseColor _vs(c5) _ps(c6) _cb(c4);
float3 DirLight0SpecularColor _vs(c6) _ps(c7) _cb(c5);
float3 DirLight1Direction _vs(c7) _ps(c8) _cb(c6);
float3 DirLight1DiffuseColor _vs(c8) _ps(c9) _cb(c7);
float3 DirLight1SpecularColor _vs(c9) _ps(c10) _cb(c8);
float3 DirLight2Direction _vs(c10) _ps(c11) _cb(c9);
float3 DirLight2DiffuseColor _vs(c11) _ps(c12) _cb(c10);
float3 DirLight2SpecularColor _vs(c12) _ps(c13) _cb(c11);
float3 EyePosition _vs(c13) _ps(c14) _cb(c12);
float3 FogColor _ps(c0) _cb(c13);
float4 FogVector _vs(c14) _cb(c14);
float4x4 World _vs(c19) _cb(c15);
float3x3 WorldInverseTranspose _vs(c23) _cb(c19);
MATRIX_CONSTANTS
float4x4 WorldViewProj _vs(c15) _cb(c0);
END_CONSTANTS