瀏覽代碼

remove some more code duplication, fix a string, and remove a potential bug

jp9000 12 年之前
父節點
當前提交
f9c2aadf53

+ 5 - 19
libobs-d3d11/GS_D3D11Texture2D.cpp

@@ -18,29 +18,15 @@
 #include "util/base.h"
 #include "GS_D3D11SubSystem.hpp"
 
-static inline uint32_t numActualLevels(uint32_t levels, uint32_t width,
-		uint32_t height)
-{
-	if (levels > 0)
-		return levels;
-
-	uint32_t size = max(width, height);
-	uint32_t numLevels = 0;
-
-	while (size  > 1) {
-		size /= 2;
-		numLevels++;
-	}
-
-	return numLevels;
-}
-
 void gs_texture_2d::InitSRD(vector<D3D11_SUBRESOURCE_DATA> &srd, void **data)
 {
-	uint32_t rowSizeBytes  = width  * get_format_bpp(format);
+	uint32_t rowSizeBytes  = width  * gs_get_format_bpp(format);
 	uint32_t texSizeBytes  = height * rowSizeBytes / 8;
 	size_t   textures      = type == GS_TEXTURE_2D ? 1 : 6;
-	uint32_t actual_levels = numActualLevels(levels, width, height);
+	uint32_t actual_levels = levels;
+	
+	if (!actual_levels)
+		actual_levels = gs_num_total_levels(width, height);
 
 	rowSizeBytes /= 8;
 

+ 3 - 0
libobs-opengl/gl-helpers.c

@@ -43,6 +43,9 @@ bool upload_face(GLenum type, uint32_t num_levels,
 		size   /= 4;
 		width  /= 2;
 		height /= 2;
+
+		if (width  == 0) width  = 1;
+		if (height == 0) height = 1;
 	}
 
 	*p_data = data;

+ 9 - 24
libobs-opengl/gl-texture2d.c

@@ -17,33 +17,17 @@
 
 #include "gl-subsystem.h"
 
-static uint32_t num_actual_levels(struct gs_texture_2d *tex)
-{
-	uint32_t num_levels;
-	uint32_t size;
-
-	if (tex->base.levels > 0)
-		return tex->base.levels;
-
-	size = tex->width > tex->height ? tex->width : tex->height;
-	num_levels = 0;
-
-	while (size  > 1) {
-		size /= 2;
-		num_levels++;
-	}
-
-	return num_levels;
-}
-
 static bool upload_texture_2d(struct gs_texture_2d *tex, void **data)
 {
-	uint32_t row_size   = tex->width  * get_format_bpp(tex->base.format);
+	uint32_t row_size   = tex->width  * gs_get_format_bpp(tex->base.format);
 	uint32_t tex_size   = tex->height * row_size / 8;
-	uint32_t num_levels = num_actual_levels(tex);
-	bool     compressed = is_compressed_format(tex->base.format);
+	uint32_t num_levels = tex->base.levels;
+	bool     compressed = gs_is_compressed_format(tex->base.format);
 	bool     success;
 
+	if (!num_levels)
+		num_levels = gs_num_total_levels(tex->width, tex->height);
+
 	if (!gl_bind_texture(GL_TEXTURE_2D, tex->base.texture))
 		return false;
 
@@ -68,7 +52,8 @@ static bool create_pixel_unpack_buffer(struct gs_texture_2d *tex)
 	if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex->unpack_buffer))
 		return false;
 
-	size = tex->width * tex->height * get_format_bpp(tex->base.format) / 8;
+	size = tex->width * tex->height * gs_get_format_bpp(tex->base.format);
+	size /= 8;
 
 	glBufferData(GL_PIXEL_UNPACK_BUFFER, size, 0, GL_DYNAMIC_DRAW);
 	if (!gl_success("glBufferData"))
@@ -176,7 +161,7 @@ bool texture_map(texture_t tex, void **ptr, uint32_t *byte_width)
 
 	gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
 
-	*byte_width = tex2d->width * get_format_bpp(tex->format) / 8;
+	*byte_width = tex2d->width * gs_get_format_bpp(tex->format) / 8;
 	return true;
 
 fail:

+ 7 - 23
libobs-opengl/gl-texturecube.c

@@ -17,34 +17,18 @@
 
 #include "gl-subsystem.h"
 
-static inline uint32_t num_actual_levels(struct gs_texture_cube *tex)
-{
-	uint32_t num_levels;
-	uint32_t size;
-
-	if (tex->base.levels > 0)
-		return tex->base.levels;
-
-	size = tex->size;
-	num_levels = 0;
-
-	while (size  > 1) {
-		size /= 2;
-		num_levels++;
-	}
-
-	return num_levels;
-}
-
 static inline bool upload_texture_cube(struct gs_texture_cube *tex, void **data)
 {
-	uint32_t row_size   = tex->size * get_format_bpp(tex->base.format);
+	uint32_t row_size   = tex->size * gs_get_format_bpp(tex->base.format);
 	uint32_t tex_size   = tex->size * row_size / 8;
-	uint32_t num_levels = num_actual_levels(tex);
-	bool     compressed = is_compressed_format(tex->base.format);
+	uint32_t num_levels = tex->base.levels;
+	bool     compressed = gs_is_compressed_format(tex->base.format);
 	bool     success    = true;
 	uint32_t i;
 
+	if (!num_levels)
+		num_levels = gs_num_total_levels(tex->size, tex->size);
+
 	for (i = 0; i < 6; i++) {
 		GLenum type = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
 
@@ -89,7 +73,7 @@ texture_t device_create_cubetexture(device_t device, uint32_t size,
 
 fail:
 	texture_destroy((texture_t)tex);
-	blog(LOG_ERROR, "device_create_texture (GL) failed");
+	blog(LOG_ERROR, "device_create_cubetexture (GL) failed");
 	return NULL;
 }
 

+ 15 - 2
libobs/graphics/graphics.h

@@ -226,7 +226,7 @@ struct gs_rect {
 	int cy;
 };
 
-static inline uint32_t get_format_bpp(enum gs_color_format format)
+static inline uint32_t gs_get_format_bpp(enum gs_color_format format)
 {
 	switch (format) {
 	case GS_A8:          return 8;
@@ -250,11 +250,24 @@ static inline uint32_t get_format_bpp(enum gs_color_format format)
 	}
 }
 
-static inline bool is_compressed_format(enum gs_color_format format)
+static inline bool gs_is_compressed_format(enum gs_color_format format)
 {
 	return (format == GS_DXT1 || format == GS_DXT3 || format == GS_DXT5);
 }
 
+static inline uint32_t gs_num_total_levels(uint32_t width, uint32_t height)
+{
+	uint32_t size = width > height ? width : height;
+	uint32_t num_levels = 0;
+
+	while (size > 1) {
+		size /= 2;
+		num_levels++;
+	}
+
+	return num_levels;
+}
+
 /* wrapped opaque data types */
 
 struct gs_texture;