1 //////////////////////////////////////////////////////////////////////////////
3 // Copyright (C) Microsoft Corporation. All Rights Reserved.
5 // File: EffectReflection.cpp
6 // Content: D3DX11 Effects public reflection APIs
8 //////////////////////////////////////////////////////////////////////////////
12 namespace D3DX11Effects
15 SEffectInvalidType g_InvalidType;
17 SEffectInvalidScalarVariable g_InvalidScalarVariable;
18 SEffectInvalidVectorVariable g_InvalidVectorVariable;
19 SEffectInvalidMatrixVariable g_InvalidMatrixVariable;
20 SEffectInvalidStringVariable g_InvalidStringVariable;
21 SEffectInvalidClassInstanceVariable g_InvalidClassInstanceVariable;
22 SEffectInvalidInterfaceVariable g_InvalidInterfaceVariable;
23 SEffectInvalidShaderResourceVariable g_InvalidShaderResourceVariable;
24 SEffectInvalidUnorderedAccessViewVariable g_InvalidUnorderedAccessViewVariable;
25 SEffectInvalidRenderTargetViewVariable g_InvalidRenderTargetViewVariable;
26 SEffectInvalidDepthStencilViewVariable g_InvalidDepthStencilViewVariable;
27 SEffectInvalidConstantBuffer g_InvalidConstantBuffer;
28 SEffectInvalidShaderVariable g_InvalidShaderVariable;
29 SEffectInvalidBlendVariable g_InvalidBlendVariable;
30 SEffectInvalidDepthStencilVariable g_InvalidDepthStencilVariable;
31 SEffectInvalidRasterizerVariable g_InvalidRasterizerVariable;
32 SEffectInvalidSamplerVariable g_InvalidSamplerVariable;
34 SEffectInvalidPass g_InvalidPass;
35 SEffectInvalidTechnique g_InvalidTechnique;
36 SEffectInvalidGroup g_InvalidGroup;
39 //////////////////////////////////////////////////////////////////////////
40 // Helper routine implementations
41 //////////////////////////////////////////////////////////////////////////
43 ID3DX11EffectConstantBuffer * NoParentCB()
45 DPF(0, "ID3DX11EffectVariable::GetParentConstantBuffer: Variable does not have a parent constant buffer");
46 // have to typecast because the type of g_InvalidScalarVariable has not been declared yet
47 return &g_InvalidConstantBuffer;
50 ID3DX11EffectVariable * GetAnnotationByIndexHelper(const char *pClassName, UINT Index, UINT AnnotationCount, SAnnotation *pAnnotations)
52 if (Index >= AnnotationCount)
54 DPF(0, "%s::GetAnnotationByIndex: Invalid index (%d, total: %d)", pClassName, Index, AnnotationCount);
55 return &g_InvalidScalarVariable;
58 return pAnnotations + Index;
61 ID3DX11EffectVariable * GetAnnotationByNameHelper(const char *pClassName, LPCSTR Name, UINT AnnotationCount, SAnnotation *pAnnotations)
64 for (i = 0; i < AnnotationCount; ++ i)
66 if (strcmp(pAnnotations[i].pName, Name) == 0)
68 return pAnnotations + i;
72 DPF(0, "%s::GetAnnotationByName: Annotation [%s] not found", pClassName, Name);
73 return &g_InvalidScalarVariable;
76 //////////////////////////////////////////////////////////////////////////
77 // Effect routines to pool interfaces
78 //////////////////////////////////////////////////////////////////////////
80 ID3DX11EffectType * CEffect::CreatePooledSingleElementTypeInterface(SType *pType)
86 DPF(0, "ID3DX11Effect: Cannot create new type interfaces since the effect has been Optimize()'ed");
87 return &g_InvalidType;
90 for (i = 0; i < m_pTypeInterfaces.GetSize(); ++ i)
92 if (m_pTypeInterfaces[i]->pType == pType)
94 return (SSingleElementType*)m_pTypeInterfaces[i];
97 SSingleElementType *pNewType;
98 if (NULL == (pNewType = NEW SSingleElementType))
100 DPF(0, "ID3DX11Effect: Out of memory while trying to create new type interface");
101 return &g_InvalidType;
104 pNewType->pType = pType;
105 m_pTypeInterfaces.Add(pNewType);
110 // Create a member variable (via GetMemberBy* or GetElement)
111 ID3DX11EffectVariable * CEffect::CreatePooledVariableMemberInterface(TTopLevelVariable<ID3DX11EffectVariable> *pTopLevelEntity, SVariable *pMember, UDataPointer Data, BOOL IsSingleElement, UINT Index)
118 DPF(0, "ID3DX11Effect: Cannot create new variable interfaces since the effect has been Optimize()'ed");
119 return &g_InvalidScalarVariable;
122 for (i = 0; i < m_pMemberInterfaces.GetSize(); ++ i)
124 if (m_pMemberInterfaces[i]->pType == pMember->pType &&
125 m_pMemberInterfaces[i]->pName == pMember->pName &&
126 m_pMemberInterfaces[i]->pSemantic == pMember->pSemantic &&
127 m_pMemberInterfaces[i]->Data.pGeneric == Data.pGeneric &&
128 m_pMemberInterfaces[i]->IsSingleElement == IsSingleElement &&
129 ((SMember*)m_pMemberInterfaces[i])->pTopLevelEntity == pTopLevelEntity)
131 return (ID3DX11EffectVariable *) m_pMemberInterfaces[i];
135 // is this annotation or runtime data?
136 if( pTopLevelEntity->pEffect->IsReflectionData(pTopLevelEntity) )
138 D3DXASSERT( pTopLevelEntity->pEffect->IsReflectionData(Data.pGeneric) );
143 // if the heap is empty, we are still loading the Effect, and thus creating a member for a variable initializer
144 // ex. Interface myInt = myClassArray[2];
145 if( pTopLevelEntity->pEffect->m_Heap.GetSize() > 0 )
147 D3DXASSERT( pTopLevelEntity->pEffect->IsRuntimeData(pTopLevelEntity) );
148 if (!pTopLevelEntity->pType->IsObjectType(EOT_String))
150 // strings are funny; their data is reflection data, so ignore those
151 D3DXASSERT( pTopLevelEntity->pEffect->IsRuntimeData(Data.pGeneric) );
154 IsAnnotation = FALSE;
159 if (NULL == (pNewMember = CreateNewMember((SType*)pMember->pType, IsAnnotation)))
161 DPF(0, "ID3DX11Effect: Out of memory while trying to create new member variable interface");
162 return &g_InvalidScalarVariable;
165 pNewMember->pType = pMember->pType;
166 pNewMember->pName = pMember->pName;
167 pNewMember->pSemantic = pMember->pSemantic;
168 pNewMember->Data.pGeneric = Data.pGeneric;
169 pNewMember->IsSingleElement = IsSingleElement;
170 pNewMember->pTopLevelEntity = pTopLevelEntity;
172 if( IsSingleElement && pMember->pMemberData )
174 D3DXASSERT( !IsAnnotation );
175 // This is an element of a global variable array
176 pNewMember->pMemberData = pMember->pMemberData + Index;
179 if (FAILED(m_pMemberInterfaces.Add(pNewMember)))
181 SAFE_DELETE(pNewMember);
182 DPF(0, "ID3DX11Effect: Out of memory while trying to create new member variable interface");
183 return &g_InvalidScalarVariable;
186 return (ID3DX11EffectVariable *) pNewMember;
189 //////////////////////////////////////////////////////////////////////////
190 // ID3DX11EffectType (SType, SSingleElementType implementations)
191 //////////////////////////////////////////////////////////////////////////
193 static ID3DX11EffectType * GetTypeByIndexHelper(UINT Index, UINT VariableCount,
194 SVariable *pVariables, UINT SizeOfVariableType)
196 LPCSTR pFuncName = "ID3DX11EffectType::GetMemberTypeByIndex";
198 if (Index >= VariableCount)
200 DPF(0, "%s: Invalid index (%d, total: %d)", pFuncName, Index, VariableCount);
201 return &g_InvalidType;
204 SVariable *pVariable = (SVariable *)((BYTE *)pVariables + Index * SizeOfVariableType);
205 if (NULL == pVariable->pName)
207 DPF(0, "%s: Cannot get member types; Effect has been Optimize()'ed", pFuncName);
208 return &g_InvalidType;
211 return (ID3DX11EffectType *) pVariable->pType;
214 static ID3DX11EffectType * GetTypeByNameHelper(LPCSTR Name, UINT VariableCount,
215 SVariable *pVariables, UINT SizeOfVariableType)
217 LPCSTR pFuncName = "ID3DX11EffectType::GetMemberTypeByName";
221 DPF(0, "%s: Parameter Name was NULL.", pFuncName);
222 return &g_InvalidType;
226 SVariable *pVariable;
228 for (i = 0; i < VariableCount; ++ i)
230 pVariable = (SVariable *)((BYTE *)pVariables + i * SizeOfVariableType);
231 if (NULL == pVariable->pName)
233 DPF(0, "%s: Cannot get member types; Effect has been Optimize()'ed", pFuncName);
234 return &g_InvalidType;
236 if (strcmp(pVariable->pName, Name) == 0)
238 return (ID3DX11EffectType *) pVariable->pType;
242 DPF(0, "%s: Member type [%s] not found", pFuncName, Name);
243 return &g_InvalidType;
247 static ID3DX11EffectType * GetTypeBySemanticHelper(LPCSTR Semantic, UINT VariableCount,
248 SVariable *pVariables, UINT SizeOfVariableType)
250 LPCSTR pFuncName = "ID3DX11EffectType::GetMemberTypeBySemantic";
252 if (NULL == Semantic)
254 DPF(0, "%s: Parameter Semantic was NULL.", pFuncName);
255 return &g_InvalidType;
259 SVariable *pVariable;
261 for (i = 0; i < VariableCount; ++ i)
263 pVariable = (SVariable *)((BYTE *)pVariables + i * SizeOfVariableType);
264 if (NULL == pVariable->pName)
266 DPF(0, "%s: Cannot get member types; Effect has been Optimize()'ed", pFuncName);
267 return &g_InvalidType;
269 if (NULL != pVariable->pSemantic &&
270 _stricmp(pVariable->pSemantic, Semantic) == 0)
272 return (ID3DX11EffectType *) pVariable->pType;
276 DPF(0, "%s: Member type with semantic [%s] not found", pFuncName, Semantic);
277 return &g_InvalidType;
280 ID3DX11EffectType * SType::GetMemberTypeByIndex(UINT Index)
282 if (VarType != EVT_Struct)
284 DPF(0, "ID3DX11EffectType::GetMemberTypeByIndex: This interface does not refer to a structure");
285 return &g_InvalidType;
288 return GetTypeByIndexHelper(Index, StructType.Members, StructType.pMembers, sizeof(SVariable));
291 ID3DX11EffectType * SType::GetMemberTypeByName(LPCSTR Name)
293 if (VarType != EVT_Struct)
295 DPF(0, "ID3DX11EffectType::GetMemberTypeByName: This interface does not refer to a structure");
296 return &g_InvalidType;
299 return GetTypeByNameHelper(Name, StructType.Members, StructType.pMembers, sizeof(SVariable));
302 ID3DX11EffectType * SType::GetMemberTypeBySemantic(LPCSTR Semantic)
304 if (VarType != EVT_Struct)
306 DPF(0, "ID3DX11EffectType::GetMemberTypeBySemantic: This interface does not refer to a structure");
307 return &g_InvalidType;
310 return GetTypeBySemanticHelper(Semantic, StructType.Members, StructType.pMembers, sizeof(SVariable));
313 LPCSTR SType::GetMemberName(UINT Index)
315 LPCSTR pFuncName = "ID3DX11EffectType::GetMemberName";
317 if (VarType != EVT_Struct)
319 DPF(0, "%s: This interface does not refer to a structure", pFuncName);
323 if (Index >= StructType.Members)
325 DPF(0, "%s: Invalid index (%d, total: %d)", pFuncName, Index, StructType.Members);
329 SVariable *pVariable = StructType.pMembers + Index;
331 if (NULL == pVariable->pName)
333 DPF(0, "%s: Cannot get member names; Effect has been Optimize()'ed", pFuncName);
337 return pVariable->pName;
340 LPCSTR SType::GetMemberSemantic(UINT Index)
342 LPCSTR pFuncName = "ID3DX11EffectType::GetMemberSemantic";
344 if (VarType != EVT_Struct)
346 DPF(0, "%s: This interface does not refer to a structure", pFuncName);
350 if (Index >= StructType.Members)
352 DPF(0, "%s: Invalid index (%d, total: %d)", pFuncName, Index, StructType.Members);
356 SVariable *pVariable = StructType.pMembers + Index;
358 if (NULL == pVariable->pName)
360 DPF(0, "%s: Cannot get member semantics; Effect has been Optimize()'ed", pFuncName);
364 return pVariable->pSemantic;
367 HRESULT SType::GetDescHelper(D3DX11_EFFECT_TYPE_DESC *pDesc, BOOL IsSingleElement) const
370 LPCSTR pFuncName = "ID3DX11EffectType::GetDesc";
372 VERIFYPARAMETER(pDesc);
374 pDesc->TypeName = pTypeName;
376 // intentionally return 0 so they know it's not a single element array
377 pDesc->Elements = IsSingleElement ? 0 : Elements;
378 pDesc->PackedSize = GetTotalPackedSize(IsSingleElement);
379 pDesc->UnpackedSize = GetTotalUnpackedSize(IsSingleElement);
380 pDesc->Stride = Stride;
385 switch (NumericType.NumericLayout)
388 if (NumericType.IsColumnMajor)
390 pDesc->Class = D3D10_SVC_MATRIX_COLUMNS;
394 pDesc->Class = D3D10_SVC_MATRIX_ROWS;
398 pDesc->Class = D3D10_SVC_VECTOR;
401 pDesc->Class = D3D10_SVC_SCALAR;
407 switch (NumericType.ScalarType)
410 pDesc->Type = D3D10_SVT_BOOL;
413 pDesc->Type = D3D10_SVT_INT;
416 pDesc->Type = D3D10_SVT_UINT;
419 pDesc->Type = D3D10_SVT_FLOAT;
425 pDesc->Rows = NumericType.Rows;
426 pDesc->Columns = NumericType.Columns;
434 pDesc->Members = StructType.Members;
435 if( StructType.ImplementsInterface )
437 pDesc->Class = D3D11_SVC_INTERFACE_CLASS;
441 pDesc->Class = D3D10_SVC_STRUCT;
443 pDesc->Type = D3D10_SVT_VOID;
450 pDesc->Class = D3D11_SVC_INTERFACE_POINTER;
451 pDesc->Type = D3D11_SVT_INTERFACE_POINTER;
458 pDesc->Class = D3D10_SVC_OBJECT;
463 pDesc->Type = D3D10_SVT_STRING;
466 pDesc->Type = D3D10_SVT_BLEND;
468 case EOT_DepthStencil:
469 pDesc->Type = D3D10_SVT_DEPTHSTENCIL;
472 pDesc->Type = D3D10_SVT_RASTERIZER;
474 case EOT_PixelShader:
475 case EOT_PixelShader5:
476 pDesc->Type = D3D10_SVT_PIXELSHADER;
478 case EOT_VertexShader:
479 case EOT_VertexShader5:
480 pDesc->Type = D3D10_SVT_VERTEXSHADER;
482 case EOT_GeometryShader:
483 case EOT_GeometryShaderSO:
484 case EOT_GeometryShader5:
485 pDesc->Type = D3D10_SVT_GEOMETRYSHADER;
487 case EOT_HullShader5:
488 pDesc->Type = D3D11_SVT_HULLSHADER;
490 case EOT_DomainShader5:
491 pDesc->Type = D3D11_SVT_DOMAINSHADER;
493 case EOT_ComputeShader5:
494 pDesc->Type = D3D11_SVT_COMPUTESHADER;
497 pDesc->Type = D3D10_SVT_TEXTURE;
500 pDesc->Type = D3D10_SVT_TEXTURE1D;
502 case EOT_Texture1DArray:
503 pDesc->Type = D3D10_SVT_TEXTURE1DARRAY;
506 pDesc->Type = D3D10_SVT_TEXTURE2D;
508 case EOT_Texture2DArray:
509 pDesc->Type = D3D10_SVT_TEXTURE2DARRAY;
511 case EOT_Texture2DMS:
512 pDesc->Type = D3D10_SVT_TEXTURE2DMS;
514 case EOT_Texture2DMSArray:
515 pDesc->Type = D3D10_SVT_TEXTURE2DMSARRAY;
518 pDesc->Type = D3D10_SVT_TEXTURE3D;
520 case EOT_TextureCube:
521 pDesc->Type = D3D10_SVT_TEXTURECUBE;
523 case EOT_TextureCubeArray:
524 pDesc->Type = D3D10_SVT_TEXTURECUBEARRAY;
527 pDesc->Type = D3D10_SVT_BUFFER;
530 pDesc->Type = D3D10_SVT_SAMPLER;
532 case EOT_RenderTargetView:
533 pDesc->Type = D3D10_SVT_RENDERTARGETVIEW;
535 case EOT_DepthStencilView:
536 pDesc->Type = D3D10_SVT_DEPTHSTENCILVIEW;
538 case EOT_RWTexture1D:
539 pDesc->Type = D3D11_SVT_RWTEXTURE1D;
541 case EOT_RWTexture1DArray:
542 pDesc->Type = D3D11_SVT_RWTEXTURE1DARRAY;
544 case EOT_RWTexture2D:
545 pDesc->Type = D3D11_SVT_RWTEXTURE2D;
547 case EOT_RWTexture2DArray:
548 pDesc->Type = D3D11_SVT_RWTEXTURE2DARRAY;
550 case EOT_RWTexture3D:
551 pDesc->Type = D3D11_SVT_RWTEXTURE3D;
554 pDesc->Type = D3D11_SVT_RWBUFFER;
556 case EOT_ByteAddressBuffer:
557 pDesc->Type = D3D11_SVT_BYTEADDRESS_BUFFER;
559 case EOT_RWByteAddressBuffer:
560 pDesc->Type = D3D11_SVT_RWBYTEADDRESS_BUFFER;
562 case EOT_StructuredBuffer:
563 pDesc->Type = D3D11_SVT_STRUCTURED_BUFFER;
565 case EOT_RWStructuredBuffer:
566 case EOT_RWStructuredBufferAlloc:
567 case EOT_RWStructuredBufferConsume:
568 pDesc->Type = D3D11_SVT_RWSTRUCTURED_BUFFER;
570 case EOT_AppendStructuredBuffer:
571 pDesc->Type = D3D11_SVT_APPEND_STRUCTURED_BUFFER;
573 case EOT_ConsumeStructuredBuffer:
574 pDesc->Type = D3D11_SVT_CONSUME_STRUCTURED_BUFFER;
591 ////////////////////////////////////////////////////////////////////////////////
592 // ID3DX11EffectShaderVariable (SAnonymousShader implementation)
593 ////////////////////////////////////////////////////////////////////////////////
595 SAnonymousShader::SAnonymousShader(SShaderBlock *pBlock)
597 pShaderBlock = pBlock;
600 BOOL SAnonymousShader::IsValid()
602 return pShaderBlock && pShaderBlock->IsValid;
605 ID3DX11EffectType * SAnonymousShader::GetType()
607 return (ID3DX11EffectType *) this;
610 HRESULT SAnonymousShader::GetDesc(D3DX11_EFFECT_VARIABLE_DESC *pDesc)
612 pDesc->Annotations = 0;
615 pDesc->Name = "$Anonymous";
616 pDesc->Semantic = NULL;
617 pDesc->BufferOffset = 0;
622 ID3DX11EffectVariable * SAnonymousShader::GetAnnotationByIndex(UINT Index)
624 DPF(0, "ID3DX11EffectVariable::GetAnnotationByIndex: Anonymous shaders cannot have annotations");
625 return &g_InvalidScalarVariable;
628 ID3DX11EffectVariable * SAnonymousShader::GetAnnotationByName(LPCSTR Name)
630 DPF(0, "ID3DX11EffectVariable::GetAnnotationByName: Anonymous shaders cannot have annotations");
631 return &g_InvalidScalarVariable;
634 ID3DX11EffectVariable * SAnonymousShader::GetMemberByIndex(UINT Index)
636 DPF(0, "ID3DX11EffectVariable::GetMemberByIndex: Variable is not a structure");
637 return &g_InvalidScalarVariable;
640 ID3DX11EffectVariable * SAnonymousShader::GetMemberByName(LPCSTR Name)
642 DPF(0, "ID3DX11EffectVariable::GetMemberByName: Variable is not a structure");
643 return &g_InvalidScalarVariable;
646 ID3DX11EffectVariable * SAnonymousShader::GetMemberBySemantic(LPCSTR Semantic)
648 DPF(0, "ID3DX11EffectVariable::GetMemberBySemantic: Variable is not a structure");
649 return &g_InvalidScalarVariable;
652 ID3DX11EffectVariable * SAnonymousShader::GetElement(UINT Index)
654 DPF(0, "ID3DX11EffectVariable::GetElement: Anonymous shaders cannot have elements");
655 return &g_InvalidScalarVariable;
658 ID3DX11EffectConstantBuffer * SAnonymousShader::GetParentConstantBuffer()
663 ID3DX11EffectShaderVariable * SAnonymousShader::AsShader()
665 return (ID3DX11EffectShaderVariable *) this;
668 HRESULT SAnonymousShader::SetRawValue(CONST void *pData, UINT Offset, UINT Count)
670 return ObjectSetRawValue();
673 HRESULT SAnonymousShader::GetRawValue(__out_bcount(Count) void *pData, UINT Offset, UINT Count)
675 return ObjectGetRawValue();
678 #define ANONYMOUS_SHADER_INDEX_CHECK() \
680 if (0 != ShaderIndex) \
682 DPF(0, "%s: Invalid index specified", pFuncName); \
686 HRESULT SAnonymousShader::GetShaderDesc(UINT ShaderIndex, D3DX11_EFFECT_SHADER_DESC *pDesc)
688 LPCSTR pFuncName = "ID3DX11EffectShaderVariable::GetShaderDesc";
690 ANONYMOUS_SHADER_INDEX_CHECK();
692 pShaderBlock->GetShaderDesc(pDesc, TRUE);
698 HRESULT SAnonymousShader::GetVertexShader(UINT ShaderIndex, ID3D11VertexShader **ppVS)
700 LPCSTR pFuncName = "ID3DX11EffectShaderVariable::GetVertexShader";
702 ANONYMOUS_SHADER_INDEX_CHECK();
704 VH( pShaderBlock->GetVertexShader(ppVS) );
710 HRESULT SAnonymousShader::GetGeometryShader(UINT ShaderIndex, ID3D11GeometryShader **ppGS)
712 LPCSTR pFuncName = "ID3DX11EffectShaderVariable::GetGeometryShader";
714 ANONYMOUS_SHADER_INDEX_CHECK();
716 VH( pShaderBlock->GetGeometryShader(ppGS) );
722 HRESULT SAnonymousShader::GetPixelShader(UINT ShaderIndex, ID3D11PixelShader **ppPS)
724 LPCSTR pFuncName = "ID3DX11EffectShaderVariable::GetPixelShader";
726 ANONYMOUS_SHADER_INDEX_CHECK();
728 VH( pShaderBlock->GetPixelShader(ppPS) );
734 HRESULT SAnonymousShader::GetHullShader(UINT ShaderIndex, ID3D11HullShader **ppHS)
736 LPCSTR pFuncName = "ID3DX11EffectShaderVariable::GetHullShader";
738 ANONYMOUS_SHADER_INDEX_CHECK();
740 VH( pShaderBlock->GetHullShader(ppHS) );
746 HRESULT SAnonymousShader::GetDomainShader(UINT ShaderIndex, ID3D11DomainShader **ppCS)
748 LPCSTR pFuncName = "ID3DX11EffectShaderVariable::GetDomainShader";
750 ANONYMOUS_SHADER_INDEX_CHECK();
752 VH( pShaderBlock->GetDomainShader(ppCS) );
758 HRESULT SAnonymousShader::GetComputeShader(UINT ShaderIndex, ID3D11ComputeShader **ppCS)
760 LPCSTR pFuncName = "ID3DX11EffectShaderVariable::GetComputeShader";
762 ANONYMOUS_SHADER_INDEX_CHECK();
764 VH( pShaderBlock->GetComputeShader(ppCS) );
770 HRESULT SAnonymousShader::GetInputSignatureElementDesc(UINT ShaderIndex, UINT Element, D3D11_SIGNATURE_PARAMETER_DESC *pDesc)
772 LPCSTR pFuncName = "ID3DX11EffectShaderVariable::GetInputSignatureElementDesc";
774 ANONYMOUS_SHADER_INDEX_CHECK();
776 VH( pShaderBlock->GetSignatureElementDesc(SShaderBlock::ST_Input, Element, pDesc) );
782 HRESULT SAnonymousShader::GetOutputSignatureElementDesc(UINT ShaderIndex, UINT Element, D3D11_SIGNATURE_PARAMETER_DESC *pDesc)
784 LPCSTR pFuncName = "ID3DX11EffectShaderVariable::GetOutputSignatureElementDesc";
786 ANONYMOUS_SHADER_INDEX_CHECK();
788 VH( pShaderBlock->GetSignatureElementDesc(SShaderBlock::ST_Output, Element, pDesc) );
794 HRESULT SAnonymousShader::GetPatchConstantSignatureElementDesc(UINT ShaderIndex, UINT Element, D3D11_SIGNATURE_PARAMETER_DESC *pDesc)
796 LPCSTR pFuncName = "ID3DX11EffectShaderVariable::GetPatchConstantSignatureElementDesc";
798 ANONYMOUS_SHADER_INDEX_CHECK();
800 VH( pShaderBlock->GetSignatureElementDesc(SShaderBlock::ST_PatchConstant, Element, pDesc) );
806 HRESULT SAnonymousShader::GetDesc(D3DX11_EFFECT_TYPE_DESC *pDesc)
808 pDesc->Class = D3D10_SVC_OBJECT;
810 switch (pShaderBlock->GetShaderType())
812 case EOT_VertexShader:
813 case EOT_VertexShader5:
814 pDesc->TypeName = "vertexshader";
815 pDesc->Type = D3D10_SVT_VERTEXSHADER;
817 case EOT_GeometryShader:
818 case EOT_GeometryShader5:
819 pDesc->TypeName = "geometryshader";
820 pDesc->Type = D3D10_SVT_GEOMETRYSHADER;
822 case EOT_PixelShader:
823 case EOT_PixelShader5:
824 pDesc->TypeName = "pixelshader";
825 pDesc->Type = D3D10_SVT_PIXELSHADER;
827 case EOT_HullShader5:
828 pDesc->TypeName = "Hullshader";
829 pDesc->Type = D3D11_SVT_HULLSHADER;
831 case EOT_DomainShader5:
832 pDesc->TypeName = "Domainshader";
833 pDesc->Type = D3D11_SVT_DOMAINSHADER;
835 case EOT_ComputeShader5:
836 pDesc->TypeName = "Computeshader";
837 pDesc->Type = D3D11_SVT_COMPUTESHADER;
845 pDesc->PackedSize = 0;
846 pDesc->UnpackedSize = 0;
852 ID3DX11EffectType * SAnonymousShader::GetMemberTypeByIndex(UINT Index)
854 DPF(0, "ID3DX11EffectType::GetMemberTypeByIndex: This interface does not refer to a structure");
855 return &g_InvalidType;
858 ID3DX11EffectType * SAnonymousShader::GetMemberTypeByName(LPCSTR Name)
860 DPF(0, "ID3DX11EffectType::GetMemberTypeByName: This interface does not refer to a structure");
861 return &g_InvalidType;
864 ID3DX11EffectType * SAnonymousShader::GetMemberTypeBySemantic(LPCSTR Semantic)
866 DPF(0, "ID3DX11EffectType::GetMemberTypeBySemantic: This interface does not refer to a structure");
867 return &g_InvalidType;
870 LPCSTR SAnonymousShader::GetMemberName(UINT Index)
872 DPF(0, "ID3DX11EffectType::GetMemberName: This interface does not refer to a structure");
876 LPCSTR SAnonymousShader::GetMemberSemantic(UINT Index)
878 DPF(0, "ID3DX11EffectType::GetMemberSemantic: This interface does not refer to a structure");
882 //////////////////////////////////////////////////////////////////////////
883 // ID3DX11EffectConstantBuffer (SConstantBuffer implementation)
884 //////////////////////////////////////////////////////////////////////////
886 BOOL SConstantBuffer::IsValid()
891 ID3DX11EffectType * SConstantBuffer::GetType()
893 return (ID3DX11EffectType *) this;
896 HRESULT SConstantBuffer::GetDesc(D3DX11_EFFECT_VARIABLE_DESC *pDesc)
898 pDesc->Annotations = AnnotationCount;
902 pDesc->Semantic = NULL;
903 pDesc->BufferOffset = 0;
905 if (ExplicitBindPoint != -1)
907 pDesc->ExplicitBindPoint = ExplicitBindPoint;
908 pDesc->Flags |= D3DX11_EFFECT_VARIABLE_EXPLICIT_BIND_POINT;
912 pDesc->ExplicitBindPoint = 0;
918 ID3DX11EffectVariable * SConstantBuffer::GetAnnotationByIndex(UINT Index)
920 return GetAnnotationByIndexHelper("ID3DX11EffectVariable", Index, AnnotationCount, pAnnotations);
923 ID3DX11EffectVariable * SConstantBuffer::GetAnnotationByName(LPCSTR Name)
925 return GetAnnotationByNameHelper("ID3DX11EffectVariable", Name, AnnotationCount, pAnnotations);
928 ID3DX11EffectVariable * SConstantBuffer::GetMemberByIndex(UINT Index)
930 SGlobalVariable *pMember;
931 UDataPointer dataPtr;
933 if (IsEffectOptimized)
935 DPF(0, "ID3DX11EffectVariable::GetMemberByIndex: Cannot get members; effect has been Optimize()'ed");
936 return &g_InvalidScalarVariable;
939 if (!GetVariableByIndexHelper<SGlobalVariable>(Index, VariableCount, (SGlobalVariable*)pVariables,
940 NULL, &pMember, &dataPtr.pGeneric))
942 return &g_InvalidScalarVariable;
945 return (ID3DX11EffectVariable *) pMember;
948 ID3DX11EffectVariable * SConstantBuffer::GetMemberByName(LPCSTR Name)
950 SGlobalVariable *pMember;
951 UDataPointer dataPtr;
954 if (IsEffectOptimized)
956 DPF(0, "ID3DX11EffectVariable::GetMemberByName: Cannot get members; effect has been Optimize()'ed");
957 return &g_InvalidScalarVariable;
960 if (!GetVariableByNameHelper<SGlobalVariable>(Name, VariableCount, (SGlobalVariable*)pVariables,
961 NULL, &pMember, &dataPtr.pGeneric, &index))
963 return &g_InvalidScalarVariable;
966 return (ID3DX11EffectVariable *) pMember;
969 ID3DX11EffectVariable * SConstantBuffer::GetMemberBySemantic(LPCSTR Semantic)
971 SGlobalVariable *pMember;
972 UDataPointer dataPtr;
975 if (IsEffectOptimized)
977 DPF(0, "ID3DX11EffectVariable::GetMemberBySemantic: Cannot get members; effect has been Optimize()'ed");
978 return &g_InvalidScalarVariable;
981 if (!GetVariableBySemanticHelper<SGlobalVariable>(Semantic, VariableCount, (SGlobalVariable*)pVariables,
982 NULL, &pMember, &dataPtr.pGeneric, &index))
984 return &g_InvalidScalarVariable;
987 return (ID3DX11EffectVariable *) pMember;
990 ID3DX11EffectVariable * SConstantBuffer::GetElement(UINT Index)
992 LPCSTR pFuncName = "ID3DX11EffectVariable::GetElement";
993 DPF(0, "%s: This interface does not refer to an array", pFuncName);
994 return &g_InvalidScalarVariable;
997 ID3DX11EffectConstantBuffer * SConstantBuffer::GetParentConstantBuffer()
999 LPCSTR pFuncName = "ID3DX11EffectVariable::GetParentConstantBuffer";
1000 DPF(0, "%s: Constant buffers do not have parent constant buffers", pFuncName);
1001 return &g_InvalidConstantBuffer;
1004 ID3DX11EffectConstantBuffer * SConstantBuffer::AsConstantBuffer()
1006 return (ID3DX11EffectConstantBuffer *) this;
1009 HRESULT SConstantBuffer::GetDesc(D3DX11_EFFECT_TYPE_DESC *pDesc)
1011 pDesc->TypeName = IsTBuffer ? "tbuffer" : "cbuffer";
1012 pDesc->Class = D3D10_SVC_OBJECT;
1013 pDesc->Type = IsTBuffer ? D3D10_SVT_TBUFFER : D3D10_SVT_CBUFFER;
1015 pDesc->Elements = 0;
1016 pDesc->Members = VariableCount;
1021 pDesc->PackedSize = 0;
1022 for (i = 0; i < VariableCount; ++ i)
1024 pDesc->PackedSize += pVariables[i].pType->PackedSize;
1027 pDesc->UnpackedSize = Size;
1028 D3DXASSERT(pDesc->UnpackedSize >= pDesc->PackedSize);
1030 pDesc->Stride = AlignToPowerOf2(pDesc->UnpackedSize, SType::c_RegisterSize);
1035 ID3DX11EffectType * SConstantBuffer::GetMemberTypeByIndex(UINT Index)
1037 return GetTypeByIndexHelper(Index, VariableCount, pVariables, sizeof (SGlobalVariable));
1040 ID3DX11EffectType * SConstantBuffer::GetMemberTypeByName(LPCSTR Name)
1042 return GetTypeByNameHelper(Name, VariableCount, pVariables, sizeof (SGlobalVariable));
1045 ID3DX11EffectType * SConstantBuffer::GetMemberTypeBySemantic(LPCSTR Semantic)
1047 return GetTypeBySemanticHelper(Semantic, VariableCount, pVariables, sizeof (SGlobalVariable));
1050 LPCSTR SConstantBuffer::GetMemberName(UINT Index)
1052 LPCSTR pFuncName = "ID3DX11EffectType::GetMemberName";
1054 if (IsEffectOptimized)
1056 DPF(0, "%s: Cannot get member names; Effect has been Optimize()'ed", pFuncName);
1060 if (Index >= VariableCount)
1062 DPF(0, "%s: Invalid index (%d, total: %d)", pFuncName, Index, VariableCount);
1066 return pVariables[Index].pName;
1069 LPCSTR SConstantBuffer::GetMemberSemantic(UINT Index)
1071 LPCSTR pFuncName = "ID3DX11EffectType::GetMemberSemantic";
1073 if (IsEffectOptimized)
1075 DPF(0, "%s: Cannot get member semantics; Effect has been Optimize()'ed", pFuncName);
1079 if (Index >= VariableCount)
1081 DPF(0, "%s: Invalid index (%d, total: %d)", pFuncName, Index, VariableCount);
1085 return pVariables[Index].pSemantic;
1088 HRESULT SConstantBuffer::SetRawValue(CONST void *pData, UINT Offset, UINT Count)
1093 LPCSTR pFuncName = "ID3DX11EffectVariable::SetRawValue";
1095 VERIFYPARAMETER(pData);
1097 if ((Offset + Count < Offset) ||
1098 (Count + (BYTE*)pData < (BYTE*)pData) ||
1099 ((Offset + Count) > Size))
1101 // overflow of some kind
1102 DPF(0, "%s: Invalid range specified", pFuncName);
1107 if (IsUsedByExpression)
1110 for (i = 0; i < VariableCount; ++ i)
1112 ((SGlobalVariable*)pVariables)[i].DirtyVariable();
1120 memcpy(pBackingStore + Offset, pData, Count);
1126 HRESULT SConstantBuffer::GetRawValue(__out_bcount(Count) void *pData, UINT Offset, UINT Count)
1131 LPCSTR pFuncName = "ID3DX11EffectVariable::GetRawValue";
1133 VERIFYPARAMETER(pData);
1135 if ((Offset + Count < Offset) ||
1136 (Count + (BYTE*)pData < (BYTE*)pData) ||
1137 ((Offset + Count) > Size))
1139 // overflow of some kind
1140 DPF(0, "%s: Invalid range specified", pFuncName);
1145 memcpy(pData, pBackingStore + Offset, Count);
1151 bool SConstantBuffer::ClonedSingle() const
1153 return IsSingle && ( pEffect->m_Flags & D3DX11_EFFECT_CLONE );
1156 HRESULT SConstantBuffer::SetConstantBuffer(ID3D11Buffer *pConstantBuffer)
1159 LPCSTR pFuncName = "ID3DX11EffectConstantBuffer::SetConstantBuffer";
1163 DPF(0, "%s: This is a texture buffer; use SetTextureBuffer instead", pFuncName);
1164 VH(D3DERR_INVALIDCALL);
1167 // Replace all references to the old shader block with this one
1168 pEffect->ReplaceCBReference(this, pConstantBuffer);
1170 if( !IsUserManaged )
1172 // Save original cbuffer in case we UndoSet
1173 D3DXASSERT( pMemberData[0].Type == MDT_Buffer );
1174 VB( pMemberData[0].Data.pD3DEffectsManagedConstantBuffer == NULL );
1175 pMemberData[0].Data.pD3DEffectsManagedConstantBuffer = pD3DObject;
1177 IsUserManaged = TRUE;
1178 IsNonUpdatable = TRUE;
1181 SAFE_ADDREF( pConstantBuffer );
1182 SAFE_RELEASE( pD3DObject );
1183 pD3DObject = pConstantBuffer;
1189 HRESULT SConstantBuffer::GetConstantBuffer(ID3D11Buffer **ppConstantBuffer)
1192 LPCSTR pFuncName = "ID3DX11EffectConstantBuffer::GetConstantBuffer";
1194 VERIFYPARAMETER(ppConstantBuffer);
1198 DPF(0, "%s: This is a texture buffer; use GetTextureBuffer instead", pFuncName);
1199 VH(D3DERR_INVALIDCALL);
1202 *ppConstantBuffer = pD3DObject;
1203 SAFE_ADDREF(*ppConstantBuffer);
1209 HRESULT SConstantBuffer::UndoSetConstantBuffer()
1212 LPCSTR pFuncName = "ID3DX11EffectConstantBuffer::UndoSetConstantBuffer";
1216 DPF(0, "%s: This is a texture buffer; use UndoSetTextureBuffer instead", pFuncName);
1217 VH(D3DERR_INVALIDCALL);
1220 if( !IsUserManaged )
1225 // Replace all references to the old shader block with this one
1226 pEffect->ReplaceCBReference(this, pMemberData[0].Data.pD3DEffectsManagedConstantBuffer);
1228 // Revert to original cbuffer
1229 SAFE_RELEASE( pD3DObject );
1230 pD3DObject = pMemberData[0].Data.pD3DEffectsManagedConstantBuffer;
1231 pMemberData[0].Data.pD3DEffectsManagedConstantBuffer = NULL;
1232 IsUserManaged = FALSE;
1233 IsNonUpdatable = ClonedSingle();
1239 HRESULT SConstantBuffer::SetTextureBuffer(ID3D11ShaderResourceView *pTextureBuffer)
1242 LPCSTR pFuncName = "ID3DX11EffectConstantBuffer::SetTextureBuffer";
1246 DPF(0, "%s: This is a constant buffer; use SetConstantBuffer instead", pFuncName);
1247 VH(D3DERR_INVALIDCALL);
1250 if( !IsUserManaged )
1252 // Save original cbuffer and tbuffer in case we UndoSet
1253 D3DXASSERT( pMemberData[0].Type == MDT_Buffer );
1254 VB( pMemberData[0].Data.pD3DEffectsManagedConstantBuffer == NULL );
1255 pMemberData[0].Data.pD3DEffectsManagedConstantBuffer = pD3DObject;
1257 D3DXASSERT( pMemberData[1].Type == MDT_ShaderResourceView );
1258 VB( pMemberData[1].Data.pD3DEffectsManagedTextureBuffer == NULL );
1259 pMemberData[1].Data.pD3DEffectsManagedTextureBuffer = TBuffer.pShaderResource;
1260 TBuffer.pShaderResource = NULL;
1261 IsUserManaged = TRUE;
1262 IsNonUpdatable = TRUE;
1265 SAFE_ADDREF( pTextureBuffer );
1266 SAFE_RELEASE(pD3DObject); // won't be needing this anymore...
1267 SAFE_RELEASE( TBuffer.pShaderResource );
1268 TBuffer.pShaderResource = pTextureBuffer;
1274 HRESULT SConstantBuffer::GetTextureBuffer(ID3D11ShaderResourceView **ppTextureBuffer)
1277 LPCSTR pFuncName = "ID3DX11EffectConstantBuffer::GetTextureBuffer";
1279 VERIFYPARAMETER(ppTextureBuffer);
1283 DPF(0, "%s: This is a constant buffer; use GetConstantBuffer instead", pFuncName);
1284 VH(D3DERR_INVALIDCALL);
1287 *ppTextureBuffer = TBuffer.pShaderResource;
1288 SAFE_ADDREF(*ppTextureBuffer);
1294 HRESULT SConstantBuffer::UndoSetTextureBuffer()
1297 LPCSTR pFuncName = "ID3DX11EffectConstantBuffer::UndoSetTextureBuffer";
1301 DPF(0, "%s: This is a texture buffer; use UndoSetConstantBuffer instead", pFuncName);
1302 VH(D3DERR_INVALIDCALL);
1305 if( !IsUserManaged )
1310 // Revert to original cbuffer
1311 SAFE_RELEASE( pD3DObject );
1312 pD3DObject = pMemberData[0].Data.pD3DEffectsManagedConstantBuffer;
1313 pMemberData[0].Data.pD3DEffectsManagedConstantBuffer = NULL;
1314 SAFE_RELEASE( TBuffer.pShaderResource );
1315 TBuffer.pShaderResource = pMemberData[1].Data.pD3DEffectsManagedTextureBuffer;
1316 pMemberData[1].Data.pD3DEffectsManagedTextureBuffer = NULL;
1317 IsUserManaged = FALSE;
1318 IsNonUpdatable = ClonedSingle();
1324 //////////////////////////////////////////////////////////////////////////
1325 // ID3DX11EffectPass (CEffectPass implementation)
1326 //////////////////////////////////////////////////////////////////////////
1328 BOOL SPassBlock::IsValid()
1330 if( HasDependencies )
1331 return pEffect->ValidatePassBlock( this );
1332 return InitiallyValid;
1335 HRESULT SPassBlock::GetDesc(D3DX11_PASS_DESC *pDesc)
1338 LPCSTR pFuncName = "ID3DX11EffectPass::GetDesc";
1340 VERIFYPARAMETER(pDesc);
1342 ZeroMemory(pDesc, sizeof(*pDesc));
1344 pDesc->Name = pName;
1345 pDesc->Annotations = AnnotationCount;
1347 SAssignment *pAssignment;
1348 SAssignment *pLastAssn;
1350 pEffect->IncrementTimer();
1352 pAssignment = pAssignments;
1353 pLastAssn = pAssignments + AssignmentCount;
1355 for(; pAssignment < pLastAssn; pAssignment++)
1357 pEffect->EvaluateAssignment(pAssignment);
1360 if( BackingStore.pVertexShaderBlock && BackingStore.pVertexShaderBlock->pInputSignatureBlob )
1362 // pInputSignatureBlob can be null if we're setting a NULL VS "SetVertexShader( NULL )"
1363 pDesc->pIAInputSignature = (BYTE*)BackingStore.pVertexShaderBlock->pInputSignatureBlob->GetBufferPointer();
1364 pDesc->IAInputSignatureSize = BackingStore.pVertexShaderBlock->pInputSignatureBlob->GetBufferSize();
1367 pDesc->StencilRef = BackingStore.StencilRef;
1368 pDesc->SampleMask = BackingStore.SampleMask;
1369 pDesc->BlendFactor[0] = BackingStore.BlendFactor[0];
1370 pDesc->BlendFactor[1] = BackingStore.BlendFactor[1];
1371 pDesc->BlendFactor[2] = BackingStore.BlendFactor[2];
1372 pDesc->BlendFactor[3] = BackingStore.BlendFactor[3];
1378 extern SShaderBlock g_NullVS;
1379 extern SShaderBlock g_NullGS;
1380 extern SShaderBlock g_NullPS;
1381 extern SShaderBlock g_NullHS;
1382 extern SShaderBlock g_NullDS;
1383 extern SShaderBlock g_NullCS;
1385 SAnonymousShader g_AnonymousNullVS(&g_NullVS);
1386 SAnonymousShader g_AnonymousNullGS(&g_NullGS);
1387 SAnonymousShader g_AnonymousNullPS(&g_NullPS);
1388 SAnonymousShader g_AnonymousNullHS(&g_NullHS);
1389 SAnonymousShader g_AnonymousNullDS(&g_NullDS);
1390 SAnonymousShader g_AnonymousNullCS(&g_NullCS);
1392 template<EObjectType EShaderType>
1393 HRESULT SPassBlock::GetShaderDescHelper(D3DX11_PASS_SHADER_DESC *pDesc)
1397 LPCSTR pFuncName = NULL;
1398 SShaderBlock *pShaderBlock = NULL;
1400 ApplyPassAssignments();
1402 switch (EShaderType)
1404 case EOT_VertexShader:
1405 case EOT_VertexShader5:
1406 #pragma prefast(suppress:__WARNING_UNUSED_POINTER_ASSIGNMENT, "pFuncName used in DPF")
1407 pFuncName = "ID3DX11EffectPass::GetVertexShaderDesc";
1408 pShaderBlock = BackingStore.pVertexShaderBlock;
1410 case EOT_PixelShader:
1411 case EOT_PixelShader5:
1412 #pragma prefast(suppress:__WARNING_UNUSED_POINTER_ASSIGNMENT, "pFuncName used in DPF")
1413 pFuncName = "ID3DX11EffectPass::GetPixelShaderDesc";
1414 pShaderBlock = BackingStore.pPixelShaderBlock;
1416 case EOT_GeometryShader:
1417 case EOT_GeometryShader5:
1418 #pragma prefast(suppress:__WARNING_UNUSED_POINTER_ASSIGNMENT, "pFuncName used in DPF")
1419 pFuncName = "ID3DX11EffectPass::GetGeometryShaderDesc";
1420 pShaderBlock = BackingStore.pGeometryShaderBlock;
1422 case EOT_HullShader5:
1423 #pragma prefast(suppress:__WARNING_UNUSED_POINTER_ASSIGNMENT, "pFuncName used in DPF")
1424 pFuncName = "ID3DX11EffectPass::GetHullShaderDesc";
1425 pShaderBlock = BackingStore.pHullShaderBlock;
1427 case EOT_DomainShader5:
1428 #pragma prefast(suppress:__WARNING_UNUSED_POINTER_ASSIGNMENT, "pFuncName used in DPF")
1429 pFuncName = "ID3DX11EffectPass::GetDomainShaderDesc";
1430 pShaderBlock = BackingStore.pDomainShaderBlock;
1432 case EOT_ComputeShader5:
1433 #pragma prefast(suppress:__WARNING_UNUSED_POINTER_ASSIGNMENT, "pFuncName used in DPF")
1434 pFuncName = "ID3DX11EffectPass::GetComputeShaderDesc";
1435 pShaderBlock = BackingStore.pComputeShaderBlock;
1441 VERIFYPARAMETER(pDesc);
1443 // in case of error (or in case the assignment doesn't exist), return something reasonable
1444 pDesc->pShaderVariable = &g_InvalidShaderVariable;
1445 pDesc->ShaderIndex = 0;
1447 if (NULL != pShaderBlock)
1449 UINT elements, varCount, anonymousShaderCount;
1450 SGlobalVariable *pVariables;
1451 SAnonymousShader *pAnonymousShaders;
1453 if (pShaderBlock == &g_NullVS)
1455 pDesc->pShaderVariable = &g_AnonymousNullVS;
1456 pDesc->ShaderIndex = 0;
1460 else if (pShaderBlock == &g_NullGS)
1462 pDesc->pShaderVariable = &g_AnonymousNullGS;
1463 pDesc->ShaderIndex = 0;
1467 else if (pShaderBlock == &g_NullPS)
1469 pDesc->pShaderVariable = &g_AnonymousNullPS;
1470 pDesc->ShaderIndex = 0;
1474 else if (pShaderBlock == &g_NullHS)
1476 pDesc->pShaderVariable = &g_AnonymousNullHS;
1477 pDesc->ShaderIndex = 0;
1481 else if (pShaderBlock == &g_NullDS)
1483 pDesc->pShaderVariable = &g_AnonymousNullDS;
1484 pDesc->ShaderIndex = 0;
1488 else if (pShaderBlock == &g_NullCS)
1490 pDesc->pShaderVariable = &g_AnonymousNullCS;
1491 pDesc->ShaderIndex = 0;
1497 VB( pEffect->IsRuntimeData(pShaderBlock) );
1498 varCount = pEffect->m_VariableCount;
1499 pVariables = pEffect->m_pVariables;
1500 anonymousShaderCount = pEffect->m_AnonymousShaderCount;
1501 pAnonymousShaders = pEffect->m_pAnonymousShaders;
1504 for (i = 0; i < varCount; ++ i)
1506 elements = max(1, pVariables[i].pType->Elements);
1507 // make sure the variable type matches, and don't forget about GeometryShaderSO's
1508 if (pVariables[i].pType->IsShader())
1510 if (pShaderBlock >= pVariables[i].Data.pShader && pShaderBlock < pVariables[i].Data.pShader + elements)
1512 pDesc->pShaderVariable = (ID3DX11EffectShaderVariable *)(pVariables + i);
1513 pDesc->ShaderIndex = (UINT)(UINT_PTR)(pShaderBlock - pVariables[i].Data.pShader);
1520 for (i = 0; i < anonymousShaderCount; ++ i)
1522 if (pShaderBlock == pAnonymousShaders[i].pShaderBlock)
1524 VB(EShaderType == pAnonymousShaders[i].pShaderBlock->GetShaderType())
1525 pDesc->pShaderVariable = (pAnonymousShaders + i);
1526 pDesc->ShaderIndex = 0;
1532 DPF(0, "%s: Internal error; shader not found", pFuncName);
1540 HRESULT SPassBlock::GetVertexShaderDesc(D3DX11_PASS_SHADER_DESC *pDesc)
1542 return GetShaderDescHelper<EOT_VertexShader>(pDesc);
1545 HRESULT SPassBlock::GetPixelShaderDesc(D3DX11_PASS_SHADER_DESC *pDesc)
1547 return GetShaderDescHelper<EOT_PixelShader>(pDesc);
1550 HRESULT SPassBlock::GetGeometryShaderDesc(D3DX11_PASS_SHADER_DESC *pDesc)
1552 return GetShaderDescHelper<EOT_GeometryShader>(pDesc);
1555 HRESULT SPassBlock::GetHullShaderDesc(D3DX11_PASS_SHADER_DESC *pDesc)
1557 return GetShaderDescHelper<EOT_HullShader5>(pDesc);
1560 HRESULT SPassBlock::GetDomainShaderDesc(D3DX11_PASS_SHADER_DESC *pDesc)
1562 return GetShaderDescHelper<EOT_DomainShader5>(pDesc);
1565 HRESULT SPassBlock::GetComputeShaderDesc(D3DX11_PASS_SHADER_DESC *pDesc)
1567 return GetShaderDescHelper<EOT_ComputeShader5>(pDesc);
1570 ID3DX11EffectVariable * SPassBlock::GetAnnotationByIndex(UINT Index)
1572 return GetAnnotationByIndexHelper("ID3DX11EffectPass", Index, AnnotationCount, pAnnotations);
1575 ID3DX11EffectVariable * SPassBlock::GetAnnotationByName(LPCSTR Name)
1577 return GetAnnotationByNameHelper("ID3DX11EffectPass", Name, AnnotationCount, pAnnotations);
1580 HRESULT SPassBlock::Apply(UINT Flags, ID3D11DeviceContext* pContext)
1584 // TODO: Flags are not yet implemented
1586 D3DXASSERT( pEffect->m_pContext == NULL );
1587 pEffect->m_pContext = pContext;
1588 pEffect->ApplyPassBlock(this);
1589 pEffect->m_pContext = NULL;
1595 HRESULT SPassBlock::ComputeStateBlockMask(D3DX11_STATE_BLOCK_MASK *pStateBlockMask)
1600 // flags indicating whether the following shader types were caught by assignment checks or not
1601 BOOL bVS = FALSE, bGS = FALSE, bPS = FALSE, bHS = FALSE, bDS = FALSE, bCS = FALSE;
1603 for (i = 0; i < AssignmentCount; ++ i)
1605 BOOL bShader = FALSE;
1607 switch (pAssignments[i].LhsType)
1609 case ELHS_VertexShaderBlock:
1613 case ELHS_GeometryShaderBlock:
1617 case ELHS_PixelShaderBlock:
1621 case ELHS_HullShaderBlock:
1625 case ELHS_DomainShaderBlock:
1629 case ELHS_ComputeShaderBlock:
1634 case ELHS_RasterizerBlock:
1635 pStateBlockMask->RSRasterizerState = 1;
1637 case ELHS_BlendBlock:
1638 pStateBlockMask->OMBlendState = 1;
1640 case ELHS_DepthStencilBlock:
1641 pStateBlockMask->OMDepthStencilState = 1;
1645 // ignore this assignment (must be a scalar/vector assignment associated with a state object)
1651 for (j = 0; j < pAssignments[i].MaxElements; ++ j)
1653 // compute state block mask for the union of ALL shaders
1654 VH( pAssignments[i].Source.pShader[j].ComputeStateBlockMask(pStateBlockMask) );
1659 // go over the state block objects in case there was no corresponding assignment
1660 if (NULL != BackingStore.pRasterizerBlock)
1662 pStateBlockMask->RSRasterizerState = 1;
1664 if (NULL != BackingStore.pBlendBlock)
1666 pStateBlockMask->OMBlendState = 1;
1668 if (NULL != BackingStore.pDepthStencilBlock)
1670 pStateBlockMask->OMDepthStencilState = 1;
1673 // go over the shaders only if an assignment didn't already catch them
1674 if (FALSE == bVS && NULL != BackingStore.pVertexShaderBlock)
1676 VH( BackingStore.pVertexShaderBlock->ComputeStateBlockMask(pStateBlockMask) );
1678 if (FALSE == bGS && NULL != BackingStore.pGeometryShaderBlock)
1680 VH( BackingStore.pGeometryShaderBlock->ComputeStateBlockMask(pStateBlockMask) );
1682 if (FALSE == bPS && NULL != BackingStore.pPixelShaderBlock)
1684 VH( BackingStore.pPixelShaderBlock->ComputeStateBlockMask(pStateBlockMask) );
1686 if (FALSE == bHS && NULL != BackingStore.pHullShaderBlock)
1688 VH( BackingStore.pHullShaderBlock->ComputeStateBlockMask(pStateBlockMask) );
1690 if (FALSE == bDS && NULL != BackingStore.pDomainShaderBlock)
1692 VH( BackingStore.pDomainShaderBlock->ComputeStateBlockMask(pStateBlockMask) );
1694 if (FALSE == bCS && NULL != BackingStore.pComputeShaderBlock)
1696 VH( BackingStore.pComputeShaderBlock->ComputeStateBlockMask(pStateBlockMask) );
1703 //////////////////////////////////////////////////////////////////////////
1704 // ID3DX11EffectTechnique (STechnique implementation)
1705 //////////////////////////////////////////////////////////////////////////
1707 BOOL STechnique::IsValid()
1709 if( HasDependencies )
1711 for( UINT i = 0; i < PassCount; i++ )
1713 if( !((SPassBlock*)pPasses)[i].IsValid() )
1718 return InitiallyValid;
1721 HRESULT STechnique::GetDesc(D3DX11_TECHNIQUE_DESC *pDesc)
1725 LPCSTR pFuncName = "ID3DX11EffectTechnique::GetDesc";
1727 VERIFYPARAMETER(pDesc);
1729 pDesc->Name = pName;
1730 pDesc->Annotations = AnnotationCount;
1731 pDesc->Passes = PassCount;
1737 ID3DX11EffectVariable * STechnique::GetAnnotationByIndex(UINT Index)
1739 return GetAnnotationByIndexHelper("ID3DX11EffectTechnique", Index, AnnotationCount, pAnnotations);
1742 ID3DX11EffectVariable * STechnique::GetAnnotationByName(LPCSTR Name)
1744 return GetAnnotationByNameHelper("ID3DX11EffectTechnique", Name, AnnotationCount, pAnnotations);
1747 ID3DX11EffectPass * STechnique::GetPassByIndex(UINT Index)
1749 LPCSTR pFuncName = "ID3DX11EffectTechnique::GetPassByIndex";
1751 if (Index >= PassCount)
1753 DPF(0, "%s: Invalid pass index (%d, total: %d)", pFuncName, Index, PassCount);
1754 return &g_InvalidPass;
1757 return (ID3DX11EffectPass *)(pPasses + Index);
1760 ID3DX11EffectPass * STechnique::GetPassByName(LPCSTR Name)
1762 LPCSTR pFuncName = "ID3DX11EffectTechnique::GetPassByName";
1766 for (i = 0; i < PassCount; ++ i)
1768 if (NULL != pPasses[i].pName &&
1769 strcmp(pPasses[i].pName, Name) == 0)
1777 DPF(0, "%s: Pass [%s] not found", pFuncName, Name);
1778 return &g_InvalidPass;
1781 return (ID3DX11EffectPass *)(pPasses + i);
1784 HRESULT STechnique::ComputeStateBlockMask(D3DX11_STATE_BLOCK_MASK *pStateBlockMask)
1789 for (i = 0; i < PassCount; ++ i)
1791 VH( ((SPassBlock*)pPasses)[i].ComputeStateBlockMask(pStateBlockMask) );
1798 //////////////////////////////////////////////////////////////////////////
1799 // ID3DX11EffectGroup (SGroup implementation)
1800 //////////////////////////////////////////////////////////////////////////
1802 BOOL SGroup::IsValid()
1804 if( HasDependencies )
1806 for( UINT i = 0; i < TechniqueCount; i++ )
1808 if( !((STechnique*)pTechniques)[i].IsValid() )
1813 return InitiallyValid;
1816 HRESULT SGroup::GetDesc(D3DX11_GROUP_DESC *pDesc)
1820 LPCSTR pFuncName = "ID3DX11EffectGroup::GetDesc";
1822 VERIFYPARAMETER(pDesc);
1824 pDesc->Name = pName;
1825 pDesc->Annotations = AnnotationCount;
1826 pDesc->Techniques = TechniqueCount;
1832 ID3DX11EffectVariable * SGroup::GetAnnotationByIndex(UINT Index)
1834 return GetAnnotationByIndexHelper("ID3DX11EffectGroup", Index, AnnotationCount, pAnnotations);
1837 ID3DX11EffectVariable * SGroup::GetAnnotationByName(LPCSTR Name)
1839 return GetAnnotationByNameHelper("ID3DX11EffectGroup", Name, AnnotationCount, pAnnotations);
1842 ID3DX11EffectTechnique * SGroup::GetTechniqueByIndex(UINT Index)
1844 LPCSTR pFuncName = "ID3DX11EffectGroup::GetTechniqueByIndex";
1846 if (Index >= TechniqueCount)
1848 DPF(0, "%s: Invalid pass index (%d, total: %d)", pFuncName, Index, TechniqueCount);
1849 return &g_InvalidTechnique;
1852 return (ID3DX11EffectTechnique *)(pTechniques + Index);
1855 ID3DX11EffectTechnique * SGroup::GetTechniqueByName(LPCSTR Name)
1857 LPCSTR pFuncName = "ID3DX11EffectGroup::GetTechniqueByName";
1861 for (i = 0; i < TechniqueCount; ++ i)
1863 if (NULL != pTechniques[i].pName &&
1864 strcmp(pTechniques[i].pName, Name) == 0)
1870 if (i == TechniqueCount)
1872 DPF(0, "%s: Technique [%s] not found", pFuncName, Name);
1873 return &g_InvalidTechnique;
1876 return (ID3DX11EffectTechnique *)(pTechniques + i);
1879 //////////////////////////////////////////////////////////////////////////
1880 // ID3DX11Effect Public Reflection APIs (CEffect)
1881 //////////////////////////////////////////////////////////////////////////
1883 HRESULT CEffect::GetDevice(ID3D11Device **ppDevice)
1886 LPCSTR pFuncName = "ID3DX11Effect::GetDevice";
1887 VERIFYPARAMETER(ppDevice);
1889 m_pDevice->AddRef();
1890 *ppDevice = m_pDevice;
1896 HRESULT CEffect::GetDesc(D3DX11_EFFECT_DESC *pDesc)
1900 LPCSTR pFuncName = "ID3DX11Effect::GetDesc";
1902 VERIFYPARAMETER(pDesc);
1904 pDesc->ConstantBuffers = m_CBCount;
1905 pDesc->GlobalVariables = m_VariableCount;
1906 pDesc->Techniques = m_TechniqueCount;
1907 pDesc->Groups = m_GroupCount;
1908 pDesc->InterfaceVariables = m_InterfaceCount;
1914 ID3DX11EffectConstantBuffer * CEffect::GetConstantBufferByIndex(UINT Index)
1916 LPCSTR pFuncName = "ID3DX11Effect::GetConstantBufferByIndex";
1918 if (Index < m_CBCount)
1920 return m_pCBs + Index;
1923 DPF(0, "%s: Invalid constant buffer index", pFuncName);
1924 return &g_InvalidConstantBuffer;
1927 ID3DX11EffectConstantBuffer * CEffect::GetConstantBufferByName(LPCSTR Name)
1930 LPCSTR pFuncName = "ID3DX11Effect::GetConstantBufferByName";
1934 DPF(0, "%s: Cannot get constant buffer interfaces by name since the effect has been Optimize()'ed", pFuncName);
1935 return &g_InvalidConstantBuffer;
1940 DPF(0, "%s: Parameter Name was NULL.", pFuncName);
1941 return &g_InvalidConstantBuffer;
1946 for (i = 0; i < m_CBCount; ++ i)
1948 if (strcmp(m_pCBs[i].pName, Name) == 0)
1954 DPF(0, "%s: Constant Buffer [%s] not found", pFuncName, Name);
1955 return &g_InvalidConstantBuffer;
1958 ID3DX11EffectVariable * CEffect::GetVariableByIndex(UINT Index)
1960 LPCSTR pFuncName = "ID3DX11Effect::GetVariableByIndex";
1962 if (Index < m_VariableCount)
1964 return m_pVariables + Index;
1967 DPF(0, "%s: Invalid variable index", pFuncName);
1968 return &g_InvalidScalarVariable;
1971 ID3DX11EffectVariable * CEffect::GetVariableByName(LPCSTR Name)
1974 LPCSTR pFuncName = "ID3DX11Effect::GetVariableByName";
1978 DPF(0, "%s: Cannot get variable interfaces by name since the effect has been Optimize()'ed", pFuncName);
1979 return &g_InvalidScalarVariable;
1984 DPF(0, "%s: Parameter Name was NULL.", pFuncName);
1985 return &g_InvalidScalarVariable;
1990 for (i = 0; i < m_VariableCount; ++ i)
1992 if (strcmp(m_pVariables[i].pName, Name) == 0)
1994 return m_pVariables + i;
1998 DPF(0, "%s: Variable [%s] not found", pFuncName, Name);
1999 return &g_InvalidScalarVariable;
2002 ID3DX11EffectVariable * CEffect::GetVariableBySemantic(LPCSTR Semantic)
2004 LPCSTR pFuncName = "ID3DX11Effect::GetVariableBySemantic";
2008 DPF(0, "%s: Cannot get variable interfaces by semantic since the effect has been Optimize()'ed", pFuncName);
2009 return &g_InvalidScalarVariable;
2012 if (NULL == Semantic)
2014 DPF(0, "%s: Parameter Semantic was NULL.", pFuncName);
2015 return &g_InvalidScalarVariable;
2020 for (i = 0; i < m_VariableCount; ++ i)
2022 if (NULL != m_pVariables[i].pSemantic &&
2023 _stricmp(m_pVariables[i].pSemantic, Semantic) == 0)
2025 return (ID3DX11EffectVariable *)(m_pVariables + i);
2029 DPF(0, "%s: Variable with semantic [%s] not found", pFuncName, Semantic);
2030 return &g_InvalidScalarVariable;
2033 ID3DX11EffectTechnique * CEffect::GetTechniqueByIndex(UINT Index)
2035 LPCSTR pFuncName = "ID3DX11Effect::GetTechniqueByIndex";
2037 if( Index < m_TechniqueCount )
2040 for( i=0; i < m_GroupCount; i++ )
2042 if( Index < m_pGroups[i].TechniqueCount )
2044 return (ID3DX11EffectTechnique *)(m_pGroups[i].pTechniques + Index);
2046 Index -= m_pGroups[i].TechniqueCount;
2048 D3DXASSERT( FALSE );
2050 DPF(0, "%s: Invalid technique index (%d)", pFuncName, Index);
2051 return &g_InvalidTechnique;
2054 ID3DX11EffectTechnique * CEffect::GetTechniqueByName(LPCSTR Name)
2057 LPCSTR pFuncName = "ID3DX11Effect::GetTechniqueByName";
2058 const UINT MAX_GROUP_TECHNIQUE_SIZE = 256;
2059 char NameCopy[MAX_GROUP_TECHNIQUE_SIZE];
2063 DPF(0, "ID3DX11Effect::GetTechniqueByName: Cannot get technique interfaces by name since the effect has been Optimize()'ed");
2064 return &g_InvalidTechnique;
2069 DPF(0, "%s: Parameter Name was NULL.", pFuncName);
2070 return &g_InvalidTechnique;
2073 if( FAILED( StringCchCopyA( NameCopy, MAX_GROUP_TECHNIQUE_SIZE, Name ) ) )
2075 DPF( 0, "Group|Technique name has a length greater than %d.", MAX_GROUP_TECHNIQUE_SIZE );
2076 return &g_InvalidTechnique;
2079 char* pDelimiter = strchr( NameCopy, '|' );
2080 if( pDelimiter == NULL )
2082 if ( m_pNullGroup == NULL )
2084 DPF( 0, "The effect contains no default group." );
2085 return &g_InvalidTechnique;
2088 return m_pNullGroup->GetTechniqueByName( Name );
2091 // separate group name and technique name
2094 return GetGroupByName( NameCopy )->GetTechniqueByName( pDelimiter + 1 );
2097 ID3D11ClassLinkage * CEffect::GetClassLinkage()
2099 SAFE_ADDREF( m_pClassLinkage );
2100 return m_pClassLinkage;
2103 ID3DX11EffectGroup * CEffect::GetGroupByIndex(UINT Index)
2105 LPCSTR pFuncName = "ID3DX11Effect::GetGroupByIndex";
2107 if( Index < m_GroupCount )
2109 return (ID3DX11EffectGroup *)(m_pGroups + Index);
2111 DPF(0, "%s: Invalid group index (%d)", pFuncName, Index);
2112 return &g_InvalidGroup;
2115 ID3DX11EffectGroup * CEffect::GetGroupByName(LPCSTR Name)
2118 LPCSTR pFuncName = "ID3DX11Effect::GetGroupByName";
2122 DPF(0, "ID3DX11Effect::GetGroupByName: Cannot get group interfaces by name since the effect has been Optimize()'ed");
2123 return &g_InvalidGroup;
2126 if (NULL == Name || Name[0] == 0 )
2128 return m_pNullGroup ? (ID3DX11EffectGroup *)m_pNullGroup : &g_InvalidGroup;
2133 for (i = 0; i < m_GroupCount; ++ i)
2135 if (NULL != m_pGroups[i].pName &&
2136 strcmp(m_pGroups[i].pName, Name) == 0)
2142 if (i == m_GroupCount)
2144 DPF(0, "%s: Group [%s] not found", pFuncName, Name);
2145 return &g_InvalidGroup;
2148 return (ID3DX11EffectGroup *)(m_pGroups + i);