- draw streamed vertices

This commit is contained in:
Magnus Norddahl 2019-08-16 07:45:34 +02:00
parent dc3bd6cb9c
commit fb137d46db
3 changed files with 50 additions and 14 deletions

View file

@ -236,7 +236,7 @@ namespace CodeImp.DoomBuilder.Rendering
public void Draw(PrimitiveType type, int startIndex, int primitiveCount, FlatVertex[] data)
{
RenderDevice_DrawData(Handle, type, startIndex, primitiveCount, data);
RenderDevice_DrawData(Handle, type, startIndex, primitiveCount, data, Marshal.SizeOf<FlatVertex>());
}
public void SetVertexDeclaration(VertexDeclaration decl)
@ -447,7 +447,7 @@ namespace CodeImp.DoomBuilder.Rendering
static extern void RenderDevice_DrawIndexed(IntPtr handle, PrimitiveType type, int startIndex, int primitiveCount);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void RenderDevice_DrawData(IntPtr handle, PrimitiveType type, int startIndex, int primitiveCount, FlatVertex[] data);
static extern void RenderDevice_DrawData(IntPtr handle, PrimitiveType type, int startIndex, int primitiveCount, FlatVertex[] data, int stride);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void RenderDevice_SetVertexDeclaration(IntPtr handle, IntPtr decl);

View file

@ -15,6 +15,7 @@ RenderDevice::RenderDevice(HWND hwnd) : Context(hwnd)
if (Context)
{
Context.Begin();
glGenBuffers(1, &mStreamVertexBuffer);
mShaderManager = std::make_unique<ShaderManager>();
Context.End();
}
@ -25,6 +26,7 @@ RenderDevice::~RenderDevice()
if (Context)
{
Context.Begin();
glDeleteBuffers(1, &mStreamVertexBuffer);
mShaderManager->ReleaseResources();
Context.End();
}
@ -171,8 +173,22 @@ void RenderDevice::DrawIndexed(PrimitiveType type, int startIndex, int primitive
Context.End();
}
void RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data)
void RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data, int stride)
{
static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP };
static const int toVertexCount[] = { 2, 3, 1 };
static const int toVertexStart[] = { 0, 0, 2 };
int vertcount = toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type];
Context.Begin();
mStreamBufferStride = stride;
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertcount * (size_t)stride, static_cast<const uint8_t*>(data) + startIndex * (size_t)stride, GL_STREAM_DRAW);
ApplyChanges();
glDrawArrays(modes[(int)type], 0, vertcount);
mStreamBufferStride = 0;
Context.End();
}
void RenderDevice::SetVertexDeclaration(VertexDeclaration* decl)
@ -418,21 +434,38 @@ void RenderDevice::ApplyVertexBuffers()
glBindVertexArray(mVAO);
}
for (size_t i = 0; i < mVertexDeclaration->Elements.size(); i++)
if (mStreamBufferStride)
{
const auto& element = mVertexDeclaration->Elements[i];
auto& vertBinding = mVertexBindings[element.Stream];
GLuint location = (int)element.Usage;
if (vertBinding.Buffer)
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
for (size_t i = 0; i < mVertexDeclaration->Elements.size(); i++)
{
GLuint vertexbuffer = vertBinding.Buffer->GetBuffer();
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
const auto& element = mVertexDeclaration->Elements[i];
GLuint location = (int)element.Usage;
glEnableVertexAttribArray(location);
glVertexAttribPointer(location, typeSize[(int)element.Type], type[(int)element.Type], typeNormalized[(int)element.Type], vertBinding.Stride, (const void*)(element.Offset + (ptrdiff_t)vertBinding.Offset));
glVertexAttribPointer(location, typeSize[(int)element.Type], type[(int)element.Type], typeNormalized[(int)element.Type], mStreamBufferStride, (const void*)element.Offset);
mEnabledVertexAttributes[location] = 2;
}
}
else
{
for (size_t i = 0; i < mVertexDeclaration->Elements.size(); i++)
{
const auto& element = mVertexDeclaration->Elements[i];
auto& vertBinding = mVertexBindings[element.Stream];
GLuint location = (int)element.Usage;
if (vertBinding.Buffer)
{
GLuint vertexbuffer = vertBinding.Buffer->GetBuffer();
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glEnableVertexAttribArray(location);
glVertexAttribPointer(location, typeSize[(int)element.Type], type[(int)element.Type], typeNormalized[(int)element.Type], vertBinding.Stride, (const void*)(element.Offset + (ptrdiff_t)vertBinding.Offset));
mEnabledVertexAttributes[location] = 2;
}
}
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
@ -654,9 +687,9 @@ void RenderDevice_DrawIndexed(RenderDevice* device, PrimitiveType type, int star
device->DrawIndexed(type, startIndex, primitiveCount);
}
void RenderDevice_DrawData(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount, const void* data)
void RenderDevice_DrawData(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount, const void* data, int stride)
{
device->DrawData(type, startIndex, primitiveCount, data);
device->DrawData(type, startIndex, primitiveCount, data, stride);
}
void RenderDevice_SetVertexDeclaration(RenderDevice* device, VertexDeclaration* decl)

View file

@ -96,7 +96,7 @@ public:
void SetSamplerState(int unit, TextureAddress addressU, TextureAddress addressV, TextureAddress addressW);
void Draw(PrimitiveType type, int startIndex, int primitiveCount);
void DrawIndexed(PrimitiveType type, int startIndex, int primitiveCount);
void DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data);
void DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data, int stride);
void SetVertexDeclaration(VertexDeclaration* decl);
void StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer);
void FinishRendering();
@ -176,6 +176,9 @@ public:
UniformEntry mUniforms[4 * 16 + 12 * 4];
GLuint mStreamVertexBuffer = 0;
int mStreamBufferStride = 0;
Cull mCullMode = Cull::None;
FillMode mFillMode = FillMode::Solid;
bool mAlphaTest = false;