Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Penumbra with RenderTarget2D and PointClamp sampler state #19

Open
matt-clegg opened this issue Nov 23, 2018 · 2 comments
Open

Penumbra with RenderTarget2D and PointClamp sampler state #19

matt-clegg opened this issue Nov 23, 2018 · 2 comments
Labels

Comments

@matt-clegg
Copy link

I'm rendering my game to a scaled down render target, which I then render at the full window size. It seems that penumbra doesn't want to work when I use a render target.

Here's some pseudo code showing my setup:

// Initialize Penumbra, some lights and hulls.
new PenumbraComponent();

// Create a render target 
RenderTarget2D renderTarget = new RenderTarget2D(GraphicsDevice, WindowWidth / 2, WindowHeight / 2);


// Inside the draw method...
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(Color.White);

spriteBatch.begin(samplerState: SamplerState.PointClamp);

// DO GAME DRAWING HERE

spruteBatch.End();

GraphicsDevice.SetRenderTarget(null);
penumbra.BeginDraw();

spriteBatch.Begin(samplerState: SamplerState.PointClamp);
spriteBatch.Draw(renderTarget, new Rectangle(0, 0, WindowWidth, WindowHeight), Color.White);

spriteBatch.End();
penumbra.Draw(gameTime);

This resulted in the lighting rendered at the original scale of the render target, and the game at the scaled size:
scaled lights

To overcome this, I scaled up the positions of all hulls and lights by 2 (the render target is half the window size). This seemed to fix it, but then I saw the SamplerState.PointClamp wasn't working.

This is what the game looks like without lighting:
no lights

And with lighting:
lighting

It's turned blurry, as if the PointClamp filter was disabled.

Are there any ways to overcome this issue? Would it be possible to allow penumbra to render to the same RenderTarget the game is drawn to, allowing it to be scaled up correctly, with the PointClamp sampler state?

@discosultan
Copy link
Owner

Hey @matt-clegg, thanks for bringing out this issue! This indeed seems to be a limitation of Penumbra and it'd be nice if we could find a nice solution for this scenario.

Penumbra is internally also operating with custom render targets. It recreates the render targets only on window size changes and it always matches the size of back buffer. You are drawing on a custom render target with a different size and this info is currently not picked up by Penumbra. One solution I see is to, instead of using back buffer size, use the size of last set render target.

Before I move forward with this, can you try the following:

  1. Instead of using the NuGet package of Penumbra, clone the repo and reference the project directly.
  2. Modify the following lines in Penumbra source for render target creation to match the size of your render target:
    Lightmap = new RenderTarget2D(Engine.Device, ViewportWidth, ViewportHeight, false,
    pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount, pp.RenderTargetUsage);
    LightmapBindings[0] = Lightmap;
    DiffuseMap = new RenderTarget2D(Engine.Device, ViewportWidth, ViewportHeight, false,
    pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount, pp.RenderTargetUsage);
    DiffuseMapBindings[0] = DiffuseMap;

See if that makes a difference (without you having to scale hulls nor lights).

If that doesn't work, perhaps you can share a demo of the issue and I can try debugging it myself.

@discosultan
Copy link
Owner

discosultan commented Apr 5, 2019

See #20 for a potential workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants