What are the pros&cons of using HLSL techniques vs native C++ API calls?

Started by
3 comments, last by 360GAMZ 12 years, 5 months ago
I'm in the middle of writing a toy rendering engine & have been using native C++ DirectX API calls to handle scene rendering.
For example, I am currently using something that looks like this to manage my shaders:

[source lang="cpp"]
//scene initialization

hr = CompileShaderFromFile(L"ShadowMapping.hlsl", shaderList.name, shaderList.model, &pShaderBlob);
pShader = CreateShader(pD3DDevice, shaderList.model[0], pShaderBlob);
shaderList.pShader = pShader;
shaderList.pShaderBlob = pShaderBlob;


//scene rendering -- eventually this will be dynamic

pImmediateContext->VSSetShader( (ID3D11VertexShader *)shaderList[1].pShader, NULL, 0 );
pImmediateContext->VSSetConstantBuffers( 0, 1, &pCBNeverChanges );
pImmediateContext->VSSetConstantBuffers( 1, 1, &pCBChangeOnResize );
pImmediateContext->VSSetConstantBuffers( 2, 1, &pCBChangesEveryFrame );

pImmediateContext->PSSetShader( (ID3D11PixelShader *)shaderList[8].pShader, NULL, 0 );
pImmediateContext->PSSetConstantBuffers( 0, 1, &pCBNeverChanges );
pImmediateContext->PSSetConstantBuffers( 2, 1, &pCBChangesEveryFrame );
pImmediateContext->PSSetShaderResources( 0, 1, &pTextureSRV );
pImmediateContext->PSSetShaderResources( 1, 1, &pShadowMapSRView);
pImmediateContext->PSSetSamplers( 0, 1, &pSamplerLinear );
pImmediateContext->PSSetSamplers( 1, 1, &pCmpSampler );

pImmediateContext->GSSetShader( NULL, NULL, 0 );
[/source]


I've also seen people use HLSL techniques like this:
[source lang='cpp']
//HLSL code
technique10 RenderPassStandard
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PS_STANDARD() ) );
SetRasterizerState( backFaceCulling );
}
}

//C++ application code
pRenderTechnique = pEffect->GetTechniqueByName("RenderPassStandard");
pRenderTechnique->GetPassByIndex(0)->Apply( 0 );
[/source]


The obvious difference is that using HLSL techniques is less complicated. What are other considerations in using one over the other?
Advertisement
To be clear, those techniques aren't really HLSL. They're effect code, that has to be used in conjunction with the Effects framework.

Anyway the major advantage is that you can keep a lot of state and constant data easily organized in one text file, which makes it simpler to author materials. So for instance if you wanted some transparent material you could author the shader to output alpha, and put a blend state in the same file that enables alpha blending. The major downside is that you have no control over how any of that stuff happens, so you can't apply custom optimizations to suit your engine/game or add new features.
The states like, rasterizer state, blendstate, depthstate, Texture Sampler States, etc can be set inside of HLSL files, however, they can also be set through c++ code. The advantage of doing this in c++ is that you can keep track of which state is currently in effect and organize your drawing so that the smallest amount of state changes occur.

c++ basically gives you the ability to manage better, where HLSL typically means it is hard-coded. Try to code as much as possible in c++ to give you the most flexibility.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
Watch out your fx code don't set states that you my need later
I'm using both methods. I use the Effects framework only to obtain information like which shaders to use and to read annotations. But for rendering, I don't use the Effects framework at all, and instead manually set the shaders, resources, constant buffers, and other state info directly to the D3D API.

This topic is closed to new replies.

Advertisement