Skip to content

Commit

Permalink
Finish some texture TODOs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakuna committed Mar 7, 2024
1 parent 8512b1b commit 8183e59
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 636 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -129,8 +129,10 @@
"#getParameterForBufferTarget": "./dist/utility/internal/getParameterForBufferTarget.js",
"#getParameterForFramebufferTarget": "./dist/utility/internal/getParameterForFramebufferTarget.js",
"#getParameterForTextureTarget": "./dist/utility/internal/getParameterForTextureTarget.js",
"#getTextureDataTypesForTextureInternalFormat": "./dist/utility/internal/getTextureDataTypesForTextureInternalFormat.js",
"#getTextureFormatForTextureInternalFormat": "./dist/utility/internal/getTextureFormatForTextureInternalFormat.js",
"#BadValueError": "./dist/utility/BadValueError.js",
"#ImmutableError": "./dist/utility/ImmutableError.js",
"#ProgramLinkError": "./dist/utility/ProgramLinkError.js",
"#ShaderCompileError": "./dist/utility/ShaderCompileError.js",
"#TextureFormatError": "./dist/utility/TextureFormatError.js",
Expand Down
58 changes: 44 additions & 14 deletions src/core/textures/Texture.ts
Expand Up @@ -30,6 +30,8 @@ import type { MipData } from "#MipData";
import type TextureDataType from "#TextureDataType";
import type TextureFormat from "#TextureFormat";
import type { TextureInternalFormat } from "#TextureInternalFormat";
import TextureUncompressedUnsizedInternalFormat from "#TextureUncompressedUnsizedInternalFormat";
import ImmutableError from "#ImmutableError";

/**
* A randomly-accessible array.
Expand Down Expand Up @@ -431,7 +433,29 @@ export default abstract class Texture extends ContextDependent {
protected readonly mipmaps: ReadonlyMap<MipmapTarget, Map<number, boolean>>;

/** The format of this texture. */
protected format?: TextureInternalFormat;
private formatCache?: TextureInternalFormat;

/** The format of this texture. */
public get format(): TextureInternalFormat {
// We don't have to worry about defaulting to an unsized internal format since the format is always set for immutable-format textures.
if (typeof this.formatCache === "undefined") {
this.formatCache = TextureUncompressedUnsizedInternalFormat.RGBA;
}

return this.formatCache;
}

/**
* The format of this texture.
* @throws {@link ImmutableError} if this is an immutable-format texture.
*/
public set format(value: TextureInternalFormat) {
if (this.isImmutableFormat) {
throw new ImmutableError();
}

this.formatCache = value;
}

/**
* The width, height (if applicable), and depth (if applicable) of this
Expand Down Expand Up @@ -465,7 +489,10 @@ export default abstract class Texture extends ContextDependent {
}

this.makeImmutableFormatInternal(levels, format, dims);
// TODO: Set `format` and `dims`.
this.format = format;
for (let i = 0; i < dims.length; i++) {
this.dims[i] = dims[i] ?? 0;
}
this.isImmutableFormatCache = true;
}

Expand Down Expand Up @@ -505,6 +532,10 @@ export default abstract class Texture extends ContextDependent {
): void {
// TODO: Ensure that format, internal format, and data type are compatible.

// TODO: May need to clear certain cache values when updating data? i.e. dims, max level, max/min LOD, etc.

// TODO: Unpack alignment.

this.setMipInternal(target, level, format, type, data, bounds);
}

Expand Down Expand Up @@ -928,20 +959,19 @@ export default abstract class Texture extends ContextDependent {
return true;
}

// TODO: Return `true` if all mips have data.

return false;
}

// TODO: Add a way to update the data in the texture.

// TODO: May need to clear certain cache values when updating data? i.e. dims, max level, max/min LOD, etc.

// TODO: Add a function that calculates the expected size of the given mip.
// Return `false` if any mip doesn't have data.
for (const mipmap of this.mipmaps.values()) {
for (const mip of mipmap.values()) {
if (!mip) {
return false;
}
}
}

// TODO: Maybe save mip data as an array of modifications. Reset when full data is supplied and append when partial data is supplied. When the mip is updated, apply modifications in order. Save an index to prevent reapplying modifications.
// Return `true` if all mips have data.

// TODO: Add a method that updates the texture when needed and a method that forces the texture to update. Update the texture when it's created and when it's about to be used (i.e. passed to a uniform). This method should also generate a mipmap automatically if the texture is not texture complete and it wouldn't override given data. This method should automatically use the correct unpack alignment if possible.
return true;
}

/**
* Get the dimensions of the mip at the given level.
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -53,6 +53,7 @@ export type { TextureUnsizedInternalFormat } from "#TextureUnsizedInternalFormat
export type { TypedArray } from "#TypedArray";
export type { UintTypedArray } from "#UintTypedArray";
export { default as BadValueError } from "#BadValueError";
export { default as ImmutableError } from "#ImmutableError";
export { default as ProgramLinkError } from "#ProgramLinkError";
export { default as ShaderCompileError } from "#ShaderCompileError";
export { default as TextureFormatError } from "#TextureFormatError";
Expand Down
13 changes: 13 additions & 0 deletions src/utility/ImmutableError.ts
@@ -0,0 +1,13 @@
/**
* An error resulting from attempting to modify an immutable value.
*/
export default class ImmutableError extends Error {
/**
* Creates a texture format error.
* @param message The message of the error.
*/
public constructor(message = "Attempted to modify an immutable value.") {
super(message);
this.name = "ImmutableError";
}
}
4 changes: 2 additions & 2 deletions src/utility/TextureFormatError.ts
@@ -1,5 +1,5 @@
/**
* An error resulting from attempting to use a data format that is not
* An error resulting from attempting to use a data format or type that is not
* compatible with a texture format.
*/
export default class TextureFormatError extends Error {
Expand All @@ -8,7 +8,7 @@ export default class TextureFormatError extends Error {
* @param message The message of the error.
*/
public constructor(
message = "Attempted to use a data format that is not compatible with a texture format."
message = "Attempted to use a data format or type that is not compatible with a texture format."
) {
super(message);
this.name = "TextureFormatError";
Expand Down
89 changes: 0 additions & 89 deletions src/utility/internal/getDataTypeForTypedArray.ts
@@ -1,90 +1,6 @@
import type { TypedArray } from "#TypedArray";
import BufferDataType from "#BufferDataType";

/**
* Returns a default buffer data type for the given typed array.
* @param array The typed array.
* @returns A default buffer data type for the given typed array.
* @internal
*/
export default function getDataTypeForTypedArray(
array: Int8Array
): BufferDataType.BYTE;

/**
* Returns a default buffer data type for the given typed array.
* @param array The typed array.
* @returns A default buffer data type for the given typed array.
* @internal
*/
export default function getDataTypeForTypedArray(
array: Uint8Array | Uint8ClampedArray
): BufferDataType.UNSIGNED_BYTE;

/**
* Returns a default buffer data type for the given typed array.
* @param array The typed array.
* @returns A default buffer data type for the given typed array.
* @internal
*/
export default function getDataTypeForTypedArray(
array: Int16Array
): BufferDataType.SHORT;

/**
* Returns a default buffer data type for the given typed array.
* @param array The typed array.
* @returns A default buffer data type for the given typed array.
* @internal
*/
export default function getDataTypeForTypedArray(
array: Uint16Array
): BufferDataType.UNSIGNED_SHORT;

/**
* Returns a default buffer data type for the given typed array.
* @param array The typed array.
* @returns A default buffer data type for the given typed array.
* @internal
*/
export default function getDataTypeForTypedArray(
array: Int32Array
): BufferDataType.INT;

/**
* Returns a default buffer data type for the given typed array.
* @param array The typed array.
* @returns A default buffer data type for the given typed array.
* @internal
*/
export default function getDataTypeForTypedArray(
array: Uint32Array
): BufferDataType.UNSIGNED_INT;

/**
* Returns a default buffer data type for the given typed array.
* @param array The typed array.
* @param half Whether the array contains 16-bit floating-point data.
* @returns A default buffer data type for the given typed array.
* @internal
*/
export default function getDataTypeForTypedArray(
array: Float32Array,
half: true
): BufferDataType.HALF_FLOAT;

/**
* Returns a default buffer data type for the given typed array.
* @param array The typed array.
* @param half Whether the array contains 16-bit floating-point data.
* @returns A default buffer data type for the given typed array.
* @internal
*/
export default function getDataTypeForTypedArray(
array: Float32Array,
half: false
): BufferDataType.FLOAT;

/**
* Returns a default buffer data type for the given typed array.
* @param array The typed array.
Expand All @@ -93,11 +9,6 @@ export default function getDataTypeForTypedArray(
* @returns A default buffer data type for the given typed array.
* @internal
*/
export default function getDataTypeForTypedArray(
array: TypedArray,
half?: boolean
): BufferDataType;

export default function getDataTypeForTypedArray(
array: TypedArray,
half = false
Expand Down
67 changes: 0 additions & 67 deletions src/utility/internal/getExtensionForRenderbufferFormat.ts
Expand Up @@ -7,73 +7,6 @@ import Extension from "#Extension";
* @returns The extension that is associated with the renderbuffer format.
* @internal
*/
export default function getExtensionForRenderbufferFormat(
format:
| RenderbufferFormat.R16F
| RenderbufferFormat.RG16F
| RenderbufferFormat.RGBA16F
| RenderbufferFormat.R32F
| RenderbufferFormat.RG32F
| RenderbufferFormat.RGBA32F
| RenderbufferFormat.R11F_G11F_B10F
): Extension.ExtColorBufferFloat;

/**
* Gets the extension that is associated with the given renderbuffer format.
* @param format The renderbuffer format.
* @returns The extension that is associated with the renderbuffer format.
* @internal
*/
export default function getExtensionForRenderbufferFormat(
format:
| RenderbufferFormat.RGBA4
| RenderbufferFormat.RGB565
| RenderbufferFormat.RGB5_A1
| RenderbufferFormat.DEPTH_COMPONENT16
| RenderbufferFormat.STENCIL_INDEX8
| RenderbufferFormat.DEPTH_STENCIL
| RenderbufferFormat.R8
| RenderbufferFormat.R8UI
| RenderbufferFormat.R8I
| RenderbufferFormat.R16UI
| RenderbufferFormat.R16I
| RenderbufferFormat.R32UI
| RenderbufferFormat.R32I
| RenderbufferFormat.RG8
| RenderbufferFormat.RG8UI
| RenderbufferFormat.RG8I
| RenderbufferFormat.RG16UI
| RenderbufferFormat.RG16I
| RenderbufferFormat.RG32UI
| RenderbufferFormat.RG32I
| RenderbufferFormat.RGB8
| RenderbufferFormat.RGBA8
| RenderbufferFormat.SRGB8_ALPHA8
| RenderbufferFormat.RGB10_A2
| RenderbufferFormat.RGBA8UI
| RenderbufferFormat.RGBA8I
| RenderbufferFormat.RGB10_A2UI
| RenderbufferFormat.RGBA16UI
| RenderbufferFormat.RGBA16I
| RenderbufferFormat.RGBA32I
| RenderbufferFormat.RGBA32UI
| RenderbufferFormat.DEPTH_COMPONENT24
| RenderbufferFormat.DEPTH_COMPONENT32F
| RenderbufferFormat.DEPTH24_STENCIL8
| RenderbufferFormat.DEPTH32F_STENCIL8
| RenderbufferFormat.RGB32F_EXT
): null;

/**
* Gets the extension that is associated with the given renderbuffer format.
* @param format The renderbuffer format.
* @returns The extension that is associated with the renderbuffer format.
* @internal
*/
export default function getExtensionForRenderbufferFormat(
format: RenderbufferFormat
): Extension | null;

export default function getExtensionForRenderbufferFormat(
format: RenderbufferFormat
): Extension | null {
Expand Down

0 comments on commit 8183e59

Please sign in to comment.