Effect Pool

반응형

Effect Pool의 사용.
pre compile를 위한 fxc의 사용은 환경변수의 path에서 미리 설정.

하나의 프로그램에서 여러개의 이펙트를 사용할 때 이펙트 파일을 셋팅하기 위해
많은 이펙트 변수들이 필요하다.
종종 많은 수의 변수들이 이펙트끼리 겹치게 된다.
여러가지 설정없이 이펙트끼리의 쉐이더나 변수를 공유하기 위해
각각의 이펙트에서 실제로 같은 데이터들을 만들어서 설정하여 호출할 필요가 있다.
effect pool은 이러한 문제를 해결해준다.
effect pool은 다른 이펙트들 간의 변수와 텍스쳐, 그리고 쉐이더를 공유하게 해준다.
이것은 공통된 변수나 데이터를 매 프레임한 한번씩만 설정해 줄수 있다는 것을 의미한다.
effect pool은 실질적으로 두개의 파트로 구성되어져 있다.
effect pool 자신과 effect 부분이다.
같은 pool에 있는 이펙트들은 자식 이펙트들에 의해 호출되어 진다.

effect pool을 사용함으로서 다른 이펙트 파일들 사이에서 데이터를 공유할 수 있다는 이점이 있다.
반면에 effect pool을 사용하지 않고 같은 헤더파일을 각각다른 이펙트 파일에 include할 수 있지만.
변수와 쉐이더는 단지 이페트들간에 복사되어진다.
만약 네가 하나의 이펙트 파일에 있는 A라는 변수를 바꾸면 데이터를 정확하게 하기 위해
다른 이펙트 파일에서 같은 변수를 고칠필요가 있다.
effect pool은 "shared"라는 변수를 사용할 수 있다.
공유 변수는 pool에 있는 모든 이펙트 파일에서 사용되어 질 수 있다.
effect pool에서 공유변수를 설정하면 pool 에 속해있는 각각의 이펙트의 같은 변수들을
설정 해 줄 필요가 없다.

dx9에서의 사용법, Sampleer은 지원하지 않는다.
shared float3 g_LightDir;               // Light's direction in world space
shared float4 g_LightDiffuse;         // Light's diffuse color
shared float4x4 g_mViewProj;       // View * Projection matrix
shared float  g_TexMove;             // random variable that offsets the V texcoord

shared texture2D g_txDiffuse;
sampler2D MeshTextureSampler = sampler_state
{
    Texture = (g_txDiffuse);
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = WRAP;
    AddressV = WRAP;
};

dx10에서는 상수 버퍼로 그룹지어줘야 한다.
상수 버퍼에 속해있는 것만 공유가 가능하다.
하나의 상수버퍼에 한계치는 없다.
shader 4.0모델에서는 sampler은 별개의 오브젝트라서 sampler은 공유가능하다.
shared cbuffer cbShared
{
float3 g_LightDir;                  // Light's direction in world space
float4 g_LightDiffuse;              // Light's diffuse color
float4x4 g_mViewProj;               // View * Projection matrix
float  g_TexMove;                   // random variable that offsets the V texcoord
};


shared texture2D g_txDiffuse;
shared sampler2D MeshTextureSampler = sampler_state
{
    Texture = (g_txDiffuse);
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = WRAP;
    AddressV = WRAP;
};


dx9이나 dx10에서나 같은것은 shader이라는 예약어에 의해 공유된다는 것이다.
shared float4 ScenePS( PS_INPUT input ) : COLOR0
{
    ...
}





Child Effects


공유변수들을 자식이펙트에서 사용하기 위해서는 헤더파일을 추가시켜야 한다.
단지 그렇게만 해주면 된다.
변수들과 쉐이더는 마치 지역변수처럼 접근이 가능해 진다.

포함파일은 공유변수를 위해 필요하지 않다.
자식이펙트들은 이펙트 파일 내부에 공유된 변수를 선언할 수 있다.
하지만 쉐이더들 사이에서 공유된 변수의 이름이 일치하는지 신경써야 한다.
이는 포함파일을 사용하기 위해 더 쉽다.

 

Creating the Effect Pool

The actual creation of the effect pool varies between Direct3D 9 and Direct3D 10. In Direct3D 9, the effect pool is created by the device, but contains no initial information about shared variables. This information is later collected when child effects are added to the effect pool.
dx9과 dx10 사이의 실제 effect pool의 생성방법을 보자.
dx9에서의 effect pool은 디바이스에 의해 생성되어지지만 공유변수에 대한 초기값을 포함하지 않는다.
이 정보는 후에 자식 이펙트들이 effect pool에 추가되어질 때 수집된다.

    V_RETURN( D3DXCreateEffectPool( &g_pEffectPool ) );


dx10의 경우 이펙트풀을 파일로부터 생성되어진다.
이 헤더파일을 자식파일에서 사용할 수 있다.

    V_RETURN( D3DX10CreateEffectPoolFromFile( str, 
                                              NULL,
                                              NULL, 
                                              "fx_4_0", 
                                              D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY,
                                              0,
                                              pd3dDevice,
                                              NULL,
                                              &g_pEffectPool,
                                              NULL,
                                              NULL ) );

Creating the Child Effects

The creation of child effects is similar between both APIs. On Direct3D 9, the effect is compiled normally, but the effect pool is passed in as one of the parameters. This makes the effect a child effect.
자식이펙트의 생성은 dx9과 dx10 의 api 가 비슷하다.
dx9의 이펙트는 일반적으로 컴파일되지만 하나의 인자가 들어간다.
이로하여금 자식이펙트가 만들어 진다.

    V_RETURN( D3DXCreateEffectFromFile( pd3dDevice,
                                        str, 
                                        NULL,
                                        NULL,
                                        dwShaderFlags,
                                        g_pEffectPool,
                                        &g_EffectData[i].pEffect,
                                        NULL ) );

D3D10_EFFECT_COMPILE_CHILD_EFFECT flag set.
마찬가지로 dx10에서도 이펙트풀에 하나의 인자가 들어가지만 자식은 반드시 D10_EFFECT_COMPILE_CHILD_EFFECT 
플래그 설정고 함께 컴파일 되어야 한다.

    V_RETURN( D3DX10CreateEffectFromFile( str, 
                                          NULL,
                                          NULL, 
                                          "fx_4_0", 
                                          dwShaderFlags, 
                                          D3D10_EFFECT_COMPILE_CHILD_EFFECT, 
                                          pd3dDevice, 
                                          g_pEffectPool, 
                                          NULL, 
                                          &g_EffectData[i].pEffect, 
                                          NULL,
                                          NULL ) );

Pre-Compiled Effects

Effects pools are not limited to effects that are compiled at runtime. Most applications can gain signification performance improvements by compiling their effects offline. Pre-compiled effects can be loaded as child effects in exactly the same way as text effect files. However, the files must be precompiled as child effects for them to work in effect pools. Precompiled effects can be used as child effects alongside text effects.

The sample uses fxc.exe to precompile EffectPools3.fx in a custom build step and saves the compiled output to EffectPools3.fxo9 or EffectPools3.fxo10 depending on API. This is then loaded in place of EffectPools3.fx at runtime. In order to precompile an effect as a child effect using fxc.exe, the following command line options must be used. On Direct3D 9, the effect needs simply to compiled with /Fo to turn it into a binary output file. On Direct3D 10, to remain analogous to the D3D10_EFFECT_COMPILE_CHILD_EFFECT paradigm of the API, the effect also needs to be compiled with /Gch. This flag compiles the effect as a child effect for fx_4_0 targets.









TAGS.

Comments