Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ShaderClass.h"
- ShaderClass::ShaderClass() {
- m_vertexShader = nullptr;
- m_pixelShader = nullptr;
- m_inputLayout = nullptr;
- m_matrixBuffer = nullptr;
- m_samplerState = nullptr;
- }
- ShaderClass::~ShaderClass() {
- }
- bool ShaderClass::Initialize(ID3D11Device* device, string vertexShader, string pixelShader) {
- bool result;
- result = InitializeShader(device, vertexShader.c_str(), pixelShader.c_str());
- if (!result) {
- cout << "[ShaderClass]\tError: failed InitializeShader()." << endl;
- return false;
- }
- return true;
- }
- void ShaderClass::Shutdown() {
- ShutdownShader();
- }
- bool ShaderClass::Render(ID3D11DeviceContext* deviceContext, TexturesTable* texturesTable, size_t vertexCount, D3DXMATRIX& world, D3DXMATRIX& view, D3DXMATRIX& proj, Material* material) {
- bool result;
- result = SetShaderParameters(deviceContext, world, view, proj, material);
- if (!result) {
- cout << "[ShaderClass]\tError: failed SetShaderParameters()" << endl;
- return false;
- }
- if (!material->map_Ka.empty()) {
- ID3D11ShaderResourceView* resourceView = texturesTable->GetTextureByName(material->map_Ka)->GetTexture();
- if (!resourceView) {
- cout << "[ShaderClass]\tError: failed GetTextureByName() for map_Ka" << material->map_Ka << endl;
- return false;
- }
- deviceContext->PSSetShaderResources(0, 1, &resourceView);
- }
- if (!material->map_Kd.empty()) {
- ID3D11ShaderResourceView* resourceView = texturesTable->GetTextureByName(material->map_Kd)->GetTexture();
- if (!resourceView) {
- cout << "[ShaderClass]\tError: failed GetTextureByName() for map_Kd" << material->map_Kd << endl;
- return false;
- }
- deviceContext->PSSetShaderResources(1, 1, &resourceView);
- }
- if (!material->map_Ks.empty()) {
- ID3D11ShaderResourceView* resourceView = texturesTable->GetTextureByName(material->map_Ks)->GetTexture();
- if (!resourceView) {
- cout << "[ShaderClass]\tError: failed GetTextureByName() for map_Ks" << material->map_Ks << endl;
- return false;
- }
- deviceContext->PSSetShaderResources(2, 1, &resourceView);
- }
- if (!material->map_Ns.empty()) {
- ID3D11ShaderResourceView* resourceView = texturesTable->GetTextureByName(material->map_Ns)->GetTexture();
- if (!resourceView) {
- cout << "[ShaderClass]\tError: failed GetTextureByName() for map_Ns" << material->map_Ns << endl;
- return false;
- }
- deviceContext->PSSetShaderResources(3, 1, &resourceView);
- }
- if (!material->map_d.empty()) {
- ID3D11ShaderResourceView* resourceView = texturesTable->GetTextureByName(material->map_d)->GetTexture();
- if (!resourceView) {
- cout << "[ShaderClass]\tError: failed GetTextureByName() for map_d" << material->map_d << endl;
- return false;
- }
- deviceContext->PSSetShaderResources(4, 1, &resourceView);
- }
- if (!material->map_bump.empty()) {
- ID3D11ShaderResourceView* resourceView = texturesTable->GetTextureByName(material->map_bump)->GetTexture();
- if (!resourceView) {
- cout << "[ShaderClass]\tError: failed GetTextureByName() for map_bump" << material->map_bump << endl;
- return false;
- }
- deviceContext->PSSetShaderResources(5, 1, &resourceView);
- }
- RenderShader(deviceContext, vertexCount);
- return true;
- }
- bool ShaderClass::InitializeShader(ID3D11Device* device, string vsFileName, string psFileName) {
- HRESULT result;
- ID3D10Blob* errorMessage;
- ID3D10Blob* vertexShaderBuffer;
- ID3D10Blob* pixelShaderBuffer;
- D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
- unsigned int numElements;
- D3D11_BUFFER_DESC matrixBufferDesc;
- D3D11_BUFFER_DESC shaderMaterialDesc;
- D3D11_SAMPLER_DESC samplerDesc;
- errorMessage = nullptr;
- vertexShaderBuffer = nullptr;
- pixelShaderBuffer = nullptr;
- result = D3DX11CompileFromFile(vsFileName.c_str(), NULL, NULL, "vsEntryPoint", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &vertexShaderBuffer, &errorMessage, &result);
- if (FAILED(result)) {
- if (errorMessage) {
- OutputShaderErrorMessage(errorMessage, vsFileName);
- }
- else {
- cout << "[ShaderClass]\tError: missing shader file " << vsFileName.c_str() << endl;
- return false;
- }
- return false;
- }
- result = D3DX11CompileFromFile(psFileName.c_str(), NULL, NULL, "psEntryPoint", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &pixelShaderBuffer, &errorMessage, &result);
- if (FAILED(result)) {
- if (errorMessage) {
- OutputShaderErrorMessage(errorMessage, psFileName);
- }
- else {
- cout << "[ShaderClass]\tError: missing shader file " << psFileName.c_str() << endl;
- return false;
- }
- return false;
- }
- result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
- if (FAILED(result)) {
- cout << "[ShaderClass]\tError: failed CreateVertexShader()" << endl;
- return false;
- }
- result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
- if (FAILED(result)) {
- cout << "[ShaderClass]\tError: failed CreatePixelShader()" << endl;
- return false;
- }
- polygonLayout[0].SemanticName = "POSITION";
- polygonLayout[0].SemanticIndex = 0;
- polygonLayout[0].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
- polygonLayout[0].InputSlot = 0;
- polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
- polygonLayout[0].InstanceDataStepRate = 0;
- polygonLayout[0].AlignedByteOffset = 0;
- polygonLayout[1].SemanticName = "NORMAL";
- polygonLayout[1].SemanticIndex = 0;
- polygonLayout[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
- polygonLayout[1].InputSlot = 0;
- polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
- polygonLayout[1].InstanceDataStepRate = 0;
- polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
- polygonLayout[2].SemanticName = "TEXCOORD";
- polygonLayout[2].SemanticIndex = 0;
- polygonLayout[2].Format = DXGI_FORMAT_R32G32_FLOAT;
- polygonLayout[2].InputSlot = 0;
- polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
- polygonLayout[2].InstanceDataStepRate = 0;
- polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
- numElements = 3;
- result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_inputLayout);
- if (FAILED(result)) {
- cout << "[ShaderClass]\tError: failed CreateInputLayout()." << endl;
- return false;
- }
- vertexShaderBuffer->Release();
- vertexShaderBuffer = nullptr;
- pixelShaderBuffer->Release();
- pixelShaderBuffer = nullptr;
- matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
- matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
- matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- matrixBufferDesc.MiscFlags = 0;
- matrixBufferDesc.StructureByteStride = 0;
- matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
- result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer);
- if (FAILED(result)) {
- cout << "[ShaderClass]\tError: failed to create constant buffer (m_matrixBuffer)." << endl;
- return false;
- }
- shaderMaterialDesc.Usage = D3D11_USAGE_DYNAMIC;
- shaderMaterialDesc.ByteWidth = sizeof(cbShaderMaterial);
- shaderMaterialDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
- shaderMaterialDesc.MiscFlags = 0;
- shaderMaterialDesc.StructureByteStride = 0;
- shaderMaterialDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- result = device->CreateBuffer(&shaderMaterialDesc, NULL, &m_materialBuffer);
- if (FAILED(result)) {
- cout << "[ShaderClass]\tError: failed to create constant buffer (m_materialBuffer)." << endl;
- return false;
- }
- samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
- samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.MipLODBias = 0.0f;
- samplerDesc.MaxAnisotropy = 1;
- samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
- samplerDesc.MinLOD = 0;
- samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
- samplerDesc.BorderColor[0] = 0;
- samplerDesc.BorderColor[1] = 0;
- samplerDesc.BorderColor[2] = 0;
- samplerDesc.BorderColor[3] = 0;
- result = device->CreateSamplerState(&samplerDesc, &m_samplerState);
- if (FAILED(result)) {
- cout << "[ShaderClass]\tError: failed CreateSamplerState()." << endl;
- return false;
- }
- return true;
- }
- bool ShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, D3DXMATRIX& world, D3DXMATRIX& view, D3DXMATRIX& proj, Material* material) {
- HRESULT result;
- D3D11_MAPPED_SUBRESOURCE mappedResource;
- MatrixBufferType* dataPtr;
- cbShaderMaterial* materialDataPtr;
- // set matrix constant buffer
- D3DXMatrixTranspose(&world, &world);
- D3DXMatrixTranspose(&view, &view);
- D3DXMatrixTranspose(&proj, &proj);
- result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
- if (FAILED(result)) {
- cout << "[ShaderClass]\tError: failed Map() for m_matrixBuffer." << endl;
- return false;
- }
- dataPtr = (MatrixBufferType*)mappedResource.pData;
- dataPtr->world = world;
- dataPtr->view = view;
- dataPtr->projection = proj;
- deviceContext->Unmap(m_matrixBuffer, 0);
- deviceContext->VSSetConstantBuffers(0, 1, &m_matrixBuffer);
- // set material constant buffer
- result = deviceContext->Map(m_materialBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
- if (FAILED(result)) {
- cout << "[ShaderClass]\tError: failed Map() for m_materialBuffer." << endl;
- return false;
- }
- materialDataPtr = (cbShaderMaterial*)mappedResource.pData;
- materialDataPtr->Ka = material->Ka;
- materialDataPtr->Kd = material->Kd;
- materialDataPtr->Ke = material->Ke;
- materialDataPtr->Ks = material->Ks;
- materialDataPtr->Ns = material->Ns;
- materialDataPtr->Tr = material->Tr;
- materialDataPtr->d = material->d;
- materialDataPtr->bias = 0.0f;
- deviceContext->Unmap(m_materialBuffer, 0);
- deviceContext->PSSetConstantBuffers(0, 1, &m_materialBuffer);
- deviceContext->PSSetSamplers(0, 1, &m_samplerState);
- return true;
- }
- void ShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, size_t vertexCount) {
- deviceContext->IASetInputLayout(m_inputLayout);
- deviceContext->VSSetShader(m_vertexShader, NULL, 0);
- deviceContext->PSSetShader(m_pixelShader, NULL, 0);
- deviceContext->Draw((UINT)vertexCount, 0);
- }
- void ShaderClass::ShutdownShader() {
- if (m_matrixBuffer) {
- m_matrixBuffer->Release();
- m_matrixBuffer = nullptr;
- }
- if (m_inputLayout) {
- m_inputLayout->Release();
- m_inputLayout = nullptr;
- }
- if (m_pixelShader) {
- m_pixelShader->Release();
- m_pixelShader = nullptr;
- }
- if (m_vertexShader) {
- m_vertexShader->Release();
- m_vertexShader = nullptr;
- }
- if (m_samplerState) {
- m_samplerState->Release();
- m_samplerState = nullptr;
- }
- }
- void ShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, string shaderFileName) {
- char* compileErrors;
- size_t bufferSize;
- ofstream fout;
- compileErrors = (char*)errorMessage->GetBufferPointer();
- bufferSize = errorMessage->GetBufferSize();
- fout.open("shader-error.txt");
- for (int i = 0; i < (int)bufferSize; i++) {
- fout << compileErrors[i];
- }
- fout.close();
- errorMessage->Release();
- errorMessage = nullptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement