| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "BsGLTextureView.h" |
| 4 | #include "BsGLTexture.h" |
| 5 | |
| 6 | namespace bs { namespace ct { |
| 7 | GLTextureView::GLTextureView(const GLTexture* texture, const TEXTURE_VIEW_DESC& desc) |
| 8 | :TextureView(desc) |
| 9 | { |
| 10 | const TextureProperties& props = texture->getProperties(); |
| 11 | |
| 12 | GLenum target; |
| 13 | switch (props.getTextureType()) |
| 14 | { |
| 15 | case TEX_TYPE_1D: |
| 16 | { |
| 17 | if (desc.numArraySlices <= 1) |
| 18 | target = GL_TEXTURE_1D; |
| 19 | else |
| 20 | target = GL_TEXTURE_1D_ARRAY; |
| 21 | } |
| 22 | break; |
| 23 | default: |
| 24 | case TEX_TYPE_2D: |
| 25 | { |
| 26 | if(props.getNumSamples() <= 1) |
| 27 | { |
| 28 | if (desc.numArraySlices <= 1) |
| 29 | target = GL_TEXTURE_2D; |
| 30 | else |
| 31 | target = GL_TEXTURE_2D_ARRAY; |
| 32 | } |
| 33 | else |
| 34 | { |
| 35 | if (desc.numArraySlices <= 1) |
| 36 | target = GL_TEXTURE_2D_MULTISAMPLE; |
| 37 | else |
| 38 | target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY; |
| 39 | } |
| 40 | } |
| 41 | break; |
| 42 | case TEX_TYPE_3D: |
| 43 | target = GL_TEXTURE_3D; |
| 44 | break; |
| 45 | case TEX_TYPE_CUBE_MAP: |
| 46 | { |
| 47 | if(desc.numArraySlices % 6 == 0) |
| 48 | { |
| 49 | if (desc.numArraySlices == 6) |
| 50 | target = GL_TEXTURE_CUBE_MAP; |
| 51 | else |
| 52 | target = GL_TEXTURE_CUBE_MAP_ARRAY; |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | if(desc.numArraySlices == 1) |
| 57 | target = GL_TEXTURE_2D; |
| 58 | else |
| 59 | target = GL_TEXTURE_2D_ARRAY; |
| 60 | } |
| 61 | } |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | #if BS_OPENGL_4_3 || BS_OPENGLES_3_1 |
| 66 | GLuint originalTexture = texture->getGLID(); |
| 67 | |
| 68 | glGenTextures(1, &mViewID); |
| 69 | BS_CHECK_GL_ERROR(); |
| 70 | |
| 71 | glTextureView( |
| 72 | mViewID, |
| 73 | target, |
| 74 | originalTexture, |
| 75 | texture->getGLFormat(), |
| 76 | desc.mostDetailMip, |
| 77 | desc.numMips, |
| 78 | desc.firstArraySlice, |
| 79 | desc.numArraySlices); |
| 80 | BS_CHECK_GL_ERROR(); |
| 81 | #endif |
| 82 | |
| 83 | mTarget = GLTexture::getGLTextureTarget(props.getTextureType(), props.getNumSamples(), desc.numArraySlices); |
| 84 | } |
| 85 | |
| 86 | GLTextureView::~GLTextureView() |
| 87 | { |
| 88 | if(mViewID != 0) |
| 89 | { |
| 90 | glDeleteTextures(1, &mViewID); |
| 91 | BS_CHECK_GL_ERROR(); |
| 92 | } |
| 93 | } |
| 94 | }} |