-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Texture Compression #143
Merged
Merged
Texture Compression #143
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f5ef63c
Add compressed texture support
erikhaandrikman 788467d
Add texture compression test
erikhaandrikman 1b65e37
Merge branch 'main' into feature/tx-compression
erikhaandrikman 7a86795
Adapt to WebGL Context Wrapper
erikhaandrikman d3240f6
Force compressed texture bind target TEXTURE_2D
erikhaandrikman 8e8a01f
Update compression test
erikhaandrikman b869dfb
Merge branch 'main' into feature/tx-compression
erikhaandrikman 287d5fd
Remove visual testing from tx compression test
erikhaandrikman f242258
Extract TX compression logic into separate file
erikhaandrikman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* If not stated otherwise in this file or this component's LICENSE file the | ||
* following copyright and licenses apply: | ||
* | ||
* Copyright 2023 Comcast Cable Communications Management, LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the License); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { ExampleSettings } from '../common/ExampleSettings.js'; | ||
|
||
export default async function ({ renderer, testRoot }: ExampleSettings) { | ||
renderer.createTextNode({ | ||
x: 100, | ||
y: 100, | ||
color: 0xffffffff, | ||
alpha: 1.0, | ||
text: 'etc1 compression in .pvr', | ||
fontFamily: 'Ubuntu', | ||
fontSize: 30, | ||
parent: testRoot, | ||
}); | ||
|
||
renderer.createNode({ | ||
x: 100, | ||
y: 170, | ||
width: 550, | ||
height: 550, | ||
src: '../assets/test-etc1.pvr', | ||
parent: testRoot, | ||
}); | ||
|
||
renderer.createTextNode({ | ||
x: 800, | ||
y: 100, | ||
color: 0xffffffff, | ||
alpha: 1.0, | ||
text: 's3tc compression in .ktx', | ||
fontFamily: 'Ubuntu', | ||
fontSize: 30, | ||
parent: testRoot, | ||
}); | ||
|
||
renderer.createNode({ | ||
x: 800, | ||
y: 170, | ||
width: 400, | ||
height: 400, | ||
src: '../assets/test-s3tc.ktx', | ||
parent: testRoot, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* If not stated otherwise in this file or this component's LICENSE file the | ||
* following copyright and licenses apply: | ||
* | ||
* Copyright 2023 Comcast Cable Communications Management, LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the License); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { type TextureData } from '../textures/Texture.js'; | ||
|
||
/** | ||
* Tests if the given location is a compressed texture container | ||
* @param url | ||
* @remarks | ||
* This function is used to determine if the given image url is a compressed | ||
* and only supports the following extensions: .ktx and .pvr | ||
* @returns | ||
*/ | ||
export function isCompressedTextureContainer(url: string): boolean { | ||
return /\.(ktx|pvr)$/.test(url); | ||
} | ||
|
||
/** | ||
* Loads a compressed texture container | ||
* @param url | ||
* @returns | ||
*/ | ||
export const loadCompressedTexture = async ( | ||
url: string, | ||
): Promise<TextureData> => { | ||
const response = await fetch(url); | ||
const arrayBuffer = await response.arrayBuffer(); | ||
|
||
if (url.indexOf('.ktx') !== -1) { | ||
return loadKTXData(arrayBuffer); | ||
} | ||
|
||
return loadPVRData(arrayBuffer); | ||
}; | ||
|
||
/** | ||
* Loads a KTX texture container and returns the texture data | ||
* @param buffer | ||
* @returns | ||
*/ | ||
const loadKTXData = async (buffer: ArrayBuffer): Promise<TextureData> => { | ||
const view = new DataView(buffer); | ||
const littleEndian = view.getUint32(12) === 16909060 ? true : false; | ||
const mipmaps = []; | ||
|
||
const data = { | ||
glInternalFormat: view.getUint32(28, littleEndian), | ||
pixelWidth: view.getUint32(36, littleEndian), | ||
pixelHeight: view.getUint32(40, littleEndian), | ||
numberOfMipmapLevels: view.getUint32(56, littleEndian), | ||
bytesOfKeyValueData: view.getUint32(60, littleEndian), | ||
}; | ||
|
||
let offset = 64; | ||
|
||
// Key Value Pairs of data start at byte offset 64 | ||
// But the only known kvp is the API version, so skipping parsing. | ||
offset += data.bytesOfKeyValueData; | ||
|
||
for (let i = 0; i < data.numberOfMipmapLevels; i++) { | ||
const imageSize = view.getUint32(offset); | ||
offset += 4; | ||
|
||
mipmaps.push(view.buffer.slice(offset, imageSize)); | ||
offset += imageSize; | ||
} | ||
|
||
return { | ||
data: { | ||
glInternalFormat: data.glInternalFormat, | ||
mipmaps, | ||
width: data.pixelWidth || 0, | ||
height: data.pixelHeight || 0, | ||
type: 'ktx', | ||
}, | ||
premultiplyAlpha: false, | ||
}; | ||
}; | ||
|
||
/** | ||
* Loads a PVR texture container and returns the texture data | ||
* @param buffer | ||
* @returns | ||
*/ | ||
const loadPVRData = async (buffer: ArrayBuffer): Promise<TextureData> => { | ||
// pvr header length in 32 bits | ||
const pvrHeaderLength = 13; | ||
// for now only we only support: COMPRESSED_RGB_ETC1_WEBGL | ||
const pvrFormatEtc1 = 0x8d64; | ||
const pvrWidth = 7; | ||
const pvrHeight = 6; | ||
const pvrMipmapCount = 11; | ||
const pvrMetadata = 12; | ||
const arrayBuffer = buffer; | ||
const header = new Int32Array(arrayBuffer, 0, pvrHeaderLength); | ||
|
||
// @ts-expect-error Object possibly undefined | ||
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands | ||
const dataOffset = header[pvrMetadata] + 52; | ||
const pvrtcData = new Uint8Array(arrayBuffer, dataOffset); | ||
const mipmaps = []; | ||
const data = { | ||
pixelWidth: header[pvrWidth], | ||
pixelHeight: header[pvrHeight], | ||
numberOfMipmapLevels: header[pvrMipmapCount] || 0, | ||
}; | ||
|
||
let offset = 0; | ||
let width = data.pixelWidth || 0; | ||
let height = data.pixelHeight || 0; | ||
|
||
for (let i = 0; i < data.numberOfMipmapLevels; i++) { | ||
const level = ((width + 3) >> 2) * ((height + 3) >> 2) * 8; | ||
const view = new Uint8Array( | ||
arrayBuffer, | ||
pvrtcData.byteOffset + offset, | ||
level, | ||
); | ||
|
||
mipmaps.push(view); | ||
offset += level; | ||
width = width >> 1; | ||
height = height >> 1; | ||
} | ||
|
||
return { | ||
data: { | ||
glInternalFormat: pvrFormatEtc1, | ||
mipmaps: mipmaps, | ||
width: data.pixelWidth || 0, | ||
height: data.pixelHeight || 0, | ||
type: 'pvr', | ||
}, | ||
premultiplyAlpha: false, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason these lines got touched with formatting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really sure, using the project configured formatter