| 27 | } |
| 28 | |
| 29 | void DecalMaterialShader::Bind(BindParameters& params) |
| 30 | { |
| 31 | // Prepare |
| 32 | auto context = params.GPUContext; |
| 33 | auto& view = params.RenderContext.View; |
| 34 | auto& drawCall = *params.DrawCall; |
| 35 | Span<byte> cb(_cbData.Get(), _cbData.Count()); |
| 36 | ASSERT_LOW_LAYER(cb.Length() >= sizeof(DecalMaterialShaderData)); |
| 37 | auto materialData = reinterpret_cast<DecalMaterialShaderData*>(cb.Get()); |
| 38 | cb = cb.Slice(sizeof(DecalMaterialShaderData)); |
| 39 | const bool isCameraInside = OrientedBoundingBox(Vector3::Half, drawCall.World).Contains(view.Position) == ContainmentType::Contains; |
| 40 | |
| 41 | // Setup parameters |
| 42 | MaterialParameter::BindMeta bindMeta; |
| 43 | bindMeta.Context = context; |
| 44 | bindMeta.Constants = cb; |
| 45 | bindMeta.Input = nullptr; |
| 46 | bindMeta.Buffers = params.RenderContext.Buffers; |
| 47 | bindMeta.CanSampleDepth = true; |
| 48 | bindMeta.CanSampleGBuffer = false; |
| 49 | MaterialParams::Bind(params.ParamsLink, bindMeta); |
| 50 | |
| 51 | // Decals use depth buffer to draw on top of the objects |
| 52 | GPUTexture* depthBuffer = params.RenderContext.Buffers->DepthBuffer; |
| 53 | GPUTextureView* depthBufferView = EnumHasAnyFlags(depthBuffer->Flags(), GPUTextureFlags::ReadOnlyDepthView) ? depthBuffer->ViewReadOnlyDepth() : depthBuffer->View(); |
| 54 | context->BindSR(0, depthBufferView); |
| 55 | context->BindSR(1, depthBuffer->ViewStencil()); |
| 56 | |
| 57 | // Setup material constants |
| 58 | { |
| 59 | Matrix::Transpose(drawCall.World, materialData->WorldMatrix); |
| 60 | |
| 61 | // Matrix for transformation from world space to decal object space |
| 62 | Matrix invWorld; |
| 63 | Matrix::Invert(drawCall.World, invWorld); |
| 64 | Matrix::Transpose(invWorld, materialData->InvWorld); |
| 65 | |
| 66 | // Matrix for transformation from SV Position space to world space |
| 67 | const Matrix offsetMatrix( |
| 68 | 2.0f * view.ScreenSize.Z, 0, 0, 0, |
| 69 | 0, -2.0f * view.ScreenSize.W, 0, 0, |
| 70 | 0, 0, 1, 0, |
| 71 | -1.0f, 1.0f, 0, 1); |
| 72 | const Matrix svPositionToWorld = offsetMatrix * view.IVP; |
| 73 | Matrix::Transpose(svPositionToWorld, materialData->SvPositionToWorld); |
| 74 | materialData->RenderLayersMask = (uint32)drawCall.SortKey; // Provided by GBufferPass::DrawDecals |
| 75 | } |
| 76 | |
| 77 | // Bind constants |
| 78 | if (_cb) |
| 79 | { |
| 80 | context->UpdateCB(_cb, _cbData.Get()); |
| 81 | context->BindCB(0, _cb); |
| 82 | } |
| 83 | |
| 84 | // Bind pipeline |
| 85 | context->SetState(isCameraInside ? _cache.Inside : _cache.Outside); |
| 86 | } |
nothing calls this directly
no test coverage detected