<commit_before>// Copyright 2015-2019 <NAME>. All rights reserved.

#include "core/Setup.h"

#if OUZEL_COMPILE_OPENGL

#include "OGLBuffer.hpp"
#include "OGLRenderDevice.hpp"

namespace ouzel
{
    namespace graphics
    {
        OGLBuffer::OGLBuffer(OGLRenderDevice& renderDeviceOGL,
                             Buffer::Usage newUsage, uint32_t newFlags,
                             const std::vector<uint8_t>& newData,
                             uint32_t newSize):
            OGLRenderResource(renderDeviceOGL),
            usage(newUsage),
            flags(newFlags),
            data(newData)
        {
            createBuffer();

            size = static_cast<GLsizeiptr>(newSize);

            if (size > 0)
            {
                renderDeviceOGL.bindBuffer(bufferType, bufferId);

                if (data.empty())
                    renderDevice.glBufferDataProc(bufferType, size, nullptr,
                                                  (flags & Texture::DYNAMIC) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
                else
                    renderDevice.glBufferDataProc(bufferType, size, data.data(),
                                                  (flags & Texture::DYNAMIC) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);

                GLenum error;

                if ((error = renderDevice.glGetErrorProc()) != GL_NO_ERROR)
                    throw std::system_error(makeErrorCode(error), "Failed to create buffer");
            }
        }

        OGLBuffer::~OGLBuffer()
        {
            if (bufferId)
                renderDevice.deleteBuffer(bufferId);
        }

        void OGLBuffer::reload()
        {
            bufferId = 0;

            createBuffer();

            if (size > 0)
            {
                renderDevice.bindBuffer(bufferType, bufferId);

                if (data.empty())
                    renderDevice.glBufferDataProc(bufferType, size, nullptr,
                                                  (flags & Texture::DYNAMIC) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
                else
                    renderDevice.glBufferDataProc(bufferType, size, data.data(),
                                                  (flags & Texture::DYNAMIC) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);

                GLenum error;

                if ((error = renderDevice.glGetErrorProc()) != GL_NO_ERROR)
                    throw std::system_error(makeErrorCode(error), "Failed to create buffer");
            }
        }

        void OGLBuffer::setData(const std::vector<uint8_t>& newData)
        {
            if (!(flags & Buffer::DYNAMIC))
                throw std::runtime_error("Buffer is not dynamic");

            if (newData.empty())
                throw std::invalid_argument("Data is empty");

            data = newData;

            if (!bufferId)
                throw std::runtime_error("Buffer not initialized");

            renderDevice.bindBuffer(bufferType, bufferId);

            if (static_cast<GLsizeiptr>(data.size()) > size)
            {
                size = static_cast<GLsizeiptr>(data.size());

                renderDevice.glBufferDataProc(bufferType, size, data.data(),
                                              (flags & Texture::DYNAMIC) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);

                GLenum error;

                if ((error = renderDevice.glGetErrorProc()) != GL_NO_ERROR)
                    throw std::system_error(makeErrorCode(error), "Failed to create buffer");
            }
            else
            {
                renderDevice.glBufferSubDataProc(bufferType, 0, static_cast<GLsizeiptr>(data.size()), data.data());

                GLenum error;

                if ((error = renderDevice.glGetErrorProc()) != GL_NO_ERROR)
                    throw std::system_error(makeErrorCode(error), "Failed to upload buffer");
            }
        }

        void OGLBuffer::createBuffer()<commit_msg>Always set OpenGL buffer data with GL_DYNAMIC_DRAW on update<commit_after>// Copyright 2015-2019 <NAME>. All rights reserved.

#include "core/Setup.h"

#if OUZEL_COMPILE_OPENGL

#include "OGLBuffer.hpp"
#include "OGLRenderDevice.hpp"

namespace ouzel
{
    namespace graphics
    {
        OGLBuffer::OGLBuffer(OGLRenderDevice& renderDeviceOGL,
                             Buffer::Usage newUsage, uint32_t newFlags,
                             const std::vector<uint8_t>& newData,
                             uint32_t newSize):
            OGLRenderResource(renderDeviceOGL),
            usage(newUsage),
            flags(newFlags),
            data(newData)
        {
            createBuffer();

            size = static_cast<GLsizeiptr>(newSize);

            if (size > 0)
            {
                renderDeviceOGL.bindBuffer(bufferType, bufferId);

                if (data.empty())
                    renderDevice.glBufferDataProc(bufferType, size, nullptr,
                                                  (flags & Texture::DYNAMIC) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
                else
                    renderDevice.glBufferDataProc(bufferType, size, data.data(),
                                                  (flags & Texture::DYNAMIC) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);

                GLenum error;

                if ((error = renderDevice.glGetErrorProc()) != GL_NO_ERROR)
                    throw std::system_error(makeErrorCode(error), "Failed to create buffer");
            }
        }

        OGLBuffer::~OGLBuffer()
        {
            if (bufferId)
                renderDevice.deleteBuffer(bufferId);
        }

        void OGLBuffer::reload()
        {
            bufferId = 0;

            createBuffer();

            if (size > 0)
            {
                renderDevice.bindBuffer(bufferType, bufferId);

                if (data.empty())
                    renderDevice.glBufferDataProc(bufferType, size, nullptr,
                                                  (flags & Texture::DYNAMIC) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
                else
                    renderDevice.glBufferDataProc(bufferType, size, data.data(),
                                                  (flags & Texture::DYNAMIC) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);

                GLenum error;

                if ((error = renderDevice.glGetErrorProc()) != GL_NO_ERROR)
                    throw std::system_error(makeErrorCode(error), "Failed to create buffer");
            }
        }

        void OGLBuffer::setData(const std::vector<uint8_t>& newData)
        {
            if (!(flags & Buffer::DYNAMIC))
                throw std::runtime_error("Buffer is not dynamic");

            if (newData.empty())
                throw std::invalid_argument("Data is empty");

            data = newData;

            if (!bufferId)
                throw std::runtime_error("Buffer not initialized");

            renderDevice.bindBuffer(bufferType, bufferId);

            if (static_cast<GLsizeiptr>(data.size()) > size)
            {
                size = static_cast<GLsizeiptr>(data.size());

                renderDevice.glBufferDataProc(bufferType, size, data.data(), GL_DYNAMIC_DRAW);

                GLenum error;

                if ((error = renderDevice.glGetErrorProc()) != GL_NO_ERROR)
                    throw std::system_error(makeErrorCode(error), "Failed to create buffer");
            }
            else
            {
                renderDevice.glBufferSubDataProc(bufferType, 0, static_cast<GLsizeiptr>(data.size()), data.data());

                GLenum error;

                if ((error = renderDevice.glGetErrorProc()) != GL_NO_ERROR)
                    throw std::system_error(makeErrorCode(error), "Failed to upload buffer");
            }
        }

        void OGLBuffer::createBuffer()
{'fraction_non_alphanumeric': 0.09708113668509707, 'fraction_numerical': 0.004629034332004629, 'mean_word_length': 1.549328089151098, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7570821', 'n_tokens_mistral': 2114, 'n_tokens_neox': 1963, 'n_words': 374}