X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali%2Finternal%2Frender%2Frenderers%2Frender-geometry.cpp;h=b407b6f9fac49a5ac8adcb4e9fef84c039259f9c;hb=c4750afbf79f15bf71e2aa8ef54f84750463aae2;hp=0c3bd0a1bac0ca42b728b61a796278db904d5a29;hpb=449cde6cfe99f1e9297bec8cdb5cedff37502afb;p=platform%2Fcore%2Fuifw%2Fdali-core.git diff --git a/dali/internal/render/renderers/render-geometry.cpp b/dali/internal/render/renderers/render-geometry.cpp index 0c3bd0a..b407b6f 100644 --- a/dali/internal/render/renderers/render-geometry.cpp +++ b/dali/internal/render/renderers/render-geometry.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * Copyright (c) 2023 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,11 +14,12 @@ * limitations under the License. */ +// CLASS HEADER #include + +// INTERNAL INCLUDES #include -#include -#include -#include +#include #include namespace Dali @@ -27,134 +28,117 @@ namespace Internal { namespace Render { - +namespace +{ +inline constexpr size_t GetSizeOfIndexFromIndexType(Dali::Graphics::Format graphicsFormat) +{ + switch(graphicsFormat) + { + case Dali::Graphics::Format::R16_UINT: + { + return sizeof(uint16_t); + } + case Dali::Graphics::Format::R32_UINT: + { + return sizeof(uint32_t); + } + default: + { + // TODO : Not implmeneted. + return sizeof(uint16_t); + } + } +} +} // unnamed namespace Geometry::Geometry() : mIndices(), - mIndexBuffer(NULL), - mGeometryType( Dali::Geometry::TRIANGLES ), + mIndexBuffer(nullptr), + mIndexType(Dali::Graphics::Format::R16_UINT), + mGeometryType(Dali::Geometry::TRIANGLES), mIndicesChanged(false), mHasBeenUpdated(false), mAttributesChanged(true) { } -Geometry::~Geometry() -{ -} - -void Geometry::GlContextCreated( Context& context ) -{ -} +Geometry::~Geometry() = default; -void Geometry::GlContextDestroyed() +void Geometry::AddVertexBuffer(Render::VertexBuffer* vertexBuffer) { + mVertexBuffers.PushBack(vertexBuffer); + mAttributesChanged = true; } -void Geometry::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer) +const Vector& Geometry::GetVertexBuffers() const { - mVertexBuffers.PushBack( propertyBuffer ); - mAttributesChanged = true; + return mVertexBuffers; } -void Geometry::SetIndexBuffer( Dali::Vector& indices ) +void Geometry::SetIndexBuffer(Uint16ContainerType& indices) { - mIndices.Swap( indices ); + mIndices.Swap(indices); mIndicesChanged = true; + mIndexType = Graphics::Format::R16_UINT; } -const Dali::Vector* Geometry::GetIndexBuffer() const +void Geometry::SetIndexBuffer(Uint32ContainerType& indices) { - return &mIndices; + // mIndices type is not matched with indices. Copy memory hardly. + mIndices.ResizeUninitialized(indices.Count() * 2); + memcpy(mIndices.Begin(), indices.Begin(), indices.Count() * sizeof(uint32_t)); + mIndicesChanged = true; + mIndexType = Graphics::Format::R32_UINT; } -void Geometry::RemovePropertyBuffer( const Render::PropertyBuffer* propertyBuffer ) +void Geometry::RemoveVertexBuffer(const Render::VertexBuffer* vertexBuffer) { - size_t bufferCount = mVertexBuffers.Size(); - for( size_t i(0); i& attributeLocation, Program& program, BufferIndex bufferIndex ) const -{ - attributeLocation.Clear(); - - for( size_t i(0); i< mVertexBuffers.Size(); ++i ) - { - unsigned int attributeCount = mVertexBuffers[i]->GetAttributeCount(); - for( unsigned int j = 0; j < attributeCount; ++j ) - { - const std::string& attributeName = mVertexBuffers[i]->GetAttributeName( j ); - unsigned int index = program.RegisterCustomAttribute( attributeName ); - GLint location = program.GetCustomAttributeLocation( index ); - - if( -1 == location ) - { - DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() ); - } - - attributeLocation.PushBack( location ); - } - } -} - void Geometry::OnRenderFinished() { - mHasBeenUpdated = false; + mHasBeenUpdated = false; mAttributesChanged = false; } -void Geometry::UploadAndDraw( - Context& context, - BufferIndex bufferIndex, - Vector& attributeLocation, - size_t elementBufferOffset, - size_t elementBufferCount ) +void Geometry::Upload(Graphics::Controller& graphicsController) { - if( !mHasBeenUpdated ) + if(!mHasBeenUpdated) { // Update buffers - if( mIndicesChanged ) + if(mIndicesChanged) { - if( mIndices.Empty() ) + if(mIndices.Empty()) { - mIndexBuffer = NULL; + mIndexBuffer = nullptr; } else { - if ( mIndexBuffer == NULL ) + if(mIndexBuffer == nullptr) { - mIndexBuffer = new GpuBuffer( context ); + mIndexBuffer = new GpuBuffer(graphicsController, 0 | Graphics::BufferUsage::INDEX_BUFFER); } - std::size_t bufferSize = sizeof( unsigned short ) * mIndices.Size(); - mIndexBuffer->UpdateDataBuffer( bufferSize, &mIndices[0], GpuBuffer::STATIC_DRAW, GpuBuffer::ELEMENT_ARRAY_BUFFER ); + uint32_t bufferSize = static_cast(sizeof(uint16_t) * mIndices.Size()); + mIndexBuffer->UpdateDataBuffer(graphicsController, bufferSize, &mIndices[0]); } mIndicesChanged = false; } - size_t count = mVertexBuffers.Count(); - for( unsigned int i = 0; i < count; ++i ) + for(auto&& buffer : mVertexBuffers) { - - if( !mVertexBuffers[i]->Update( context ) ) + if(!buffer->Update(graphicsController)) { //Vertex buffer is not ready ( Size, data or format has not been specified yet ) return; @@ -163,104 +147,141 @@ void Geometry::UploadAndDraw( mHasBeenUpdated = true; } +} +bool Geometry::BindVertexAttributes(Graphics::CommandBuffer& commandBuffer) +{ //Bind buffers to attribute locations - unsigned int base = 0u; - size_t vertexBufferCount(mVertexBuffers.Count()); - for( unsigned int i = 0; i < vertexBufferCount; ++i ) + const auto vertexBufferCount = static_cast(mVertexBuffers.Count()); + + std::vector buffers; + std::vector offsets; + + for(uint32_t i = 0; i < vertexBufferCount; ++i) { - mVertexBuffers[i]->BindBuffer( GpuBuffer::ARRAY_BUFFER ); - base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base ); + const GpuBuffer* gpuBuffer = mVertexBuffers[i]->GetGpuBuffer(); + if(gpuBuffer) + { + const Graphics::Buffer* buffer = gpuBuffer->GetGraphicsObject(); + + if(buffer) + { + buffers.push_back(buffer); + offsets.push_back(0u); + } + } + //@todo Figure out why this is being drawn without geometry having been uploaded } + if(buffers.empty()) + { + return false; + } + + commandBuffer.BindVertexBuffers(0, buffers, offsets); - size_t numIndices(0u); + return true; +} + +bool Geometry::Draw( + Graphics::Controller& graphicsController, + Graphics::CommandBuffer& commandBuffer, + uint32_t elementBufferOffset, + uint32_t elementBufferCount) +{ + uint32_t numIndices(0u); intptr_t firstIndexOffset(0u); - if( mIndexBuffer ) + if(mIndexBuffer) { - numIndices = mIndices.Size(); + std::size_t sizeOfIndex = GetSizeOfIndexFromIndexType(mIndexType); - if( elementBufferOffset != 0u ) + numIndices = static_cast(mIndices.Size() * sizeof(uint16_t) / sizeOfIndex); + + if(elementBufferOffset != 0u) { - elementBufferOffset = elementBufferOffset >= numIndices ? numIndices - 1 : elementBufferOffset; - firstIndexOffset = elementBufferOffset * sizeof(GLushort); + elementBufferOffset = (elementBufferOffset >= numIndices) ? numIndices - 1 : elementBufferOffset; + firstIndexOffset = intptr_t(elementBufferOffset * sizeOfIndex); numIndices -= elementBufferOffset; } - if( elementBufferCount != 0u ) + if(elementBufferCount != 0u) + { + numIndices = std::min(elementBufferCount, numIndices); + } + } + + //Draw call + if(mIndexBuffer && mGeometryType != Dali::Geometry::POINTS) + { + //Indexed draw call + const Graphics::Buffer* ibo = mIndexBuffer->GetGraphicsObject(); + if(ibo) + { + commandBuffer.BindIndexBuffer(*ibo, 0, mIndexType); + } + + commandBuffer.DrawIndexed(numIndices, 1, firstIndexOffset, 0, 0); + } + else + { + // Un-indexed draw call + uint32_t numVertices(0u); + + if(mVertexBuffers.Count() > 0) { - numIndices = std::min( elementBufferCount, numIndices ); + // truncated, no value loss happening in practice + numVertices = static_cast(mVertexBuffers[0]->GetElementCount()); } + + commandBuffer.Draw(numVertices, 1, 0, 0); } + return true; +} + +Graphics::PrimitiveTopology Geometry::GetTopology() const +{ + Graphics::PrimitiveTopology topology = Graphics::PrimitiveTopology::TRIANGLE_LIST; - GLenum geometryGLType(GL_NONE); switch(mGeometryType) { case Dali::Geometry::TRIANGLES: { - geometryGLType = GL_TRIANGLES; + topology = Graphics::PrimitiveTopology::TRIANGLE_LIST; break; } case Dali::Geometry::LINES: { - geometryGLType = GL_LINES; + topology = Graphics::PrimitiveTopology::LINE_LIST; break; } case Dali::Geometry::POINTS: { - geometryGLType = GL_POINTS; + topology = Graphics::PrimitiveTopology::POINT_LIST; break; } case Dali::Geometry::TRIANGLE_STRIP: { - geometryGLType = GL_TRIANGLE_STRIP; + topology = Graphics::PrimitiveTopology::TRIANGLE_STRIP; break; } case Dali::Geometry::TRIANGLE_FAN: { - geometryGLType = GL_TRIANGLE_FAN; + topology = Graphics::PrimitiveTopology::TRIANGLE_FAN; break; } case Dali::Geometry::LINE_LOOP: { - geometryGLType = GL_LINE_LOOP; + topology = Graphics::PrimitiveTopology::LINE_LOOP; break; } case Dali::Geometry::LINE_STRIP: { - geometryGLType = GL_LINE_STRIP; + topology = Graphics::PrimitiveTopology::LINE_STRIP; break; } } - - //Draw call - if( mIndexBuffer && geometryGLType != GL_POINTS ) - { - //Indexed draw call - mIndexBuffer->Bind( GpuBuffer::ELEMENT_ARRAY_BUFFER ); - context.DrawElements(geometryGLType, numIndices, GL_UNSIGNED_SHORT, reinterpret_cast(firstIndexOffset)); - } - else - { - //Unindex draw call - unsigned int numVertices(0u); - if( vertexBufferCount > 0 ) - { - numVertices = mVertexBuffers[0]->GetElementCount(); - } - - context.DrawArrays( geometryGLType, 0, numVertices ); - } - - //Disable attributes - for( unsigned int i = 0; i < attributeLocation.Count(); ++i ) - { - if( attributeLocation[i] != -1 ) - { - context.DisableVertexAttributeArray( attributeLocation[i] ); - } - } + return topology; } -} // namespace SceneGraph +} // namespace Render } // namespace Internal } // namespace Dali