Jump to content
  • Advertisement
Sign in to follow this  
maogenc

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

This topic is 2591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!