[Bf-blender-cvs] [173291bc6dd] KTX_support: building and running with ktx now works on windows. Loading/saving most likely still broken

Antonis Ryakiotakis noreply at git.blender.org
Mon Jan 24 20:28:26 CET 2022


Commit: 173291bc6dd83ee65687b4a7bc9e9a7b4d3114c8
Author: Antonis Ryakiotakis
Date:   Mon Jan 24 20:28:05 2022 +0100
Branches: KTX_support
https://developer.blender.org/rB173291bc6dd83ee65687b4a7bc9e9a7b4d3114c8

building and running with ktx now works on windows. Loading/saving most likely still broken

===================================================================

M	build_files/cmake/platform/platform_win32.cmake
M	source/blender/imbuf/CMakeLists.txt
M	source/blender/imbuf/intern/ktx.c
M	source/creator/CMakeLists.txt

===================================================================

diff --git a/build_files/cmake/platform/platform_win32.cmake b/build_files/cmake/platform/platform_win32.cmake
index 0439e2fa5c2..2a4e16e56d1 100644
--- a/build_files/cmake/platform/platform_win32.cmake
+++ b/build_files/cmake/platform/platform_win32.cmake
@@ -456,6 +456,12 @@ if(WITH_IMAGE_TIFF)
   endif()
 endif()
 
+if(WITH_IMAGE_KTX)
+  set(KTX_INCLUDE ${LIBDIR}/ktx2/include)
+  set(KTX_LIBRARY ${LIBDIR}/ktx2/lib/ktx.lib)
+  set(KTX_LIBRARY_DLL ${LIBDIR}/ktx2/lib/ktx.dll)
+endif()
+
 if(WITH_JACK)
   set(JACK_INCLUDE_DIRS
     ${LIBDIR}/jack/include/jack
diff --git a/source/blender/imbuf/CMakeLists.txt b/source/blender/imbuf/CMakeLists.txt
index 564bb4f3e45..a14e91179d6 100644
--- a/source/blender/imbuf/CMakeLists.txt
+++ b/source/blender/imbuf/CMakeLists.txt
@@ -193,9 +193,6 @@ if(WITH_IMAGE_KTX)
 	list(APPEND INC
 		${KTX_INCLUDE}
 	)
-	list(APPEND INC_SYS
-		${GLEW_INCLUDE_PATH}
-	)
 	list(APPEND SRC
 		intern/ktx.c
 	)
diff --git a/source/blender/imbuf/intern/ktx.c b/source/blender/imbuf/intern/ktx.c
index efac0fe666b..05e539ed992 100644
--- a/source/blender/imbuf/intern/ktx.c
+++ b/source/blender/imbuf/intern/ktx.c
@@ -29,9 +29,6 @@
  *  \ingroup imbuf
  */
 
-#define KTX_OPENGL 1
-#define KTX_USE_GETPROC 1
-
 #include "ktx.h"
 
 #include "IMB_imbuf.h"
@@ -45,131 +42,115 @@
 #include <string.h>
 #include <stdlib.h>
 
-#include "GL/glew.h"
-
 static char KTX_HEAD[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A};
 
 
 bool check_ktx(const unsigned char *mem, size_t size)
 {
-  return memcmp(KTX_HEAD, mem, sizeof(KTX_HEAD)) ? 0 : 1;
+  return memcmp(KTX_HEAD, mem, sizeof(KTX_HEAD)) == 0;
 }
 
 struct ImBuf *imb_loadktx(const unsigned char *mem, size_t size, int flags, char * UNUSED(colorspace))
 {
-	GLuint texture = 0;
-	GLenum target;
-	GLenum glerror;
-	GLboolean isMipmapped;
-	KTX_error_code ktxerror;
-	unsigned int numKeys;
-	unsigned char *keys;
-
-	/* thumbnails are run from a thread so opengl generation below will fail */
-	if (flags & IB_thumbnail) {
-		return NULL;
-	}
-
-	ktxerror = ktxLoadTextureM(mem, size, &texture, &target, NULL, &isMipmapped, &glerror, &numKeys, &keys);
-
-	if (ktxerror == KTX_SUCCESS) {
-		ImBuf *ibuf;
-		int xsize, ysize;
-		int internal_format;
-		bool flipx = false, flipy = false;
-		glEnable(target);
-
-		if (isMipmapped)
-			/* Enable bilinear mipmapping */
-			glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
-		else
-			glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &xsize);
-		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &ysize);
-
-		ibuf = IMB_allocImBuf(xsize, ysize, 32, (int)IB_rect);
-
-		glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
-		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &internal_format);
-
-		glDeleteTextures(1, &texture);
-
-		if (internal_format) {
-			flipx = flipy = true;
-		}
-		else {
-			if (keys) {
-				KTX_hash_table table;
-				ktxerror = ktxHashTable_Deserialize(numKeys, keys, &table);
-				if (ktxerror == KTX_SUCCESS) {
-					unsigned int valLength;
-					unsigned char *value;
-					ktxerror = ktxHashTable_FindValue(table, KTX_ORIENTATION_KEY, &valLength, (void **)&value);
-
-					if (ktxerror == KTX_SUCCESS) {
-						if (value[6] == 'd')
-							flipy = true;
-					}
-				}
-			}
-		}
-
-		if (flipx && flipy) {
-			int i;
-			size_t imbuf_size = ibuf->x * ibuf->y;
-
-			for (i = 0; i < imbuf_size / 2; i++) {
-				SWAP(unsigned int, ibuf->rect[i], ibuf->rect[imbuf_size - i -1]);
-			}
-		}
-		else if (flipy) {
-			size_t i, j;
-			for (j = 0; j < ibuf->y / 2; j++) {
-				for (i = 0; i < ibuf->x; i++) {
-					SWAP(unsigned int, ibuf->rect[i + j * ibuf->x], ibuf->rect[i + (ibuf->y - j - 1) * ibuf->x]);
-				}
-			}
-		}
-
-		if (keys)
-			free(keys);
-
-		return ibuf;
-	}
-
-	if (keys)
-		free(keys);
-
-	return NULL;
+  ktxTexture *tex;
+  KTX_error_code ktxerror = ktxTexture_CreateFromMemory(mem, size, 0, &tex);
+
+  if (ktxerror != KTX_SUCCESS) {
+    return NULL;
+  }
+
+  ktx_size_t offset;
+  ktxerror = ktxTexture_GetImageOffset(tex, 0, 0, 0, &offset);
+
+  if (ktxerror != KTX_SUCCESS) {
+    ktxTexture_Destroy(tex);
+    return NULL;
+  }
+
+  ktx_uint8_t *image = ktxTexture_GetData(tex) + offset;
+
+  ktx_uint32_t xsize = tex->baseWidth;
+  ktx_uint32_t ysize = tex->baseHeight;
+
+  ImBuf *ibuf = IMB_allocImBuf(xsize, ysize, 32, (int)IB_rect);
+
+  bool flipx = false, flipy = false;
+
+  for (ktx_uint32_t i = 0; i < xsize + ysize; ++i)
+    ibuf->rect[i] = image[i];
+
+  char *pValue;
+  uint32_t valueLen;
+  ktxerror = ktxHashList_FindValue(&tex->kvDataHead, KTX_ORIENTATION_KEY, &valueLen, (void **)&pValue);
+  if (ktxerror != KTX_SUCCESS) {
+    char cx, cy;
+    if (sscanf(pValue, KTX_ORIENTATION2_FMT, &cx, &cy) == 2) {
+      flipx = (cx == 'd');
+      flipy = (cy == 'd');
+    }
+  }
+
+  if (flipx && flipy) {
+    int i;
+    size_t imbuf_size = ibuf->x * ibuf->y;
+
+    for (i = 0; i < imbuf_size / 2; i++) {
+      SWAP(unsigned int, ibuf->rect[i], ibuf->rect[imbuf_size - i - 1]);
+    }
+  }
+  else if (flipy) {
+    size_t i, j;
+    for (j = 0; j < ibuf->y / 2; j++) {
+      for (i = 0; i < ibuf->x; i++) {
+        SWAP(unsigned int,
+             ibuf->rect[i + j * ibuf->x],
+             ibuf->rect[i + (ibuf->y - j - 1) * ibuf->x]);
+      }
+    }
+  }
+
+  ktxTexture_Destroy(tex);
+
+  return ibuf;
 }
 
 
 bool imb_savektx(struct ImBuf *ibuf, const char *name, int UNUSED(flags))
 {
-	KTX_texture_info tinfo;
-	KTX_image_info info;
-	KTX_error_code ktxerror;
-
-	tinfo.glType = GL_UNSIGNED_BYTE;
-	/* compression type, expose as option later */
-	tinfo.glTypeSize = 1;
-	tinfo.glFormat = GL_RGBA;
-	tinfo.glInternalFormat = GL_RGBA;
-	tinfo.glBaseInternalFormat = GL_RGBA;
-	tinfo.pixelWidth = ibuf->x;
- 	tinfo.pixelHeight = ibuf->y;
-	tinfo.pixelDepth = 0;
-	tinfo.numberOfArrayElements = 0;
-	tinfo.numberOfFaces = 1;
-	tinfo.numberOfMipmapLevels = 1;
-
-	info.data = (GLubyte *) ibuf->rect;
-	info.size = ((size_t)ibuf->x) * ibuf->y * 4 * sizeof(char);
-
-	ktxerror = ktxWriteKTXN(name, &tinfo, 0, NULL, 1, &info);
-
-	if (KTX_SUCCESS == ktxerror) {
-		return 1;
-	}
-	else return 0;
+  ktxTextureCreateInfo createInfo;
+  createInfo.glInternalformat = 0x8058; // GL_RGBA8
+  createInfo.baseWidth = ibuf->x;
+  createInfo.baseHeight = ibuf->y;
+  createInfo.baseDepth = 0;
+  createInfo.numDimensions = 2;
+  // Note: it is not necessary to provide a full mipmap pyramid.
+  createInfo.numLevels = 1;
+  createInfo.numLayers = 1;
+  createInfo.numFaces = 1;
+  createInfo.isArray = KTX_FALSE;
+  createInfo.generateMipmaps = KTX_FALSE;
+  KTX_error_code result;
+  ktxTexture2 *tex;
+  result = ktxTexture2_Create(&createInfo, KTX_TEXTURE_CREATE_ALLOC_STORAGE, &tex);
+  if (KTX_SUCCESS != result) {
+    return false;
+  }
+
+  ktxTexture *texture = ktxTexture(tex);
+
+  ktx_uint32_t level, layer, faceSlice;
+  level = 0;
+  layer = 0;
+  faceSlice = 0;
+  result = ktxTexture_SetImageFromMemory(
+      texture, level, layer, faceSlice, (ktx_uint8_t*)ibuf->rect, (size_t)ibuf->x * (size_t)ibuf->y * (size_t) 4);
+
+  if (KTX_SUCCESS != result) {
+    ktxTexture_Destroy(texture);
+    return false;
+  }
+  result = ktxTexture_WriteToNamedFile(texture, name);
+  ktxTexture_Destroy(texture);
+
+  return KTX_SUCCESS == result;
 }
diff --git a/source/creator/CMakeLists.txt b/source/creator/CMakeLists.txt
index 0609b8fd792..8425c872d28 100644
--- a/source/creator/CMakeLists.txt
+++ b/source/creator/CMakeLists.txt
@@ -730,6 +730,14 @@ elseif(WIN32)
     )
   endif()
 
+  if(WITH_IMAGE_KTX AND WIN32)
+    install(
+          FILES ${KTX_LIBRARY_DLL}
+          DESTINATION "."
+    )
+  endif()
+
+
   if(WITH_FFTW3)
     install(
           FILES ${LIBDIR}/fftw3/lib/libfftw3-3.dll



More information about the Bf-blender-cvs mailing list