Ctrulib Question

Discussion in '3DS - Homebrew Development and Emulators' started by cal64, Oct 31, 2016.

  1. cal64
    OP

    cal64 Member

    Newcomer
    43
    4
    Oct 31, 2016
    Belgium
    Hi! I've been studying the portal3DS source code for a while now, and there's something I can't quite understand:
    In the source code are functions like GPU_DrawArrays() of which I can't find the declaration in the ctrulib source code, however they're listed in the libctru.cfg file (in the ctrulib repository).
    Can anyone tell me what I'm not getting here/how these functions work?
    Thanks in advance!
     
  2. mashers

    mashers Stubborn ape

    Member
    3,837
    5,155
    Jun 10, 2015
    Kongo Jungle
    If they're not defined in the ctrulib headers, they're probably functions provided by *hax. If you get warnings when building your homebrew, you could add a definition of the function and as long as it's compatible with the implementation (wherever it is) it should be fine.
     
  3. cal64
    OP

    cal64 Member

    Newcomer
    43
    4
    Oct 31, 2016
    Belgium
    So, where can I find these functions? By *hax I get "whatever hax you're using", but in this case, where can I find them in the *hax source code?
    (Sorry if I don't fully understand and this sounds silly, but might as well make sure :) ).
    Also, before the section corresponding to the GPU_DrawElements() function I gave as an example in libctru.cfg is the line <!-- gpu/gpu.c -->, leading to a file that I could only find in the ctrulib repository.
    Thanks for replying!
     
    Last edited by cal64, Oct 31, 2016
  4. mashers

    mashers Stubborn ape

    Member
    3,837
    5,155
    Jun 10, 2015
    Kongo Jungle
    I'm assuming they're provided by the hax payload which is loaded using the entrypoint. The entrypoint probably doesn't matter - it's what's provided by the payload which is, I suspect, providing those functions and services.

    I'm not sure actually. The ninjhax repo on GitHub seems really out of date as there have definitely been updates to the payloads since then. I'll see if I can locate the source code for the newer hax payloads.

    Are you sure it's written like that? That is HTML comment markup. I would be surprised if it worked in a c file.

    — Posts automatically merged - Please don't double post! —

    Oh I was looking at the wrong repo. This is the repo for the current hax payloads:

    https://github.com/smealum/ninjhax2.x

    However, I can't find any reference to that function in there either. Does the function definitely work? I'm wondering if it's actually deprecated which is why you can no longer find it in the headers.
     
  5. cal64
    OP

    cal64 Member

    Newcomer
    43
    4
    Oct 31, 2016
    Belgium
    Yes, however it's from a .cfg file, and not a .c file.
    Thank you so much for all your help!
    (I didn't try to compile the game, so I can't tell if the function actually works, figuring this whole thing out was mainly for Learning purposes. I can try to compile it if you want :) ).
     
    Last edited by cal64, Oct 31, 2016
  6. mashers

    mashers Stubborn ape

    Member
    3,837
    5,155
    Jun 10, 2015
    Kongo Jungle
    Ahh, sorry, it's XML. That is definitely a comment. Looking at the file now, it would seem that the <function name=""> tags actually define functions which are probably then defined at build time. The implementation for these functions can be found in gpu-old.c (I didn't see it before). Why the functions aren't simply defined in a header I can't tell.

    I think it will be fine. If you look at the implementation of these functions, I don't think there's anything you need to use in usermode programming. For example:

    Code:
    void GPU_DrawElements(GPU_Primitive_t primitive, u32* indexArray, u32 n)
    {
        //set primitive type
        GPUCMD_AddMaskedWrite(GPUREG_PRIMITIVE_CONFIG, 0x2, primitive);
        GPUCMD_AddMaskedWrite(GPUREG_RESTART_PRIMITIVE, 0x2, 0x00000001);
        //index buffer (TODO : support multiple types)
        GPUCMD_AddWrite(GPUREG_INDEXBUFFER_CONFIG, 0x80000000|((u32)indexArray));
        //pass number of vertices
        GPUCMD_AddWrite(GPUREG_NUMVERTICES, n);
       
        GPUCMD_AddWrite(GPUREG_VERTEX_OFFSET, 0x00000000);
       
        GPUCMD_AddMaskedWrite(GPUREG_GEOSTAGE_CONFIG, 0x2, 0x00000100);
        GPUCMD_AddMaskedWrite(GPUREG_GEOSTAGE_CONFIG2, 0x2, 0x00000100);
       
        GPUCMD_AddMaskedWrite(GPUREG_START_DRAW_FUNC0, 0x1, 0x00000000);
        GPUCMD_AddWrite(GPUREG_DRAWELEMENTS, 0x00000001);
        GPUCMD_AddMaskedWrite(GPUREG_START_DRAW_FUNC0, 0x1, 0x00000001);
        GPUCMD_AddWrite(GPUREG_VTX_FUNC, 0x00000001);
       
        // CHECKME: does this one also require GPUREG_FRAMEBUFFER_FLUSH at the end?
    }
    
    This all looks like low level GPU access; you don't actually need to do that as there are higher-level drawing functions available.
     
  7. cal64
    OP

    cal64 Member

    Newcomer
    43
    4
    Oct 31, 2016
    Belgium
    Wait, what? Where? I've always used writing to registers to draw stuff...
    Also, Thanks A LOT for clearing all that up...
    It even seems gpu-old.c was removed from the ctrulib repository...
     
    Last edited by cal64, Oct 31, 2016
  8. mashers

    mashers Stubborn ape

    Member
    3,837
    5,155
    Jun 10, 2015
    Kongo Jungle
    Have a look at gfx.h in the homebrew launcher source code.

    https://github.com/smealum/3ds_hb_menu/blob/master/source/gfx.h

    Edit - actually that's framebuffer drawing. Probably not what you want if you're looking into GPU drawing...
     
    Last edited by mashers, Oct 31, 2016
  9. cal64
    OP

    cal64 Member

    Newcomer
    43
    4
    Oct 31, 2016
    Belgium
    Well, thanks anyway!
    (Also, just to make sure if you don't mind, are there higher-level drawing functions in the end?)
    Again, thanks for everything!
     
  10. mashers

    mashers Stubborn ape

    Member
    3,837
    5,155
    Jun 10, 2015
    Kongo Jungle
    I don't know if there are any higher level GPU functions as everything I've done in ctrulib uses the framebuffer. But since you're already familiar with GPU rendering, you could just bypass ctrulib for that and write your own convenience functions which do the register stuff.
     
  11. elhobbs

    elhobbs GBAtemp Advanced Fan

    Member
    806
    296
    Jul 28, 2008
    United States
    Portal3ds was written with a very early version of ctrulib. Nearly all of the 3D API calls have been deprecated and are now part of citro3d. Which you can get here https://github.com/fincs/citro3d and the are examples with devkitpro here https://github.com/devkitPro/3ds-examples/tree/master/graphics/gpu