Texture2DArray.cpp 3.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include "GPUdefs.h"
#include "Texture2DArray.h"
#include "FrameBuffer.h"
#include <iostream>




bool GPU::Texture2DArray::Allocate()
{
19
    gpuAssert( glGenTextures( 1, &m_Id ) );
20 21 22 23 24 25
    return m_Id != 0;
}


bool GPU::Texture2DArray::Unallocate()
{
26
    gpuAssert( glDeleteTextures( 1, &m_Id ) );
27 28 29 30 31 32 33 34 35
    m_Id = 0;
    return true;
}


bool GPU::Texture2DArray::Create()
{
    if( Instantiate() )
    {
36
        gpuAssert( glBindTexture( m_Target, m_Id ) );
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        m_Width = m_Height = m_Depth = 0;
        return true;
    }
    else
        return false;
}


bool GPU::Texture2DArray::Create( GLenum internalFormats[],
                                  GLsizei width,
                                  GLsizei height,
                                  GLsizei depth,
                                  GLsizei nMipMaps )
{
    if( !Instantiate() )
        return false;


55
    gpuAssert( glBindTexture( m_Target, m_Id ) );
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    int currentFormatId = -1;
    GLint isSupported;
    do
    {
        if( internalFormats[++currentFormatId] == GL_NONE )
        {
            m_Width = m_Height = m_Depth = 0;
            std::cout << "Texture3D error - specified internal format(s) not supported." << std::endl;
            for( int i=0; i<currentFormatId; ++i )
                std::cout << internalFormats[i] << "  ";
            std::cout << std::endl;
            Release();
            return false;
        }

        if( glGetInternalformativ )
            glGetInternalformativ( m_Target, internalFormats[currentFormatId], GL_INTERNALFORMAT_SUPPORTED, 1, &isSupported );
        else
            switch( internalFormats[currentFormatId] )
            {
                case GL_DEPTH_COMPONENT  :
                case GL_DEPTH_COMPONENT16:
                case GL_DEPTH_COMPONENT24:
                case GL_DEPTH_COMPONENT32:  isSupported = GL_TRUE;  break;
                default:
                {
                    glGetError();
                    glTexStorage3D(
                        m_Target,
                        nMipMaps,
                        internalFormats[currentFormatId],
                        width,
                        height,
                        depth
                    );
                    isSupported = glGetError() == GL_NO_ERROR;
                }
            }
    } while( !isSupported );

97
    gpuAssert( glTexStorage3D(
98 99 100 101 102 103
        m_Target,
        nMipMaps,
        internalFormats[currentFormatId],
        width,
        height,
        depth
104
    ) );
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

    m_Width  = width;
    m_Height = height;
    m_Depth  = depth;

    return true;
}


bool GPU::Texture2DArray::LoadSlice( GLsizei sliceDepth,
                                     GLenum format,
                                     GLenum type,
                                     const GLvoid* data,
                                     GLint level )
{
    if( m_Id )
    {
122 123
        gpuAssert( glBindTexture( m_Target, m_Id ) );
        gpuAssert( glTexSubImage3D(
124 125 126 127 128 129 130 131 132 133 134 135 136 137
            m_Target,
            level,

            0,
            0,
            sliceDepth,

            m_Width,
            m_Height,
            1,

            format,
            type,
            data
138
        ) );
139 140 141 142 143 144

        return true;
    }

    return false;
}