1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 #include "libANGLE/renderer/d3d/d3d11/ExternalImageSiblingImpl11.h"
8
9 #include "libANGLE/Context.h"
10 #include "libANGLE/Error.h"
11 #include "libANGLE/angletypes.h"
12 #include "libANGLE/formatutils.h"
13 #include "libANGLE/renderer/d3d/d3d11/Context11.h"
14 #include "libANGLE/renderer/d3d/d3d11/RenderTarget11.h"
15 #include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
16 #include "libANGLE/renderer/d3d/d3d11/texture_format_table.h"
17
18 namespace rx
19 {
ExternalImageSiblingImpl11(Renderer11 * renderer,EGLClientBuffer buffer,const egl::AttributeMap & attribs)20 ExternalImageSiblingImpl11::ExternalImageSiblingImpl11(Renderer11 *renderer,
21 EGLClientBuffer buffer,
22 const egl::AttributeMap &attribs)
23 : mRenderer(renderer), mBuffer(buffer), mAttribs(attribs)
24 {}
25
~ExternalImageSiblingImpl11()26 ExternalImageSiblingImpl11::~ExternalImageSiblingImpl11() {}
27
initialize(const egl::Display * display)28 egl::Error ExternalImageSiblingImpl11::initialize(const egl::Display *display)
29 {
30 const angle::Format *angleFormat = nullptr;
31 ANGLE_TRY(mRenderer->getD3DTextureInfo(nullptr, static_cast<IUnknown *>(mBuffer), mAttribs,
32 &mWidth, &mHeight, &mSamples, &mFormat, &angleFormat,
33 &mArraySlice));
34 ID3D11Texture2D *texture =
35 d3d11::DynamicCastComObject<ID3D11Texture2D>(static_cast<IUnknown *>(mBuffer));
36 ASSERT(texture != nullptr);
37 // TextureHelper11 will release texture on destruction.
38 mTexture.set(texture, d3d11::Format::Get(angleFormat->glInternalFormat,
39 mRenderer->getRenderer11DeviceCaps()));
40 D3D11_TEXTURE2D_DESC textureDesc = {};
41 mTexture.getDesc(&textureDesc);
42
43 IDXGIResource *resource = d3d11::DynamicCastComObject<IDXGIResource>(mTexture.get());
44 ASSERT(resource != nullptr);
45 DXGI_USAGE resourceUsage = 0;
46 resource->GetUsage(&resourceUsage);
47 SafeRelease(resource);
48
49 mIsRenderable = (textureDesc.BindFlags & D3D11_BIND_RENDER_TARGET) &&
50 (resourceUsage & DXGI_USAGE_RENDER_TARGET_OUTPUT) &&
51 !(resourceUsage & DXGI_USAGE_READ_ONLY);
52
53 mIsTexturable = (textureDesc.BindFlags & D3D11_BIND_SHADER_RESOURCE) &&
54 (resourceUsage & DXGI_USAGE_SHADER_INPUT);
55
56 mIsTextureArray = (textureDesc.ArraySize > 1);
57
58 mYUV = (textureDesc.Format == DXGI_FORMAT_NV12 || textureDesc.Format == DXGI_FORMAT_P010 ||
59 textureDesc.Format == DXGI_FORMAT_P016);
60
61 return egl::NoError();
62 }
63
getFormat() const64 gl::Format ExternalImageSiblingImpl11::getFormat() const
65 {
66 return mFormat;
67 }
68
isRenderable(const gl::Context * context) const69 bool ExternalImageSiblingImpl11::isRenderable(const gl::Context *context) const
70 {
71 return mIsRenderable;
72 }
73
isTexturable(const gl::Context * context) const74 bool ExternalImageSiblingImpl11::isTexturable(const gl::Context *context) const
75 {
76 return mIsTexturable;
77 }
78
isYUV() const79 bool ExternalImageSiblingImpl11::isYUV() const
80 {
81 return mYUV;
82 }
83
hasProtectedContent() const84 bool ExternalImageSiblingImpl11::hasProtectedContent() const
85 {
86 return mHasProtectedContent;
87 }
88
getSize() const89 gl::Extents ExternalImageSiblingImpl11::getSize() const
90 {
91 return gl::Extents(mWidth, mHeight, 1);
92 }
93
getSamples() const94 size_t ExternalImageSiblingImpl11::getSamples() const
95 {
96 return mSamples;
97 }
98
getAttachmentRenderTarget(const gl::Context * context,GLenum binding,const gl::ImageIndex & imageIndex,GLsizei samples,FramebufferAttachmentRenderTarget ** rtOut)99 angle::Result ExternalImageSiblingImpl11::getAttachmentRenderTarget(
100 const gl::Context *context,
101 GLenum binding,
102 const gl::ImageIndex &imageIndex,
103 GLsizei samples,
104 FramebufferAttachmentRenderTarget **rtOut)
105 {
106 ANGLE_TRY(createRenderTarget(context));
107 *rtOut = mRenderTarget.get();
108 return angle::Result::Continue;
109 }
110
initializeContents(const gl::Context * context,const gl::ImageIndex & imageIndex)111 angle::Result ExternalImageSiblingImpl11::initializeContents(const gl::Context *context,
112 const gl::ImageIndex &imageIndex)
113 {
114 UNREACHABLE();
115 return angle::Result::Stop;
116 }
117
createRenderTarget(const gl::Context * context)118 angle::Result ExternalImageSiblingImpl11::createRenderTarget(const gl::Context *context)
119 {
120 if (mRenderTarget)
121 return angle::Result::Continue;
122
123 Context11 *context11 = GetImplAs<Context11>(context);
124 const d3d11::Format &formatInfo = mTexture.getFormatSet();
125
126 d3d11::RenderTargetView rtv;
127 if (mIsRenderable)
128 {
129 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
130 rtvDesc.Format = formatInfo.rtvFormat;
131 if (mIsTextureArray)
132 {
133 if (mSamples == 0)
134 {
135 rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
136 rtvDesc.Texture2DArray.MipSlice = 0;
137 rtvDesc.Texture2DArray.FirstArraySlice = mArraySlice;
138 rtvDesc.Texture2DArray.ArraySize = 1;
139 }
140 else
141 {
142 rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY;
143 rtvDesc.Texture2DMSArray.FirstArraySlice = mArraySlice;
144 rtvDesc.Texture2DMSArray.ArraySize = 1;
145 }
146 }
147 else
148 {
149 if (mSamples == 0)
150 {
151 rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
152 rtvDesc.Texture2D.MipSlice = 0;
153 }
154 else
155 {
156 rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
157 }
158 }
159
160 ANGLE_TRY(mRenderer->allocateResource(context11, rtvDesc, mTexture.get(), &rtv));
161 rtv.setInternalName("getAttachmentRenderTarget.RTV");
162 }
163
164 d3d11::SharedSRV srv;
165 if (mIsTexturable)
166 {
167 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
168 srvDesc.Format = formatInfo.srvFormat;
169 if (mIsTextureArray)
170 {
171 if (mSamples == 0)
172 {
173 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
174 srvDesc.Texture2DArray.MostDetailedMip = 0;
175 srvDesc.Texture2DArray.MipLevels = 1;
176 srvDesc.Texture2DArray.FirstArraySlice = mArraySlice;
177 srvDesc.Texture2DArray.ArraySize = 1;
178 }
179 else
180 {
181 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY;
182 srvDesc.Texture2DArray.FirstArraySlice = mArraySlice;
183 srvDesc.Texture2DArray.ArraySize = 1;
184 }
185 }
186 else
187 {
188 if (mSamples == 0)
189 {
190 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
191 srvDesc.Texture2D.MostDetailedMip = 0;
192 srvDesc.Texture2D.MipLevels = 1;
193 }
194 else
195 {
196 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
197 }
198 }
199
200 ANGLE_TRY(mRenderer->allocateResource(context11, srvDesc, mTexture.get(), &srv));
201 srv.setInternalName("getAttachmentRenderTarget.SRV");
202 }
203 d3d11::SharedSRV blitSrv = srv.makeCopy();
204
205 mRenderTarget = std::make_unique<TextureRenderTarget11>(
206 std::move(rtv), mTexture, std::move(srv), std::move(blitSrv), mFormat.info->internalFormat,
207 formatInfo, mWidth, mHeight, 1, mSamples);
208 return angle::Result::Continue;
209 }
210
211 } // namespace rx
212