Page 1 of 1

Citro2d textures/vram/image-type/etc

Posted: Tue May 25, 2021 11:42 pm
by haloopdy
I'd like to create a basic drawing app (just for fun) on the 3DS utilizing citro2d. I looked at many of the examples in devkitpro, and some examples on these forums. The one that is most intriguing is viewtopic.php?f=39&t=9071#p16777

I have several questions in regards to this code and code that uses these objects:

Code: Select all

    const Tex3DS_SubTexture subtex = {
        512, 256,
        0.0f, 1.0f, 1.0f, 0.0f
    };
    C3D_Tex tex;
    C3D_TexInitVRAM(&tex, 512, 256, GPU_RGBA8);
    C3D_RenderTarget* target = C3D_RenderTargetCreateFromTex(&tex, GPU_TEXFACE_2D, 0, -1);
    C2D_Image img = {&tex, &subtex};
So from what I can gather, we create a subtexture, initialize vram for a 512x256 block in RGBA8 format and get back a pointer, create a target (which can be written to?) for that area in memory, and then... create an image from it with the subtexture describing it?

So my questions are:
  • Looking at the source code for "Tex3DS_SubTexture" makes it seem like it's just a struct that describes a region, is this correct?
  • Initializing vram produces a texture, but this texture is... what, unusable in the current state, or not easily usable through the 2D library? Is that why we have to create a render target?
  • What's the purpose of then creating an 'image' from the texture (but not the target) using the subtexture info? Looking at the code in the linked post, it looks to me like the render target is what I can write into, and the image is a different kind of "view" into the texture which can be used to draw the texture to the screen.
  • I know resources are limited on the 3ds, but I was wondering if there are either practical or hard limits on contiguous textures? I noticed the fields for Tex3DS_SubTexture for width/height are 16 bit unsigned. If I'm not going to be putting basically ANYTHING else into vram (save for perhaps some smaller buffers), would it be possible for me to create a contiguous texture region that's, say, 320px wide by 4000px tall (RGBA8)?
  • If the previous is possible, will it be completely inefficient to draw just a screen-sized region of this big texture onto the lower screen, or does the size of the texture not matter too much because I can specify a small region? Imagine the screen is a vertical sliding window on the 320x4000 texture.

Re: Citro2d textures/vram/image-type/etc

Posted: Sat Jun 05, 2021 12:53 pm
by fincs
Looking at the source code for "Tex3DS_SubTexture" makes it seem like it's just a struct that describes a region, is this correct?

What's the purpose of then creating an 'image' from the texture (but not the target) using the subtexture info? Looking at the code in the linked post, it looks to me like the render target is what I can write into, and the image is a different kind of "view" into the texture which can be used to draw the texture to the screen.
That is correct. Many users create Tex3DS_SubTexture structs at runtime in order to crop and draw derived textures, in fact.
Initializing vram produces a texture, but this texture is... what, unusable in the current state, or not easily usable through the 2D library? Is that why we have to create a render target?
C3D_TexInitVRAM doesn't "initialize vram". It initializes a texture, allocating its backing memory from VRAM. The contents of VRAM are undefined on startup. You still need to upload a texture image to it, or use it as a rendertarget (clearing before use of course).
I know resources are limited on the 3ds, but I was wondering if there are either practical or hard limits on contiguous textures? I noticed the fields for Tex3DS_SubTexture for width/height are 16 bit unsigned. If I'm not going to be putting basically ANYTHING else into vram (save for perhaps some smaller buffers), would it be possible for me to create a contiguous texture region that's, say, 320px wide by 4000px tall (RGBA8)?

If the previous is possible, will it be completely inefficient to draw just a screen-sized region of this big texture onto the lower screen, or does the size of the texture not matter too much because I can specify a small region? Imagine the screen is a vertical sliding window on the 320x4000 texture.
The main thing you need to take into account is that texture width/height need to be one of: 8 16 32 64 128 256 512 1024. As for rendertargets, they must all fit in VRAM, which is 6MB in size, and have width/height not greater than 1024. Render targets used as textures need to obey both restrictions. Also keep in mind that the main screen rendertargets are also in VRAM. This means your 320x4000 texture violates both restrictions, if you intend to use it as a texture and as a rendertarget.

Re: Citro2d textures/vram/image-type/etc

Posted: Sat Jun 05, 2021 4:57 pm
by haloopdy
I really appreciate it; I understand the render target stuff now. And yes, I found out about the power of 2 thing, I just went with a 1024x1024 texture.

Thank you!