1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
static bool bInited = false;
//--------------------------------------------------------------------------------------
// Nom: RenduQuadInst()
// Desc: rendu quadscreen avec coordonnées écran
/*
|---------|
|D C|
| |
| |
|A B|
|---------|
O<-----branch
*/
//--------------------------------------------------------------------------------------
void RenduQuadInst( ID3D10Device* pd3dDevice, int iXCoord, int iYCoord,
int iLargeur, int iHauteur, ID3D10ShaderResourceView* pQuadScreenSRV )
{
HRESULT hr = E_FAIL;
iXCoord = iYCoord = 0;
iLargeur = iHauteur = 128;
pQuadScreenSRV = g_pPaletteCam_SRV;
const UINT uiNbInstances = 2;
//ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView();
//ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView();
//pd3dDevice->OMSetRenderTargets( 1, &pRTV, pDSV );
//ID3D10ShaderResourceView *const pSRV[1] = {NULL};
//pd3dDevice->VSSetShaderResources( 0, 1, pSRV );
//pd3dDevice->PSSetShaderResources( 0, 1, pSRV );
// créé le quad vb en triangle strip
struct QUADSCREEN_VERTEX4 // structure de description du quad screen vertex
{
D3DXVECTOR3 pos; // coord
D3DXVECTOR2 tex; // coordonnées de mapping texture
};
// acquiert dimensions du backbuffer
const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc = DXUTGetDXGIBackBufferSurfaceDesc();
// convertit de screen coordinates en clip space coordinates, ( 0, iWidth ) -> ( -1.0f, 1.0f )
float fLeft, fRight, fTop, fBottom;
fLeft = ( iXCoord * 2.0f /pBackBufferSurfaceDesc->Width )- 1.0f;
fRight = ( ( iXCoord + iLargeur ) * 2.0f / pBackBufferSurfaceDesc->Width ) - 1.0f;
fTop = 1.0f -( iYCoord * 2.0f / pBackBufferSurfaceDesc->Height );
fBottom = 1.0f - ( ( iYCoord + iHauteur ) * 2.0f / pBackBufferSurfaceDesc->Height );
// remplit data vb avec coord, normal, map texture
QUADSCREEN_VERTEX4 pVertices[] =
{
{ D3DXVECTOR3( fLeft, fBottom, 0.0f ) , /*D3DXVECTOR3( 0.0f, 0.0f, 1.0f ),*/ D3DXVECTOR2( 0.0f, 1.0f ) },
{ D3DXVECTOR3( fRight, fBottom, 0.0f ), /*D3DXVECTOR3( 0.0f, 0.0f, 1.0f ),*/ D3DXVECTOR2( 1.0f, 1.0f ) },
{ D3DXVECTOR3( fRight, fTop, 0.0f ), /*D3DXVECTOR3( 0.0f, 0.0f, 1.0f ),*/ D3DXVECTOR2( 1.0f, 0.0f ) },
{ D3DXVECTOR3( fLeft, fTop, 0.0f ), /*D3DXVECTOR3( 0.0f, 0.0f, 1.0f ),*/ D3DXVECTOR2( 0.0f, 0.0f ) },
};
if ( bInited == false )
{
bInited = true;
// shader quadinst
DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
dwShaderFlags |= D3D10_SHADER_DEBUG | D3D10_SHADER_WARNINGS_ARE_ERRORS;
ID3D10Blob* lpEffectErrors = NULL;
WCHAR* szEffectFilePath = L"../Shaders/QuadInst.fx";
VerifHR( D3DX10CreateEffectFromFileW(
szEffectFilePath, // filename
NULL, // shader macros
NULL, // fichier include
"fx_4_0", // profile ??
dwShaderFlags, // drapeaux HLSL shader
0, // drapeaux fx file
pd3dDevice, // device
NULL, // effect pool
NULL, // thread pump
&g_pQuadInstEffect, // pointeur vers l'effet
&lpEffectErrors, // pp* Erreur
NULL // HRESULT*
) );
if( FAILED( hr ) )
{
MessageBox( NULL, L"ERREUR D3DX10CreateEffectFromFileW(c://DOOM2011/Shaders/QuadInst.fx) ", L"ERREUR", MB_OK );
char* pError = NULL;
if( lpEffectErrors )
{
pError =(char*) ( lpEffectErrors->GetBufferPointer() ); // then cast to a char* to see it in the locals window
unsigned int bufferSize = lpEffectErrors->GetBufferSize();
WCHAR sz[MAX_PATH];
for( UINT i=0; i<bufferSize; i++)
{
sz[i] = pError[i];
}
OutputDebugString( sz );
OutputDebugString( L"\n" );
}
return;
}
else
OutputDebugString( L"compilation c://DOOM2011/Shaders/QuadInst.fx OK\n" );
// acquiert technique
g_pQuadInstTechnique = g_pQuadInstEffect->GetTechniqueByName( "RenderQuadInst" );
if ( g_pQuadInstTechnique->IsValid() != TRUE )
OutputDebugString( L" ERREUR!!! technique instanciation\n" );
// acquiert variables shader
g_pQuadtxDiffuseVariable = g_pQuadInstEffect->GetVariableByName( "txDiffuse" )->AsShaderResource();// texture de fond par défaut
g_pQuadWorldVariable = g_pQuadInstEffect->GetVariableByName( "mWorld" )->AsMatrix();
g_pQuadViewVariable = g_pQuadInstEffect->GetVariableByName( "mView" )->AsMatrix();
g_pQuadProjectionVariable = g_pQuadInstEffect->GetVariableByName( "mProj" )->AsMatrix();
g_pmWorldViewProj = g_pQuadInstEffect->GetVariableByName( "g_mWorldViewProj" )->AsMatrix();
g_pmWorldView = g_pQuadInstEffect->GetVariableByName( "g_mWorldView" )->AsMatrix();
// défini le instanced vertex data layout
// 4 x float4, soit 16 float pour la définition de la matrice d'instance
const D3D10_INPUT_ELEMENT_DESC instlayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
// { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXTURE", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "mTransform", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D10_INPUT_PER_INSTANCE_DATA, 1 },
{ "mTransform", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 16, D3D10_INPUT_PER_INSTANCE_DATA, 1 },
{ "mTransform", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 32, D3D10_INPUT_PER_INSTANCE_DATA, 1 },
{ "mTransform", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 48, D3D10_INPUT_PER_INSTANCE_DATA, 1 },
};
int iNumElements = sizeof( instlayout ) / sizeof( D3D10_INPUT_ELEMENT_DESC );
// créé le vertex layout pour l'instanciation
D3D10_PASS_DESC PassDesc;
ID3D10EffectPass* pPass = g_pQuadInstTechnique->GetPassByIndex( 0 );
pPass->GetDesc( &PassDesc );
VerifHR( pd3dDevice->CreateInputLayout(
instlayout,
iNumElements,
PassDesc.pIAInputSignature,
PassDesc.IAInputSignatureSize,
&g_pQuadInstLayout
) );
if( hr == S_OK )
OutputDebugString( L"InitQuadInst() g_pQuadInstLayout créé\n" );
else
OutputDebugString( L"ERREUR!!! InitQuadInst() g_pQuadInstLayout création échouée\n" );
// créé quad vertex buffer
D3D10_BUFFER_DESC BufDesc;
BufDesc.ByteWidth = sizeof( QUADSCREEN_VERTEX4 ) * 4;
BufDesc.Usage = D3D10_USAGE_DYNAMIC;
BufDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
BufDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
BufDesc.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA InitData;
InitData.pSysMem = pVertices; // copie IB
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;
VerifHR( pd3dDevice->CreateBuffer( &BufDesc, &InitData, &g_pQuadInstVB ) );
if( hr == S_OK )
OutputDebugString( L"InitQuadInst() g_pQuadInstVB quad screen vertex buffer créé\n" );
else
OutputDebugString( L"ERREUR!!! InitQuadInst() g_pQuadInstVB création échouée\n" );
// copie data dans vb quad
QUADSCREEN_VERTEX4* pVB;
VerifHR( g_pQuadInstVB->Map( D3D10_MAP_WRITE_DISCARD , 0, ( LPVOID* )&pVB ) ); // D3D10_MAP_READ_WRITE D3D10_MAP_WRITE
memcpy( pVB, pVertices, sizeof( pVertices ) );
g_pQuadInstVB->Unmap();
// créé un index buffer pour le quad en TRIANGLE_STRIP
DWORD dwNumIndices = 6;
D3D10_BUFFER_DESC bufferDesc;
bufferDesc.ByteWidth = dwNumIndices * sizeof( WORD );
bufferDesc.Usage = D3D10_USAGE_DEFAULT;
bufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
WORD wIndices[] =
{
0,1,2,
0,2,3,
};
// copie les valeurs des index dans l'IB
D3D10_SUBRESOURCE_DATA ibInitData;
ZeroMemory( &ibInitData, sizeof( D3D10_SUBRESOURCE_DATA ) );
ibInitData.pSysMem = wIndices; // copie indices dans buffer
VerifHR( pd3dDevice->CreateBuffer( &bufferDesc, &ibInitData, &g_pQuadInstIB ) );
if( hr == S_OK )
OutputDebugString( L"InitQuadInst() g_pQuadInstIB quad screen index buffer créé\n" );
else
OutputDebugString( L"ERREUR!!! InitQuadInst() g_pQuadInstIB création échouée\n" );
// créé un buffer d'instanciation dynamique pour les input matrices
D3D10_BUFFER_DESC InstbufferDesc =
{
uiNbInstances * sizeof( D3DXMATRIX ),
D3D10_USAGE_DYNAMIC,
D3D10_BIND_VERTEX_BUFFER,
D3D10_CPU_ACCESS_WRITE,
0
};
VerifHR( pd3dDevice->CreateBuffer( &InstbufferDesc, NULL, &g_pQuadInstanceData ) );
if( hr == S_OK )
OutputDebugString( L"InitQuadInst() g_pQuadInstanceData quad screen instance buffer créé\n" );
else
OutputDebugString( L"ERREUR!!! InitQuadInst() g_pQuadInstanceData création échouée\n" );
// créé tableau de input matrices data
float fRot = D3DX_PI/8.0f;
D3DXMATRIX mTrans;
D3DXMATRIX mRot;
D3DXMatrixRotationZ( &mRot, fRot );
D3DXMatrixTranslation( &mTrans, 0, 0, 1 );
g_pInstanceMatrices[0] = mTrans;
g_pInstanceMatrices[1] = mRot * mTrans;
D3DXMATRIX* pDestMatrices = NULL;
D3DXMATRIX* pScrMatrices = g_pInstanceMatrices;
// copie tableau de matrices dans vb dynamique
g_pQuadInstanceData->Map( D3D10_MAP_WRITE_DISCARD, NULL, ( void** )&pDestMatrices );
memcpy( pDestMatrices, pScrMatrices, NB_QUAD_INSTANCES * sizeof( D3DXMATRIX ) );
g_pQuadInstanceData->Unmap();
}
// assigne variables shader
D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
g_pQuadWorldVariable->SetMatrix( ( float* )&matWorld );
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(
&matProj,
D3DX_PI / 2.0f,
1.0f, // ratio écran
0.01f,
1000.0f
);
g_pQuadProjectionVariable->SetMatrix( ( float* )&matProj ); // matrice de projection
D3DXMATRIX matView;
D3DXVECTOR3 vEye( 0.0f, 0.0f, 10.0f ); // distance de vue de la skybox
D3DXVECTOR3 vAt( 0.0f, 0.0f, 1.0f );
D3DXVECTOR3 vUp( 0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH( &matView, &vEye, &vAt, &vUp );
g_pQuadViewVariable->SetMatrix( ( float* )&matView ); // maj matrice de vue
// calcul matrices shader
D3DXMATRIX mWorldViewProj;
D3DXMatrixMultiply( &mWorldViewProj, &g_matView, &g_matProj );
g_pmWorldViewProj->SetMatrix( ( float* )&mWorldViewProj );
g_pmWorldView->SetMatrix( ( float* )&g_matView );
// acquiert technique
g_pQuadInstTechnique = g_pQuadInstEffect->GetTechniqueByName( "RenderQuadInst" );
if ( g_pQuadInstTechnique->IsValid() != TRUE )
OutputDebugString( L" ERREUR!!! technique instanciation\n" );
// rendu quads indexés avec instanciation
UINT strides[2] = { sizeof( QUADSCREEN_VERTEX4 ), sizeof( D3DXMATRIX ) };
UINT offsets[2] = {0, 0};
ID3D10Buffer* pBuffers[2] =
{
g_pQuadInstVB, g_pQuadInstanceData
};
pd3dDevice->IASetInputLayout( g_pQuadInstLayout );
pd3dDevice->IASetVertexBuffers( 0, 2, pBuffers, strides, offsets );
pd3dDevice->IASetIndexBuffer( g_pQuadInstIB, DXGI_FORMAT_R16_UINT, 0 );
pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP );
VerifHR( g_pQuadtxDiffuseVariable->SetResource( g_pPaletteCam_SRV ) ); // assigne texture au quad
pd3dDevice->VSSetShaderResources(0, 1, &g_pPaletteCam_SRV);
pd3dDevice->PSSetShaderResources( 0, 1, &g_pPaletteCam_SRV );
D3D10_TECHNIQUE_DESC techDesc;
g_pQuadInstTechnique->GetDesc( &techDesc );
for( UINT iPass = 0; iPass < techDesc.Passes; iPass++ )
{
g_pQuadInstTechnique->GetPassByIndex( iPass )->Apply( 0 );
pd3dDevice->DrawIndexedInstanced( 6, 1 * uiNbInstances, 0, 0, 0 );
}
return;
} |
Partager