Ptex fixes:

- fix build compiling & linking to accomodate recent code churn in Ptex
- fix FindPTex.cmake module to correctly extract version number
- fix dxPtexViewer & glPtexViewer source to compile with new Ptex namespace changes
- add alpha channel padding function to ptexMipmapLoader as a workaround to the absence of 3-channel DXGI formats
- mirror ptex memory limit function from glPtexViewer to dxPtexViewer
This commit is contained in:
manuelk 2016-02-19 16:58:56 -08:00
parent a6fe9261ae
commit 4926fd2c3e
12 changed files with 102 additions and 30 deletions

View File

@ -345,6 +345,7 @@ if(NOT NO_OPENGL AND NOT ANDROID AND NOT IOS)
endif()
if(NOT NO_PTEX)
find_package(PTex 2.0)
find_package(Zlib 1.2)
endif()
if (OPENGL_FOUND AND NOT IOS)
add_definitions(
@ -520,6 +521,7 @@ endif()
if(PTEX_FOUND)
add_definitions(
-DOPENSUBDIV_HAS_PTEX
-DPTEX_STATIC
)
else()
if(NOT NO_PTEX)

View File

@ -101,15 +101,21 @@ else ()
DOC "The Ptex library")
endif ()
if (PTEX_INCLUDE_DIR AND EXISTS "${PTEX_INCLUDE_DIR}/Ptexture.h" )
if (PTEX_INCLUDE_DIR AND EXISTS "${PTEX_INCLUDE_DIR}/PtexVersion.h")
set (PTEX_VERSION_FILE "${PTEX_INCLUDE_DIR}/PtexVersion.h")
elseif (PTEX_INCLUDE_DIR AND EXISTS "${PTEX_INCLUDE_DIR}/Ptexture.h")
set (PTEX_VERSION_FILE "${PTEX_INCLUDE_DIR}/Ptexture.h")
endif()
file(STRINGS "${PTEX_INCLUDE_DIR}/Ptexture.h" TMP REGEX "^#define PtexAPIVersion.*$")
if (PTEX_VERSION_FILE)
file(STRINGS "${PTEX_VERSION_FILE}" TMP REGEX "^#define PtexAPIVersion.*$")
string(REGEX MATCHALL "[0-9]+" API ${TMP})
file(STRINGS "${PTEX_INCLUDE_DIR}/Ptexture.h" TMP REGEX "^#define PtexFileMajorVersion.*$")
file(STRINGS "${PTEX_VERSION_FILE}" TMP REGEX "^#define PtexFileMajorVersion.*$")
string(REGEX MATCHALL "[0-9]+" MAJOR ${TMP})
file(STRINGS "${PTEX_INCLUDE_DIR}/Ptexture.h" TMP REGEX "^#define PtexFileMinorVersion.*$")
file(STRINGS "${PTEX_VERSION_FILE}" TMP REGEX "^#define PtexFileMinorVersion.*$")
string(REGEX MATCHALL "[0-9]+" MINOR ${TMP})
set(PTEX_VERSION ${API}.${MAJOR}.${MINOR})

View File

@ -39,7 +39,7 @@ if (NOT NO_OPENGL)
add_subdirectory(glPaintTest)
endif()
if (PTEX_FOUND)
if (PTEX_FOUND AND ZLIB_FOUND)
add_subdirectory(glPtexViewer)
endif()
@ -73,7 +73,7 @@ if (DXSDK_FOUND AND NOT NO_DX)
add_subdirectory(dxViewer)
if (PTEX_FOUND)
if (PTEX_FOUND AND ZLIB_FOUND)
add_subdirectory(dxPtexViewer)
endif()

View File

@ -26,7 +26,6 @@
#include "ptexMipmapTextureLoader.h"
#include <far/error.h> // XXX: to be replaced
#include <Ptexture.h>
#include <D3D11.h>
#include <cassert>
@ -96,16 +95,25 @@ genTextureBuffer(ID3D11DeviceContext *deviceContext, int size, void const * data
D3D11PtexMipmapTexture *
D3D11PtexMipmapTexture::Create(ID3D11DeviceContext *deviceContext,
PtexTexture * reader,
int maxLevels) {
int maxLevels,
size_t targetMemory) {
D3D11PtexMipmapTexture * result = NULL;
int maxNumPages = D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION;
// Read the ptex data and pack the texels
PtexMipmapTextureLoader loader(reader, maxNumPages, maxLevels);
bool padAlpha = reader->numChannels()==3 and reader->dataType()!=Ptex::dt_float;
int numFaces = loader.GetNumFaces();
PtexMipmapTextureLoader loader(reader,
maxNumPages,
maxLevels,
targetMemory,
true, // seamlessMipmap
padAlpha);
int numChannels = reader->numChannels() + padAlpha,
numFaces = loader.GetNumFaces();
ID3D11Buffer *layout = genTextureBuffer(deviceContext,
numFaces * 6 * sizeof(short),
@ -114,7 +122,6 @@ D3D11PtexMipmapTexture::Create(ID3D11DeviceContext *deviceContext,
DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
int bpp = 0;
int numChannels = reader->numChannels();
switch (reader->dataType()) {
case Ptex::dt_uint16:
switch (numChannels) {
@ -138,7 +145,7 @@ D3D11PtexMipmapTexture::Create(ID3D11DeviceContext *deviceContext,
switch (numChannels) {
case 1: format = DXGI_FORMAT_R16_FLOAT; break;
case 2: format = DXGI_FORMAT_R16G16_FLOAT; break;
case 3:assert(false); break;
case 3: assert(false); break;
case 4: format = DXGI_FORMAT_R16G16B16A16_FLOAT; break;
}
bpp = numChannels * 2;

View File

@ -27,7 +27,8 @@
#include <osd/nonCopyable.h>
class PtexTexture;
#include <Ptexture.h>
struct ID3D11Buffer;
struct ID3D11Texture2D;
struct ID3D11DeviceContext;
@ -37,7 +38,8 @@ class D3D11PtexMipmapTexture : OpenSubdiv::Osd::NonCopyable<D3D11PtexMipmapTextu
public:
static D3D11PtexMipmapTexture * Create(ID3D11DeviceContext *deviceContext,
PtexTexture * reader,
int maxLevels=10);
int maxLevels=10,
size_t targetMemory=0);
/// Returns GLSL shader snippet to fetch ptex
static const char *GetShaderSource();

View File

@ -27,8 +27,6 @@
#include <osd/opengl.h>
#include <Ptexture.h>
GLPtexMipmapTexture::GLPtexMipmapTexture()
: _width(0), _height(0), _depth(0), _layout(0), _texels(0), _memoryUsage(0)
{

View File

@ -28,10 +28,9 @@
#include <osd/nonCopyable.h>
#include <osd/opengl.h>
#include <Ptexture.h>
#include <stdlib.h>
class PtexTexture;
class GLPtexMipmapTexture : OpenSubdiv::Osd::NonCopyable<GLPtexMipmapTexture> {
public:
static GLPtexMipmapTexture * Create(PtexTexture * reader,

View File

@ -24,7 +24,6 @@
#include "ptexMipmapTextureLoader.h"
#include <Ptexture.h>
#include <vector>
#include <list>
#include <algorithm>
@ -447,10 +446,11 @@ PtexMipmapTextureLoader::PtexMipmapTextureLoader(PtexTexture *ptex,
int maxNumPages,
int maxLevels,
size_t targetMemory,
bool seamlessMipmap) :
bool seamlessMipmap,
bool padAlpha) :
_ptex(ptex), _maxLevels(maxLevels), _bpp(0),
_pageWidth(0), _pageHeight(0), _texelBuffer(NULL), _layoutBuffer(NULL),
_memoryUsage(0)
_pageWidth(0), _pageHeight(0), _padAlpha(padAlpha),
_texelBuffer(NULL), _layoutBuffer(NULL), _memoryUsage(0)
{
// byte per pixel
_bpp = ptex->numChannels() * Ptex::DataSize(ptex->dataType());
@ -474,6 +474,10 @@ PtexMipmapTextureLoader::PtexMipmapTextureLoader(PtexTexture *ptex,
optimizePacking(maxNumPages, targetMemory);
generateBuffers();
if (padAlpha) {
addAlphaChannel();
}
}
PtexMipmapTextureLoader::~PtexMipmapTextureLoader()
@ -485,6 +489,48 @@ PtexMipmapTextureLoader::~PtexMipmapTextureLoader()
delete _layoutBuffer;
}
// add alpha channel to texelbuffer :
// assumes the texel buffer has been generated and apply a raw copy
// into a larger buffer with an alpha channel padded in
// note : this is not a particularly elegant solution...
void
PtexMipmapTextureLoader::addAlphaChannel() {
assert(_ptex);
// allocate new larger texel buffer
int bpc = Ptex::DataSize(_ptex->dataType()),
srcStride = _ptex->numChannels() * bpc,
dstStride = srcStride + bpc;
size_t numTexels = _pageWidth * _pageHeight * _pages.size(),
memoryUsed = dstStride * numTexels;
unsigned char * texBuffer = new unsigned char[memoryUsed];
// loop over every texel & copy + pad
unsigned char const * src = _texelBuffer;
unsigned char * dest = texBuffer;
for (int i=0; i<numTexels; ++i, src+=srcStride, dest+=dstStride) {
memcpy(dest, src, srcStride);
/// set alpha to 1
switch (_ptex->dataType()) {
case Ptex::dt_uint8 : *(uint8_t *)(dest+srcStride)= 0xFF; break;
case Ptex::dt_uint16 : *(uint16_t *)(dest+srcStride) = 0xFFFF; break;
case Ptex::dt_half : *(uint16_t *)(dest+srcStride) = 0x3C00; break;
case Ptex::dt_float : *(float *)(dest+srcStride) = 1.0f; break;
}
}
// remove old buffer & adjust class members
delete [] _texelBuffer;
_texelBuffer = texBuffer;
_memoryUsage = memoryUsed;
_bpp = dstStride;
}
// resample border texels for guttering
//
int

View File

@ -25,11 +25,11 @@
#ifndef OPENSUBDIV_EXAMPLES_PTEX_MIPMAP_TEXTURE_LOADER_H
#define OPENSUBDIV_EXAMPLES_PTEX_MIPMAP_TEXTURE_LOADER_H
#include <Ptexture.h>
#include <stdlib.h>
#include <stdint.h>
#include <vector>
class PtexTexture;
class PtexMipmapTextureLoader {
public:
@ -37,7 +37,8 @@ public:
int maxNumPages,
int maxLevels = -1,
size_t targetMemory = 0,
bool seamlessMipmap = true);
bool seamlessMipmap = true,
bool padAlpha = false);
~PtexMipmapTextureLoader();
@ -136,6 +137,7 @@ private:
int resampleBorder(int face, int edgeId, unsigned char *result,
int dstLength, int bpp,
float srcStart = 0.0f, float srcEnd = 1.0f);
void addAlphaChannel();
std::vector<Block> _blocks;
std::vector<Page *> _pages;
@ -145,6 +147,8 @@ private:
int _bpp;
int _pageWidth, _pageHeight;
bool _padAlpha;
unsigned char *_texelBuffer;
unsigned char *_layoutBuffer;

View File

@ -32,6 +32,7 @@ set(PLATFORM_LIBRARIES
"${OSD_LINK_TARGET}"
"${DXSDK_LIBRARIES}"
"${PTEX_LIBRARY}"
"${ZLIB_LIBRARY}"
)
include_directories(

View File

@ -644,7 +644,7 @@ ShaderCache g_shaderCache;
//------------------------------------------------------------------------------
D3D11PtexMipmapTexture *
createPtex(const char *filename) {
createPtex(const char *filename, int memLimit) {
Ptex::String ptexError;
printf("Loading ptex : %s\n", filename);
@ -663,8 +663,11 @@ createPtex(const char *filename) {
printf("Error in reading %s\n", filename);
exit(1);
}
size_t targetMemory = memLimit * 1024 * 1024; // MB
D3D11PtexMipmapTexture *osdPtex = D3D11PtexMipmapTexture::Create(
g_pd3dDeviceContext, ptex, g_maxMipmapLevels);
g_pd3dDeviceContext, ptex, g_maxMipmapLevels, targetMemory);
ptex->release();
@ -1735,6 +1738,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmd
const char *diffuseEnvironmentMap = NULL, *specularEnvironmentMap = NULL;
const char *colorFilename = NULL, *displacementFilename = NULL,
*occlusionFilename = NULL, *specularFilename = NULL;
int memLimit;
for (int i = 0; i < (int)argv.size(); ++i) {
if (strstr(argv[i].c_str(), ".obj"))
@ -1751,6 +1755,8 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmd
g_yup = true;
else if (argv[i] == "-m")
g_maxMipmapLevels = atoi(argv[++i].c_str());
else if (argv[i] == "-x")
memLimit = atoi(argv[++i].c_str());
else if (argv[i] == "--disp")
g_displacementScale = (float)atof(argv[++i].c_str());
else if (colorFilename == NULL)
@ -1781,13 +1787,13 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmd
createOsdMesh(g_level, g_kernel);
// load ptex files
g_osdPTexImage = createPtex(colorFilename);
g_osdPTexImage = createPtex(colorFilename, memLimit);
if (displacementFilename)
g_osdPTexDisplacement = createPtex(displacementFilename);
g_osdPTexDisplacement = createPtex(displacementFilename, memLimit);
if (occlusionFilename)
g_osdPTexOcclusion = createPtex(occlusionFilename);
g_osdPTexOcclusion = createPtex(occlusionFilename, memLimit);
if (specularFilename)
g_osdPTexSpecular = createPtex(specularFilename);
g_osdPTexSpecular = createPtex(specularFilename, memLimit);
initHUD();

View File

@ -35,6 +35,7 @@ list(APPEND PLATFORM_LIBRARIES
"${OPENGL_LIBRARY}"
"${GLFW_LIBRARIES}"
"${PTEX_LIBRARY}"
"${ZLIB_LIBRARY}"
)
include_directories(