# DX11 No blending, something missing?

## Recommended Posts

Hi, new here. I have been dealing with the Frank Luna's programming book codes for a while, and I am having problems with the blending part. My code has been changed to not use the Effect11 framework. Instead I have been using pre-compiled PS/VS shaders (or compiled shaders during built time) and managing resources/shaders using a solution he posted in his blog due Win8+ no longer supporting the D3DX11 library, I am also using DirecXTK for texture loading. Those are the changes made vs the original code.

I think I have everything set up but it doesn't seem to work, no blending. I omitted all the irrelevant code

RenderStates.cpp (RenderStates is a static class)


ComPtr<ID3D11BlendState> RenderStates::transBlendSt = 0;
ComPtr<ID3D11BlendState> RenderStates::normalBlendSt = 0;

void RenderStates::InitAll(ID3D11Device* dev)
{
D3D11_BLEND_DESC desc1 = {0};
desc1.AlphaToCoverageEnable = false;
desc1.IndependentBlendEnable = false;
desc1.RenderTarget[0].BlendEnable = true;
desc1.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc1.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc1.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
desc1.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;

//Transparent Blend State
HR( dev->CreateBlendState(&desc1, &transBlendSt) );

D3D11_BLEND_DESC desc2 = {0};
desc2.AlphaToCoverageEnable = true;
desc2.IndependentBlendEnable = false;
desc2.RenderTarget[0].BlendEnable = false;

//Normal Blend State
HR( dev->CreateBlendState(&desc2, &normalBlendSt) );

} 

Main.cpp

bool InitD3D::Init()
{
RenderStates::InitAll(md3dDevice);
}

{
HRESULT h = CoInitialize(NULL);
h = DirectX::CreateDDSTextureFromFile(md3dDevice, L"Directory.../Textures/grass.dds", nullptr, &mHillTex);
if(!SUCCEEDED(h))
{
return;
}

h = DirectX::CreateDDSTextureFromFile(md3dDevice, L"Directory.../Textures/water1.dds", nullptr, &mWavesTex);
if(!SUCCEEDED(h))
{
return;
}
}

void InitD3D::DrawScene()
{

md3dImmediateContext->OMSetBlendState(RenderStates::normalBlendSt.Get(), 0, 0xffffffff);

/* Other resources for the Hill set here */

/* Draw Hills here */

float blendF[] = {0.0f, 0.0f, 0.0f, 0.0f};
md3dImmediateContext->OMSetBlendState(RenderStates::transBlendSt.Get(), blendF, 0xffffffff);

/* Other resources for the Waves set here */

/* Draw Waves here */

}
My results

Can anyone can help to figure out what I might be missing here? if you need any other part of the code or anything else let me know. Thank you.

##### Share on other sites

Sorry to ask the obvious, but what does the alpha channel of your wave texture look like? What does the pixel shader used for the waves output in the alpha channel?

##### Share on other sites

Sorry to ask the obvious, but what does the alpha channel of your wave texture look like? What does the pixel shader used for the waves output in the alpha channel?

Interesting questions to me, might reveal what I am missing.

1. The texture used for the wave is a .dds file which is provided by the author of the book, I haven't modified it or anything. I am not sure whether if has alpha channel or not. Anyway I could find that out?

2. This is what my pixel shader looks like, no operations using alpha channel other that using the alpha component of the texture and the diffuse material.

float4 main(VertexOut vin) : SV_TARGET
{
vin.NormW = normalize(vin.NormW);
float3 toEye = normalize(gEyePos - vin.PosW);

float4 ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 specular = float4(0.0f, 0.0f, 0.0f, 0.0f);

float4 A, D, S;
[unroll]
for(int i = 0; i < 3; i++)
{
ComputeDirectionalLight(gMaterial, dirLight[i], vin.NormW, toEye, A, D, S);
ambient += A;
diffuse += D;
specular += S;
}

float4 TexColor = diffuseMap.Sample(anisotropic, vin.TexC0); //Sample from the texture

float4 LitColor = TexColor * (ambient + diffuse) + specular;

LitColor.a = gMaterial.Diffuse.a * TexColor.a;

return LitColor;
}


##### Share on other sites

The texture used for the wave is a .dds file which is provided by the author of the book, I haven't modified it or anything. I am not sure whether if has alpha channel or not. Anyway I could find that out?

Use a tool that can view DDS textures, like DDSView: https://github.com/Microsoft/DirectXTex

Your pixel shader looks like it supports an alpha channel in the Diffuse color property of your material. What are you passing for that value?

##### Share on other sites

You can use visual studio to check DDS textures too. https://msdn.microsoft.com/en-us/library/hh873119.aspx

##### Share on other sites

The texture used for the wave is a .dds file which is provided by the author of the book, I haven't modified it or anything. I am not sure whether if has alpha channel or not. Anyway I could find that out?

Use a tool that can view DDS textures, like DDSView: https://github.com/Microsoft/DirectXTex

Your pixel shader looks like it supports an alpha channel in the Diffuse color property of your material. What are you passing for that value?

Thanks both of you, the alpha channel is a completely blank image (white).

	//Land material properties
mLandMaterial.Diffuse  = XMFLOAT4(0.48f, 0.77f, 0.46f, 1.0f);

//Wave material properties
mWaveMaterial.Diffuse  = XMFLOAT4(0.137f, 0.42f, 0.556f, 1.0f); 

And just 1 is what get passed to it, I have tried omitting that operation as well on the PS and still no difference. Its really strange.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627726
• Total Posts
2978813
• ### Similar Content

• By Baemz
Hello,
I've been working on some culling-techniques for a project. We've built our own engine so pretty much everything is built from scratch. I've set up a frustum with the following code, assuming that the FOV is 90 degrees.
float angle = CU::ToRadians(45.f); Plane<float> nearPlane(Vector3<float>(0, 0, aNear), Vector3<float>(0, 0, -1)); Plane<float> farPlane(Vector3<float>(0, 0, aFar), Vector3<float>(0, 0, 1)); Plane<float> right(Vector3<float>(0, 0, 0), Vector3<float>(angle, 0, -angle)); Plane<float> left(Vector3<float>(0, 0, 0), Vector3<float>(-angle, 0, -angle)); Plane<float> up(Vector3<float>(0, 0, 0), Vector3<float>(0, angle, -angle)); Plane<float> down(Vector3<float>(0, 0, 0), Vector3<float>(0, -angle, -angle)); myVolume.AddPlane(nearPlane); myVolume.AddPlane(farPlane); myVolume.AddPlane(right); myVolume.AddPlane(left); myVolume.AddPlane(up); myVolume.AddPlane(down); When checking the intersections I am using a BoundingSphere of my models, which is calculated by taking the average position of all vertices and then choosing the furthest distance to a vertex for radius. The actual intersection test looks like this, where the "myFrustum90" is the actual frustum described above.
The orientationInverse is the viewMatrix in this case.
bool CFrustum::Intersects(const SFrustumCollider& aCollider) { CU::Vector4<float> position = CU::Vector4<float>(aCollider.myCenter.x, aCollider.myCenter.y, aCollider.myCenter.z, 1.f) * myOrientationInverse; return myFrustum90.Inside({ position.x, position.y, position.z }, aCollider.myRadius); } The Inside() function looks like this.
template <typename T> bool PlaneVolume<T>::Inside(Vector3<T> aPosition, T aRadius) const { for (unsigned short i = 0; i < myPlaneList.size(); ++i) { if (myPlaneList[i].ClassifySpherePlane(aPosition, aRadius) > 0) { return false; } } return true; } And this is the ClassifySpherePlane() function. (The plane is defined as a Vector4 called myABCD, where ABC is the normal)
template <typename T> inline int Plane<T>::ClassifySpherePlane(Vector3<T> aSpherePosition, float aSphereRadius) const { float distance = (aSpherePosition.Dot(myNormal)) - myABCD.w; // completely on the front side if (distance >= aSphereRadius) { return 1; } // completely on the backside (aka "inside") if (distance <= -aSphereRadius) { return -1; } //sphere intersects the plane return 0; }
Please bare in mind that this code is not optimized nor well-written by any means. I am just looking to get it working.
The result of this culling is that the models seem to be culled a bit "too early", so that the culling is visible and the models pops away.
How do I get the culling to work properly?
I have tried different techniques but haven't gotten any of them to work.
If you need more code or explanations feel free to ask for it.

Thanks.

• hi,
i have read very much about the binding of a constantbuffer to a shader but something is still unclear to me.
e.g. when performing :   vertexshader.setConstantbuffer ( buffer,  slot )
is the buffer bound
or
b. to the VertexShader that is currently set as the active VertexShader
Is it possible to bind a constantBuffer to a VertexShader e.g. VS_A and keep this binding even after the active VertexShader has changed ?
I mean i want to bind constantbuffer_A  to VS_A, an Constantbuffer_B to VS_B  and  only use updateSubresource without using setConstantBuffer command every time.

Look at this example:
perform drawcall       ( buffer_A is used )

perform drawcall   ( buffer_B is used )
perform drawcall   (now which buffer is used ??? )

I ask this question because i have made a custom render engine an want to optimize to
the minimum  updateSubresource, and setConstantbuffer  calls

• I got a quick question about buffers when it comes to DirectX 11. If I bind a buffer using a command like:
IASetVertexBuffers IASetIndexBuffer VSSetConstantBuffers PSSetConstantBuffers  and then later on I update that bound buffer's data using commands like Map/Unmap or any of the other update commands.
Do I need to rebind the buffer again in order for my update to take effect? If I dont rebind is that really bad as in I get a performance hit? My thought process behind this is that if the buffer is already bound why do I need to rebind it? I'm using that same buffer it is just different data

• I am really stuck with something that should be very simple in DirectX 11.
1. I can draw lines using a PC (position, colored) vertices and a simple shader just fine.
2. I can draw 3D triangles using PCN (position, colored, normal) vertices just fine (even transparency and SpecularBlinnPhong shaders).

However, if I'm using my 3D shader, and I want to draw my PC lines in the same scene how can I do that?

If I change my lines to PCN and pass them to the 3D shader with my triangles, then the lighting screws them all up.  I only want the lighting for the 3D triangles, but no SpecularBlinnPhong/Lighting for the lines (just PC).
I am sure this is because if I change the lines to PNC there is not really a correct "normal" for the lines.
I assume I somehow need to draw the 3D triangles using one shader, and then "switch" to another shader and draw the lines?  But I have no clue how to use two different shaders in the same scene.  And then are the lines just drawn on top of the triangles, or vice versa (maybe draw order dependent)?
I must be missing something really basic, so if anyone can just point me in the right direction (or link to an example showing the implementation of multiple shaders) that would be REALLY appreciated.

I'm also more than happy to post my simple test code if that helps as well!

• By Reitano
Hi,
I am writing a linear allocator of per-frame constants using the DirectX 11.1 API. My plan is to replace the traditional constant allocation strategy, where most of the work is done by the driver behind my back, with a manual one inspired by the DirectX 12 and Vulkan APIs.
In brief, the allocator maintains a list of 64K pages, each page owns a constant buffer managed as a ring buffer. Each page has a history of the N previous frames. At the beginning of a new frame, the allocator retires the frames that have been processed by the GPU and frees up the corresponding space in each page. I use DirectX 11 queries for detecting when a frame is complete and the ID3D11DeviceContext1::VS/PSSetConstantBuffers1 methods for binding constant buffers with an offset.
The new allocator appears to be working but I am not 100% confident it is actually correct. In particular:
1) it relies on queries which I am not too familiar with. Are they 100% reliable ?
2) it maps/unmaps the constant buffer of each page at the beginning of a new frame and then writes the mapped memory as the frame is built. In pseudo code:
BeginFrame:
page.data = device.Map(page.buffer)
device.Unmap(page.buffer)
RenderFrame
Alloc(size, initData)
...
memcpy(page.data + page.start, initData, size)
Alloc(size, initData)
...
memcpy(page.data + page.start, initData, size)
(Note: calling Unmap at the end of a frame prevents binding the mapped constant buffers and triggers an error in the debug layer)
Is this valid ?
3) I don't fully understand how many frames I should keep in the history. My intuition says it should be equal to the maximum latency reported by IDXGIDevice1::GetMaximumFrameLatency, which is 3 on my machine. But, this value works fine in an unit test while on a more complex demo I need to manually set it to 5, otherwise the allocator starts overwriting previous frames that have not completed yet. Shouldn't the swap chain Present method block the CPU in this case ?
4) Should I expect this approach to be more efficient than the one managed by the driver ? I don't have meaningful profile data yet.
Is anybody familiar with the approach described above and can answer my questions and discuss the pros and cons of this technique based on his experience ?
For reference, I've uploaded the (WIP) allocator code at https://paste.ofcode.org/Bq98ujP6zaAuKyjv4X7HSv.  Feel free to adapt it in your engine and please let me know if you spot any mistakes
Thanks
Stefano Lanza

• 10
• 9
• 21
• 14
• 12