Skip to content

Commit

Permalink
Support for textures to preferentially use LPDDR memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
xfangfang committed Dec 27, 2024
1 parent ff01619 commit 0b66b07
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ cmake -B build -DENABLE_EXAMPLE=ON
cmake --build build
```

## New Features

### 1. Compressed texture support

When creating an RGBA texture, you can specify the following image flag and pass in the compressed texture.
This can significantly reduce the memory footprint when the texture dimensions are large.

- NVG_IMAGE_DXT1
- NVG_IMAGE_DXT5

### 2. Texture memory location support

You can manually specify that textures should be stored in system memory (by default, textures are stored in video memory).

- NVG_IMAGE_LPDDR

## TODO

#### 1. blend function
Expand Down
2 changes: 1 addition & 1 deletion example/example_gxm.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ int main() {
memset(&old_pad, 0, sizeof(old_pad));
gxmClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);

int compressedImage = loadCompressedImage(vg, "app0:/example/images/image1.jpg", NVG_IMAGE_DXT1);
int compressedImage = loadCompressedImage(vg, "app0:/example/images/image1.jpg", NVG_IMAGE_DXT1 | NVG_IMAGE_LPDDR);

for (;;) {
sceCtrlPeekBufferPositive(0, &pad, 1);
Expand Down
24 changes: 16 additions & 8 deletions src/nanovg_gxm.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ enum NVGimageFlagsGXM {
NVG_IMAGE_NODELETE = 1 << 16, // Do not delete GXM texture handle.
NVG_IMAGE_DXT1 = 1 << 15,
NVG_IMAGE_DXT5 = 1 << 14,
NVG_IMAGE_LPDDR = 1 << 13,
};

int __attribute__((weak)) nvg_gxm_vertex_buffer_size = 1024 * 1024;
Expand Down Expand Up @@ -115,7 +116,6 @@ struct GXMNVGtexture {
int type;
int flags;

int stride;
int unused;

NVGXMtexture texture;
Expand Down Expand Up @@ -805,7 +805,7 @@ static int gxmnvg__renderCreateTexture(void *uptr, int type, int w, int h, int i
type == NVG_TEXTURE_RGBA ? SCE_GXM_TEXTURE_FORMAT_U8U8U8U8_ABGR : SCE_GXM_TEXTURE_FORMAT_U8_000R;
int aligned_w = ALIGN(w, 8);
int spp = type == NVG_TEXTURE_RGBA ? 4 : 1;
uint32_t tex_size;
uint32_t tex_size, stride, mem_type1, mem_type2;
int ret;
int swizzled = ((imageFlags & NVG_IMAGE_DXT1) || (imageFlags & NVG_IMAGE_DXT5)) && (type == NVG_TEXTURE_RGBA);

Expand All @@ -818,12 +818,19 @@ static int gxmnvg__renderCreateTexture(void *uptr, int type, int w, int h, int i
tex_size = aligned_w * h * spp;
}

tex->stride = aligned_w * spp;
tex->texture.data = (uint8_t *) gpu_alloc_map(SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW,
if (imageFlags & NVG_IMAGE_LPDDR) {
mem_type1 = SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE;
mem_type2 = SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW;
} else {
mem_type1 = SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW;
mem_type2 = SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE;
}

tex->texture.data = (uint8_t *) gpu_alloc_map(mem_type1,
SCE_GXM_MEMORY_ATTRIB_RW,
tex_size, &tex->texture.uid);
if (tex->texture.data == NULL) {
tex->texture.data = (uint8_t *) gpu_alloc_map(SCE_KERNEL_MEMBLOCK_TYPE_USER_RW_UNCACHE,
tex->texture.data = (uint8_t *) gpu_alloc_map(mem_type2,
SCE_GXM_MEMORY_ATTRIB_RW,
tex_size, &tex->texture.uid);
}
Expand All @@ -837,8 +844,9 @@ static int gxmnvg__renderCreateTexture(void *uptr, int type, int w, int h, int i
} else if (swizzled || aligned_w == w) {
memcpy(tex->texture.data, data, tex_size);
} else {
stride = aligned_w * spp;
for (int i = 0; i < h; i++) {
memcpy(tex->texture.data + i * tex->stride, data + i * w * spp, w * spp);
memcpy(tex->texture.data + i * stride, data + i * w * spp, w * spp);
}
}

Expand Down Expand Up @@ -918,9 +926,9 @@ static int gxmnvg__renderUpdateTexture(void *uptr, int image, int x, int y, int
}

int spp = tex->type == NVG_TEXTURE_RGBA ? 4 : 1;

uint32_t stride = ALIGN(tex->width, 8);
for (int i = 0; i < h; i++) {
int start = (i + y) * tex->stride + x * spp;
uint32_t start = ((i + y) * stride + x ) * spp;
memcpy(tex->texture.data + start, data + start, w * spp);
}

Expand Down

0 comments on commit 0b66b07

Please sign in to comment.