<commit_before>// Copyright (C) 2017 <NAME>
// This file is part of the Ouzel engine.

#include "TextureMetal.h"
#include "core/Engine.h"
#include "RendererMetal.h"
#include "math/MathUtils.h"
#include "utils/Log.h"

namespace ouzel
{
    namespace graphics
    {
        TextureMetal::TextureMetal()
        {
        }

        TextureMetal::~TextureMetal()
        {
            if (msaaTexture)
            {
                [msaaTexture release];
            }

            if (renderPassDescriptor)
            {
                [renderPassDescriptor release];
            }

            if (texture)
            {
                [texture release];
            }
        }

        bool TextureMetal::upload()
        {
            std::lock_guard<std::mutex> lock(uploadMutex);

            if (dirty)
            {
                RendererMetal* rendererMetal = static_cast<RendererMetal*>(sharedEngine->getRenderer());

                if (size.v[0] > 0 &&
                    size.v[1] > 0)
                {
                    if (!texture ||
                        static_cast<NSUInteger>(size.v[0]) != width ||
                        static_cast<NSUInteger>(size.v[1]) != height)
                    {
                        if (texture) [texture release];

                        width = static_cast<NSUInteger>(size.v[0]);
                        height = static_cast<NSUInteger>(size.v[1]);

                        if (width > 0 && height > 0)
                        {
                            MTLTextureDescriptor* textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
                                                                                                                         width:width
                                                                                                                        height:height
                                                                                                                     mipmapped:mipmaps ? YES : NO];
                            textureDescriptor.textureType = MTLTextureType2D;
                            textureDescriptor.usage = MTLTextureUsageShaderRead | (renderTarget ? MTLTextureUsageRenderTarget : 0);
                            colorFormat = textureDescriptor.pixelFormat;

                            texture = [rendererMetal->getDevice() newTextureWithDescriptor:textureDescriptor];

                            if (!texture)
                            {
                                Log(Log::Level::ERR) << "Failed to create Metal texture";
                                return false;
                            }
                        }

                        if (renderTarget)
                        {
                            colorBufferLoadAction = clearColorBuffer ? MTLLoadActionClear : MTLLoadActionDontCare;
                            depthBufferLoadAction = clearDepthBuffer ? MTLLoadActionClear : MTLLoadActionDontCare;

                            if (!renderPassDescriptor)
                            {
                                renderPassDescriptor = [[MTLRenderPassDescriptor renderPassDescriptor] retain];

                                if (!renderPassDescriptor)
                                {
                                    Log(Log::Level::ERR) << "Failed to create Metal render pass descriptor";
                                    return false;
                                }
                            }<commit_msg>Use mipMapsGenerated attribute<commit_after>// Copyright (C) 2017 <NAME>
// This file is part of the Ouzel engine.

#include "TextureMetal.h"
#include "core/Engine.h"
#include "RendererMetal.h"
#include "math/MathUtils.h"
#include "utils/Log.h"

namespace ouzel
{
    namespace graphics
    {
        TextureMetal::TextureMetal()
        {
        }

        TextureMetal::~TextureMetal()
        {
            if (msaaTexture)
            {
                [msaaTexture release];
            }

            if (renderPassDescriptor)
            {
                [renderPassDescriptor release];
            }

            if (texture)
            {
                [texture release];
            }
        }

        bool TextureMetal::upload()
        {
            std::lock_guard<std::mutex> lock(uploadMutex);

            if (dirty)
            {
                RendererMetal* rendererMetal = static_cast<RendererMetal*>(sharedEngine->getRenderer());

                if (size.v[0] > 0 &&
                    size.v[1] > 0)
                {
                    if (!texture ||
                        static_cast<NSUInteger>(size.v[0]) != width ||
                        static_cast<NSUInteger>(size.v[1]) != height)
                    {
                        if (texture) [texture release];

                        width = static_cast<NSUInteger>(size.v[0]);
                        height = static_cast<NSUInteger>(size.v[1]);

                        if (width > 0 && height > 0)
                        {
                            MTLTextureDescriptor* textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
                                                                                                                         width:width
                                                                                                                        height:height
                                                                                                                     mipmapped:mipMapsGenerated ? YES : NO];
                            textureDescriptor.textureType = MTLTextureType2D;
                            textureDescriptor.usage = MTLTextureUsageShaderRead | (renderTarget ? MTLTextureUsageRenderTarget : 0);
                            colorFormat = textureDescriptor.pixelFormat;

                            texture = [rendererMetal->getDevice() newTextureWithDescriptor:textureDescriptor];

                            if (!texture)
                            {
                                Log(Log::Level::ERR) << "Failed to create Metal texture";
                                return false;
                            }
                        }

                        if (renderTarget)
                        {
                            colorBufferLoadAction = clearColorBuffer ? MTLLoadActionClear : MTLLoadActionDontCare;
                            depthBufferLoadAction = clearDepthBuffer ? MTLLoadActionClear : MTLLoadActionDontCare;

                            if (!renderPassDescriptor)
                            {
                                renderPassDescriptor = [[MTLRenderPassDescriptor renderPassDescriptor] retain];

                                if (!renderPassDescriptor)
                                {
                                    Log(Log::Level::ERR) << "Failed to create Metal render pass descriptor";
                                    return false;
                                }
                            }
{'fraction_non_alphanumeric': 0.07307476110174255, 'fraction_numerical': 0.00505902192242833, 'mean_word_length': 0.9461307082307903, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4652308', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1427, 'n_words': 268}