Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrD3DPipelineState.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
23
25 sk_sp<GrD3DPipeline> pipeline,
26 sk_sp<GrD3DRootSignature> rootSignature,
27 const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
28 const UniformInfoArray& uniforms,
29 uint32_t uniformSize,
30 uint32_t numSamplers,
31 std::unique_ptr<GrGeometryProcessor::ProgramImpl> gpImpl,
32 std::unique_ptr<GrXferProcessor::ProgramImpl> xpImpl,
33 std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls,
34 size_t vertexStride,
35 size_t instanceStride)
36 : fPipeline(std::move(pipeline))
37 , fRootSignature(std::move(rootSignature))
38 , fBuiltinUniformHandles(builtinUniformHandles)
39 , fGPImpl(std::move(gpImpl))
40 , fXPImpl(std::move(xpImpl))
41 , fFPImpls(std::move(fpImpls))
42 , fDataManager(uniforms, uniformSize)
43 , fNumSamplers(numSamplers)
44 , fVertexStride(vertexStride)
45 , fInstanceStride(instanceStride) {}
46
48 const GrRenderTarget* renderTarget,
49 const GrProgramInfo& programInfo) {
50 this->setRenderTargetState(renderTarget, programInfo.origin());
51
52 fGPImpl->setData(fDataManager, *gpu->caps()->shaderCaps(), programInfo.geomProc());
53
54 for (int i = 0; i < programInfo.pipeline().numFragmentProcessors(); ++i) {
55 const auto& fp = programInfo.pipeline().getFragmentProcessor(i);
56 fp.visitWithImpls([&](const GrFragmentProcessor& fp,
58 impl.setData(fDataManager, fp);
59 }, *fFPImpls[i]);
60 }
61
62 programInfo.pipeline().setDstTextureUniforms(fDataManager, &fBuiltinUniformHandles);
63 fXPImpl->setData(fDataManager, programInfo.pipeline().getXferProcessor());
64
65 D3D12_GPU_VIRTUAL_ADDRESS constantsAddress = fDataManager.uploadConstants(gpu);
68 constantsAddress);
69}
70
71void GrD3DPipelineState::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
72 // Set RT adjustment and RT flip
73 SkISize dimensions = rt->dimensions();
74 SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
75 if (fRenderTargetState.fRenderTargetOrigin != origin ||
76 fRenderTargetState.fRenderTargetSize != dimensions) {
77 fRenderTargetState.fRenderTargetSize = dimensions;
78 fRenderTargetState.fRenderTargetOrigin = origin;
79
80 // The client will mark a swap buffer as kTopLeft when making a SkSurface because
81 // D3D's framebuffer space has (0, 0) at the top left. This agrees with Skia's device
82 // coords. However, in NDC (-1, -1) is the bottom left. So we flip when origin is kTopLeft.
83 bool flip = (origin == kTopLeft_GrSurfaceOrigin);
84 std::array<float, 4> v = SkSL::Compiler::GetRTAdjustVector(dimensions, flip);
85 fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, v.data());
86 if (fBuiltinUniformHandles.fRTFlipUni.isValid()) {
87 // Note above that framebuffer space has origin top left. So we need !flip here.
88 std::array<float, 2> d = SkSL::Compiler::GetRTFlipVector(rt->height(), !flip);
89 fDataManager.set2fv(fBuiltinUniformHandles.fRTFlipUni, 1, d.data());
90 }
91 }
92}
93
95 const GrGeometryProcessor& geomProc,
96 const GrSurfaceProxy* const geomProcTextures[],
97 const GrPipeline& pipeline) {
98 SkASSERT(geomProcTextures || !geomProc.numTextureSamplers());
99
100 std::vector<D3D12_CPU_DESCRIPTOR_HANDLE> shaderResourceViews(fNumSamplers);
101 std::vector<D3D12_CPU_DESCRIPTOR_HANDLE> samplers(fNumSamplers);
102 unsigned int currTextureBinding = 0;
103
104 for (int i = 0; i < geomProc.numTextureSamplers(); ++i) {
105 SkASSERT(geomProcTextures[i]->asTextureProxy());
106 const auto& sampler = geomProc.textureSampler(i);
107 auto texture = static_cast<GrD3DTexture*>(geomProcTextures[i]->peekTexture());
108 shaderResourceViews[currTextureBinding] = texture->shaderResourceView();
109 samplers[currTextureBinding++] =
110 gpu->resourceProvider().findOrCreateCompatibleSampler(sampler.samplerState());
112 }
113
114 if (GrTexture* dstTexture = pipeline.peekDstTexture()) {
115 auto texture = static_cast<GrD3DTexture*>(dstTexture);
116 shaderResourceViews[currTextureBinding] = texture->shaderResourceView();
117 samplers[currTextureBinding++] = gpu->resourceProvider().findOrCreateCompatibleSampler(
118 GrSamplerState::Filter::kNearest);
120 }
121
122 pipeline.visitTextureEffects([&](const GrTextureEffect& te) {
123 GrSamplerState samplerState = te.samplerState();
124 auto* texture = static_cast<GrD3DTexture*>(te.texture());
125 shaderResourceViews[currTextureBinding] = texture->shaderResourceView();
126 samplers[currTextureBinding++] =
129 });
130
131 SkASSERT(fNumSamplers == currTextureBinding);
132
133 // fill in descriptor tables and bind to root signature
134 if (fNumSamplers > 0) {
135 // set up descriptor tables and bind heaps
137 gpu->resourceProvider().findOrCreateShaderViewTable(shaderResourceViews);
138 sk_sp<GrD3DDescriptorTable> samplerTable =
140 gpu->currentCommandList()->setDescriptorHeaps(srvTable->heap(), samplerTable->heap());
141
142 // bind shader resource view table
145 srvTable->baseGpuDescriptor());
146
147 // bind sampler table
150 samplerTable->baseGpuDescriptor());
151 }
152}
153
155 sk_sp<const GrBuffer> instanceBuffer,
156 sk_sp<const GrBuffer> vertexBuffer,
157 GrD3DDirectCommandList* commandList) {
158 // Here our vertex and instance inputs need to match the same 0-based bindings they were
159 // assigned in the PipelineState. That is, vertex first (if any) followed by instance.
160 if (vertexBuffer) {
161 auto* d3dVertexBuffer = static_cast<const GrD3DBuffer*>(vertexBuffer.get());
162 SkASSERT(!d3dVertexBuffer->isCpuBuffer());
163 SkASSERT(!d3dVertexBuffer->isMapped());
164 const_cast<GrD3DBuffer*>(d3dVertexBuffer)->setResourceState(
165 gpu, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
166 }
167 if (instanceBuffer) {
168 auto* d3dInstanceBuffer = static_cast<const GrD3DBuffer*>(instanceBuffer.get());
169 SkASSERT(!d3dInstanceBuffer->isCpuBuffer());
170 SkASSERT(!d3dInstanceBuffer->isMapped());
171 const_cast<GrD3DBuffer*>(d3dInstanceBuffer)->setResourceState(
172 gpu, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
173 }
174 commandList->setVertexBuffers(0, std::move(vertexBuffer), fVertexStride,
175 std::move(instanceBuffer), fInstanceStride);
176
177 if (auto* d3dIndexBuffer = static_cast<const GrD3DBuffer*>(indexBuffer.get())) {
178 SkASSERT(!d3dIndexBuffer->isCpuBuffer());
179 SkASSERT(!d3dIndexBuffer->isMapped());
180 const_cast<GrD3DBuffer*>(d3dIndexBuffer)->setResourceState(
181 gpu, D3D12_RESOURCE_STATE_INDEX_BUFFER);
182 commandList->setIndexBuffer(std::move(indexBuffer));
183 }
184}
GrSurfaceOrigin
Definition GrTypes.h:147
@ kTopLeft_GrSurfaceOrigin
Definition GrTypes.h:148
#define SkASSERT(cond)
Definition SkAssert.h:116
const GrShaderCaps * shaderCaps() const
Definition GrCaps.h:63
void addSampledTextureRef(GrD3DTexture *)
void setGraphicsRootConstantBufferView(unsigned int rootParameterIndex, D3D12_GPU_VIRTUAL_ADDRESS bufferLocation)
void setVertexBuffers(unsigned int startSlot, sk_sp< const GrBuffer > vertexBuffer, size_t vertexStride, sk_sp< const GrBuffer > instanceBuffer, size_t instanceStride)
void setDescriptorHeaps(ID3D12DescriptorHeap *srvDescriptorHeap, ID3D12DescriptorHeap *samplerDescriptorHeap)
void setIndexBuffer(sk_sp< const GrBuffer > indexBuffer)
void setGraphicsRootDescriptorTable(unsigned int rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE bufferLocation)
GrD3DDirectCommandList * currentCommandList() const
Definition GrD3DGpu.h:48
GrD3DResourceProvider & resourceProvider()
Definition GrD3DGpu.h:38
D3D12_GPU_VIRTUAL_ADDRESS uploadConstants(GrD3DGpu *gpu)
void setAndBindTextures(GrD3DGpu *, const GrGeometryProcessor &, const GrSurfaceProxy *const geomProcTextures[], const GrPipeline &)
const sk_sp< GrD3DPipeline > & pipeline() const
void setAndBindConstants(GrD3DGpu *, const GrRenderTarget *, const GrProgramInfo &)
void bindBuffers(GrD3DGpu *, sk_sp< const GrBuffer > indexBuffer, sk_sp< const GrBuffer > instanceBuffer, sk_sp< const GrBuffer > vertexBuffer, GrD3DDirectCommandList *commandList)
GrD3DPipelineState(sk_sp< GrD3DPipeline > pipeline, sk_sp< GrD3DRootSignature > rootSignature, const GrGLSLBuiltinUniformHandles &builtinUniformHandles, const UniformInfoArray &uniforms, uint32_t uniformSize, uint32_t numSamplers, std::unique_ptr< GrGeometryProcessor::ProgramImpl > gpImpl, std::unique_ptr< GrXferProcessor::ProgramImpl > xpImpl, std::vector< std::unique_ptr< GrFragmentProcessor::ProgramImpl > > fpImpls, size_t vertexStride, size_t instanceStride)
sk_sp< GrD3DDescriptorTable > findOrCreateShaderViewTable(const std::vector< D3D12_CPU_DESCRIPTOR_HANDLE > &shaderViews)
D3D12_CPU_DESCRIPTOR_HANDLE findOrCreateCompatibleSampler(const GrSamplerState &params)
sk_sp< GrD3DDescriptorTable > findOrCreateSamplerTable(const std::vector< D3D12_CPU_DESCRIPTOR_HANDLE > &samplers)
D3D12_CPU_DESCRIPTOR_HANDLE shaderResourceView()
void setData(const GrGLSLProgramDataManager &pdman, const GrFragmentProcessor &processor)
const TextureSampler & textureSampler(int index) const
const GrCaps * caps() const
Definition GrGpu.h:73
void setDstTextureUniforms(const GrGLSLProgramDataManager &pdm, GrGLSLBuiltinUniformHandles *fBuiltinUniformHandles) const
const GrFragmentProcessor & getFragmentProcessor(int idx) const
Definition GrPipeline.h:157
const GrXferProcessor & getXferProcessor() const
Definition GrPipeline.h:116
GrSurfaceOrigin origin() const
const GrPipeline & pipeline() const
const GrGeometryProcessor & geomProc() const
GrTexture * peekTexture() const
SkISize dimensions() const
Definition GrSurface.h:27
int height() const
Definition GrSurface.h:37
GrTexture * texture() const
GrSamplerState samplerState() const
void set4fv(UniformHandle, int arrayCount, const float v[]) const override
void set2fv(UniformHandle, int arrayCount, const float v[]) const override
static std::array< float, 2 > GetRTFlipVector(int rtHeight, bool flipY)
static std::array< float, 4 > GetRTAdjustVector(SkISize rtDims, bool flipY)
T * get() const
Definition SkRefCnt.h:303
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
FlTexture * texture
Definition ref_ptr.h:256
GrGLSLProgramDataManager::UniformHandle fRTFlipUni
GrGLSLProgramDataManager::UniformHandle fRTAdjustmentUni