forked from microsoft/FX11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EffectLoad.cpp
4002 lines (3356 loc) · 171 KB
/
EffectLoad.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--------------------------------------------------------------------------------------
// File: EffectLoad.cpp
//
// Direct3D Effects file loading code
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// http://go.microsoft.com/fwlink/p/?LinkId=271568
//--------------------------------------------------------------------------------------
#include "pchfx.h"
#include "EffectStates11.h"
#define PRIVATENEW new(m_BulkHeap)
namespace D3DX11Effects
{
static LPCSTR g_szEffectLoadArea = "D3D11EffectLoader";
SRasterizerBlock g_NullRasterizer;
SDepthStencilBlock g_NullDepthStencil;
SBlendBlock g_NullBlend;
SShaderResource g_NullTexture;
SInterface g_NullInterface;
SUnorderedAccessView g_NullUnorderedAccessView;
SRenderTargetView g_NullRenderTargetView;
SDepthStencilView g_NullDepthStencilView;
// these VTables must be setup in the proper order:
// 1) SetShader
// 2) SetConstantBuffers
// 3) SetSamplers
// 4) SetShaderResources
// 5) CreateShader
SD3DShaderVTable g_vtPS = {
(void (__stdcall ID3D11DeviceContext::*)(ID3D11DeviceChild*, ID3D11ClassInstance*const*, uint32_t)) &ID3D11DeviceContext::PSSetShader,
&ID3D11DeviceContext::PSSetConstantBuffers,
&ID3D11DeviceContext::PSSetSamplers,
&ID3D11DeviceContext::PSSetShaderResources,
(HRESULT (__stdcall ID3D11Device::*)(const void *, size_t, ID3D11ClassLinkage*, ID3D11DeviceChild **)) &ID3D11Device::CreatePixelShader
};
SD3DShaderVTable g_vtVS = {
(void (__stdcall ID3D11DeviceContext::*)(ID3D11DeviceChild*, ID3D11ClassInstance*const*, uint32_t)) &ID3D11DeviceContext::VSSetShader,
&ID3D11DeviceContext::VSSetConstantBuffers,
&ID3D11DeviceContext::VSSetSamplers,
&ID3D11DeviceContext::VSSetShaderResources,
(HRESULT (__stdcall ID3D11Device::*)(const void *, size_t, ID3D11ClassLinkage*, ID3D11DeviceChild **)) &ID3D11Device::CreateVertexShader
};
SD3DShaderVTable g_vtGS = {
(void (__stdcall ID3D11DeviceContext::*)(ID3D11DeviceChild*, ID3D11ClassInstance*const*, uint32_t)) &ID3D11DeviceContext::GSSetShader,
&ID3D11DeviceContext::GSSetConstantBuffers,
&ID3D11DeviceContext::GSSetSamplers,
&ID3D11DeviceContext::GSSetShaderResources,
(HRESULT (__stdcall ID3D11Device::*)(const void *, size_t, ID3D11ClassLinkage*, ID3D11DeviceChild **)) &ID3D11Device::CreateGeometryShader
};
SD3DShaderVTable g_vtHS = {
(void (__stdcall ID3D11DeviceContext::*)(ID3D11DeviceChild*, ID3D11ClassInstance*const*, uint32_t)) &ID3D11DeviceContext::HSSetShader,
&ID3D11DeviceContext::HSSetConstantBuffers,
&ID3D11DeviceContext::HSSetSamplers,
&ID3D11DeviceContext::HSSetShaderResources,
(HRESULT (__stdcall ID3D11Device::*)(const void *, size_t, ID3D11ClassLinkage*, ID3D11DeviceChild **)) &ID3D11Device::CreateHullShader
};
SD3DShaderVTable g_vtDS = {
(void (__stdcall ID3D11DeviceContext::*)(ID3D11DeviceChild*, ID3D11ClassInstance*const*, uint32_t)) &ID3D11DeviceContext::DSSetShader,
&ID3D11DeviceContext::DSSetConstantBuffers,
&ID3D11DeviceContext::DSSetSamplers,
&ID3D11DeviceContext::DSSetShaderResources,
(HRESULT (__stdcall ID3D11Device::*)(const void *, size_t, ID3D11ClassLinkage*, ID3D11DeviceChild **)) &ID3D11Device::CreateDomainShader
};
SD3DShaderVTable g_vtCS = {
(void (__stdcall ID3D11DeviceContext::*)(ID3D11DeviceChild*, ID3D11ClassInstance*const*, uint32_t)) &ID3D11DeviceContext::CSSetShader,
&ID3D11DeviceContext::CSSetConstantBuffers,
&ID3D11DeviceContext::CSSetSamplers,
&ID3D11DeviceContext::CSSetShaderResources,
(HRESULT (__stdcall ID3D11Device::*)(const void *, size_t, ID3D11ClassLinkage*, ID3D11DeviceChild **)) &ID3D11Device::CreateComputeShader
};
SShaderBlock g_NullVS(&g_vtVS);
SShaderBlock g_NullGS(&g_vtGS);
SShaderBlock g_NullPS(&g_vtPS);
SShaderBlock g_NullHS(&g_vtHS);
SShaderBlock g_NullDS(&g_vtDS);
SShaderBlock g_NullCS(&g_vtCS);
D3D_SHADER_VARIABLE_TYPE GetSimpleParameterTypeFromObjectType(EObjectType ObjectType)
{
switch (ObjectType)
{
case EOT_String:
return D3D_SVT_STRING;
case EOT_Blend:
return D3D_SVT_BLEND;
case EOT_DepthStencil:
return D3D_SVT_DEPTHSTENCIL;
case EOT_Rasterizer:
return D3D_SVT_RASTERIZER;
case EOT_PixelShader:
case EOT_PixelShader5:
return D3D_SVT_PIXELSHADER;
case EOT_VertexShader:
case EOT_VertexShader5:
return D3D_SVT_VERTEXSHADER;
case EOT_GeometryShader:
case EOT_GeometryShaderSO:
case EOT_GeometryShader5:
return D3D_SVT_GEOMETRYSHADER;
case EOT_HullShader5:
return D3D_SVT_HULLSHADER;
case EOT_DomainShader5:
return D3D_SVT_DOMAINSHADER;
case EOT_ComputeShader5:
return D3D_SVT_COMPUTESHADER;
case EOT_RenderTargetView:
return D3D_SVT_RENDERTARGETVIEW;
case EOT_DepthStencilView:
return D3D_SVT_DEPTHSTENCILVIEW;
case EOT_Texture:
case EOT_Texture1D:
case EOT_Texture1DArray:
case EOT_Texture2D:
case EOT_Texture2DArray:
case EOT_Texture2DMS:
case EOT_Texture2DMSArray:
case EOT_Texture3D:
case EOT_TextureCube:
case EOT_TextureCubeArray:
return D3D_SVT_TEXTURE;
case EOT_Buffer:
return D3D_SVT_BUFFER;
case EOT_Sampler:
return D3D_SVT_SAMPLER;
case EOT_ByteAddressBuffer:
return D3D_SVT_BYTEADDRESS_BUFFER;
case EOT_StructuredBuffer:
return D3D_SVT_STRUCTURED_BUFFER;
case EOT_RWTexture1D:
return D3D_SVT_RWTEXTURE1D;
case EOT_RWTexture1DArray:
return D3D_SVT_RWTEXTURE1DARRAY;
case EOT_RWTexture2D:
return D3D_SVT_RWTEXTURE2D;
case EOT_RWTexture2DArray:
return D3D_SVT_RWTEXTURE2DARRAY;
case EOT_RWTexture3D:
return D3D_SVT_RWTEXTURE3D;
case EOT_RWBuffer:
return D3D_SVT_RWBUFFER;
case EOT_RWByteAddressBuffer:
return D3D_SVT_RWBYTEADDRESS_BUFFER;
case EOT_RWStructuredBuffer:
case EOT_RWStructuredBufferAlloc:
case EOT_RWStructuredBufferConsume:
return D3D_SVT_RWSTRUCTURED_BUFFER;
case EOT_AppendStructuredBuffer:
return D3D_SVT_APPEND_STRUCTURED_BUFFER;
case EOT_ConsumeStructuredBuffer:
return D3D_SVT_CONSUME_STRUCTURED_BUFFER;
default:
assert(0);
}
return D3D_SVT_VOID;
}
inline HRESULT VerifyPointer(uint32_t oBase, uint32_t dwSize, uint32_t dwMaxSize)
{
uint32_t dwAdd = oBase + dwSize;
if (dwAdd < oBase || dwAdd > dwMaxSize)
return E_FAIL;
return S_OK;
}
//////////////////////////////////////////////////////////////////////////
// EffectHeap
// A simple class which assists in adding data to a block of memory
//////////////////////////////////////////////////////////////////////////
CEffectHeap::CEffectHeap() : m_pData(nullptr), m_dwSize(0), m_dwBufferSize(0)
{
}
CEffectHeap::~CEffectHeap()
{
SAFE_DELETE_ARRAY(m_pData);
}
uint32_t CEffectHeap::GetSize()
{
return m_dwSize;
}
HRESULT CEffectHeap::ReserveMemory(uint32_t dwSize)
{
HRESULT hr = S_OK;
assert(!m_pData);
assert(dwSize == AlignToPowerOf2(dwSize, c_DataAlignment));
m_dwBufferSize = dwSize;
VN( m_pData = new uint8_t[m_dwBufferSize] );
// make sure that we have machine word alignment
assert(m_pData == AlignToPowerOf2(m_pData, c_DataAlignment));
lExit:
return hr;
}
_Use_decl_annotations_
HRESULT CEffectHeap::AddString(const char *pString, char **ppPointer)
{
size_t size = strlen(pString) + 1;
assert( size <= 0xffffffff );
return AddData(pString, (uint32_t)size, (void**) ppPointer);
}
// This data is forcibly aligned, so make sure you account for that in calculating heap size
template <bool bCopyData>
HRESULT CEffectHeap::AddDataInternal(_In_reads_bytes_(dwSize) const void *pData, _In_ uint32_t dwSize, _Outptr_ void **ppPointer)
{
CCheckedDword chkFinalSize( m_dwSize );
uint32_t finalSize;
HRESULT hr = S_OK;
chkFinalSize += dwSize;
chkFinalSize += c_DataAlignment; // account for alignment
VHD( chkFinalSize.GetValue(&finalSize), "Overflow while adding data to Effect heap." );
// align original value
finalSize = AlignToPowerOf2(finalSize - c_DataAlignment, c_DataAlignment);
VBD( finalSize <= m_dwBufferSize, "Overflow adding data to Effect heap." );
*ppPointer = m_pData + m_dwSize;
assert(*ppPointer == AlignToPowerOf2(*ppPointer, c_DataAlignment));
if( bCopyData )
{
memcpy(*ppPointer, pData, dwSize);
}
m_dwSize = finalSize;
lExit:
if (FAILED(hr))
*ppPointer = nullptr;
return hr;
}
_Use_decl_annotations_
HRESULT CEffectHeap::AddData(const void *pData, uint32_t dwSize, void **ppPointer)
{
return AddDataInternal<true>( pData, dwSize, ppPointer );
}
// Moves a string from the general heap to the private heap and modifies the pointer to
// point to the new memory block.
// The general heap is freed as a whole, so we don't worry about leaking the given string pointer.
// This data is forcibly aligned, so make sure you account for that in calculating heap size
_Use_decl_annotations_
HRESULT CEffectHeap::MoveString(char **ppString)
{
HRESULT hr;
char *pNewPointer;
if (*ppString == nullptr)
return S_OK;
hr = AddString(*ppString, &pNewPointer);
if ( SUCCEEDED(hr) )
*ppString = pNewPointer;
return hr;
}
// Allocates space but does not move data
// The general heap is freed as a whole, so we don't worry about leaking the given string pointer.
// This data is forcibly aligned, so make sure you account for that in calculating heap size
_Use_decl_annotations_
HRESULT CEffectHeap::MoveEmptyDataBlock(void **ppData, uint32_t size)
{
HRESULT hr;
void *pNewPointer;
hr = AddDataInternal<false>(*ppData, size, &pNewPointer);
if (SUCCEEDED(hr))
{
*ppData = pNewPointer;
if (size == 0)
{
// To help catch bugs, set zero-byte blocks to null. There's no real reason to do this
*ppData = nullptr;
}
}
return hr;
}
// Moves an array of SInterfaceParameters from the general heap to the private heap and modifies the pointer to
// point to the new memory block.
// The general heap is freed as a whole, so we don't worry about leaking the given string pointer.
// This data is forcibly aligned, so make sure you account for that in calculating heap size
_Use_decl_annotations_
HRESULT CEffectHeap::MoveInterfaceParameters(uint32_t InterfaceCount, SShaderBlock::SInterfaceParameter **ppInterfaces)
{
HRESULT hr;
SShaderBlock::SInterfaceParameter *pNewPointer;
if (*ppInterfaces == nullptr)
return S_OK;
VBD( InterfaceCount <= D3D11_SHADER_MAX_INTERFACES, "Internal loading error: InterfaceCount > D3D11_SHADER_MAX_INTERFACES." );
VH( AddData(*ppInterfaces, InterfaceCount * sizeof(SShaderBlock::SInterfaceParameter), (void**)&pNewPointer) );
for( size_t i=0; i < InterfaceCount; i++ )
{
VH( MoveString( &pNewPointer[i].pName ) );
}
*ppInterfaces = pNewPointer;
lExit:
return hr;
}
// Moves data from the general heap to the private heap and modifies the pointer to
// point to the new memory block
// The general heap is freed as a whole, so we don't worry about leaking the given pointer.
// This data is forcibly aligned, so make sure you account for that in calculating heap size
_Use_decl_annotations_
HRESULT CEffectHeap::MoveData(void **ppData, uint32_t size)
{
HRESULT hr;
void *pNewPointer;
hr = AddData(*ppData, size, &pNewPointer);
if ( SUCCEEDED(hr) )
{
*ppData = pNewPointer;
if (size == 0)
{
// To help catch bugs, set zero-byte blocks to null. There's no real reason to do this
*ppData = nullptr;
}
}
return hr;
}
//////////////////////////////////////////////////////////////////////////
// Load API
//////////////////////////////////////////////////////////////////////////
_Use_decl_annotations_
HRESULT CEffect::LoadEffect(const void *pEffectBuffer, uint32_t cbEffectBuffer)
{
HRESULT hr = S_OK;
CEffectLoader loader;
if (!pEffectBuffer)
{
DPF(0, "%s: pEffectBuffer is nullptr.", g_szEffectLoadArea);
VH( E_INVALIDARG );
}
VH( loader.LoadEffect(this, pEffectBuffer, cbEffectBuffer) );
lExit:
if( FAILED( hr ) )
{
// Release here because m_pShaderBlocks may still be in loader.m_BulkHeap if loading failed before we reallocated the memory
ReleaseShaderRefection();
}
return hr;
}
//////////////////////////////////////////////////////////////////////////
// CEffectLoader
// A helper class which loads an effect
//////////////////////////////////////////////////////////////////////////
_Use_decl_annotations_
HRESULT CEffectLoader::GetUnstructuredDataBlock(uint32_t offset, uint32_t *pdwSize, void **ppData)
{
HRESULT hr = S_OK;
uint32_t *pBlockSize;
VH( m_msUnstructured.ReadAtOffset(offset, sizeof(*pBlockSize), (void**) &pBlockSize ) );
*pdwSize = *pBlockSize;
VH( m_msUnstructured.Read(ppData, *pdwSize) );
lExit:
return hr;
}
// position in buffer is lost on error
//
// This function should be used in 1:1 conjunction with CEffectHeap::MoveString;
// that is, any string added to the reflection heap with this function
// must be relocated with MoveString at some point later on.
_Use_decl_annotations_
HRESULT CEffectLoader::GetStringAndAddToReflection(uint32_t offset, char **ppString)
{
HRESULT hr = S_OK;
LPCSTR pName;
size_t oldPos;
if (offset == 0)
{
*ppString = nullptr;
goto lExit;
}
oldPos = m_msUnstructured.GetPosition();
VH( m_msUnstructured.ReadAtOffset(offset, &pName) );
m_ReflectionMemory += AlignToPowerOf2( (uint32_t)strlen(pName) + 1, c_DataAlignment);
*ppString = const_cast<char*>(pName);
m_msUnstructured.Seek(oldPos);
lExit:
return hr;
}
// position in buffer is lost on error
//
// This function should be used in 1:1 conjunction with CEffectHeap::MoveInterfaceParameters;
// that is, any array of parameters added to the reflection heap with this function
// must be relocated with MoveInterfaceParameters at some point later on.
_Use_decl_annotations_
HRESULT CEffectLoader::GetInterfaceParametersAndAddToReflection( uint32_t InterfaceCount, uint32_t offset, SShaderBlock::SInterfaceParameter **ppInterfaces )
{
HRESULT hr = S_OK;
SBinaryInterfaceInitializer* pInterfaceInitializer;
size_t oldPos;
if (offset == 0)
{
*ppInterfaces = nullptr;
goto lExit;
}
oldPos = m_msUnstructured.GetPosition();
VBD( InterfaceCount <= D3D11_SHADER_MAX_INTERFACES, "Internal loading error: InterfaceCount > D3D11_SHADER_MAX_INTERFACES." );
m_ReflectionMemory += AlignToPowerOf2(InterfaceCount * sizeof(SShaderBlock::SInterfaceParameter), c_DataAlignment);
assert( ppInterfaces != 0 );
_Analysis_assume_( ppInterfaces != 0 );
(*ppInterfaces) = PRIVATENEW SShaderBlock::SInterfaceParameter[InterfaceCount];
VN( *ppInterfaces );
VHD( m_msUnstructured.ReadAtOffset(offset, sizeof(SBinaryInterfaceInitializer) * InterfaceCount, (void**)&pInterfaceInitializer),
"Invalid pEffectBuffer: cannot read interface initializer." );
for( size_t i=0; i < InterfaceCount; i++ )
{
(*ppInterfaces)[i].Index = pInterfaceInitializer[i].ArrayIndex;
VHD( m_msUnstructured.ReadAtOffset(pInterfaceInitializer[i].oInstanceName, const_cast<LPCSTR*>(&(*ppInterfaces)[i].pName)),
"Invalid pEffectBuffer: cannot read interface initializer." );
m_ReflectionMemory += AlignToPowerOf2( (uint32_t)strlen((*ppInterfaces)[i].pName) + 1, c_DataAlignment);
}
m_msUnstructured.Seek(oldPos);
lExit:
return hr;
}
HRESULT CEffectLoader::FixupCBPointer(_Inout_ SConstantBuffer **ppCB)
{
HRESULT hr = S_OK;
size_t index = (SConstantBuffer*)*ppCB - m_pOldCBs;
assert( index * sizeof(SConstantBuffer) == ((size_t)(SConstantBuffer*)*ppCB - (size_t)m_pOldCBs) );
VBD( index < m_pEffect->m_CBCount, "Internal loading error: invalid constant buffer index." );
*ppCB = (SConstantBuffer*)(m_pEffect->m_pCBs + index);
lExit:
return hr;
}
HRESULT CEffectLoader::FixupShaderPointer(_Inout_ SShaderBlock **ppShaderBlock)
{
HRESULT hr = S_OK;
if (*ppShaderBlock != &g_NullVS && *ppShaderBlock != &g_NullGS && *ppShaderBlock != &g_NullPS &&
*ppShaderBlock != &g_NullHS && *ppShaderBlock != &g_NullDS && *ppShaderBlock != &g_NullCS &&
*ppShaderBlock != nullptr)
{
size_t index = *ppShaderBlock - m_pOldShaders;
assert( index * sizeof(SShaderBlock) == ((size_t)*ppShaderBlock - (size_t)m_pOldShaders) );
VBD( index < m_pEffect->m_ShaderBlockCount, "Internal loading error: invalid shader index." );
*ppShaderBlock = m_pEffect->m_pShaderBlocks + index;
}
lExit:
return hr;
}
HRESULT CEffectLoader::FixupDSPointer(_Inout_ SDepthStencilBlock **ppDSBlock)
{
HRESULT hr = S_OK;
if (*ppDSBlock != &g_NullDepthStencil && *ppDSBlock != nullptr)
{
size_t index = *ppDSBlock - m_pOldDS;
assert( index * sizeof(SDepthStencilBlock) == ((size_t)*ppDSBlock - (size_t)m_pOldDS) );
VBD( index < m_pEffect->m_DepthStencilBlockCount, "Internal loading error: invalid depth-stencil state index." );
*ppDSBlock = m_pEffect->m_pDepthStencilBlocks + index;
}
lExit:
return hr;
}
HRESULT CEffectLoader::FixupABPointer(_Inout_ SBlendBlock **ppABBlock)
{
HRESULT hr = S_OK;
if (*ppABBlock != &g_NullBlend && *ppABBlock != nullptr)
{
size_t index = *ppABBlock - m_pOldAB;
assert( index * sizeof(SBlendBlock) == ((size_t)*ppABBlock - (size_t)m_pOldAB) );
VBD( index < m_pEffect->m_BlendBlockCount, "Internal loading error: invalid blend state index." );
*ppABBlock = m_pEffect->m_pBlendBlocks + index;
}
lExit:
return hr;
}
HRESULT CEffectLoader::FixupRSPointer(_Inout_ SRasterizerBlock **ppRSBlock)
{
HRESULT hr = S_OK;
if (*ppRSBlock != &g_NullRasterizer && *ppRSBlock != nullptr)
{
size_t index = *ppRSBlock - m_pOldRS;
assert( index * sizeof(SRasterizerBlock) == ((size_t)*ppRSBlock - (size_t)m_pOldRS) );
VBD( index < m_pEffect->m_RasterizerBlockCount, "Internal loading error: invalid rasterizer state index." );
*ppRSBlock = m_pEffect->m_pRasterizerBlocks + index;
}
lExit:
return hr;
}
HRESULT CEffectLoader::FixupSamplerPointer(_Inout_ SSamplerBlock **ppSampler)
{
HRESULT hr = S_OK;
size_t index = *ppSampler - m_pOldSamplers;
assert( index * sizeof(SSamplerBlock) == ((size_t)*ppSampler - (size_t)m_pOldSamplers) );
VBD( index < m_pEffect->m_SamplerBlockCount, "Internal loading error: invalid sampler index." );
*ppSampler = m_pEffect->m_pSamplerBlocks + index;
lExit:
return hr;
}
HRESULT CEffectLoader::FixupInterfacePointer(_Inout_ SInterface **ppInterface, _In_ bool CheckBackgroundInterfaces)
{
HRESULT hr = S_OK;
if (*ppInterface != &g_NullInterface && *ppInterface != nullptr)
{
size_t index = *ppInterface - m_pOldInterfaces;
if(index < m_OldInterfaceCount)
{
assert( index * sizeof(SInterface) == ((size_t)*ppInterface - (size_t)m_pOldInterfaces) );
*ppInterface = m_pEffect->m_pInterfaces + index;
}
else
{
VBD( CheckBackgroundInterfaces, "Internal loading error: invalid interface pointer." );
for( index=0; index < m_BackgroundInterfaces.GetSize(); index++ )
{
if( *ppInterface == m_BackgroundInterfaces[ (uint32_t)index ] )
{
// The interfaces m_BackgroundInterfaces were concatenated to the original ones in m_pEffect->m_pInterfaces
*ppInterface = m_pEffect->m_pInterfaces + (m_OldInterfaceCount + index);
break;
}
}
VBD( index < m_BackgroundInterfaces.GetSize(), "Internal loading error: invalid interface pointer." );
}
}
lExit:
return hr;
}
HRESULT CEffectLoader::FixupShaderResourcePointer(_Inout_ SShaderResource **ppResource)
{
HRESULT hr = S_OK;
if (*ppResource != &g_NullTexture && *ppResource != nullptr)
{
size_t index = *ppResource - m_pOldShaderResources;
assert( index * sizeof(SShaderResource) == ((size_t)*ppResource - (size_t)m_pOldShaderResources) );
// could be a TBuffer or a texture; better check first
if (index < m_pEffect->m_ShaderResourceCount)
{
*ppResource = m_pEffect->m_pShaderResources + index;
}
else
{
// if this is a TBuffer, then the shader resource pointer
// actually points into a SConstantBuffer's TBuffer field
index = (SConstantBuffer*)*ppResource - (SConstantBuffer*)&m_pOldCBs->TBuffer;
assert( index * sizeof(SConstantBuffer) == ((size_t)(SConstantBuffer*)*ppResource - (size_t)(SConstantBuffer*)&m_pOldCBs->TBuffer) );
VBD( index < m_pEffect->m_CBCount, "Internal loading error: invalid SRV index." );
*ppResource = &m_pEffect->m_pCBs[index].TBuffer;
}
}
lExit:
return hr;
}
HRESULT CEffectLoader::FixupUnorderedAccessViewPointer(_Inout_ SUnorderedAccessView **ppUnorderedAccessView)
{
HRESULT hr = S_OK;
if (*ppUnorderedAccessView != &g_NullUnorderedAccessView && *ppUnorderedAccessView != nullptr)
{
size_t index = *ppUnorderedAccessView - m_pOldUnorderedAccessViews;
assert( index * sizeof(SUnorderedAccessView) == ((size_t)*ppUnorderedAccessView - (size_t)m_pOldUnorderedAccessViews) );
VBD( index < m_pEffect->m_UnorderedAccessViewCount, "Internal loading error: invalid UAV index." );
*ppUnorderedAccessView = m_pEffect->m_pUnorderedAccessViews + index;
}
lExit:
return hr;
}
HRESULT CEffectLoader::FixupRenderTargetViewPointer(_Inout_ SRenderTargetView **ppRenderTargetView)
{
HRESULT hr = S_OK;
if (*ppRenderTargetView != &g_NullRenderTargetView && *ppRenderTargetView != nullptr)
{
size_t index = *ppRenderTargetView - m_pOldRenderTargetViews;
assert( index * sizeof(SRenderTargetView) == ((size_t)*ppRenderTargetView - (size_t)m_pOldRenderTargetViews) );
VBD( index < m_pEffect->m_RenderTargetViewCount, "Internal loading error: invalid RTV index." );
*ppRenderTargetView = m_pEffect->m_pRenderTargetViews + index;
}
lExit:
return hr;
}
HRESULT CEffectLoader::FixupDepthStencilViewPointer(_Inout_ SDepthStencilView **ppDepthStencilView)
{
HRESULT hr = S_OK;
if (*ppDepthStencilView != &g_NullDepthStencilView && *ppDepthStencilView != nullptr)
{
size_t index = *ppDepthStencilView - m_pOldDepthStencilViews;
assert( index * sizeof(SDepthStencilView) == ((size_t)*ppDepthStencilView - (size_t)m_pOldDepthStencilViews) );
VBD( index < m_pEffect->m_DepthStencilViewCount, "Internal loading error: invalid DSV index." );
*ppDepthStencilView = m_pEffect->m_pDepthStencilViews + index;
}
lExit:
return hr;
}
HRESULT CEffectLoader::FixupStringPointer(_Inout_ SString **ppString)
{
HRESULT hr = S_OK;
size_t index = *ppString - m_pOldStrings;
assert( index * sizeof(SString) == ((size_t)*ppString - (size_t)m_pOldStrings) );
VBD(index < m_pEffect->m_StringCount, "Internal loading error: invalid string index." );
*ppString = m_pEffect->m_pStrings + index;
lExit:
return hr;
}
HRESULT CEffectLoader::FixupMemberDataPointer(_Inout_ SMemberDataPointer **ppMemberData)
{
HRESULT hr = S_OK;
size_t index = *ppMemberData - m_pOldMemberDataBlocks;
assert( index * sizeof(SMemberDataPointer) == ((size_t)*ppMemberData - (size_t)m_pOldMemberDataBlocks) );
VBD( index < m_pEffect->m_MemberDataCount, "Internal loading error: invalid member block index." );
*ppMemberData = m_pEffect->m_pMemberDataBlocks + index;
lExit:
return hr;
}
HRESULT CEffectLoader::FixupVariablePointer(_Inout_ SGlobalVariable **ppVar)
{
HRESULT hr = S_OK;
size_t index = *ppVar - m_pOldVars;
if( index < m_pEffect->m_VariableCount )
{
assert( index * sizeof(SGlobalVariable) == ((size_t)*ppVar - (size_t)m_pOldVars) );
*ppVar = m_pEffect->m_pVariables + index;
}
else if( m_pvOldMemberInterfaces )
{
// When cloning, m_pvOldMemberInterfaces may be non-nullptr, and *ppVar may point to a variable in it.
const size_t Members = m_pvOldMemberInterfaces->GetSize();
for( index=0; index < Members; index++ )
{
if( (ID3DX11EffectVariable*)(*m_pvOldMemberInterfaces)[ (uint32_t)index] == (ID3DX11EffectVariable*)*ppVar )
{
break;
}
}
VBD( index < Members, "Internal loading error: invalid member pointer." );
*ppVar = (SGlobalVariable*)m_pEffect->m_pMemberInterfaces[ (uint32_t)index];
}
lExit:
return hr;
}
HRESULT CEffectLoader::FixupGroupPointer(_Inout_ SGroup **ppGroup)
{
HRESULT hr = S_OK;
if( *ppGroup != nullptr )
{
size_t index = *ppGroup - m_pOldGroups;
assert( index * sizeof(SGroup) == ((size_t)*ppGroup - (size_t)m_pOldGroups) );
VBD( index < m_pEffect->m_GroupCount, "Internal loading error: invalid group index." );
*ppGroup = m_pEffect->m_pGroups + index;
}
lExit:
return hr;
}
static HRESULT GetEffectVersion( _In_ uint32_t effectFileTag, _Out_ DWORD* pVersion )
{
assert( pVersion != nullptr );
if( !pVersion )
return E_FAIL;
for( size_t i = 0; i < _countof(g_EffectVersions); i++ )
{
if( g_EffectVersions[i].m_Tag == effectFileTag )
{
*pVersion = g_EffectVersions[i].m_Version;
return S_OK;
}
}
return E_FAIL;
}
_Use_decl_annotations_
HRESULT CEffectLoader::LoadEffect(CEffect *pEffect, const void *pEffectBuffer, uint32_t cbEffectBuffer)
{
HRESULT hr = S_OK;
uint32_t i, varSize, cMemberDataBlocks;
CCheckedDword chkVariables = 0;
// Used for cloning
m_pvOldMemberInterfaces = nullptr;
m_BulkHeap.EnableAlignment();
assert(pEffect && pEffectBuffer);
m_pEffect = pEffect;
m_EffectMemory = m_ReflectionMemory = 0;
VN( m_pEffect->m_pReflection = new CEffectReflection() );
m_pReflection = m_pEffect->m_pReflection;
// Begin effect load
VN( m_pEffect->m_pTypePool = new CEffect::CTypeHashTable );
VN( m_pEffect->m_pStringPool = new CEffect::CStringHashTable );
VN( m_pEffect->m_pPooledHeap = new CDataBlockStore );
m_pEffect->m_pPooledHeap->EnableAlignment();
m_pEffect->m_pTypePool->SetPrivateHeap(m_pEffect->m_pPooledHeap);
m_pEffect->m_pStringPool->SetPrivateHeap(m_pEffect->m_pPooledHeap);
VH( m_pEffect->m_pTypePool->AutoGrow() );
VH( m_pEffect->m_pStringPool->AutoGrow() );
// Load from blob
m_pData = (uint8_t*)pEffectBuffer;
m_dwBufferSize = cbEffectBuffer;
VH( m_msStructured.SetData(m_pData, m_dwBufferSize) );
// At this point, we assume that the blob is valid
VHD( m_msStructured.Read((void**) &m_pHeader, sizeof(*m_pHeader)), "pEffectBuffer is too small." );
// Verify the version
if( FAILED( hr = GetEffectVersion( m_pHeader->Tag, &m_Version ) ) )
{
DPF(0, "Effect version is unrecognized. This runtime supports fx_5_0 to %s.", g_EffectVersions[_countof(g_EffectVersions)-1].m_pName );
VH( hr );
}
if( m_pHeader->RequiresPool() || m_pHeader->Pool.cObjectVariables > 0 || m_pHeader->Pool.cNumericVariables > 0 )
{
DPF(0, "Effect11 does not support EffectPools." );
VH( E_FAIL );
}
// Get shader block count
VBD( m_pHeader->cInlineShaders <= m_pHeader->cTotalShaders, "Invalid Effect header: cInlineShaders > cTotalShaders." );
// Make sure the counts for the Effect don't overflow
chkVariables = m_pHeader->Effect.cObjectVariables;
chkVariables += m_pHeader->Effect.cNumericVariables;
chkVariables += m_pHeader->cInterfaceVariables;
chkVariables *= sizeof(SGlobalVariable);
VH( chkVariables.GetValue(&varSize) );
// Make sure the counts for the SMemberDataPointers don't overflow
chkVariables = m_pHeader->cClassInstanceElements;
chkVariables += m_pHeader->cBlendStateBlocks;
chkVariables += m_pHeader->cRasterizerStateBlocks;
chkVariables += m_pHeader->cDepthStencilBlocks;
chkVariables += m_pHeader->cSamplers;
chkVariables += m_pHeader->Effect.cCBs; // Buffer (for CBuffers and TBuffers)
chkVariables += m_pHeader->Effect.cCBs; // SRV (for TBuffers)
VHD( chkVariables.GetValue(&cMemberDataBlocks), "Overflow: too many Effect variables." );
// Allocate effect resources
VN( m_pEffect->m_pCBs = PRIVATENEW SConstantBuffer[m_pHeader->Effect.cCBs] );
VN( m_pEffect->m_pDepthStencilBlocks = PRIVATENEW SDepthStencilBlock[m_pHeader->cDepthStencilBlocks] );
VN( m_pEffect->m_pRasterizerBlocks = PRIVATENEW SRasterizerBlock[m_pHeader->cRasterizerStateBlocks] );
VN( m_pEffect->m_pBlendBlocks = PRIVATENEW SBlendBlock[m_pHeader->cBlendStateBlocks] );
VN( m_pEffect->m_pSamplerBlocks = PRIVATENEW SSamplerBlock[m_pHeader->cSamplers] );
// we allocate raw bytes for variables because they are polymorphic types that need to be placement new'ed
VN( m_pEffect->m_pVariables = (SGlobalVariable *)PRIVATENEW uint8_t[varSize] );
VN( m_pEffect->m_pAnonymousShaders = PRIVATENEW SAnonymousShader[m_pHeader->cInlineShaders] );
VN( m_pEffect->m_pGroups = PRIVATENEW SGroup[m_pHeader->cGroups] );
VN( m_pEffect->m_pShaderBlocks = PRIVATENEW SShaderBlock[m_pHeader->cTotalShaders] );
VN( m_pEffect->m_pStrings = PRIVATENEW SString[m_pHeader->cStrings] );
VN( m_pEffect->m_pShaderResources = PRIVATENEW SShaderResource[m_pHeader->cShaderResources] );
VN( m_pEffect->m_pUnorderedAccessViews = PRIVATENEW SUnorderedAccessView[m_pHeader->cUnorderedAccessViews] );
VN( m_pEffect->m_pInterfaces = PRIVATENEW SInterface[m_pHeader->cInterfaceVariableElements] );
VN( m_pEffect->m_pMemberDataBlocks = PRIVATENEW SMemberDataPointer[cMemberDataBlocks] );
VN( m_pEffect->m_pRenderTargetViews = PRIVATENEW SRenderTargetView[m_pHeader->cRenderTargetViews] );
VN( m_pEffect->m_pDepthStencilViews = PRIVATENEW SDepthStencilView[m_pHeader->cDepthStencilViews] );
uint32_t oStructured = m_pHeader->cbUnstructured + sizeof(SBinaryHeader5);
VHD( m_msStructured.Seek(oStructured), "Invalid pEffectBuffer: Missing structured data block." );
VH( m_msUnstructured.SetData(m_pData + sizeof(SBinaryHeader5), oStructured - sizeof(SBinaryHeader5)) );
VH( LoadCBs() );
VH( LoadObjectVariables() );
VH( LoadInterfaceVariables() );
VH( LoadGroups() );
// Build shader dependencies
for (i=0; i<m_pEffect->m_ShaderBlockCount; i++)
{
VH( BuildShaderBlock(&m_pEffect->m_pShaderBlocks[i]) );
}
for( size_t iGroup=0; iGroup<m_pHeader->cGroups; iGroup++ )
{
SGroup *pGroup = &m_pEffect->m_pGroups[iGroup];
pGroup->HasDependencies = false;
for( size_t iTechnique=0; iTechnique < pGroup->TechniqueCount; iTechnique++ )
{
STechnique* pTech = &pGroup->pTechniques[iTechnique];
pTech->HasDependencies = false;
for( size_t iPass=0; iPass < pTech->PassCount; iPass++ )
{
SPassBlock *pPass = &pTech->pPasses[iPass];
pTech->HasDependencies |= pPass->CheckDependencies();
}
pGroup->HasDependencies |= pTech->HasDependencies;
}
}
VH( InitializeReflectionDataAndMoveStrings() );
VH( ReallocateReflectionData() );
VH( ReallocateEffectData() );
VB( m_pReflection->m_Heap.GetSize() == m_ReflectionMemory );
// Verify that all of the various block/variable types were loaded
VBD( m_pEffect->m_VariableCount == (m_pHeader->Effect.cObjectVariables + m_pHeader->Effect.cNumericVariables + m_pHeader->cInterfaceVariables), "Internal loading error: mismatched variable count." );
VBD( m_pEffect->m_ShaderBlockCount == m_pHeader->cTotalShaders, "Internal loading error: mismatched shader block count." );
VBD( m_pEffect->m_AnonymousShaderCount == m_pHeader->cInlineShaders, "Internal loading error: mismatched anonymous variable count." );
VBD( m_pEffect->m_ShaderResourceCount == m_pHeader->cShaderResources, "Internal loading error: mismatched SRV count." );
VBD( m_pEffect->m_InterfaceCount == m_pHeader->cInterfaceVariableElements + m_BackgroundInterfaces.GetSize(), "Internal loading error: mismatched interface count." );
VBD( m_pEffect->m_UnorderedAccessViewCount == m_pHeader->cUnorderedAccessViews, "Internal loading error: mismatched UAV count." );
VBD( m_pEffect->m_MemberDataCount == cMemberDataBlocks, "Internal loading error: mismatched member data block count." );
VBD( m_pEffect->m_RenderTargetViewCount == m_pHeader->cRenderTargetViews, "Internal loading error: mismatched RTV count." );
VBD( m_pEffect->m_DepthStencilViewCount == m_pHeader->cDepthStencilViews, "Internal loading error: mismatched DSV count." );
VBD( m_pEffect->m_DepthStencilBlockCount == m_pHeader->cDepthStencilBlocks, "Internal loading error: mismatched depth-stencil state count." );
VBD( m_pEffect->m_BlendBlockCount == m_pHeader->cBlendStateBlocks, "Internal loading error: mismatched blend state count." );
VBD( m_pEffect->m_RasterizerBlockCount == m_pHeader->cRasterizerStateBlocks, "Internal loading error: mismatched rasterizer state count." );
VBD( m_pEffect->m_SamplerBlockCount == m_pHeader->cSamplers, "Internal loading error: mismatched sampler count." );
VBD( m_pEffect->m_StringCount == m_pHeader->cStrings, "Internal loading error: mismatched string count." );
// Uncomment if you really need this information
// DPF(0, "Effect heap size: %d, reflection heap size: %d, allocations avoided: %d", m_EffectMemory, m_ReflectionMemory, m_BulkHeap.m_cAllocations);
lExit:
return hr;
}
// position in buffer is lost on error
_Use_decl_annotations_
HRESULT CEffectLoader::LoadStringAndAddToPool(char **ppString, uint32_t dwOffset)
{
HRESULT hr = S_OK;
char *pName;
uint32_t hash;
size_t oldPos;
CEffect::CStringHashTable::CIterator iter;
uint32_t len;
if (dwOffset == 0)
{
*ppString = nullptr;
goto lExit;
}
oldPos = m_msUnstructured.GetPosition();
VHD( m_msUnstructured.ReadAtOffset(dwOffset, (LPCSTR *) &pName), "Invalid pEffectBuffer: cannot read string." );
len = (uint32_t)strlen(pName);
hash = ComputeHash((uint8_t *)pName, len);
if (FAILED(m_pEffect->m_pStringPool->FindValueWithHash(pName, hash, &iter)))
{
assert( m_pEffect->m_pPooledHeap != 0 );
_Analysis_assume_( m_pEffect->m_pPooledHeap != 0 );
VN( (*ppString) = new(*m_pEffect->m_pPooledHeap) char[len + 1] );
memcpy(*ppString, pName, len + 1);
VHD( m_pEffect->m_pStringPool->AddValueWithHash(*ppString, hash), "Internal loading error: failed to add string to pool." );
}
else
{
*ppString = const_cast<LPSTR>(iter.GetData());
}
m_msUnstructured.Seek(oldPos);
lExit:
return hr;
}
_Use_decl_annotations_
HRESULT CEffectLoader::LoadTypeAndAddToPool(SType **ppType, uint32_t dwOffset)
{
HRESULT hr = S_OK;
SBinaryType *psType;
SBinaryNumericType *pNumericType;
EObjectType *pObjectType;
uint32_t cMembers, iMember, cInterfaces;
uint32_t oBaseClassType;
SType temporaryType;
CEffect::CTypeHashTable::CIterator iter;
uint8_t *pHashBuffer;
uint32_t hash;
SVariable *pTempMembers = nullptr;
m_HashBuffer.Empty();
VHD( m_msUnstructured.ReadAtOffset(dwOffset, sizeof(SBinaryType), (void**) &psType), "Invalid pEffectBuffer: cannot read type." );
VHD( LoadStringAndAddToPool(&temporaryType.pTypeName, psType->oTypeName), "Invalid pEffectBuffer: cannot read type name." );
temporaryType.VarType = psType->VarType;
temporaryType.Elements = psType->Elements;
temporaryType.TotalSize = psType->TotalSize;
temporaryType.Stride = psType->Stride;
temporaryType.PackedSize = psType->PackedSize;
// sanity check elements, size, stride, etc.
uint32_t cElements = std::max<uint32_t>(1, temporaryType.Elements);
VBD( cElements * temporaryType.Stride == AlignToPowerOf2(temporaryType.TotalSize, SType::c_RegisterSize), "Invalid pEffectBuffer: invalid type size." );
VBD( temporaryType.Stride % SType::c_RegisterSize == 0, "Invalid pEffectBuffer: invalid type stride." );
VBD( temporaryType.PackedSize <= temporaryType.TotalSize && temporaryType.PackedSize % cElements == 0, "Invalid pEffectBuffer: invalid type packed size." );
switch(temporaryType.VarType)
{
case EVT_Object:
VHD( m_msUnstructured.Read((void**) &pObjectType, sizeof(uint32_t)), "Invalid pEffectBuffer: cannot read object type." );
temporaryType.ObjectType = *pObjectType;
VBD( temporaryType.VarType > EOT_Invalid && temporaryType.VarType < EOT_Count, "Invalid pEffectBuffer: invalid object type." );
VN( pHashBuffer = m_HashBuffer.AddRange(sizeof(temporaryType.VarType) + sizeof(temporaryType.Elements) +
sizeof(temporaryType.pTypeName) + sizeof(temporaryType.ObjectType)) );
memcpy(pHashBuffer, &temporaryType.VarType, sizeof(temporaryType.VarType));
pHashBuffer += sizeof(temporaryType.VarType);
memcpy(pHashBuffer, &temporaryType.Elements, sizeof(temporaryType.Elements));
pHashBuffer += sizeof(temporaryType.Elements);
memcpy(pHashBuffer, &temporaryType.pTypeName, sizeof(temporaryType.pTypeName));
pHashBuffer += sizeof(temporaryType.pTypeName);