2013-09-21 01:22:09 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Copyright 2013 Pixar
|
2013-09-21 01:22:09 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
|
|
// with the following modification; you may not use this file except in
|
|
|
|
// compliance with the Apache License and the following modification to it:
|
|
|
|
// Section 6. Trademarks. is deleted and replaced with:
|
2013-09-21 01:22:09 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// 6. Trademarks. This License does not grant permission to use the trade
|
|
|
|
// names, trademarks, service marks, or product names of the Licensor
|
|
|
|
// and its affiliates, except as required to comply with Section 4(c) of
|
|
|
|
// the License and to reproduce the content of the NOTICE file.
|
2013-09-21 01:22:09 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// You may obtain a copy of the Apache License at
|
2013-09-21 01:22:09 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-21 01:22:09 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the Apache License with the above modification is
|
|
|
|
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
// KIND, either express or implied. See the Apache License for the specific
|
|
|
|
// language governing permissions and limitations under the Apache License.
|
2013-09-21 01:22:09 +00:00
|
|
|
//
|
|
|
|
|
2015-05-07 20:56:48 +00:00
|
|
|
#include "glPtexMipmapTexture.h"
|
|
|
|
#include "ptexMipmapTextureLoader.h"
|
2013-09-21 01:22:09 +00:00
|
|
|
|
2015-05-07 20:56:48 +00:00
|
|
|
#include <osd/opengl.h>
|
2013-09-21 01:22:09 +00:00
|
|
|
|
|
|
|
#include <Ptexture.h>
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
GLPtexMipmapTexture::GLPtexMipmapTexture()
|
2013-10-08 02:05:19 +00:00
|
|
|
: _width(0), _height(0), _depth(0), _layout(0), _texels(0), _memoryUsage(0)
|
2013-09-21 01:22:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
GLPtexMipmapTexture::~GLPtexMipmapTexture()
|
2013-09-21 01:22:09 +00:00
|
|
|
{
|
|
|
|
if (glIsTexture(_layout))
|
|
|
|
glDeleteTextures(1, &_layout);
|
|
|
|
|
|
|
|
if (glIsTexture(_texels))
|
|
|
|
glDeleteTextures(1, &_texels);
|
|
|
|
}
|
|
|
|
|
2015-05-07 20:56:48 +00:00
|
|
|
/*static*/
|
|
|
|
const char *
|
|
|
|
GLPtexMipmapTexture::GetShaderSource()
|
|
|
|
{
|
|
|
|
static const char *ptexShaderSource =
|
|
|
|
#include "glslPtexCommon.gen.h"
|
|
|
|
;
|
|
|
|
return ptexShaderSource;
|
|
|
|
}
|
|
|
|
|
2013-09-21 01:22:09 +00:00
|
|
|
static GLuint
|
|
|
|
genTextureBuffer(GLenum format, GLsizeiptr size, GLvoid const * data)
|
|
|
|
{
|
|
|
|
GLuint buffer, result;
|
|
|
|
glGenBuffers(1, &buffer);
|
2014-04-12 01:03:36 +00:00
|
|
|
glGenTextures(1, & result);
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(GL_EXT_direct_state_access)
|
2014-04-12 04:54:35 +00:00
|
|
|
if (glNamedBufferDataEXT) {
|
|
|
|
glNamedBufferDataEXT(buffer, size, data, GL_STATIC_DRAW);
|
|
|
|
glTextureBufferEXT(result, GL_TEXTURE_BUFFER, format, buffer);
|
|
|
|
} else {
|
2014-04-12 01:03:36 +00:00
|
|
|
#else
|
2014-04-12 04:54:35 +00:00
|
|
|
{
|
|
|
|
#endif
|
|
|
|
glBindBuffer(GL_TEXTURE_BUFFER, buffer);
|
|
|
|
glBufferData(GL_TEXTURE_BUFFER, size, data, GL_STATIC_DRAW);
|
2013-09-21 01:22:09 +00:00
|
|
|
|
2014-04-12 04:54:35 +00:00
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, result);
|
|
|
|
glTexBuffer(GL_TEXTURE_BUFFER, format, buffer);
|
2013-09-21 01:22:09 +00:00
|
|
|
|
2014-04-12 04:54:35 +00:00
|
|
|
// need to reset texture binding before deleting the source buffer.
|
|
|
|
glBindTexture(GL_TEXTURE_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_TEXTURE_BUFFER, 0);
|
|
|
|
}
|
2014-04-12 01:03:36 +00:00
|
|
|
|
2013-09-21 01:22:09 +00:00
|
|
|
glDeleteBuffers(1, &buffer);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
GLPtexMipmapTexture *
|
|
|
|
GLPtexMipmapTexture::Create(PtexTexture * reader,
|
2013-10-08 02:05:19 +00:00
|
|
|
int maxLevels,
|
|
|
|
size_t targetMemory)
|
2013-09-21 01:22:09 +00:00
|
|
|
{
|
2014-09-05 22:07:46 +00:00
|
|
|
GLPtexMipmapTexture * result = NULL;
|
2013-09-21 01:22:09 +00:00
|
|
|
|
|
|
|
GLint maxNumPages = 0;
|
|
|
|
glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &maxNumPages);
|
|
|
|
|
|
|
|
// Read the ptexture data and pack the texels
|
2014-09-05 22:07:46 +00:00
|
|
|
PtexMipmapTextureLoader loader(reader,
|
|
|
|
maxNumPages,
|
|
|
|
maxLevels,
|
|
|
|
targetMemory);
|
2013-09-21 01:22:09 +00:00
|
|
|
|
|
|
|
// Setup GPU memory
|
|
|
|
int numFaces = loader.GetNumFaces();
|
|
|
|
|
|
|
|
GLuint layout = genTextureBuffer(GL_R16I,
|
2013-10-16 00:47:58 +00:00
|
|
|
numFaces * 6 * sizeof(GLshort),
|
2013-09-21 01:22:09 +00:00
|
|
|
loader.GetLayoutBuffer());
|
|
|
|
|
|
|
|
GLenum format, type;
|
|
|
|
switch (reader->dataType()) {
|
|
|
|
case Ptex::dt_uint16 : type = GL_UNSIGNED_SHORT; break;
|
|
|
|
case Ptex::dt_float : type = GL_FLOAT; break;
|
|
|
|
case Ptex::dt_half : type = GL_HALF_FLOAT; break;
|
|
|
|
default : type = GL_UNSIGNED_BYTE; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (reader->numChannels()) {
|
|
|
|
case 1 : format = GL_RED; break;
|
|
|
|
case 2 : format = GL_RG; break;
|
|
|
|
case 3 : format = GL_RGB; break;
|
|
|
|
case 4 : format = GL_RGBA; break;
|
|
|
|
default: format = GL_RED; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// actual texels texture array
|
|
|
|
GLuint texels;
|
|
|
|
glGenTextures(1, &texels);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, texels);
|
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0,
|
|
|
|
(type == GL_FLOAT) ? GL_RGBA32F : GL_RGBA,
|
|
|
|
loader.GetPageWidth(),
|
|
|
|
loader.GetPageHeight(),
|
|
|
|
loader.GetNumPages(),
|
|
|
|
0, format, type,
|
|
|
|
loader.GetTexelBuffer());
|
|
|
|
|
|
|
|
// loader.ClearBuffers();
|
|
|
|
|
|
|
|
// Return the Osd Ptexture object
|
2014-09-05 22:07:46 +00:00
|
|
|
result = new GLPtexMipmapTexture;
|
2013-09-21 01:22:09 +00:00
|
|
|
|
|
|
|
result->_width = loader.GetPageWidth();
|
|
|
|
result->_height = loader.GetPageHeight();
|
|
|
|
result->_depth = loader.GetNumPages();
|
|
|
|
|
|
|
|
result->_format = format;
|
|
|
|
|
|
|
|
result->_layout = layout;
|
|
|
|
result->_texels = texels;
|
2013-10-08 02:05:19 +00:00
|
|
|
result->_memoryUsage = loader.GetMemoryUsage();
|
2013-09-21 01:22:09 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|