291 lines
6.8 KiB
C++
291 lines
6.8 KiB
C++
#include "pch.h"
|
|
#include "Shader.h"
|
|
|
|
CShader::CShader(ID3D11Device* pDevice, ID3D11DeviceContext* pContext)
|
|
: CComponent{ pDevice, pContext }
|
|
{
|
|
|
|
}
|
|
|
|
CShader::CShader(const CShader& rhs)
|
|
: CComponent(rhs)
|
|
, m_pEffect{ rhs.m_pEffect }
|
|
, m_iNumPasses{ rhs.m_iNumPasses }
|
|
, m_InputLayouts{ rhs.m_InputLayouts }
|
|
|
|
#ifdef _DEBUG
|
|
, m_strShaderPath { rhs.m_strShaderPath }
|
|
, m_pElements{ rhs.m_pElements }
|
|
, m_iNumElements{ rhs.m_iNumElements }
|
|
#endif
|
|
{
|
|
SAFE_ADDREF(m_pEffect);
|
|
|
|
for (auto& pInputLayout : m_InputLayouts)
|
|
SAFE_ADDREF(pInputLayout);
|
|
}
|
|
|
|
HRESULT CShader::Initialize_Prototype(const _tchar* pShaderFilePath, const D3D11_INPUT_ELEMENT_DESC* pElements, _uint iNumElements)
|
|
{
|
|
|
|
|
|
#ifdef _DEBUG
|
|
m_strShaderPath = pShaderFilePath;
|
|
m_pElements = pElements;
|
|
m_iNumElements = iNumElements;
|
|
#endif
|
|
|
|
|
|
if (FAILED(CreateShader(pShaderFilePath, pElements, iNumElements)))
|
|
return E_FAIL;
|
|
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CShader::Initialize(void* pArg)
|
|
{
|
|
return S_OK;
|
|
}
|
|
|
|
|
|
|
|
HRESULT CShader::Bind_RawValue(const _char* pConstantName, const void* pData, _uint iLength)
|
|
{
|
|
ID3DX11EffectVariable* pVariable = m_pEffect->GetVariableByName(pConstantName);
|
|
if (nullptr == pVariable)
|
|
return E_FAIL;
|
|
|
|
return pVariable->SetRawValue(pData, 0, iLength);
|
|
}
|
|
|
|
HRESULT CShader::Bind_Matrix(const _char* pConstantName, const _float4x4* pMatrix)
|
|
{
|
|
ID3DX11EffectVariable* pVariable = m_pEffect->GetVariableByName(pConstantName);
|
|
if (nullptr == pVariable)
|
|
return E_FAIL;
|
|
|
|
ID3DX11EffectMatrixVariable* pMatrixVariable = pVariable->AsMatrix();
|
|
if (nullptr == pMatrixVariable)
|
|
return E_FAIL;
|
|
|
|
return pMatrixVariable->SetMatrix(reinterpret_cast<const _float*>(pMatrix));
|
|
}
|
|
|
|
HRESULT CShader::Bind_Matrices(const _char* pConstantName, const _float4x4* pMatrix, _uint iNumMatrices)
|
|
{
|
|
ID3DX11EffectVariable* pVariable = m_pEffect->GetVariableByName(pConstantName);
|
|
if (nullptr == pVariable)
|
|
return E_FAIL;
|
|
|
|
ID3DX11EffectMatrixVariable* pMatrixVariable = pVariable->AsMatrix();
|
|
if (nullptr == pMatrixVariable)
|
|
return E_FAIL;
|
|
|
|
return pMatrixVariable->SetMatrixArray(reinterpret_cast<const _float*>(pMatrix), 0, iNumMatrices);
|
|
}
|
|
|
|
HRESULT CShader::Bind_SRV(const _char* pConstantName, ID3D11ShaderResourceView* pSRV)
|
|
{
|
|
ID3DX11EffectVariable* pVariable = m_pEffect->GetVariableByName(pConstantName);
|
|
if (nullptr == pVariable)
|
|
return E_FAIL;
|
|
|
|
ID3DX11EffectShaderResourceVariable* pSRVariable = pVariable->AsShaderResource();
|
|
if (nullptr == pSRVariable)
|
|
return E_FAIL;
|
|
|
|
return pSRVariable->SetResource(pSRV);
|
|
}
|
|
|
|
HRESULT CShader::Bind_SRVs(const _char* pConstantName, ID3D11ShaderResourceView** ppSRV, _uint iNumSRVs)
|
|
{
|
|
ID3DX11EffectVariable* pVariable = m_pEffect->GetVariableByName(pConstantName);
|
|
if (nullptr == pVariable)
|
|
return E_FAIL;
|
|
|
|
ID3DX11EffectShaderResourceVariable* pSRVariable = pVariable->AsShaderResource();
|
|
if (nullptr == pSRVariable)
|
|
return E_FAIL;
|
|
|
|
return pSRVariable->SetResourceArray(ppSRV, 0, iNumSRVs);
|
|
}
|
|
|
|
HRESULT CShader::Bind_IntArray(const _char* pConstantName, const int* pData, _uint elementCount)
|
|
{
|
|
if (m_pEffect == nullptr || pConstantName == nullptr || pData == nullptr || elementCount == 0) {
|
|
return E_FAIL;
|
|
}
|
|
|
|
ID3DX11EffectScalarVariable* effectScalarVariable =
|
|
m_pEffect->GetVariableByName(pConstantName)->AsScalar();
|
|
|
|
if (effectScalarVariable == nullptr || effectScalarVariable->IsValid() == false) {
|
|
return E_FAIL;
|
|
}
|
|
|
|
return effectScalarVariable->SetIntArray(pData, 0, elementCount);
|
|
}
|
|
|
|
|
|
HRESULT CShader::CreateShader(const _tchar* pShaderFilePath, const D3D11_INPUT_ELEMENT_DESC* pElements, _uint iNumElements)
|
|
{
|
|
|
|
_uint iHlslFlag = {};
|
|
|
|
#ifdef _DEBUG
|
|
iHlslFlag = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
|
|
#else
|
|
iHlslFlag = D3DCOMPILE_OPTIMIZATION_LEVEL1;
|
|
#endif
|
|
|
|
ID3DBlob* pBlob;
|
|
|
|
if (FAILED(D3DX11CompileEffectFromFile(pShaderFilePath, nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, iHlslFlag, 0, m_pDevice, &m_pEffect, &pBlob))) {
|
|
if (pBlob) {
|
|
OutputDebugStringA((char*)pBlob->GetBufferPointer());
|
|
pBlob->Release();
|
|
}
|
|
return E_FAIL;
|
|
}
|
|
|
|
ID3DX11EffectTechnique* pTechnique = m_pEffect->GetTechniqueByIndex(0);
|
|
if (nullptr == pTechnique)
|
|
return E_FAIL;
|
|
|
|
D3DX11_TECHNIQUE_DESC TechniqueDesc{};
|
|
|
|
if (FAILED(pTechnique->GetDesc(&TechniqueDesc)))
|
|
return E_FAIL;
|
|
|
|
m_iNumPasses = TechniqueDesc.Passes;
|
|
|
|
for (_uint i = 0; i < m_iNumPasses; i++)
|
|
{
|
|
ID3D11InputLayout* pInputLayout = { nullptr };
|
|
|
|
ID3DX11EffectPass* pPass = pTechnique->GetPassByIndex(i);
|
|
if (nullptr == pPass)
|
|
return E_FAIL;
|
|
|
|
D3DX11_PASS_DESC PassDesc{};
|
|
if (FAILED(pPass->GetDesc(&PassDesc)))
|
|
return E_FAIL;
|
|
|
|
if (FAILED(m_pDevice->CreateInputLayout(
|
|
pElements,
|
|
iNumElements,
|
|
PassDesc.pIAInputSignature/*쉐이더에서 받아줄수 있는 정점의 정보*/,
|
|
PassDesc.IAInputSignatureSize/*쉐이더에서 받아줄수 있는 정점의 멤버변수갯수*/,
|
|
|
|
&pInputLayout)))
|
|
return E_FAIL;
|
|
|
|
m_InputLayouts.push_back(pInputLayout);
|
|
}
|
|
return S_OK;
|
|
}
|
|
|
|
#ifdef _DEBUG
|
|
|
|
HRESULT CShader::Shader_Refresh()
|
|
{
|
|
// 내 인풋 레이아웃 이펙트 소멸시키고
|
|
|
|
Safe_Release(m_pEffect);
|
|
|
|
for (auto& pInputLayout : m_InputLayouts)
|
|
Safe_Release(pInputLayout);
|
|
|
|
m_InputLayouts.clear();
|
|
|
|
//그 경로로 재 생성한다.
|
|
|
|
if (FAILED(CreateShader(m_strShaderPath.c_str(), m_pElements, m_iNumElements)))
|
|
{
|
|
MessageBox(NULL, L"쉐이더 변경 실패", L"System Message", MB_OK);
|
|
return E_FAIL;
|
|
}
|
|
|
|
|
|
MessageBox(NULL, L"쉐이더 변경 성공", L"System Message", MB_OK);
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
#endif //
|
|
|
|
|
|
HRESULT CShader::Begin(_uint iPassIndex)
|
|
{
|
|
if (iPassIndex >= m_iNumPasses)
|
|
return E_FAIL;
|
|
|
|
m_pContext->IASetInputLayout(m_InputLayouts[iPassIndex]);
|
|
|
|
/* Apply를 반드시 호출해야만 쉐이더로 그려진다. */
|
|
/* Apply이전에 쉐이더에 전달할 모든 데이터들을 다 던져놓아야한다. */
|
|
|
|
m_pEffect->GetTechniqueByIndex(0)->GetPassByIndex(iPassIndex)->Apply(0, m_pContext);
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
CShader* CShader::Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, const _tchar* pShaderFilePath, const D3D11_INPUT_ELEMENT_DESC* pElements, _uint iNumElements)
|
|
{
|
|
CShader* pInstance = new CShader(pDevice, pContext);
|
|
|
|
if (FAILED(pInstance->Initialize_Prototype(pShaderFilePath, pElements, iNumElements)))
|
|
{
|
|
MSG_BOX("Failed to Created : CShader");
|
|
SAFE_RELEASE(pInstance);
|
|
}
|
|
|
|
return pInstance;
|
|
}
|
|
|
|
CComponent* CShader::Clone(void* pArg, class CGameObject* pOwner)
|
|
{
|
|
CShader* pInstance = new CShader(*this);
|
|
pInstance->m_pOwner = pOwner;
|
|
if (FAILED(pInstance->Initialize(pArg)))
|
|
{
|
|
MSG_BOX("Failed to Cloned : CShader");
|
|
SAFE_RELEASE(pInstance);
|
|
}
|
|
|
|
return pInstance;
|
|
}
|
|
|
|
void CShader::Free()
|
|
{
|
|
__super::Free();
|
|
|
|
SAFE_RELEASE(m_pEffect);
|
|
|
|
for (auto& pInputLayout : m_InputLayouts)
|
|
SAFE_RELEASE(pInputLayout);
|
|
|
|
m_InputLayouts.clear();
|
|
}
|
|
#ifdef _DEBUG
|
|
|
|
void CShader::Describe_Entity()
|
|
{
|
|
|
|
if(GUI::TreeNode("SHADER"))
|
|
{
|
|
if (GUI::Button("Shader Refresh"))
|
|
{
|
|
|
|
Shader_Refresh();
|
|
|
|
}
|
|
|
|
GUI::TreePop();
|
|
}
|
|
|
|
}
|
|
|
|
#endif // _DEBUG
|