- render to texture bug fixes

This commit is contained in:
Magnus Norddahl 2019-08-17 03:08:34 +02:00
parent cea032e025
commit c88b94e1c9
9 changed files with 65 additions and 39 deletions

View file

@ -3458,7 +3458,7 @@ namespace CodeImp.DoomBuilder.Data
// Setup matrices
Vector3 offset = new Vector3(0f, 0f, -1.8f); // Sphere size is 10 mu
Matrix mworld = Matrix.Multiply(Matrix.Identity, Matrix.Translation(offset) * Matrix.Scaling(1.0f, 1.0f, yscale));
Matrix mworld = Matrix.Translation(offset) * Matrix.Scaling(1.0f, 1.0f, yscale);
Matrix mprojection = Matrix.PerspectiveFov(Angle2D.PIHALF, 1.0f, 0.5f, 100.0f);
// Place camera at origin
@ -3489,7 +3489,7 @@ namespace CodeImp.DoomBuilder.Data
meshes.Meshes[j].Draw(General.Map.Graphics);
}
General.Map.Graphics.CopyTexture(rendertarget, cubemap, (CubeMapFace)i);
General.Map.Graphics.CopyTexture(cubemap, (CubeMapFace)i);
}
// End rendering
@ -3703,23 +3703,23 @@ namespace CodeImp.DoomBuilder.Data
switch(face)
{
case CubeMapFace.PositiveX:
lookdir = new Vector3(1.0f, 0.0f, 0.0f);
lookdir = new Vector3(-1.0f, 0.0f, 0.0f);
updir = new Vector3(0.0f, 1.0f, 0.0f);
break;
case CubeMapFace.NegativeX:
lookdir = new Vector3(-1.0f, 0.0f, 0.0f);
lookdir = new Vector3(1.0f, 0.0f, 0.0f);
updir = new Vector3(0.0f, 1.0f, 0.0f);
break;
case CubeMapFace.PositiveY:
lookdir = new Vector3(0.0f, 1.0f, 0.0f);
updir = new Vector3(0.0f, 0.0f, -1.0f);
updir = new Vector3(0.0f, 0.0f, 1.0f);
break;
case CubeMapFace.NegativeY:
lookdir = new Vector3(0.0f, -1.0f, 0.0f);
updir = new Vector3(0.0f, 0.0f, 1.0f);
updir = new Vector3(0.0f, 0.0f, -1.0f);
break;
case CubeMapFace.PositiveZ:

View file

@ -25,6 +25,8 @@ namespace CodeImp.DoomBuilder.Rendering
device.SetVertexBuffer(Vertices);
device.SetIndexBuffer(Indices);
device.DrawIndexed(PrimitiveType.TriangleList, 0, Count / 3);
device.SetIndexBuffer(null);
device.SetVertexBuffer(null);
}
public void Dispose()

View file

@ -189,7 +189,7 @@ namespace CodeImp.DoomBuilder.Rendering
public void SetSamplerFilter(int unit, TextureFilter filter)
{
SetSamplerFilter(unit, filter, filter, filter, 0.0f);
SetSamplerFilter(unit, filter, filter, TextureFilter.None, 0.0f);
}
public void SetSamplerFilter(int unit, TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy)
@ -229,7 +229,7 @@ namespace CodeImp.DoomBuilder.Rendering
public void StartRendering(bool clear, Color4 backcolor, Texture target, bool usedepthbuffer)
{
RenderDevice_StartRendering(Handle, clear, backcolor.ToArgb(), target.Handle, true);
RenderDevice_StartRendering(Handle, clear, backcolor.ToArgb(), target.Handle, usedepthbuffer);
}
public void FinishRendering()
@ -247,9 +247,9 @@ namespace CodeImp.DoomBuilder.Rendering
RenderDevice_ClearTexture(Handle, backcolor.ToArgb(), texture.Handle);
}
public void CopyTexture(Texture src, CubeTexture dst, CubeMapFace face)
public void CopyTexture(CubeTexture dst, CubeMapFace face)
{
RenderDevice_CopyTexture(Handle, src.Handle, dst.Handle, face);
RenderDevice_CopyTexture(Handle, dst.Handle, face);
}
public void SetBufferData(IndexBuffer buffer, int[] data)
@ -449,7 +449,7 @@ namespace CodeImp.DoomBuilder.Rendering
static extern void RenderDevice_ClearTexture(IntPtr handle, int backcolor, IntPtr texture);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void RenderDevice_CopyTexture(IntPtr handle, IntPtr src, IntPtr dst, CubeMapFace face);
static extern void RenderDevice_CopyTexture(IntPtr handle, IntPtr dst, CubeMapFace face);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void RenderDevice_SetIndexBufferData(IntPtr handle, IntPtr buffer, int[] data, long size);
@ -551,7 +551,7 @@ namespace CodeImp.DoomBuilder.Rendering
world3d_main_highlight_fog_vertexcolor,
world3d_vertex_color,
world3d_constant_color,
world3d_lightpass // AlphaBlendEnable = true
world3d_lightpass
}
public enum UniformName : int

View file

@ -239,7 +239,7 @@ namespace CodeImp.DoomBuilder.Rendering
if((backimageverts == null) || (General.Map.Grid.Background.Texture == null)) break;
graphics.SetShader(aapass);
graphics.SetTexture(0, General.Map.Grid.Background.Texture);
SetDisplay2DSettings(1f / windowsize.Width, 1f / windowsize.Height, FSAA_FACTOR, layer.alpha, false);
SetDisplay2DSettings(1f / windowsize.Width, 1f / windowsize.Height, FSAA_FACTOR, layer.alpha, false, true);
graphics.Draw(PrimitiveType.TriangleStrip, 0, 2, backimageverts);
graphics.SetVertexBuffer(screenverts);
break;
@ -248,7 +248,7 @@ namespace CodeImp.DoomBuilder.Rendering
case RendererLayer.Grid:
graphics.SetShader(aapass);
graphics.SetTexture(0, backtex);
SetDisplay2DSettings(1f / backsize.Width, 1f / backsize.Height, FSAA_FACTOR, layer.alpha, false);
SetDisplay2DSettings(1f / backsize.Width, 1f / backsize.Height, FSAA_FACTOR, layer.alpha, false, true);
graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
break;
@ -256,7 +256,7 @@ namespace CodeImp.DoomBuilder.Rendering
case RendererLayer.Geometry:
graphics.SetShader(aapass);
graphics.SetTexture(0, plottertex);
SetDisplay2DSettings(1f / structsize.Width, 1f / structsize.Height, FSAA_FACTOR, layer.alpha, false);
SetDisplay2DSettings(1f / structsize.Width, 1f / structsize.Height, FSAA_FACTOR, layer.alpha, false, false);
graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
break;
@ -264,7 +264,7 @@ namespace CodeImp.DoomBuilder.Rendering
case RendererLayer.Things:
graphics.SetShader(aapass);
graphics.SetTexture(0, thingstex);
SetDisplay2DSettings(1f / thingssize.Width, 1f / thingssize.Height, FSAA_FACTOR, layer.alpha, false);
SetDisplay2DSettings(1f / thingssize.Width, 1f / thingssize.Height, FSAA_FACTOR, layer.alpha, false, true);
graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
break;
@ -272,7 +272,7 @@ namespace CodeImp.DoomBuilder.Rendering
case RendererLayer.Overlay:
graphics.SetShader(aapass);
graphics.SetTexture(0, overlaytex);
SetDisplay2DSettings(1f / overlaysize.Width, 1f / overlaysize.Height, FSAA_FACTOR, layer.alpha, false);
SetDisplay2DSettings(1f / overlaysize.Width, 1f / overlaysize.Height, FSAA_FACTOR, layer.alpha, false, true);
graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
break;
@ -280,7 +280,7 @@ namespace CodeImp.DoomBuilder.Rendering
case RendererLayer.Surface:
graphics.SetShader(aapass);
graphics.SetTexture(0, surfacetex);
SetDisplay2DSettings(1f / overlaysize.Width, 1f / overlaysize.Height, FSAA_FACTOR, layer.alpha, false);
SetDisplay2DSettings(1f / overlaysize.Width, 1f / overlaysize.Height, FSAA_FACTOR, layer.alpha, false, true);
graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
break;
}
@ -494,11 +494,14 @@ namespace CodeImp.DoomBuilder.Rendering
}
}
private void SetDisplay2DSettings(float texelx, float texely, float fsaafactor, float alpha, bool bilinear)
private void SetDisplay2DSettings(float texelx, float texely, float fsaafactor, float alpha, bool bilinear, bool flipY = false)
{
Vector4 values = new Vector4(texelx, texely, fsaafactor, alpha);
graphics.SetUniform(UniformName.rendersettings, values);
graphics.SetUniform(UniformName.transformsettings, worldmatrix * viewmatrix);
if (flipY)
graphics.SetUniform(UniformName.transformsettings, worldmatrix * viewmatrix * Matrix.Scaling(1f, -1f, 1f));
else
graphics.SetUniform(UniformName.transformsettings, worldmatrix * viewmatrix);
graphics.SetSamplerFilter(0, bilinear ? TextureFilter.Linear : TextureFilter.Point);
}

View file

@ -27,5 +27,5 @@ IndexBuffer* IndexBuffer_New()
void IndexBuffer_Delete(IndexBuffer* buffer)
{
delete buffer;
//delete buffer;
}

View file

@ -167,6 +167,7 @@ void RenderDevice::Draw(PrimitiveType type, int startIndex, int primitiveCount)
Context.Begin();
if (mNeedApply) ApplyChanges();
glDrawArrays(modes[(int)type], startIndex, toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type]);
CheckError();
Context.End();
}
@ -179,6 +180,7 @@ void RenderDevice::DrawIndexed(PrimitiveType type, int startIndex, int primitive
Context.Begin();
if (mNeedApply) ApplyChanges();
glDrawElements(modes[(int)type], toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type], GL_UNSIGNED_INT, (const void*)(startIndex * sizeof(uint32_t)));
CheckError();
Context.End();
}
@ -197,7 +199,9 @@ void RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCou
glBufferData(GL_ARRAY_BUFFER, vertcount * (size_t)VertexBuffer::FlatStride, static_cast<const uint8_t*>(data) + startIndex * (size_t)VertexBuffer::FlatStride, GL_STREAM_DRAW);
glBindVertexArray(mStreamVAO);
glDrawArrays(modes[(int)type], 0, vertcount);
CheckError();
ApplyVertexBuffer();
CheckError();
Context.End();
}
@ -246,13 +250,11 @@ void RenderDevice::Present()
void RenderDevice::ClearTexture(int backcolor, Texture* texture)
{
glBindFramebuffer(GL_FRAMEBUFFER, texture->GetFramebuffer(false));
glViewport(0, 0, texture->GetWidth(), texture->GetHeight());
glClearColor(RPART(backcolor) / 255.0f, GPART(backcolor) / 255.0f, BPART(backcolor) / 255.0f, APART(backcolor) / 255.0f);
glClear(GL_COLOR_BUFFER_BIT);
StartRendering(true, backcolor, texture, false);
FinishRendering();
}
void RenderDevice::CopyTexture(Texture* src, Texture* dst, CubeMapFace face)
void RenderDevice::CopyTexture(Texture* dst, CubeMapFace face)
{
static const GLenum facegl[] = {
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
@ -263,52 +265,56 @@ void RenderDevice::CopyTexture(Texture* src, Texture* dst, CubeMapFace face)
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
};
GLint oldFramebuffer = 0;
Context.Begin();
GLint oldTexture = 0;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &oldFramebuffer);
glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &oldTexture);
glBindFramebuffer(GL_READ_FRAMEBUFFER, src->GetFramebuffer(false));
glBindTexture(GL_TEXTURE_CUBE_MAP, dst->GetTexture());
glCopyTexSubImage2D(facegl[(int)face], 0, 0, 0, 0, 0, dst->GetWidth(), dst->GetHeight());
if (face == CubeMapFace::NegativeZ)
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, oldTexture);
glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFramebuffer);
Context.End();
}
void RenderDevice::SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size, VertexFormat format)
{
Context.Begin();
CheckError();
buffer->Format = format;
GLint oldbinding = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding);
glBindBuffer(GL_ARRAY_BUFFER, buffer->GetBuffer());
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, oldbinding);
CheckError();
Context.End();
}
void RenderDevice::SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size)
{
Context.Begin();
CheckError();
GLint oldbinding = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding);
glBindBuffer(GL_ARRAY_BUFFER, buffer->GetBuffer());
glBufferSubData(GL_ARRAY_BUFFER, destOffset, size, data);
glBindBuffer(GL_ARRAY_BUFFER, oldbinding);
CheckError();
Context.End();
}
void RenderDevice::SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t size)
{
Context.Begin();
CheckError();
GLint oldbinding = 0;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &oldbinding);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer->GetBuffer());
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oldbinding);
CheckError();
Context.End();
}
@ -363,13 +369,21 @@ Shader* RenderDevice::GetActiveShader()
void RenderDevice::ApplyChanges()
{
CheckError();
ApplyShader();
CheckError();
ApplyVertexBuffer();
CheckError();
ApplyIndexBuffer();
CheckError();
ApplyUniforms();
CheckError();
ApplyTextures();
CheckError();
ApplyRasterizerState();
CheckError();
ApplyBlendState();
CheckError();
ApplyDepthState();
CheckError();
@ -678,9 +692,9 @@ void RenderDevice_ClearTexture(RenderDevice* device, int backcolor, Texture* tex
device->ClearTexture(backcolor, texture);
}
void RenderDevice_CopyTexture(RenderDevice* device, Texture* src, Texture* dst, CubeMapFace face)
void RenderDevice_CopyTexture(RenderDevice* device, Texture* dst, CubeMapFace face)
{
device->CopyTexture(src, dst, face);
device->CopyTexture(dst, face);
}
void RenderDevice_SetVertexBufferData(RenderDevice* device, VertexBuffer* buffer, void* data, int64_t size, VertexFormat format)

View file

@ -101,7 +101,7 @@ public:
void FinishRendering();
void Present();
void ClearTexture(int backcolor, Texture* texture);
void CopyTexture(Texture* src, Texture* dst, CubeMapFace face);
void CopyTexture(Texture* dst, CubeMapFace face);
void SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size, VertexFormat format);
void SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size);

View file

@ -1,6 +1,7 @@
#include "Precomp.h"
#include "Texture.h"
#include <stdexcept>
Texture::Texture()
{
@ -76,7 +77,8 @@ GLuint Texture::GetTexture()
glBindTexture(GL_TEXTURE_2D, mTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, mWidth, mHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, !mPixels[0].empty() ? mPixels[0].data() : nullptr);
glGenerateMipmap(GL_TEXTURE_2D);
if (!mPixels[0].empty())
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, oldBinding);
}
@ -92,7 +94,8 @@ GLuint Texture::GetTexture()
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA8, mWidth, mHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, !mPixels[3].empty() ? mPixels[3].data() : nullptr);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA8, mWidth, mHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, !mPixels[4].empty() ? mPixels[4].data() : nullptr);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA8, mWidth, mHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, !mPixels[5].empty() ? mPixels[5].data() : nullptr);
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
if (!mPixels[0].empty())
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, oldBinding);
}
@ -111,7 +114,9 @@ GLuint Texture::GetFramebuffer(bool usedepthbuffer)
GLuint texture = GetTexture();
glGenFramebuffers(1, &mFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
throw std::runtime_error("glCheckFramebufferStatus did not return GL_FRAMEBUFFER_COMPLETE");
}
return mFramebuffer;
}
@ -130,8 +135,10 @@ GLuint Texture::GetFramebuffer(bool usedepthbuffer)
GLuint texture = GetTexture();
glGenFramebuffers(1, &mFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mDepthRenderbuffer);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
throw std::runtime_error("glCheckFramebufferStatus did not return GL_FRAMEBUFFER_COMPLETE");
}
return mFramebufferDepth;
}
@ -146,7 +153,7 @@ Texture* Texture_New()
void Texture_Delete(Texture* tex)
{
delete tex;
//delete tex;
}
void Texture_Set2DImage(Texture* handle, int width, int height)

View file

@ -66,5 +66,5 @@ VertexBuffer* VertexBuffer_New()
void VertexBuffer_Delete(VertexBuffer* buffer)
{
delete buffer;
//delete buffer;
}