123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- #include "Shader.h"
- #include "Text.h"
- #include "Datei.h"
- #include <d3d11.h>
- #include <D3Dcompiler.h>
- #include <iostream>
- using namespace Framework;
- Shader::Shader()
- {
- shader = new Text();
- shaderBuffer = 0;
- type = UNBEKANNT;
- for( int i = 0; i < 14; i++ )
- {
- constBuffers[ i ] = 0;
- buffLen[ i ] = 0;
- }
- buffAnz = 0;
- ref = 1;
- }
- Shader::~Shader()
- {
- shader->release();
- if( shaderBuffer )
- shaderBuffer->Release();
- for( int i = 0; i < 14; i++ )
- {
- if( constBuffers[ i ] )
- constBuffers[ i ]->Release();
- }
- }
- bool Shader::ladeAusDatei( const char *pfad )
- {
- Datei d;
- d.setDatei( pfad );
- __int64 gr = d.getSize();
- if( gr > 10 * 1024 )
- return 0;
- shader->fillText( ' ', (int)gr );
- if( !d.open( Datei::Style::lesen ) )
- return 0;
- d.lese( shader->getText(), (int)gr );
- d.close();
- return 1;
- }
- void Shader::setShaderCode( Text *zCode )
- {
- shader->setText( zCode->getText() );
- }
- bool Shader::compile( ID3D11Device *zD3d11Device, const char *einstiegsFunktion, const char *type )
- {
- ID3D10Blob* errorMessage = 0;
- unsigned int flag = D3D10_SHADER_ENABLE_STRICTNESS;
- #ifdef _DEBUG
- flag |= D3D10_SHADER_DEBUG;
- #endif
- if( shaderBuffer )
- shaderBuffer->Release();
- HRESULT result = D3DCompile( shader->getText(), shader->getLength(), 0, 0, 0, einstiegsFunktion, type, flag, 0, &shaderBuffer, &errorMessage );
- if( errorMessage )
- {
- char *err = (char*)errorMessage->GetBufferPointer();
- std::cout << err;
- errorMessage->Release();
- }
- return result == S_OK;
- }
- void Shader::benutzeShader( ID3D11DeviceContext *zD3d11Context )
- {}
- bool Shader::erstelleConstBuffer( ID3D11Device *zD3d11Device, int größe, int index )
- {
- if( index < 0 || index >= 14 )
- return 0;
- bool ok = 1;
- for( int i = 0; i < index; i++ )
- ok &= constBuffers[ index ] != 0;
- if( !ok )
- return 0;
- D3D11_BUFFER_DESC bufferDesc;
- bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
- bufferDesc.ByteWidth = größe;
- bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
- bufferDesc.MiscFlags = 0;
- bufferDesc.StructureByteStride = 0;
- if( constBuffers[ index ] )
- {
- constBuffers[ index ]->Release();
- constBuffers[ index ] = 0;
- buffLen[ index ] = 0;
- }
- HRESULT res = zD3d11Device->CreateBuffer( &bufferDesc, 0, &constBuffers[ index ] );
- if( res == S_OK )
- buffLen[ index ] = größe;
- for( buffAnz = 0; buffAnz < 14 && constBuffers[ buffAnz ]; buffAnz++ );
- return res == S_OK;
- }
- bool Shader::removeConstBuffer( int index )
- {
- if( index < 0 || index >= 14 )
- return 0;
- bool ok = 1;
- for( int i = 13; i > index; i-- )
- ok &= constBuffers[ i ] == 0;
- if( !ok )
- return 0;
- if( constBuffers[ index ] )
- {
- constBuffers[ index ]->Release();
- constBuffers[ index ] = 0;
- buffLen[ index ] = 0;
- }
- for( buffAnz = 0; buffAnz < 14 && constBuffers[ buffAnz ]; buffAnz++ );
- return 1;
- }
- bool Shader::füllConstBuffer( ID3D11DeviceContext *zD3d11Context, char *data, int index, int län )
- {
- if( index < 0 || index >= 14 )
- return 0;
- if( !constBuffers[ index ] )
- return 0;
- if( län < 0 )
- län = buffLen[ index ];
- D3D11_MAPPED_SUBRESOURCE mappedResource;
- HRESULT res = zD3d11Context->Map( constBuffers[ index ], 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource );
- if( res != S_OK )
- return 0;
- memcpy( mappedResource.pData, data, län );
- zD3d11Context->Unmap( constBuffers[ index ], 0 );
- return 1;
- }
- int Shader::getConstBufferLänge( int index ) const
- {
- if( index < 0 || index >= 14 )
- return 0;
- return buffLen[ index ];
- }
- ShaderType Shader::getType() const
- {
- return type;
- }
- Shader *Shader::getThis()
- {
- ref++;
- return this;
- }
- Shader *Shader::release()
- {
- ref--;
- if( !ref )
- delete this;
- return 0;
- }
- PixelShader::PixelShader()
- : Shader()
- {
- pixelShader = 0;
- }
- PixelShader::~PixelShader()
- {
- if( pixelShader )
- pixelShader->Release();
- }
- bool PixelShader::compile( ID3D11Device *zD3d11Device, const char *einstiegsFunktion, const char *version )
- {
- Text v = "ps_";
- v += version;
- if( !__super::compile( zD3d11Device, einstiegsFunktion, v ) )
- return 0;
- if( pixelShader )
- pixelShader->Release();
- pixelShader = 0;
- HRESULT result = zD3d11Device->CreatePixelShader( shaderBuffer->GetBufferPointer(), shaderBuffer->GetBufferSize(), 0, &pixelShader );
- shaderBuffer->Release();
- shaderBuffer = 0;
- return result == S_OK;
- }
- void PixelShader::benutzeShader( ID3D11DeviceContext *zD3d11Context )
- {
- if( buffAnz )
- zD3d11Context->PSSetConstantBuffers( 0, buffAnz, constBuffers );
- if( pixelShader )
- zD3d11Context->PSSetShader( pixelShader, 0, 0 );
- }
- Shader *PixelShader::release()
- {
- ref--;
- if( !ref )
- delete this;
- return 0;
- }
- VertexShader::VertexShader()
- : Shader()
- {
- vertexShader = 0;
- inputLayout = 0;
- }
- VertexShader::~VertexShader()
- {
- if( vertexShader )
- vertexShader->Release();
- if( inputLayout )
- inputLayout->Release();
- }
- bool VertexShader::compile( ID3D11Device *zD3d11Device, const char *einstiegsFunktion, const char *version )
- {
- Text v = "vs_";
- v += version;
- if( !__super::compile( zD3d11Device, einstiegsFunktion, v ) )
- return 0;
- if( vertexShader )
- vertexShader->Release();
- vertexShader = 0;
- HRESULT result = zD3d11Device->CreateVertexShader( shaderBuffer->GetBufferPointer(), shaderBuffer->GetBufferSize(), 0, &vertexShader );
- return result == S_OK;
- }
- bool VertexShader::erstelleInputLayout( ID3D11Device *zD3d11Device, D3D11_INPUT_ELEMENT_DESC *descArray, int anz )
- {
- if( !shaderBuffer )
- return 0;
- if( inputLayout )
- inputLayout->Release();
- inputLayout = 0;
- HRESULT res = zD3d11Device->CreateInputLayout( descArray, anz, shaderBuffer->GetBufferPointer(), shaderBuffer->GetBufferSize(), &inputLayout );
- if( res == S_OK )
- {
- shaderBuffer->Release();
- shaderBuffer = 0;
- }
- return res == S_OK;
- }
- void VertexShader::benutzeShader( ID3D11DeviceContext *zD3d11Context )
- {
- if( buffAnz )
- zD3d11Context->VSSetConstantBuffers( 0, buffAnz, constBuffers );
- if( inputLayout )
- zD3d11Context->IASetInputLayout( inputLayout );
- if( vertexShader )
- zD3d11Context->VSSetShader( vertexShader, 0, 0 );
- }
- Shader *VertexShader::release()
- {
- ref--;
- if( !ref )
- delete this;
- return 0;
- }
|