# DX11 [DirectX11] Instancing

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

## Recommended Posts

I've managed to get basic instancing working - what I mean is that I can easily add instances of a given model and they all render correctly. For now I can only change the positions, but the buffers already include data for rotations and scales. Problem starts, when I set the instance buffer as a dynamic one, allow CPU to write there and try to change the data (position of the model) each frame.

 Texture2D color_map : register( t0 ); SamplerState sample_type : register( s0 ); cbuffer world_view_proj : register( b0 ) { matrix world; matrix view; matrix projection; }; struct Vertex_Input_Type { float4 position : POSITION; float2 tex : TEXCOORD0; float3 normal : NORMAL; float3 tangent : TANGENT; float3 binormal : BINORMAL; float3 instance_pos : TEXCOORD1; float3 instance_rot : TEXCOORD2; float3 instance_scale : TEXCOORD3; }; struct Pixel_Input_Type { float4 position : SV_POSITION; float2 tex : TEXCOORD0; }; Pixel_Input_Type VS( Vertex_Input_Type input ) { Pixel_Input_Type output; input.position.w = 1.0f; input.position.x += input.instance_pos.x; input.position.y += input.instance_pos.y; input.position.z += input.instance_pos.z; output.position = mul( input.position, world ); output.position = mul( output.position, view ); output.position = mul( output.position, projection ); output.tex = input.tex; return output; } float4 PS( Pixel_Input_Type input ) : SV_TARGET { return color_map.Sample( sample_type, input.tex ); } technique11 Render { pass P0 { SetVertexShader( CompileShader( vs_4_0, VS() ) ); SetGeometryShader( 0 ); SetPixelShader( CompileShader( ps_4_0, PS() ) ); } } 

Creation of the layout:
 D3D11_INPUT_ELEMENT_DESC polygon_layout[8]; polygon_layout[0].SemanticName = "POSITION"; polygon_layout[0].SemanticIndex = 0; polygon_layout[0].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; polygon_layout[0].InputSlot = 0; polygon_layout[0].AlignedByteOffset = 0; polygon_layout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygon_layout[0].InstanceDataStepRate = 0; polygon_layout[1].SemanticName = "TEXCOORD"; polygon_layout[1].SemanticIndex = 0; polygon_layout[1].Format = DXGI_FORMAT_R32G32_FLOAT; polygon_layout[1].InputSlot = 0; polygon_layout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygon_layout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygon_layout[1].InstanceDataStepRate = 0; polygon_layout[2].SemanticName = "NORMAL"; polygon_layout[2].SemanticIndex = 0; polygon_layout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygon_layout[2].InputSlot = 0; polygon_layout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygon_layout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygon_layout[2].InstanceDataStepRate = 0; polygon_layout[3].SemanticName = "TANGENT"; polygon_layout[3].SemanticIndex = 0; polygon_layout[3].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygon_layout[3].InputSlot = 0; polygon_layout[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygon_layout[3].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygon_layout[3].InstanceDataStepRate = 0; polygon_layout[4].SemanticName = "BINORMAL"; polygon_layout[4].SemanticIndex = 0; polygon_layout[4].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygon_layout[4].InputSlot = 0; polygon_layout[4].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygon_layout[4].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygon_layout[4].InstanceDataStepRate = 0; // INSTANCED DATA // position polygon_layout[5].SemanticName = "TEXCOORD"; polygon_layout[5].SemanticIndex = 1; polygon_layout[5].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygon_layout[5].InputSlot = 1; polygon_layout[5].AlignedByteOffset = 0; polygon_layout[5].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA; polygon_layout[5].InstanceDataStepRate = 1; // rotation polygon_layout[6].SemanticName = "TEXCOORD"; polygon_layout[6].SemanticIndex = 2; polygon_layout[6].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygon_layout[6].InputSlot = 1; polygon_layout[6].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygon_layout[6].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA; polygon_layout[6].InstanceDataStepRate = 1; // scale polygon_layout[7].SemanticName = "TEXCOORD"; polygon_layout[7].SemanticIndex = 3; polygon_layout[7].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygon_layout[7].InputSlot = 1; polygon_layout[7].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygon_layout[7].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA; polygon_layout[7].InstanceDataStepRate = 1; unsigned int num_elements = ARRAYSIZE( polygon_layout ); ID3DX11EffectTechnique* technique; technique = m_effect->GetTechniqueByName( "Render" ); ID3DX11EffectPass* pass = technique->GetPassByIndex( 0U ); D3DX11_PASS_SHADER_DESC pass_desc; D3DX11_EFFECT_SHADER_DESC shader_desc; pass->GetVertexShaderDesc( &pass_desc ); pass_desc.pShaderVariable->GetShaderDesc( pass_desc.ShaderIndex, &shader_desc ); if( FAILED( device->CreateInputLayout( polygon_layout, num_elements, shader_desc.pBytecode, shader_desc.BytecodeLength, &m_layout ) ) ) return false; 

The effect file gets loaded properly, textures show up as expected, thus I've cut it out. Creating the vertex buffer:
vertices - table that contains the vertex data of type Vertex_Type
 D3D11_BUFFER_DESC vertex_buf_desc; D3D11_SUBRESOURCE_DATA vertex_data; vertex_buf_desc.Usage = D3D11_USAGE_DEFAULT; vertex_buf_desc.ByteWidth = sizeof( Vertex_Type ) * vertex_count; vertex_buf_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vertex_buf_desc.CPUAccessFlags = 0; vertex_buf_desc.MiscFlags = 0; vertex_buf_desc.StructureByteStride = 0; vertex_data.pSysMem = vertices; vertex_data.SysMemPitch = 0; vertex_data.SysMemSlicePitch = 0; if( FAILED( device->CreateBuffer( &vertex_buf_desc, &vertex_data, &m_vertex_buf ) ) ) { delete[] vertices; return false; } delete[] vertices; 

And here the instance buffer:
instances - table that contains the instance data of type Model_Instance_Type
 D3D11_BUFFER_DESC instance_buf_desc; instance_buf_desc.Usage = D3D11_USAGE_DYNAMIC; instance_buf_desc.ByteWidth = sizeof( Model_Instance_Type ) * m_model_instance_list.size(); instance_buf_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; instance_buf_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; instance_buf_desc.MiscFlags = 0; instance_buf_desc.StructureByteStride = 0; D3D11_SUBRESOURCE_DATA instance_data; instance_data.pSysMem = instances; instance_data.SysMemPitch = 0; instance_data.SysMemSlicePitch = 0; if( FAILED( device->CreateBuffer( &instance_buf_desc, &instance_data, &m_instance_buf ) ) ) { delete[] instances; return false; } delete[] instances; 

Everything works perfectly until I try to access the data in the instance buffer and change it (on a per-frame basis). Here is the function that does that, along with the structs I'm using (in case there's an error).

 D3D11_MAPPED_SUBRESOURCE mapped_subresource; if( FAILED( device_context->Map( m_instance_buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_subresource ) ) ) return false; Model_Instance_Type* instance_data = static_cast<Model_Instance_Type*>( mapped_subresource.pData ); instance_data[index].pos = XMFLOAT3( posX, posY, posZ ); instance_data[index].rot = XMFLOAT3( rotX, rotY, rotZ ); instance_data[index].scale = XMFLOAT3( scaleX, scaleY, scaleZ ); device_context->Unmap( m_instance_buf, 0 ); 

I think that the functions that actually render the model and set up the shader variables will be needed:
 void IceModel::Render( ID3D11DeviceContext* device_context ) { RenderBuffers( device_context ); XMFLOAT4X4 world, view, projection; XMMATRIX xna_world, xna_view, xna_projection; GetWorldMatrix( xna_world ); XMStoreFloat4x4( &world, xna_world ); GetViewMatrix( xna_view ); XMStoreFloat4x4( &view, xna_view ); GetProjectionMatrix( xna_projection ); XMStoreFloat4x4( &projection, xna_projection ); shader->Render( device_context, m_model.size(), m_model_instance_list.size(), world, view, projection, m_tex ); } void Model::RenderBuffers( ID3D11DeviceContext* device_context ) { unsigned int strides[] = { sizeof( Vertex_Type ), sizeof( Model_Instance_Type ) }; unsigned int offsets[] = { 0, 0 }; ID3D11Buffer* buf_ptrs[] = { m_vertex_buf, m_instance_buf }; device_context->IASetVertexBuffers( 0, 2, buf_ptrs, strides, offsets ); device_context->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); } bool Shader2D::Render( ID3D11DeviceContext* device_context, const int& vertex_count, const int& instance_count, XMFLOAT4X4 world, XMFLOAT4X4 view, XMFLOAT4X4 projection, const std::vector<Texture*>& tex ) { XMMATRIX xna_world = XMLoadFloat4x4( &world ); XMMATRIX xna_view = XMLoadFloat4x4( &view ); XMMATRIX xna_projection = XMLoadFloat4x4( &projection ); ID3DX11EffectShaderResourceVariable* color_map = m_effect->GetVariableByName( "color_map" )->AsShaderResource(); if( FAILED( color_map->SetResource( tex[1]->GetTexture() ) ) ) return false; ID3DX11EffectSamplerVariable* sample_type = m_effect->GetVariableByName( "sample_type" )->AsSampler(); if( FAILED( sample_type->SetSampler( 0, m_sample_state ) ) ) return false; ID3DX11EffectMatrixVariable* world_matrix = m_effect->GetVariableByName( "world" )->AsMatrix(); if( FAILED( world_matrix->SetMatrix( reinterpret_cast<float*>( &xna_world ) ) ) ) return false; ID3DX11EffectMatrixVariable* view_matrix = m_effect->GetVariableByName( "view" )->AsMatrix(); if( FAILED( view_matrix->SetMatrix( reinterpret_cast<float*>( &xna_view ) ) ) ) return false; ID3DX11EffectMatrixVariable* projection_matrix = m_effect->GetVariableByName( "projection" )->AsMatrix(); if( FAILED( projection_matrix->SetMatrix( reinterpret_cast<float*>( &xna_projection ) ) ) ) return false; RenderShader( device_context, vertex_count, instance_count ); return true; } void Shader::RenderShader( ID3D11DeviceContext* device_context, const int& vertex_count, const int& instance_count ) { device_context->IASetInputLayout( m_layout ); ID3DX11EffectTechnique* technique = m_effect->GetTechniqueByName( "Render" ); D3DX11_TECHNIQUE_DESC tech_desc; technique->GetDesc( &tech_desc ); ID3DX11EffectPass* pass; for( unsigned int i = 0; i < tech_desc.Passes; ++i ) { pass = technique->GetPassByIndex( i ); if( pass ) { pass->Apply( 0, device_context ); device_context->DrawInstanced( vertex_count, instance_count, 0, 0 ); } } } 

 struct Vertex_Type { XMFLOAT4 pos; XMFLOAT2 tex; XMFLOAT3 normal; XMFLOAT3 tangent; XMFLOAT3 binormal; }; struct Model_Instance_Type { XMFLOAT3 pos; XMFLOAT3 rot; XMFLOAT3 scale; }; 

Now about how it's not working. The model I want to move (the one I'm updating with new position) renders ideally, moves, no artifacts. However all other instanced objects are blinking, like they were rendered each 2nd frame so it's clearly seen that they're not rendered properly. If that wasn't enough, the instance I'm moving, not only renders in the proper spot, but it keeps rendering itself in the original position with the same kind of blinking. I'm completely lost on this, since if I get that well - when you update the instance buffer data, the old content gets overwritten. So how come the object renders itself at the original position?

I know that it's a lot of code, but I thought that I understood instancing as for static objects it works (I can set as many instances of each model as I wish with any coordinates) and this just destroys the day. If there's something more You need to know, please ask as I'd really like to get this going.

##### Share on other sites
 D3D11_MAPPED_SUBRESOURCE mapped_subresource; if( FAILED( device_context->Map( m_instance_buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_subresource ) ) ) return false; Model_Instance_Type* instance_data = static_cast<Model_Instance_Type*>( mapped_subresource.pData ); instance_data[index].pos = XMFLOAT3( posX, posY, posZ ); instance_data[index].rot = XMFLOAT3( rotX, rotY, rotZ ); instance_data[index].scale = XMFLOAT3( scaleX, scaleY, scaleZ ); device_context->Unmap( m_instance_buf, 0 ); 

Could you post all of the code for this part? You have an index which suggests you're using a loop, but there's no loop here.

##### Share on other sites
That's not really a loop index, but rather instance index in the vector, that contains them (raw data, not the instance buffer). Here is the whole function:

 bool Model::UpdateInstance( const int& index, const float& posX, const float& posY, const float& posZ, const float& rotX, const float& rotY, const float& rotZ, const float& scaleX, const float& scaleY, const float& scaleZ ) { m_model_instance_list[index]->pos = XMFLOAT3( posX, posY, posZ ); m_model_instance_list[index]->rot = XMFLOAT3( rotX, rotY, rotZ ); m_model_instance_list[index]->scale = XMFLOAT3( scaleX, scaleY, scaleZ ); D3D11_MAPPED_SUBRESOURCE mapped_subresource; if( FAILED( device_context->Map( m_instance_buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_subresource ) ) ) return false; Model_Instance_Type* instance_data = static_cast<Model_Instance_Type*>( mapped_subresource.pData ); instance_data[index].pos = XMFLOAT3( posX, posY, posZ ); instance_data[index].rot = XMFLOAT3( rotX, rotY, rotZ ); instance_data[index].scale = XMFLOAT3( scaleX, scaleY, scaleZ ); device_context->Unmap( m_instance_buf, 0 ); return true; } 

m_model_instance_list :
 std::vector<Model_Instance_Type*> m_model_instance_list;

The idea is to not update whole buffer, when I want to update one given instance. As the data is a table I assumed the [] operator should work fine. The data gets changed when I debug that part, and the the instance moves. What happens are the blinking and "copying itself" I described earlier.

EDIT: No idea if that helps, but I'm unsure of the memory alignments. While I did manage to get around the XMMATRIX requirements (storing them as XMFLOAT4X4 and using Store and Load functions), I might have an error when describing layout. How I understood it is, that the instance part of a layout does not need (nor have to) be aligned to the per vertex data - thus the 1st thing from instance vector - position, has 0 as a byte alignment. Also made InputSlot : 0 for per-vertex, 1 for per-instance, as there are 2 vertex buffers (haven't seen D3D11_BIND_INSTANCE_BUFFER flag, so I used vertex).

##### Share on other sites

The idea is to not update whole buffer, when I want to update one given instance. As the data is a table I assumed the [] operator should work fine. The data gets changed when I debug that part, and the the instance moves. What happens are the blinking and "copying itself" I described earlier.

Ah, well that's your problem. When you map with D3D11_MAP_WRITE_DISCARD (which is what you should be doing for a dynamic VB) the entire contents of the vertex buffer are invalidated. So you can't just copy in the data for one instance at a time, you have to copy in the data for all instances.

##### Share on other sites

[quote name='vipeout' timestamp='1311356682' post='4839043']
The idea is to not update whole buffer, when I want to update one given instance. As the data is a table I assumed the [] operator should work fine. The data gets changed when I debug that part, and the the instance moves. What happens are the blinking and "copying itself" I described earlier.

Ah, well that's your problem. When you map with D3D11_MAP_WRITE_DISCARD (which is what you should be doing for a dynamic VB) the entire contents of the vertex buffer are invalidated. So you can't just copy in the data for one instance at a time, you have to copy in the data for all instances.
[/quote]

Aw, so bad of me, I've been reading the meaning of flags, just forgot that :/. Thanks for showing this to me. I'll try fixing this ASAP, but I've got 1 question then: What do you do if you have thousands of instances? Buffer can get quite big isn't there a way to not write whole buffer each frame, when just 1% of it changed?

EDIT: Worked perfectly. Thank you very much, been trying to solve it even trying to add padding values to the buffer structs (I've been thinking it could be reading "dirty" data, not the one I've assigned).

##### Share on other sites
If you don't want to update the whole buffer, then you can't use DISCARD. For dynamic buffers, the driver will create multiple buffers behind the scenes and cycle through them whenever you update them so that you avoid any synchronization issues with the GPU (since you don't want to write to an area of memory while the GPU is accessing it). This fits in nicely with the semantics of DISCARD, since the the driver can cycle to the next buffer since the contents are undefined by the spec. If you don't want to update the entire buffer you can use NO_OVERWRITE, but when you do that you can only update a portion of the buffer that the GPU isn't currently using. This can work for adding new instances, but not for updating existing instances.

• ### Similar Content

• I have a problem with SSAO. On left hand black area.
Texture2D<uint> texGBufferNormal : register(t0); Texture2D<float> texGBufferDepth : register(t1); Texture2D<float4> texSSAONoise : register(t2); float3 GetUV(float3 position) { float4 vp = mul(float4(position, 1.0), ViewProject); vp.xy = float2(0.5, 0.5) + float2(0.5, -0.5) * vp.xy / vp.w; return float3(vp.xy, vp.z / vp.w); } float3 GetNormal(in Texture2D<uint> texNormal, in int3 coord) { return normalize(2.0 * UnpackNormalSphermap(texNormal.Load(coord)) - 1.0); } float3 GetPosition(in Texture2D<float> texDepth, in int3 coord) { float4 position = 1.0; float2 size; texDepth.GetDimensions(size.x, size.y); position.x = 2.0 * (coord.x / size.x) - 1.0; position.y = -(2.0 * (coord.y / size.y) - 1.0); position.z = texDepth.Load(coord); position = mul(position, ViewProjectInverse); position /= position.w; return position.xyz; } float3 GetPosition(in float2 coord, float depth) { float4 position = 1.0; position.x = 2.0 * coord.x - 1.0; position.y = -(2.0 * coord.y - 1.0); position.z = depth; position = mul(position, ViewProjectInverse); position /= position.w; return position.xyz; } float DepthInvSqrt(float nonLinearDepth) { return 1 / sqrt(1.0 - nonLinearDepth); } float GetDepth(in Texture2D<float> texDepth, float2 uv) { return texGBufferDepth.Sample(samplerPoint, uv); } float GetDepth(in Texture2D<float> texDepth, int3 screenPos) { return texGBufferDepth.Load(screenPos); } float CalculateOcclusion(in float3 position, in float3 direction, in float radius, in float pixelDepth) { float3 uv = GetUV(position + radius * direction); float d1 = DepthInvSqrt(GetDepth(texGBufferDepth, uv.xy)); float d2 = DepthInvSqrt(uv.z); return step(d1 - d2, 0) * min(1.0, radius / abs(d2 - pixelDepth)); } float GetRNDTexFactor(float2 texSize) { float width; float height; texGBufferDepth.GetDimensions(width, height); return float2(width, height) / texSize; } float main(FullScreenPSIn input) : SV_TARGET0 { int3 screenPos = int3(input.Position.xy, 0); float depth = DepthInvSqrt(GetDepth(texGBufferDepth, screenPos)); float3 normal = GetNormal(texGBufferNormal, screenPos); float3 position = GetPosition(texGBufferDepth, screenPos) + normal * SSAO_NORMAL_BIAS; float3 random = normalize(2.0 * texSSAONoise.Sample(samplerNoise, input.Texcoord * GetRNDTexFactor(SSAO_RND_TEX_SIZE)).rgb - 1.0); float SSAO = 0; [unroll] for (int index = 0; index < SSAO_KERNEL_SIZE; index++) { float3 dir = reflect(SamplesKernel[index].xyz, random); SSAO += CalculateOcclusion(position, dir * sign(dot(dir, normal)), SSAO_RADIUS, depth); } return 1.0 - SSAO / SSAO_KERNEL_SIZE; }

• I've been following this tutorial -> https://www.3dgep.com/introduction-to-directx-11/#The_Main_Function , did all the steps,and I ended up with the main.cpp you can see below.
The problem is the call at line 516
g_d3dDeviceContext->UpdateSubresource(g_d3dConstantBuffers[CB_Frame], 0, nullptr, &g_ViewMatrix, 0, 0); which is crashing the program, and the very odd thing is that the first time trough it works fine, it crash the app the second time it is called...
Can someone help me understand why? 😕    I have no idea...

• Hi guys, I'm trying to learn this stuff but running into some problems 😕
I've compiled my .hlsl into a header file which contains the global variable with the precompiled shader data:
//... // Approximately 83 instruction slots used #endif const BYTE g_vs[] = { 68, 88, 66, 67, 143, 82, 13, 236, 152, 133, 219, 113, 173, 135, 18, 87, 122, 208, 124, 76, 1, 0, 0, 0, 16, 76, 0, 0, 6, 0, //.... And now following the "Compiling at build time to header files" example at this msdn link , I've included the header files in my main.cpp and I'm trying to create the vertex shader like this:
hr = g_d3dDevice->CreateVertexShader(g_vs, sizeof(g_vs), nullptr, &g_d3dVertexShader); if (FAILED(hr)) { return -1; } and this is failing, entering the if and returing -1.
Can someone point out what I'm doing wrong? 😕

• Hello everyone,
After a few years of break from coding and my planet render game I'm giving it a go again from a different angle. What I'm struggling with now is that I have created a Frustum that works fine for now atleast, it does what it's supose to do alltho not perfect. But with the frustum came very low FPS, since what I'm doing right now just to see if the Frustum worked is to recreate the vertex buffer every frame that the camera detected movement. This is of course very costly and not the way to do it. Thats why I'm now trying to learn how to create a dynamic vertexbuffer instead and to map and unmap the vertexes, in the end my goal is to update only part of the vertexbuffer that is needed, but one step at a time ^^

So below is my code which I use to create the Dynamic buffer. The issue is that I want the size of the vertex buffer to be big enough to handle bigger vertex buffers then just mPlanetMesh.vertices.size() due to more vertices being added later when I start to do LOD and stuff, the first render isn't the biggest one I will need.
vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC; vertexBufferDesc.ByteWidth = mPlanetMesh.vertices.size(); vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; vertexBufferDesc.MiscFlags = 0; vertexBufferDesc.StructureByteStride = 0; vertexData.pSysMem = &mPlanetMesh.vertices[0]; vertexData.SysMemPitch = 0; vertexData.SysMemSlicePitch = 0; result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &mVertexBuffer); if (FAILED(result)) { return false; } What happens is that the
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &mVertexBuffer); Makes it crash due to Access Violation. When I put the vertices.size() in it works without issues, but when I try to set it to like vertices.size() * 2 it crashes.
I googled my eyes dry tonight but doesn't seem to find people with the same kind of issue, I've read that the vertex buffer can be bigger if needed. What I'm I doing wrong here?

Best Regards and Thanks in advance
Toastmastern
• By yonisi
Hi,
I have a terrain engine where the terrain and water are on different grids. So I'm trying to render planar reflections of the terrain into the water grid. After reading some web pages and docs and also trying to learn from the RasterTek reflections demo and the small water bodies demo as well. What I do is as follows:
1. Create a Reflection view matrix  - Technically I ONLY flip the camera position in the Y direction (Positive Y is up) and add to it 2 * waterLevel. Then I update the View matrix and I save that matrix for later. The code:
void Camera::UpdateReflectionViewMatrix( float waterLevel ) { mBackupPosition = mPosition; mBackupLook = mLook; mPosition.y = -mPosition.y + 2.0f * waterLevel; //mLook.y = -mLook.y + 2.0f * waterLevel; UpdateViewMatrix(); mReflectionView = View(); } 2. I render the Terrain geometry to a 512x512 sized Render target by using the Reflection view matrix and an opposite culling (My Terrain is using front culling by nature so I'm using back culling for the Reflction render pass). Let me say that I checked with the Graphics debugger and the Reflection Render target looks "OK" at this stage (Picture attached). I don't know if the fact that the terrain is shown only at the top are of the texture is expected or not, but it seems OK.

3. Render the Reflection texture into the water using projective texturing - I hope this step is OK code wise. Basically I'm sending to the shader the WorldReflectionViewProj matrix that was created at step 1 in order to use it for the projective texture coordinates, I then convert the position in the DS (Water and terrain are drawn with Tessellation) to the projective tex coords using that WorldReflectionViewProj matrix, then I sample the reflection texture after setting up the coordinates in the PS. Here is the code:
//Send the ReflectionWorldViewProj matrix to the shader: XMStoreFloat4x4(&mPerFrameCB.Data.ReflectionWorldViewProj, XMMatrixTranspose( ( mWorld * pCam->GetReflectedView() ) * mProj )); //Setting up the Projective tex coords in the DS: Output.projTexPosition = mul(float4(worldPos.xyz, 1), g_ReflectionWorldViewProj); //Setting up the coords in the PS and sampling the reflection texture: float2 projTexCoords; projTexCoords.x = input.projTexPosition.x / input.projTexPosition.w / 2.0 + 0.5; projTexCoords.y = -input.projTexPosition.y / input.projTexPosition.w / 2.0 + 0.5; projTexCoords += normal.xz * 0.025; float4 reflectionColor = gReflectionMap.SampleLevel(SamplerClampLinear, projTexCoords, 0); texColor += reflectionColor * 0.25; I'll add that when compiling the PS I'm getting a warning on those dividing by input.projTexPosition.w for a possible float division by 0, I tried to add some offset or some minimum to the dividing term but that still not solved my issue.
Here is the problem itself. At relatively flat view angles I'm seeing correct reflections (Or at least so it seems), but as I pitch the camera down, I'm seeing those artifacts which I have no idea where are coming from. I'm culling the terrain in the reflection render pass when it's lower than water height (I have heightmaps for that).

Any help will be appreciated because I don't know what is wrong or where else to look.
• By thmfrnk
Hi,
I am looking for a usefull commandline based texture compression tool with the rights to be able to ship with my application. It should have following caps:
Supports all major image format as source files (jpeg, png, tga, bmp) Export as DDS Compression Formats BC1, BC2, BC3, BC4, BC7 I am actually using the nvdxt tool from Nvidia, but it does not support BC4 (which I need for one-channel 8bit textures). Everything else which I found wasn't really useful.
Any suggestions?
Thx

• I have been trying to create a BlendState for my UI text sprites so that they are both alpha-blended (so you can see them) and invert the pixel they are rendered over (again, so you can see them).
In order to get alpha blending you would need:
SrcBlend = SRC_ALPHA DestBlend = INV_SRC_ALPHA and in order to have inverted colours you would need something like:
SrcBlend = INV_DEST_COLOR DestBlend = INV_SRC_COLOR and you can't have both.
So I have come to the conclusion that it's not possible; am I right?
• By Royma
I want to know the reason that I reduced the drawcalls from 8 to 1, but it runs slow down.Should I abandon this method or is there any way to optimize this method to run more efficiently than multi-pass rendering?
Here is the gs code:

[maxvertexcount(24)]
void main(
triangle DepthGsIn input[3] : SV_POSITION,
inout TriangleStream< DepthPsIn > output
)
{
for (uint k = 0; k < 8; ++k)
{
DepthPsIn element;
element.RTIndex = k;
for (uint i = 0; i < 3; ++i)
{
element.position = input.position + shadowBias * g_cameras[k].world[1];
element.position = mul(element.position, g_cameras[k].viewProjection);
element.depth = element.position.z / element.position.w;

output.Append(element);
}
output.RestartStrip();
}
}

• By savail
Hey,
There are a few things which confuse me regarding DirectX 11 and HLSL shaders in general. I would be very grateful for your advice!
1. Let's take for example a scene which invokes 2 totally separate pipeline render passes interchangeably. I understand I need to bind correct shaders for each of the render pass and potentially blend/depth or rasterizer state but what about resources such as Constant Buffers, Shader Resource Views and Unordered Access Views? Assuming that the second render pass uses none of the resources used by the first pass, do I still need to unbind the resources and clean pipeline state after first pass? Or is it ok to leave pipeline with unbound garbage since anything I'd need to bind for second pass would overwrite contents in the appropriate register slots anyway?
2. Is it a good practice to assign register slots manually to all resources in HLSL?
3. I thought about assigning manually register slots for every distinct render pass up to the maximum slot limit if neccessary. For example in 1 render pass I invoke 3 CS's, 2 VS's and 2 PS's and for all resources used by those shaders I try to fill as many register slots as neccessary and potentially reuse many times the same slot in shaders sharing the same resource. I was wondering if there is any performance penalty or gain when I bind all of my needed resources at the start of render pass and never gonna have to do it again until next render pass? - this means potentially binding a lot of registers and having excessive number of bound resources for every shader that is run.
4. Is it a good practice to create a separate include file for every resource that occurs in >= 2 shader files or is it better to duplicate the declarations? In first case, the code is imo easier to maintain and edit but might be harder to read if there's too many includes. I've come up with a compromise between these 2 like this: create a separate include file for every CB that occurs in >= 2 shader files and a separate include file for every sampler I ever need to use. All other resources like srvs and uavs I prefer to duplicate in multiple shaders because they take much less space than CB for example... I'm not sure however if that's a good practice
• By Kris1992
I want implement Particle system based on stream out structure to my bigger project. I saw few articles about that method and I build one particle. It works almost correctly but in geometry shader with stream out i cant get value of InitVel.z and age because it always is 0. If i change order of age(for example age is before Position) it works fine for age but 6th float of order is still 0. It looks like he push only 5 first positions. I had no idea what i do wrong because i try change almost all(create input layout for vertex, the same like entry SO Declaration, change number of strides for static 28, change it to 32 but in this case he draw chaotic so size of strides is probably good). I think it is problem with limits of NumEntry in declaration Entry but on site msdn i saw the limit for directx is D3D11_SO_STREAM_COUNT(4)*D3D11_SO_OUTPUT_COMPONENT_COUNT(128) not 5. Pls can you look in this code and give me the way or hope of implement it correctly?? Thanks a lot for help.

• 33
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631353
• Total Posts
2999488
×