Skip to content

Commit b97bc85

Browse files
authored
Merge pull request #208 from BabylonJS/CompressedTextureSize
Review texture size computation
2 parents a081f22 + b4d3041 commit b97bc85

File tree

5 files changed

+48
-22
lines changed

5 files changed

+48
-22
lines changed

dist/spector.bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/spector.bundle.func.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/spector.bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/backend/recorders/texture2DRecorder.ts

+30-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface ITextureRecorderData {
1313
format?: number;
1414
type?: number;
1515
depth?: number;
16+
isCompressed: boolean;
1617
}
1718

1819
export class Texture2DRecorder extends BaseRecorder<WebGLTexture> {
@@ -71,24 +72,35 @@ export class Texture2DRecorder extends BaseRecorder<WebGLTexture> {
7172
}
7273

7374
const previousLength = (instance as any).__SPECTOR_Object_CustomData ? (instance as any).__SPECTOR_Object_CustomData.length : 0;
74-
const cubeMapMultiplier = target === "TEXTURE_2D" ? 1 : 6;
75-
let internalFormat = customData.internalFormat;
76-
77-
// @ivanpopelyshev: this hack is made according to tests on PixiJS applications
78-
// Float textures is not a rare case
79-
// WebGL1 does not have RGBA32F, RGBA16F, we need to look in `type` field
80-
if (internalFormat === WebGlConstants.RGBA.value) {
81-
if (customData.type === WebGlConstants.FLOAT.value) {
82-
internalFormat = WebGlConstants.RGBA32F.value;
75+
if (customData.isCompressed) {
76+
// Compressed textures are worth the size of their data.
77+
if (functionInformation.arguments.length >= 7) {
78+
const viewOrSize = functionInformation.arguments[6];
79+
customData.length = (typeof viewOrSize === "number") ? viewOrSize : viewOrSize?.byteLength;
8380
}
84-
if (customData.type === WebGlConstants.HALF_FLOAT_OES.value) {
85-
internalFormat = WebGlConstants.RGBA16F.value;
81+
}
82+
else {
83+
const cubeMapMultiplier = target === "TEXTURE_2D" ? 1 : 6;
84+
let internalFormat = customData.internalFormat;
85+
86+
// @ivanpopelyshev: this hack is made according to tests on PixiJS applications
87+
// Float textures is not a rare case
88+
// WebGL1 does not have RGBA32F, RGBA16F, we need to look in `type` field
89+
if (internalFormat === WebGlConstants.RGBA.value) {
90+
if (customData.type === WebGlConstants.FLOAT.value) {
91+
internalFormat = WebGlConstants.RGBA32F.value;
92+
}
93+
if (customData.type === WebGlConstants.HALF_FLOAT_OES.value) {
94+
internalFormat = WebGlConstants.RGBA16F.value;
95+
}
8696
}
97+
98+
// @ivanpopelyshev: This calculation should be fine for most cases, but not if we start counting mips
99+
// TODO: move width/height inside and make pluggable functions based on compressed textures extensions
100+
customData.length = (customData.width * customData.height * cubeMapMultiplier * this.getByteSizeForInternalFormat(internalFormat));
87101
}
88102

89-
// @ivanpopelyshev: This calculation should be fine for most cases, but not if we start counting mips
90-
// TODO: move width/height inside and make pluggable functions based on compressed textures extensions
91-
customData.length = (customData.width * customData.height * cubeMapMultiplier * this.getByteSizeForInternalFormat(internalFormat)) | 0;
103+
customData.length = customData.length | 0;
92104
(instance as any).__SPECTOR_Object_CustomData = customData;
93105
return customData.length - previousLength;
94106
}
@@ -117,6 +129,7 @@ export class Texture2DRecorder extends BaseRecorder<WebGLTexture> {
117129
width: functionInformation.arguments[3],
118130
height: functionInformation.arguments[4],
119131
length: 0,
132+
isCompressed: false,
120133
};
121134
}
122135

@@ -140,6 +153,7 @@ export class Texture2DRecorder extends BaseRecorder<WebGLTexture> {
140153
width: functionInformation.arguments[3],
141154
height: functionInformation.arguments[4],
142155
length: 0,
156+
isCompressed: true,
143157
};
144158
}
145159

@@ -165,6 +179,7 @@ export class Texture2DRecorder extends BaseRecorder<WebGLTexture> {
165179
format: functionInformation.arguments[6],
166180
type: functionInformation.arguments[7],
167181
length: 0,
182+
isCompressed: false,
168183
};
169184
}
170185
else if (functionInformation.arguments.length === 6) {
@@ -178,6 +193,7 @@ export class Texture2DRecorder extends BaseRecorder<WebGLTexture> {
178193
format: functionInformation.arguments[3],
179194
type: functionInformation.arguments[4],
180195
length: 0,
196+
isCompressed: false,
181197
};
182198
}
183199

src/backend/recorders/texture3DRecorder.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,20 @@ export class Texture3DRecorder extends BaseRecorder<WebGLTexture> {
5454
}
5555

5656
const previousLength = (instance as any).__SPECTOR_Object_CustomData ? (instance as any).__SPECTOR_Object_CustomData.length : 0;
57-
customData.length = customData.width * customData.height * customData.depth
58-
* this.getByteSizeForInternalFormat(customData.internalFormat);
59-
60-
if (customData) {
61-
(instance as any).__SPECTOR_Object_CustomData = customData;
57+
if (customData.isCompressed) {
58+
// Compressed textures are worth the size of their data.
59+
if (functionInformation.arguments.length >= 7) {
60+
const viewOrSize = functionInformation.arguments[6];
61+
customData.length = (typeof viewOrSize === "number") ? viewOrSize : viewOrSize?.byteLength;
62+
}
63+
}
64+
else {
65+
customData.length = customData.width * customData.height * customData.depth
66+
* this.getByteSizeForInternalFormat(customData.internalFormat);
6267
}
6368

69+
customData.length = customData.length | 0;
70+
(instance as any).__SPECTOR_Object_CustomData = customData;
6471
return customData.length - previousLength;
6572
}
6673

@@ -89,6 +96,7 @@ export class Texture3DRecorder extends BaseRecorder<WebGLTexture> {
8996
height: functionInformation.arguments[4],
9097
depth: functionInformation.arguments[5],
9198
length: 0,
99+
isCompressed: false,
92100
};
93101
}
94102

@@ -113,6 +121,7 @@ export class Texture3DRecorder extends BaseRecorder<WebGLTexture> {
113121
height: functionInformation.arguments[4],
114122
depth: functionInformation.arguments[5],
115123
length: 0,
124+
isCompressed: true,
116125
};
117126
}
118127

@@ -139,6 +148,7 @@ export class Texture3DRecorder extends BaseRecorder<WebGLTexture> {
139148
format: functionInformation.arguments[7],
140149
type: functionInformation.arguments[8],
141150
length: 0,
151+
isCompressed: false,
142152
};
143153
}
144154

0 commit comments

Comments
 (0)