IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

DirectX Discussion :

Problème d'instanciation [DirectX 10]


Sujet :

DirectX

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    143
    Détails du profil
    Informations personnelles :
    Localisation : France, Indre et Loire (Centre)

    Informations forums :
    Inscription : Mars 2006
    Messages : 143
    Par défaut Problème d'instanciation
    bonjour
    j'essaye d'instancier des quads et de les dessiner avec DrawIndexedInstanced()

    mais je rencontre un message d'erreur dont j'ai du mal à comprendre la signification :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    D3D10: ERROR: ID3D10Device::DrawIndexedInstanced: Input Assembler - Vertex Shader linkage error: 
    Signatures between stages are incompatible. 
    The reason is that Semantic 'POSITION' of the input stage has a hardware register component mask that is not a subset of the output of the previous stage. 
    [ EXECUTION ERROR #345: DEVICE_SHADER_LINKAGE_REGISTERMASK ]
    ce qui semble m'indiquer un problème au niveau des VB

    problème de POSITION dans le PS ?

    mais qu'est ce qu'un : hardware register component mask

    voici ce que je fais
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    	HRESULT hr = E_FAIL;
     
     
    	// acquiert technique
    	g_pRenderInstancedVertLighting = g_pQuadScreenEffect->GetTechniqueByName( "RenderInstancedVertLighting" );
     
    	if ( g_pRenderInstancedVertLighting->IsValid() != TRUE )
    		OutputDebugString( L" ERREUR!!! technique instanciation\n" );
     
    	// acquiert variables shader
    	g_pQuadtxDiffuseVariable	= g_pQuadScreenEffect->GetVariableByName( "txDiffuse" )->AsShaderResource();// texture de fond par défaut
    	g_pQuadWorldVariable		= g_pQuadScreenEffect->GetVariableByName( "mWorld" )->AsMatrix();
    	g_pQuadViewVariable 		= g_pQuadScreenEffect->GetVariableByName( "mView" )->AsMatrix();
    	g_pQuadProjectionVariable	= g_pQuadScreenEffect->GetVariableByName( "mProj" )->AsMatrix();
    	g_pQuadColorVariable		= g_pQuadScreenEffect->GetVariableByName( "vColorCorrect" )->AsVector();
    	g_pmWorldViewProj			= g_pQuadScreenEffect->GetVariableByName( "g_mWorldViewProj" )->AsMatrix();
        g_pmWorldView				= g_pQuadScreenEffect->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, 24, 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_pRenderInstancedVertLighting->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" );
     
     
     
    	/*     
    	//-------------------------------//
       
          |---------|
          |D       C|
          |         |
          |         |
          |A       B|
          |---------|
          O<-----branch
       
    	//----------------------------//  
    	 */ 
     
    	// créé le quad vb en triangle strip
    	struct QUADSCREEN_VERTEX4			// structure de description du quad screen vertex
    	{
    		float x,y,z;					// coord
    		float nx, ny, nz;				// normale
    		float tu, tv;					// 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[] =
        {
            { fLeft, fBottom,	0.0f,		0.0f, 0.0f, 1.0f,		0.0f, 1.0f },
            { fRight, fBottom,	0.0f,		0.0f, 0.0f, 1.0f,		1.0f, 1.0f },
            { fRight, fTop,		0.0f,		0.0f, 0.0f, 1.0f,		1.0f, 0.0f },
            { fLeft, fTop,		0.0f,		0.0f, 0.0f, 1.0f,		0.0f, 0.0f },
        };
     
     
    	// 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;
    	InitData.SysMemPitch = 0;
    	InitData.SysMemSlicePitch = 0;
     
    	VerifHR( pd3dDevice->CreateBuffer( &BufDesc, &InitData, &g_pQuadScreenVB ) );
    	if( hr == S_OK )
    		OutputDebugString( L"InitQuadInst() g_pQuadScreenVB quad screen vertex buffer créé\n" );
    	else
    		OutputDebugString( L"ERREUR!!! InitQuadInst() g_pQuadScreenVB création échouée\n" );
     
     
    	// copie data dans vb quad
    	QUADSCREEN_VERTEX4* pVB;
    	VerifHR( g_pQuadScreenVB->Map( D3D10_MAP_WRITE_DISCARD , 0, ( LPVOID* )&pVB ) );	// D3D10_MAP_READ_WRITE  D3D10_MAP_WRITE 
    	memcpy( pVB, pVertices, sizeof( pVertices ) );
    	g_pQuadScreenVB->Unmap();
     
     
     
        // créé un index buffer pour le quad en TRIANGLE_STRIP
    	DWORD dwNumIndices = 12;
        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,
            0,2,1,
            0,3,2,
        };
     
    	// copie les valeurs des index dans l'IB
        D3D10_SUBRESOURCE_DATA ibInitData;
        ZeroMemory( &ibInitData, sizeof( D3D10_SUBRESOURCE_DATA ) );
        ibInitData.pSysMem = wIndices;
     
        VerifHR( pd3dDevice->CreateBuffer( &bufferDesc, &ibInitData, &g_pQuadScreenInstIB ) );
    	if( hr == S_OK )
    		OutputDebugString( L"InitQuadInst() g_pQuadScreenIB quad screen index buffer créé\n" );
    	else
    		OutputDebugString( L"ERREUR!!! InitQuadInst() g_pQuadScreenIB création échouée\n" );
     
     
     
    	// créé un buffer d'instanciation dynamique pour les input matrices
        D3D10_BUFFER_DESC InstbufferDesc =
        {
            g_uiNumInstances * 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/4.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 couleur de correction
    	VerifHR( g_pQuadColorVariable->SetFloatVector( (float*)&D3DXVECTOR4( 1.0f, 0.5f, 0.5f, 1.0f ) ) );
     
    	// 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, -1.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 );
     
    	VerifHR( g_pQuadtxDiffuseVariable->SetResource( pQuadScreenSRV ) );				// assigne texture au quad 
     
     
    	// rendu quads indexés avec instanciation
        UINT strides[2] = { sizeof( QUADSCREEN_VERTEX4 ), sizeof( D3DXMATRIX ) };
        UINT offsets[2] = {0, 0};
     
        ID3D10Buffer* pBuffers[2] =
        {
           g_pQuadScreenVB, g_pQuadInstanceData
        };
     
    	pd3dDevice->IASetInputLayout( g_pQuadInstLayout );
        pd3dDevice->IASetVertexBuffers( 0, 2, pBuffers, strides, offsets );
        pd3dDevice->IASetIndexBuffer( g_pQuadScreenIB, DXGI_FORMAT_R16_UINT, 0 );
        pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP );
    	pd3dDevice->VSSetShaderResources(0, 1, &pQuadScreenSRV);
    	pd3dDevice->PSSetShaderResources( 0, 1, &pQuadScreenSRV );	
     
    	D3D10_TECHNIQUE_DESC techDesc;
        g_pRenderInstancedVertLighting->GetDesc( &techDesc );
     
    	for( UINT iPass = 0; iPass < techDesc.Passes; iPass++ )
        {
            g_pQuadScreenTechnique->GetPassByIndex( iPass )->Apply( 0 );
            pd3dDevice->DrawIndexedInstanced( 12, 1 * NB_QUAD_INSTANCES, 0, 0, 0 );
        }
     
    	return;
    et voici le shader associé

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    //--------------------------------------------------------------------------------------
    // Buffer Variables
    //--------------------------------------------------------------------------------------
    Texture2D	txDiffuse;
     
    cbuffer cbChangesEveryFrame
    {
    	float fTranslation;
    	float vShaderContrast;
    	float vShaderBrightness;
    	float4 vColorCorrect;			// correction couleur diffuse
        matrix mWorld;
        matrix mView;
        matrix mProj;
        float4x4 g_mWorldViewProj;
        float4x4 g_mWorldView;
     
    	};
     
     
    //--------------------------------------------------------------------------------------
    // échantillonneurs
    //--------------------------------------------------------------------------------------
     
    SamplerState samLinear
    {
        Filter = MIN_MAG_MIP_LINEAR;
        AddressU = Wrap;
        AddressV = Wrap;
    };
     
     
    //--------------------------------------------------------------------------------------
    // DepthStates, stencil de profondeur
    //--------------------------------------------------------------------------------------
    DepthStencilState EnableDepth
    {
        DepthEnable = TRUE;
        DepthWriteMask = ALL;
        DepthFunc = LESS_EQUAL;
    };
     
    DepthStencilState DisableDepth
    {
        DepthEnable = FALSE;
    };
     
    DepthStencilState EnableDepthTestWrite
    {
        DepthEnable = TRUE;
        DepthWriteMask = ALL;
    };
    //--------------------------------------------------------------------------------------
    // Blending States, mode de blending
    //--------------------------------------------------------------------------------------
    BlendState NoBlending
    {
        BlendEnable[0] = FALSE;
    };
     
    BlendState SrcAlphaInvSrcAlpha
    {
        BlendEnable[0] = TRUE;
        SrcBlend = SRC_ALPHA;
        DestBlend = INV_SRC_ALPHA;
        BlendOp = ADD;
        SrcBlendAlpha = ZERO;
        DestBlendAlpha = ZERO;
        BlendOpAlpha = ADD;
        RenderTargetWriteMask[0] = 0x0F;
    };
     
     
    //--------------------------------------------------------------------------------------
    // structures d'entrée /sortie shader
    //--------------------------------------------------------------------------------------
     
    struct QUADVS_INPUT
    {
        float4 Pos : POSITION;
        float2 Tex : TEXCOORD0;
    };
     
    struct QUADVS_OUTPUT
    {
        float4 Pos		: SV_POSITION;              // transformed position
    //	float4 Diffuse	: COLOR0;					// couleur diffuse
        float2 Tex		: TEXCOORD0;
    };
     
     
    struct VSInstIn									// structure pour instanciation
    {
        float3 pos : POSITION;
        float3 norm : NORMAL;
        float2 tex : TEXTURE0;
        row_major float4x4 mTransform : mTransform;
    };
     
    struct PSSceneIn
    {
        float4 pos : SV_Position;	
        float2 tex : TEXTURE0;
        float4 color : COLOR0;
    };
     
     
     
    //--------------------------------------------------------------------------------------
    // Instancing vertex shader.  Positionne les vertices avec les matrices stockées 
    // dans le second vertex stream.
    //--------------------------------------------------------------------------------------
    PSSceneIn VSInstmain(VSInstIn input)
    {
        PSSceneIn output;
     
        // transforme par la matrice d'instanciation
        float4 InstancePosition = mul( float4(input.pos, 1), input.mTransform );
        float4 ViewPos = mul( InstancePosition, g_mWorldView );
     
     
        // transforme les vertices en view-space
        float4 v4Position = mul(InstancePosition, g_mWorldViewProj);
        output.pos = v4Position;
     
        // transfert coord de mapping
        output.tex = input.tex;
     
    	// couleur de correction transmise au PS
    	output.color = float4( 1, 1, 1, 1 );
     
        return output;
    }
     
    //--------------------------------------------------------------------------------------
    // Pixel shader pour instanciation
    //--------------------------------------------------------------------------------------
    float4 PSScenemain(PSSceneIn input) : SV_Target
    {
        float4 color = txDiffuse.Sample( samLinear, input.tex ) * input.color;
        return color;
    }
     
    //--------------------------------------------------------------------------------------
    // Render instanced meshes with vertex lighting
    //--------------------------------------------------------------------------------------
    technique10 RenderInstancedVertLighting
    {
        pass p0
        {
            SetVertexShader( CompileShader( vs_4_0, VSInstmain() ) );
            SetGeometryShader( NULL );
            SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );     
            SetBlendState( NoBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
            SetDepthStencilState( EnableDepthTestWrite, 0 );
        }  
    }
    merci de m'éclairer de vos lumières

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    143
    Détails du profil
    Informations personnelles :
    Localisation : France, Indre et Loire (Centre)

    Informations forums :
    Inscription : Mars 2006
    Messages : 143
    Par défaut
    à noter:

    il y 2 fois trop d'indices pour le quad
    mais après correction, cela ne change rien pour l'erreur
    avec DrawIndexedInstanced(...)

  3. #3
    Membre chevronné

    Inscrit en
    Août 2007
    Messages
    300
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 300
    Par défaut
    A première vue, ce n'est pas une erreur de POSITION dans le PS, mais pplus probablement le fait que POSITION est attribué à un champ float3 (edit: dans votre structure VSInstIn). J'avoue avoir toujours appliqué bêtement la doc indiquant que POSITION est toujours float4, et donc je suppose que quand on a une "input layout" avec des float3 (cas courant), la fonction CreateInputLayout fait le lien entre la technique compilée et la description par programme des VB (votre tableau instlayout).

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    143
    Détails du profil
    Informations personnelles :
    Localisation : France, Indre et Loire (Centre)

    Informations forums :
    Inscription : Mars 2006
    Messages : 143
    Par défaut
    le problème venait de la déclaration du layout et du shader

    sémantique TEXTURE dans le layout
    et TEXTCOORD0 dans le shader

    j'ai essayé de uploader un projet démo mais je n'y arrive pas

    la position en float3 semble OK

    je n'ai plus de message d'erreur lors du Draw, mais pas d'affichage

    voici le code en une fonction ( pas de clean , init à chaque passage )
    faut-il spécifier des rendertarget ?


    la procédure
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    static bool bInited = false;
    //--------------------------------------------------------------------------------------
    // Nom:	RenduQuadInst()
    // Desc: rendu quadscreen avec coordonnées écran
    //--------------------------------------------------------------------------------------
    void RenduQuadInst( ID3D10Device* pd3dDevice, int iXCoord, int iYCoord, 
    	int iLargeur, int iHauteur, ID3D10ShaderResourceView* pQuadScreenSRV )
    {
    	HRESULT hr = E_FAIL;
     
     
     
    	/*     
    	//-------------------------------//
       
          |---------|
          |D       C|
          |         |
          |         |
          |A       B|
          |---------|
          O<-----branch
       
    	//----------------------------//  
    	 */ 
     
    	// créé le quad vb en triangle strip
    	struct QUADSCREEN_VERTEX4			// structure de description du quad screen vertex
    	{
    		float x,y,z;					// coord
    		float nx, ny, nz;				// normale
    		float tu, tv;					// 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[] =
        {
            { fLeft, fBottom,	0.0f,		0.0f, 0.0f, 1.0f,		0.0f, 1.0f },
            { fRight, fBottom,	0.0f,		0.0f, 0.0f, 1.0f,		1.0f, 1.0f },
            { fRight, fTop,		0.0f,		0.0f, 0.0f, 1.0f,		1.0f, 0.0f },
            { fLeft, fTop,		0.0f,		0.0f, 0.0f, 1.0f,		0.0f, 0.0f },
        };
     
     
     
    	if ( bInited == false )
    	{
    		bInited = true;
     
    		// acquiert technique
    		g_pRenderInstancedVertLighting = g_pQuadScreenEffect->GetTechniqueByName( "RenderInstancedVertLighting" );
     
    		if ( g_pRenderInstancedVertLighting->IsValid() != TRUE )
    			OutputDebugString( L" ERREUR!!! technique instanciation\n" );
     
    		// acquiert variables shader
    		g_pQuadtxDiffuseVariable	= g_pQuadScreenEffect->GetVariableByName( "txDiffuse" )->AsShaderResource();// texture de fond par défaut
    		g_pQuadWorldVariable		= g_pQuadScreenEffect->GetVariableByName( "mWorld" )->AsMatrix();
    		g_pQuadViewVariable 		= g_pQuadScreenEffect->GetVariableByName( "mView" )->AsMatrix();
    		g_pQuadProjectionVariable	= g_pQuadScreenEffect->GetVariableByName( "mProj" )->AsMatrix();
    		g_pQuadColorVariable		= g_pQuadScreenEffect->GetVariableByName( "vColorCorrect" )->AsVector();
    		g_pmWorldViewProj			= g_pQuadScreenEffect->GetVariableByName( "g_mWorldViewProj" )->AsMatrix();
    		g_pmWorldView				= g_pQuadScreenEffect->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, 24, 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_pRenderInstancedVertLighting->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;
    		InitData.SysMemPitch = 0;
    		InitData.SysMemSlicePitch = 0;
     
    		VerifHR( pd3dDevice->CreateBuffer( &BufDesc, &InitData, &g_pQuadScreenVB ) );
    		if( hr == S_OK )
    			OutputDebugString( L"InitQuadInst() g_pQuadScreenVB quad screen vertex buffer créé\n" );
    		else
    			OutputDebugString( L"ERREUR!!! InitQuadInst() g_pQuadScreenVB création échouée\n" );
     
     
    		// copie data dans vb quad
    		QUADSCREEN_VERTEX4* pVB;
    		VerifHR( g_pQuadScreenVB->Map( D3D10_MAP_WRITE_DISCARD , 0, ( LPVOID* )&pVB ) );	// D3D10_MAP_READ_WRITE  D3D10_MAP_WRITE 
    		memcpy( pVB, pVertices, sizeof( pVertices ) );
    		g_pQuadScreenVB->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;
     
    		VerifHR( pd3dDevice->CreateBuffer( &bufferDesc, &ibInitData, &g_pQuadScreenInstIB ) );
    		if( hr == S_OK )
    			OutputDebugString( L"InitQuadInst() g_pQuadScreenIB quad screen index buffer créé\n" );
    		else
    			OutputDebugString( L"ERREUR!!! InitQuadInst() g_pQuadScreenIB création échouée\n" );
     
     
     
    		// créé un buffer d'instanciation dynamique pour les input matrices
    		D3D10_BUFFER_DESC InstbufferDesc =
    		{
    			g_uiNumInstances * 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/4.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 couleur de correction
    	VerifHR( g_pQuadColorVariable->SetFloatVector( (float*)&D3DXVECTOR4( 1.0f, 0.5f, 0.5f, 1.0f ) ) );
     
    	// 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, -1.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 );
     
    	VerifHR( g_pQuadtxDiffuseVariable->SetResource( pQuadScreenSRV ) );				// assigne texture au quad 
     
     
    	// rendu quads indexés avec instanciation
        UINT strides[2] = { sizeof( QUADSCREEN_VERTEX4 ), sizeof( D3DXMATRIX ) };
        UINT offsets[2] = {0, 0};
     
        ID3D10Buffer* pBuffers[2] =
        {
           g_pQuadScreenVB, g_pQuadInstanceData
        };
     
    	pd3dDevice->IASetInputLayout( g_pQuadScreenVertexLayout );
        pd3dDevice->IASetVertexBuffers( 0, 2, pBuffers, strides, offsets );
        pd3dDevice->IASetIndexBuffer( g_pQuadScreenIB, DXGI_FORMAT_R16_UINT, 0 );
        pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP );
    	pd3dDevice->VSSetShaderResources(0, 1, &pQuadScreenSRV);
    	pd3dDevice->PSSetShaderResources( 0, 1, &g_pPaletteCam_SRV );	
     
    	D3D10_TECHNIQUE_DESC techDesc;
        g_pRenderInstancedVertLighting->GetDesc( &techDesc );
     
    	for( UINT iPass = 0; iPass < techDesc.Passes; iPass++ )
        {
            g_pQuadScreenTechnique->GetPassByIndex( iPass )->Apply( 0 );
            pd3dDevice->DrawIndexedInstanced( 6, 1 * NB_QUAD_INSTANCES, 0, 0, 0 );
        }
     
    	return;
    }
    le shader
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    //--------------------------------------------------------------------------------------
    // File: QuadScreen.fx
    // rendu quad écran
    //--------------------------------------------------------------------------------------
     
     
     
    //--------------------------------------------------------------------------------------
    // Buffer Variables
    //--------------------------------------------------------------------------------------
    Texture2D	txDiffuse;
     
    cbuffer cbChangesEveryFrame
    {
    	float fTranslation;
    	float vShaderContrast;
    	float vShaderBrightness;
    	float4 vColorCorrect;			// correction couleur diffuse
        matrix mWorld;
        matrix mView;
        matrix mProj;
        float4x4 g_mWorldViewProj;
        float4x4 g_mWorldView;
     
    	};
     
     
    //--------------------------------------------------------------------------------------
    // échantillonneurs
    //--------------------------------------------------------------------------------------
     
    SamplerState samLinear
    {
        Filter = MIN_MAG_MIP_LINEAR;
        AddressU = Wrap;
        AddressV = Wrap;
    };
     
     
    //--------------------------------------------------------------------------------------
    // DepthStates, stencil de profondeur
    //--------------------------------------------------------------------------------------
    DepthStencilState EnableDepth
    {
        DepthEnable = TRUE;
        DepthWriteMask = ALL;
        DepthFunc = LESS_EQUAL;
    };
     
    DepthStencilState DisableDepth
    {
        DepthEnable = FALSE;
    };
     
    DepthStencilState EnableDepthTestWrite
    {
        DepthEnable = TRUE;
        DepthWriteMask = ALL;
    };
    //--------------------------------------------------------------------------------------
    // Blending States, mode de blending
    //--------------------------------------------------------------------------------------
    BlendState NoBlending
    {
        BlendEnable[0] = FALSE;
    };
     
    BlendState SrcAlphaInvSrcAlpha
    {
        BlendEnable[0] = TRUE;
        SrcBlend = SRC_ALPHA;
        DestBlend = INV_SRC_ALPHA;
        BlendOp = ADD;
        SrcBlendAlpha = ZERO;
        DestBlendAlpha = ZERO;
        BlendOpAlpha = ADD;
        RenderTargetWriteMask[0] = 0x0F;
    };
     
     
    //--------------------------------------------------------------------------------------
    // structures d'entrée /sortie shader
    //--------------------------------------------------------------------------------------
     
    struct QUADVS_INPUT
    {
        float4 Pos : POSITION;
        float2 Tex : TEXCOORD0;
    };
     
    struct QUADVS_OUTPUT
    {
        float4 Pos		: SV_POSITION;              // transformed position
    //	float4 Diffuse	: COLOR0;					// couleur diffuse
        float2 Tex		: TEXCOORD0;
    };
     
     
    struct VSInstIn									// structure pour instanciation
    {
        float3 pos : POSITION;
        float3 norm : NORMAL;
        float2 tex : TEXTURE0;
        row_major float4x4 mTransform : mTransform;
    };
     
    struct PSSceneIn
    {
        float4 pos : SV_Position;	
        float2 tex : TEXTURE0;
        float4 color : COLOR0;
    };
     
     
     
    //--------------------------------------------------------------------------------------
    // Instancing vertex shader.  Positionne les vertices avec les matrices stockées 
    // dans le second vertex stream.
    //--------------------------------------------------------------------------------------
    PSSceneIn VSInstmain(VSInstIn input)
    {
        PSSceneIn output;
     
        // transforme par la matrice d'instanciation
        float4 InstancePosition = mul( float4(input.pos, 1), input.mTransform );
        float4 ViewPos = mul( InstancePosition, g_mWorldView );
     
     
        // transforme les vertices en view-space
        float4 v4Position = mul(InstancePosition, g_mWorldViewProj);
        output.pos = v4Position;
     
        // transfert coord de mapping
        output.tex = input.tex;
     
    	// couleur de correction transmise au PS
    	output.color = float4( 1, 1, 1, 1 );
     
        return output;
    }
     
    //--------------------------------------------------------------------------------------
    // Pixel shader pour instanciation
    //--------------------------------------------------------------------------------------
    float4 PSScenemain(PSSceneIn input) : SV_Target
    {
        float4 color = txDiffuse.Sample( samLinear, input.tex ) * input.color;
        return color;
    }
     
     
     
     
     
    //-----------------------------------------------------------------------------
    // Quad Vertex Shaders
    //-----------------------------------------------------------------------------
    QUADVS_OUTPUT QuadVS( QUADVS_INPUT Input )
    {
        QUADVS_OUTPUT Output;
        Output.Pos = mul( Input.Pos, mWorld );
        Output.Pos = mul( Output.Pos, mView );
        Output.Pos = mul( Output.Pos, mProj );
        Output.Tex = Input.Tex;
     
        return Output;
    }
     
    //--------------------------------------------------------------------------------------
    // Quad Pixel Shader
    //--------------------------------------------------------------------------------------
    float4 QuadPS( QUADVS_OUTPUT Input) : SV_Target
    {
       return txDiffuse.Sample( samLinear, Input.Tex );
     
    }
     
     
    //--------------------------------------------------------------------------------------
    // Quad Pixel Shader + translation sur X
    //--------------------------------------------------------------------------------------
    float4 QuadPSTrans( QUADVS_OUTPUT Input) : SV_Target
    {
    	Input.Tex.x += fTranslation;						// incrémente coordonnées de mapping sur X
    	return txDiffuse.Sample( samLinear, Input.Tex );
     
    }
    //--------------------------------------------------------------------------------------
    // pixel shader avec couleur diffuse de correction du quad
    //--------------------------------------------------------------------------------------
    float4 QuadColorPS( QUADVS_OUTPUT Input) : SV_Target
    {
     
    	float4 cDiffuse = txDiffuse.Sample( samLinear, Input.Tex );		// échantillonage texture
    	cDiffuse *= vColorCorrect;										// facteur de correction couleur du quad
    	return cDiffuse;	
    }
     
     
    //------------------//
    //    techniques    //
    //------------------//
     
     
    //--------------------------------------------------------------------------------------
    // rendu quad solide
    //--------------------------------------------------------------------------------------
    technique10 RenderQuadSolid
    {
        pass P0
        {
            SetVertexShader( CompileShader( vs_4_0, QuadVS() ) );
            SetGeometryShader( NULL );
            SetPixelShader( CompileShader( ps_4_0, QuadPS() ) );               
            SetBlendState( NoBlending, float4( 1.0f, 1.0f, 1.0f, 1.0f ), 0xFFFFFFFF );
        }
    }
     
    //--------------------------------------------------------------------------------------
    // rendu quad avec transparence sur l'alpha
    //--------------------------------------------------------------------------------------
    technique10 RenderQuadAlpha
    {
        pass P0
        {
            SetVertexShader( CompileShader( vs_4_0, QuadVS() ) );
            SetGeometryShader( NULL );
            SetPixelShader( CompileShader( ps_4_0, QuadPS() ) );   
    		SetBlendState( SrcAlphaInvSrcAlpha, float4( 1.0f, 1.0f, 1.0f, 1.0f ), 0xFFFFFFFF );
        }
    }
     
    //--------------------------------------------------------------------------------------
    // rendu quad avec transparence sur l'alpha et quad au premier plan
    //--------------------------------------------------------------------------------------
    technique10 RenderQuadAlphaNoDepth
    {
        pass P0
        {
            SetVertexShader( CompileShader( vs_4_0, QuadVS() ) );
            SetGeometryShader( NULL );
            SetPixelShader( CompileShader( ps_4_0, QuadPS() ) );   
    		SetBlendState( SrcAlphaInvSrcAlpha, float4( 1.0f, 1.0f, 1.0f, 1.0f ), 0xFFFFFFFF );
    		SetDepthStencilState( DisableDepth, 0 );												// pas de z-buffering
        }
    }
     
     
    //--------------------------------------------------------------------------------------
    // rendu quad avec correction couleur diffuse
    //--------------------------------------------------------------------------------------
    technique10 RenderQuadColorCorrect
    {
        pass P0
        {
            SetVertexShader( CompileShader( vs_4_0, QuadVS() ) );
            SetGeometryShader( NULL );
            SetPixelShader( CompileShader( ps_4_0, QuadColorPS() ) );   
    //		SetBlendState( NoBlending, float4( 1.0f, 1.0f, 1.0f, 1.0f ), 0xFFFFFFFF );
    		SetBlendState( SrcAlphaInvSrcAlpha, float4( 1.0f, 1.0f, 1.0f, 1.0f ), 0xFFFFFFFF );
    		SetDepthStencilState( DisableDepth, 0 );												// pas de z-buffering
        }
    }
     
    //--------------------------------------------------------------------------------------
    // rendu quad test
    //--------------------------------------------------------------------------------------
    technique10 RenderQuadTest
    {
        pass P0
        {
            SetVertexShader( CompileShader( vs_4_0, QuadVS() ) );
            SetGeometryShader( NULL );
            SetPixelShader( CompileShader( ps_4_0, QuadPS() ) );   
    		SetBlendState( SrcAlphaInvSrcAlpha, float4( 1.0f, 1.0f, 1.0f, 1.0f ), 0xFFFFFFFF );
    		SetDepthStencilState( DisableDepth, 0 );												// pas de z-buffering
        }
    }
     
    //--------------------------------------------------------------------------------------
    // rendu solid quad + translation sur X
    //--------------------------------------------------------------------------------------
    technique10 RenderQuadTrans
    {
        pass P0
        {
            SetVertexShader( CompileShader( vs_4_0, QuadVS() ) );
            SetGeometryShader( NULL );
            SetPixelShader( CompileShader( ps_4_0, QuadPSTrans() ) );
            SetBlendState( NoBlending, float4( 1.0f, 1.0f, 1.0f, 1.0f ), 0xFFFFFFFF );
    		SetDepthStencilState( DisableDepth, 0 );	 
    	}
    }
     
     
     
    //--------------------------------------------------------------------------------------
    // Render instanced meshes with vertex lighting
    //--------------------------------------------------------------------------------------
    technique10 RenderInstancedVertLighting
    {
        pass p0
        {
            SetVertexShader( CompileShader( vs_4_0, VSInstmain() ) );
            SetGeometryShader( NULL );
            SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );     
            SetBlendState( NoBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
            SetDepthStencilState( DisableDepth, 0 );
        }  
    }

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    143
    Détails du profil
    Informations personnelles :
    Localisation : France, Indre et Loire (Centre)

    Informations forums :
    Inscription : Mars 2006
    Messages : 143
    Par défaut
    voici la dernière version qui ne plante plus, mais n'affiche rien

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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;
    }
    le shader
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    //--------------------------------------------------------------------------------------
    // File: QuadInst.fx
    // rendu quad écran instancié
    //--------------------------------------------------------------------------------------
     
     
     
    //--------------------------------------------------------------------------------------
    // Buffer Variables
    //--------------------------------------------------------------------------------------
    Texture2D	txDiffuse;
     
    cbuffer cbChangesEveryFrame
    {
        matrix mWorld;
        matrix mView;
        matrix mProj;
        float4x4 g_mWorldViewProj;
        float4x4 g_mWorldView;
     
    };
     
     
    //--------------------------------------------------------------------------------------
    // échantillonneurs
    //--------------------------------------------------------------------------------------
     
    SamplerState samLinear
    {
        Filter = MIN_MAG_MIP_LINEAR;
        AddressU = Wrap;
        AddressV = Wrap;
    };
     
     
    //--------------------------------------------------------------------------------------
    // DepthStates, stencil de profondeur
    //--------------------------------------------------------------------------------------
    DepthStencilState EnableDepth
    {
        DepthEnable = TRUE;
        DepthWriteMask = ALL;
        DepthFunc = LESS_EQUAL;
    };
     
    DepthStencilState DisableDepth
    {
        DepthEnable = FALSE;
    };
     
    DepthStencilState EnableDepthTestWrite
    {
        DepthEnable = TRUE;
        DepthWriteMask = ALL;
    };
    //--------------------------------------------------------------------------------------
    // Blending States, mode de blending
    //--------------------------------------------------------------------------------------
    BlendState NoBlending
    {
        BlendEnable[0] = FALSE;
    };
     
    BlendState SrcAlphaInvSrcAlpha
    {
        BlendEnable[0] = TRUE;
        SrcBlend = SRC_ALPHA;
        DestBlend = INV_SRC_ALPHA;
        BlendOp = ADD;
        SrcBlendAlpha = ZERO;
        DestBlendAlpha = ZERO;
        BlendOpAlpha = ADD;
        RenderTargetWriteMask[0] = 0x0F;
    };
     
     
    //--------------------------------------------------------------------------------------
    // structures d'entrée /sortie shader
    //--------------------------------------------------------------------------------------
     
    struct VSInstIn									// structure pour instanciation
    {
        float3 pos : POSITION;
        float2 tex : TEXTURE0;
        row_major float4x4 mTransform : mTransform;
    };
     
    struct PSSceneIn
    {
        float4 pos : SV_Position;
    	float2 tex : TEXTURE0;
        float4 color : COLOR;
    };
     
     
     
    //--------------------------------------------------------------------------------------
    // Instancing vertex shader.  Positionne les vertices avec les matrices stockées 
    // dans le second vertex stream.
    //--------------------------------------------------------------------------------------
    PSSceneIn VSInstmain(VSInstIn input)
    {
        PSSceneIn output;
     
        // transforme par la matrice d'instanciation
        float4 InstancePosition = mul( float4(input.pos, 1), input.mTransform );
        float4 ViewPos = mul( InstancePosition, g_mWorldView );
     
     
        // transforme les vertices en view-space
        float4 v4Position = mul(InstancePosition, g_mWorldViewProj);
        output.pos = v4Position;
     
        // transfert coord de mapping
        output.tex = input.tex;
     
     
    	// couleur de correction transmise au PS
    	output.color = float4( 1, 1, 1, 1 );
     
     
     
        return output;
    }
     
    //--------------------------------------------------------------------------------------
    // Pixel shader pour instanciation
    //--------------------------------------------------------------------------------------
    float4 PSScenemain(PSSceneIn input) : SV_Target
    {
        float4 color = txDiffuse.Sample( samLinear, input.tex ) * input.color;
        return color;
    }
     
     
     
     
     
     
    //--------------------------------------------------------------------------------------
    // Rendu quad instancié
    //--------------------------------------------------------------------------------------
    technique10 RenderQuadInst
    {
        pass p0
        {
            SetVertexShader( CompileShader( vs_4_0, VSInstmain() ) );
            SetGeometryShader( NULL );
            SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );     
            SetBlendState( NoBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
            SetDepthStencilState( EnableDepth, 0 );
        }  
    }

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    143
    Détails du profil
    Informations personnelles :
    Localisation : France, Indre et Loire (Centre)

    Informations forums :
    Inscription : Mars 2006
    Messages : 143
    Par défaut
    avec les bonnes matrices de vue, ça marche

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    	// 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, -1.0f );											// distance de vue
    	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, &matView, &matProj );
    	g_pmWorldViewProj->SetMatrix( ( float* )&mWorldViewProj );
     
    	g_pmWorldView->SetMatrix( ( float* )&matView );
    merci pour l'aide

  7. #7
    Membre extrêmement actif

    Profil pro
    Inscrit en
    Février 2006
    Messages
    2 408
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 2 408
    Par défaut
    bonjour, il y a moyen que tu mettes un code complet, genre avec le main, pour tester "out of the box" ?

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [C#][Architecture] Problème d'instanciation
    Par loic_86 dans le forum C#
    Réponses: 1
    Dernier message: 06/03/2007, 14h59
  2. [JSP-Bean] Probléme d'instanciation d'une classe !
    Par adil_vpb dans le forum Servlets/JSP
    Réponses: 5
    Dernier message: 02/03/2007, 19h29
  3. Problème d'instanciation d'une classe
    Par Jahjouh dans le forum C++
    Réponses: 2
    Dernier message: 03/02/2007, 12h50
  4. Réponses: 7
    Dernier message: 18/07/2006, 12h23
  5. problème pour instancier une class
    Par gronaze dans le forum C++
    Réponses: 8
    Dernier message: 30/06/2006, 12h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo