Homepage Forums RetroPie Project Video Output on RetroPie help with OpelGL / barrel distortion

Viewing 24 posts - 1 through 24 (of 24 total)
  • Author
    Posts
  • #106205
    kraken0667
    Participant

    Hello

    Im working on a project called PE358 and need some curvature / barrel distortion to simulate/mimic a CRT-monitor. I know this is possible through OpenGL programming, but that’s not my field, and I could use any help I can get. I’ve already tried the official RPI forum, and the people there wont help out due to my lack of programming knowledge. I simply need a programmer to make this happen, and I don’t know where to look. If there’s any one in here that would consider helping me out on this one, there’s a budget involved. For a visual idea of my project, please see [URL=”http://www.lovehulten.com/uploads/3/0/4/8/30489724/2451185_orig.jpg”%5Dhere%5B/URL%5D

    #106208
    dankcushions
    Participant

    these already exist as shaders in retroarch:
    https://github.com/libretro/common-shaders/tree/master/crt (see http://filthypants.blogspot.in/2015/04/more-crt-shaders.html for examples). you can view the source code for each: https://github.com/libretro/common-shaders/tree/master/crt/shaders

    i think for most of these you can remove the extra passes (scanlines, blurs, etc) within retroarch shader options until you’re left with just the curvature part of the shader, but even then i think it’s going to be way too slow.

    perhaps someone with opengl experience can let you know if there’s any further optimisation possible, but it seems to be a fairly basic opengl warp operation so i’m not optimistic :(

    #106213
    kraken0667
    Participant

    Yeah, that’s what I’ve heard too, way too demanding on the RPI. But then I got the answer below from a guy at RPI official forum:

    Yes it’s possible. The HLSL shaders you’ve seen do a lot more than just barrel distortion and whilst the Pi might have problems implementing the full range of effects it should be easily able to do the barrel distortion and probably a few others as well.

    It’s not complicated OpenGL ES to do this. It’s basically upload the image you want to display as an OpenGL texture, draw two triangles to make up the rectangle representing the full screen and run something like the above code in a fragment shader.

    When this guy realized I couldn’t do programing myself, he didn’t want help out anymore.

    #106218
    dankcushions
    Participant

    but my understanding is that these shaders split all the various functions into different passes – one shader preset is a load of separate passes, each with a different utility; eg, in a CRT shader, one pass will add scanlines, one will add blurring, one will add the curvature, etc. you can turn off all but the curvature pass, and achieve what the post is describing without touching any code. you can even save that as a new preset. this is exactly what i did when i wanted to use a LCD-type filter for GBA games, without killing my performance by including a load of extra passes i didn’t care for.

    you might be in luck in that your project only needs a small resolution, i guess? if you set your retroarch output to that resolution (through the retropie launch menu), it may well cope with a single pass shader ok!

    #106222
    kraken0667
    Participant

    I’ve got a 3.5″ LCD with 320×240 res, and I’m gonna try this tomorrow when Im in the studio, thanks.

    I’ve never played around with shaders before. Does all of the Retroarch shaders work with the lr-nestopia emulator? I’m basically going to play NES. And do you happen to know which ones using curvature for effect?

    #106228
    davej
    Participant

    I’m the guy from the RPi forum who said it could be done.

    The not wanting to help bit was mainly because I didn’t know what I would have been signing up to – it may have been hundreds of hours work for all I knew. That’s why I suggested you needed to provide people with more information about your project to interest them.

    So, having got that out of the way…

    That bit of information saying you only need 320×240 resolution is crucial.

    The reason the RaspberryPi has problems running the shaders in retroarch is to do with how fast it’s GPU can access memory. Desktop GPUs, which retroarch shaders have been written for, have very fast memory access but, because accessing memory by GPUs is power hungry, mobile GPUs use a different design approach that minimises the amount of memory accesses they have to do to save power. Explanation why [url=http://blog.imgtec.com/powervr/a-look-at-the-powervr-graphics-architecture-tile-based-rendering]here[/url] if you’re interested in the details.

    The multiple pass rendering technique that retroarch uses is pretty much the worst case scenario for mobile GPUs because it needs lots of memory accesses. When people say the Raspberry Pi is too slow running these shaders they are typically considering running them at desktop resolutions.

    The fact that you only need such a small resolution makes a very big difference. The display you use takes up about 5% of the memory an emulator running at 1080 would. This means there is a good chance the Raspberry Pi could run some of those shaders at a decent frame rate.

    dankcushions’s suggestion of trying just the curvature shader is a good one.

    There’s a problem you need to consider when doing barrel distortion on a display that is so close in resolution to the game’s. When the vertical resolution after barrel distortion is less than the game’s vertical resolution, you will lose parts of the game screen. This may cause unacceptable problems – you might lose important bits of the game. You might be able to mitigate this with an anti-aliasing shader but they need lots of memory accesses so you might run into performance problems again.

    #106229
    kraken0667
    Participant

    Hi again Dave

    Thanks for the detailed info. I had no idea res was of such an importance. I will give this a shot tomorrow, and we’ll see. The issue of loosing bits in the games that you’re talking about would really go against what Im trying to achieve here though. My goal with barrel distortion is to prevent the loss of game information in the corners, when using a curved front panel / bezel like I am. Fingers crossed, and Thanks again!

    [quote=106228]after b[/quote]

    #106259
    kraken0667
    Participant

    I’m not sure how to add new shaders to retroarch. There’s no default shader in there dealing with curature, so I wanted to try out GRT-Geom for example. Do I simply download the shader from

    http://filthypants.blogspot.se/2012/07/customizing-cgwgs-crt-pixel-shader.html

    and place it in the retroarch/shader directory?

    Sorry for being a complete n00b.

    #106261
    dankcushions
    Participant

    there are curvature shaders in there by default – just try the various ones in the shaders/CRT folder. that crt-geom one should already be there: https://github.com/libretro/common-shaders/tree/master/crt

    you might have to update to the latest version of retroarch, depending on what version you’re running right now.

    #106266
    kraken0667
    Participant

    Found it, just had to change the directory from shader/ to shader/crt/

    When I’m hot-key scrolling through the CRT shaders in-game, most of them seem to work, but there’s no curvature on anyone, at least not by default I guess.

    As I said, most of them work, but the crt-geom.glsl(that suppose to have curvature by default?) just gives me an empty pink screen.

    Maybe this is because Im trying it on a 1280 x 1024 LCD?

    [quote=106261]there are curvature shaders in there by default – just try the various ones in the shaders/CRT folder. that crt-geom one should already be there: https://github.com/libretro/common-shaders/tree/master/crt

    you might have to update to the latest version of retroarch, depending on what version you’re running right now.

    [/quote]

    #106286
    dankcushions
    Participant

    All but the first one have curvature for me. eg, crt-interlaced-halation. the curvature isn’t quite right, but i’ve fiddled with various display settings in my setup so it might be ok for you, or at other resolutions.

    you’re right, cgcw-geom.glsl doesn’t seem to work, but the rest worked fine for me, at low framerates. i’ve attached a screenshot of an example:

    [attachment file=”image1.JPG”]

    (if you can’t see the image, go to https://www.petrockblock.com/wp-content/uploads/2015/09/image1.jpg – images don’t seem to be showing up for me)

    #106362
    kraken0667
    Participant

    Thanks. I scanned though the crt sub folders and found some more shaders, but for me, only “crt-interlaced-halation” has curvature. It’s slowing the system down a lot as predicted, but this is on my 1280 x 1024 display. Might work better on the small 320×240 lcd Ive ordered. Btw, can you edit the curvature in crt-interlaced-halation? I would like more symmetry, not like the default, which is wider down the bottom(as your screenshot)

    Or could you give an example of another curvature shader, cause for me, crt-interlaced-halation is the only one with curvature?

    [quote=106286]crt-interlaced-halation[/quote]

    #106535
    davej
    Participant

    I got bored and wrote a barrel distortion only shader.

    It runs speedily on my original 256Mb model B, which matches a model A+ hardware wise, even on a 1080 screen.

    It doesn’t do any sort of anti-aliasing so the results don’t look great by default – it has the image problems I described earlier. Because you are going to be using such a small screen however you can get a substantial image improvement by specifying that the input texture should have linear filtering rather than nearest. These two images show examples of the same screen with nearest and linear filtering.

    [attachment file=”106536″]

    [attachment file=”106537″]

    I’ve tried adding it as an attachment but it doesn’t let me and code tags don’t seem to like it either so here it is – until the #endif:

    #if defined(VERTEX)
    uniform mediump mat4 MVPMatrix;
    attribute mediump vec4 VertexCoord;
    attribute mediump vec2 TexCoord;

    varying mediump vec2 TEX0;

    void main()
    {
    TEX0 = TexCoord;
    gl_Position = MVPMatrix * VertexCoord;
    }
    #elif defined(FRAGMENT)
    uniform sampler2D Texture;
    uniform vec2 InputSize;
    uniform vec2 TextureSize;
    varying vec2 TEX0;

    const mediump float BARREL_DISTORTION = 0.25;
    const mediump float rescale = 1.0 – (0.25 * BARREL_DISTORTION);

    void main()
    {
    vec2 scale = TextureSize / InputSize;
    vec2 tex0 = TEX0 * scale;
    vec2 texcoord = tex0 – vec2(0.5);
    float rsq = texcoord.x * texcoord.x + texcoord.y * texcoord.y;
    texcoord = texcoord + (texcoord * (BARREL_DISTORTION * rsq));
    texcoord *= rescale;

    if (abs(texcoord.x) > 0.5 || abs(texcoord.y) > 0.5)
    gl_FragColor = vec4(0.0);
    else
    {
    texcoord += vec2(0.5);
    texcoord /= scale;
    vec3 colour = texture2D(Texture, texcoord).rgb;

    gl_FragColor = vec4(colour,1.0);
    }
    }
    #endif

    #106544
    kraken0667
    Participant

    That looks amazing Dave, just what I need. Thank you for being bored!!!

    And sorry again for being a n00b. How do I turn this code into a shader, do I make it into a .glsl file somehow?

    #106548
    davej
    Participant

    Save everything from the [b]#if defined(VERTEX)[/b] to the [b]#endif[/b] inclusive to a file with a name that ends in .glsl and copy it into the same directory as the other .glsl files. It should list it when you next try to pick a shader in retroarch.

    You can tweak the amount of barrel distortion by altering the value in the BARREL_DISTORTION line. Changing it to 0.0 will produce no distortion, the 0.25 it’s currently set to is quite large to demonstrate the effect.

    #106559
    kraken0667
    Participant

    Hi again Dave. Sorry, but I can’t get the shader to work. I copied everything from “#if defined(VERTEX)” to “#endif” and made a .glsl file, put it in there with the rest of the shaders. It’s listed, but without effect. Probably something stupid I missed. Any idea?

    [quote=106548]Save everything from the <strong class=”d4pbbc-bold”>#if defined(VERTEX) to the <strong class=”d4pbbc-bold”>#endif inclusive to a file with a name that ends in .glsl and copy it into the same directory as the other .glsl files. It should list it when you next try to pick a shader in retroarch.

    You can tweak the amount of barrel distortion by altering the value in the BARREL_DISTORTION line. Changing it to 0.0 will produce no distortion, the 0.25 it’s currently set to is quite large to demonstrate the effect.

    [/quote]

    #106594
    davej
    Participant

    I didn’t have to do anything that’s not included here. Once you select Apply Shader Changes you can see effect of the shader as the menu is semi-transparent.

    #106638
    kraken0667
    Participant

    I tried Apply Shader Changes in retroarch, no effect what so ever.

    Maybe you can download my shader and and check if there something wrong with the file maybe?

    http://www.filedropper.com/pe358shader

    #106660
    davej
    Participant

    I had a look at your file and found the problem. The forum has changed the minus signs to dashes – so OpenGL no longer thinks it’s a valid shader program.

    Change them to minus signs and it should be OK.

    #106761
    kraken0667
    Participant

    Thanks Dave, it’s working now and it looks PERFECT!

    Just PM me if you’d want something in exchange for the help.

    This is so appreciated!

    #107028
    dankcushions
    Participant

    thanks so much for this shader! i’ve tried it (with the – fix) and it works great, but i do get some slowdown. it’s strange – in something like sonic 1 you can see the screen hitching a little bit when it pans, when compared to without the shader. it’s annoying i can’t give better figures as the framerate counter in retroarch never seems to work for me! it is miles faster than the other CRT shaders, though.

    i’m running a pi 2 at 1080p (including my retroarch render resolution, which is set to ‘video output’).

    #107747
    kraken0667
    Participant

    I have a problem. The shader(s) works when scrolling through the dir with hotkey, but I can’t seem to get the shader to load on startup

    Am I doing something wrong? My retroarch.cfg looks like this:

    video_shader = /opt/retropie/emulators/retroarch/shader/pe358shader.glsl

    video_shader_enable = “true”

    video_shader_dir = /opt/retropie/emulators/retroarch/shader/

    #107753
    petrockblog
    Keymaster

    does it make a difference if you remove the quotes from video_shader_enable ?

    Can you put the full configurations you have edited somewhere (pastebin or similar)

    #107823
    kraken0667
    Participant

    No change when removing the quotes, no.

    I added a hotkey for scrolling through the shaders in-game, but that’s the only edit Ive made in retroarch.cfg really, other than editing the shader options as mentioned above.

Viewing 24 posts - 1 through 24 (of 24 total)
  • The forum ‘Video Output on RetroPie’ is closed to new topics and replies.