2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "test-graphics-framebuffer.h"
18 #include <dali/integration-api/gl-defines.h>
19 #include "test-graphics-controller.h"
20 #include "test-graphics-texture.h"
24 const GLenum COLOR_ATTACHMENTS[] =
36 struct DEPTH_STENCIL_ATTACHMENT_TYPE
38 constexpr explicit DEPTH_STENCIL_ATTACHMENT_TYPE(Graphics::Format textureFormat)
42 case Graphics::Format::D16_UNORM:
43 case Graphics::Format::D32_SFLOAT:
44 case Graphics::Format::X8_D24_UNORM_PACK32:
46 attachment = GL_DEPTH_ATTACHMENT;
50 case Graphics::Format::S8_UINT:
52 attachment = GL_STENCIL_ATTACHMENT;
56 case Graphics::Format::D16_UNORM_S8_UINT:
57 case Graphics::Format::D24_UNORM_S8_UINT:
58 case Graphics::Format::D32_SFLOAT_S8_UINT:
60 attachment = GL_DEPTH_STENCIL_ATTACHMENT;
70 GLenum attachment{GL_NONE};
78 TestGraphicsFramebuffer::TestGraphicsFramebuffer(
79 TraceCallStack& callStack,
80 TestGlAbstraction& glAbstraction,
81 const Graphics::FramebufferCreateInfo& createInfo)
85 mCreateInfo.colorAttachments = std::move(createInfo.colorAttachments);
86 mCreateInfo.depthStencilAttachment = createInfo.depthStencilAttachment;
87 mCreateInfo.size = createInfo.size;
90 TestGraphicsFramebuffer::~TestGraphicsFramebuffer()
94 mGl.DeleteFramebuffers(1, &mId);
98 void TestGraphicsFramebuffer::Initialize()
100 mCallStack.PushCall("Initialize", "");
102 mGl.GenFramebuffers(1, &mId);
103 mGl.BindFramebuffer(GL_FRAMEBUFFER, mId);
105 for(Graphics::ColorAttachment& attachment : mCreateInfo.colorAttachments)
107 AttachTexture(attachment.texture, COLOR_ATTACHMENTS[attachment.attachmentId], attachment.layerId, attachment.levelId);
109 mGl.DrawBuffers(mCreateInfo.colorAttachments.size(), COLOR_ATTACHMENTS);
111 if(mCreateInfo.depthStencilAttachment.depthTexture)
113 // Create a depth or depth/stencil render target.
114 auto depthTexture = Uncast<TestGraphicsTexture>(mCreateInfo.depthStencilAttachment.depthTexture);
115 auto attachmentId = DEPTH_STENCIL_ATTACHMENT_TYPE(depthTexture->GetFormat()).attachment;
117 mGl.GenRenderbuffers(1, &mDepthBuffer);
118 mGl.BindRenderbuffer(GL_RENDERBUFFER, mDepthBuffer);
119 mGl.RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mCreateInfo.size.width, mCreateInfo.size.height);
120 mGl.FramebufferRenderbuffer(GL_FRAMEBUFFER, attachmentId, GL_RENDERBUFFER, mDepthBuffer);
122 AttachTexture(depthTexture, attachmentId, 0, mCreateInfo.depthStencilAttachment.depthLevel);
125 if(mCreateInfo.depthStencilAttachment.stencilTexture)
127 auto stencilTexture = Uncast<TestGraphicsTexture>(mCreateInfo.depthStencilAttachment.stencilTexture);
128 auto attachmentId = DEPTH_STENCIL_ATTACHMENT_TYPE(stencilTexture->GetFormat()).attachment;
130 // Create a stencil render target.
131 mGl.GenRenderbuffers(1, &mStencilBuffer);
132 mGl.BindRenderbuffer(GL_RENDERBUFFER, mStencilBuffer);
133 mGl.RenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, mCreateInfo.size.width, mCreateInfo.size.height);
134 mGl.FramebufferRenderbuffer(GL_FRAMEBUFFER, attachmentId, GL_RENDERBUFFER, mStencilBuffer);
136 AttachTexture(stencilTexture, attachmentId, 0, mCreateInfo.depthStencilAttachment.stencilLevel);
138 mGl.BindFramebuffer(GL_FRAMEBUFFER, 0);
141 void TestGraphicsFramebuffer::AttachTexture(Graphics::Texture* texture, uint32_t attachmentId, uint32_t layerId, uint32_t levelId)
143 auto graphicsTexture = Uncast<TestGraphicsTexture>(texture);
144 if(graphicsTexture->GetType() == Graphics::TextureType::TEXTURE_2D)
146 mGl.FramebufferTexture2D(GL_FRAMEBUFFER, attachmentId, graphicsTexture->GetTarget(), graphicsTexture->mId, levelId);
150 mGl.FramebufferTexture2D(GL_FRAMEBUFFER, attachmentId, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layerId, graphicsTexture->mId, levelId);
154 void TestGraphicsFramebuffer::Bind()
156 mCallStack.PushCall("Bind", "");
162 mGl.BindFramebuffer(GL_FRAMEBUFFER, mId);