Skip to content

Commit

Permalink
[GLES] More incremental work on OpenGL ES support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mellinoe committed Apr 6, 2018
1 parent d9eba89 commit 78eff33
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/NeoDemo/NeoDemo.csproj
Expand Up @@ -35,8 +35,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ShaderGen.Primitives" Version="1.1.0-gd961cf2e51" />
<PackageReference Include="ShaderGen.Build" Version="1.1.0-gd961cf2e51" PrivateAssets="All" />
<PackageReference Include="ShaderGen.Primitives" Version="1.2.0-ge02b1917b3" />
<PackageReference Include="ShaderGen.Build" Version="1.2.0-ge02b1917b3" PrivateAssets="All" />
<ProjectReference Include="..\Veldrid\Veldrid.csproj" />
<ProjectReference Include="..\Veldrid.SDL2\Veldrid.SDL2.csproj" />
<ProjectReference Include="..\Veldrid.StartupUtilities\Veldrid.StartupUtilities.csproj" />
Expand Down
11 changes: 6 additions & 5 deletions src/NeoDemo/ShaderHelper.cs
Expand Up @@ -6,10 +6,10 @@ namespace Veldrid.NeoDemo
public static class ShaderHelper
{
public static Shader LoadShader(
GraphicsDevice gd,
ResourceFactory factory,
string setName,
ShaderStages stage,
GraphicsDevice gd,
ResourceFactory factory,
string setName,
ShaderStages stage,
string entryPoint)
{
Shader shader = factory.CreateShader(new ShaderDescription(stage, LoadBytecode(factory, setName, stage), entryPoint));
Expand Down Expand Up @@ -56,8 +56,9 @@ private static string GetSourceExtension(GraphicsBackend backend)
case GraphicsBackend.Direct3D11: return ".hlsl";
case GraphicsBackend.Vulkan: return ".450.glsl";
case GraphicsBackend.OpenGL:
case GraphicsBackend.OpenGLES:
return ".330.glsl";
case GraphicsBackend.OpenGLES:
return ".300.glsles";
case GraphicsBackend.Metal:
return ".metallib";
default: throw new InvalidOperationException("Invalid Graphics backend: " + backend);
Expand Down
14 changes: 14 additions & 0 deletions src/Veldrid.ImGui/Assets/GLSLES/imgui-frag.glsles
@@ -0,0 +1,14 @@
#version 300 es
precision highp float;

uniform sampler2D FontTexture;

in vec4 color;
in vec2 texCoord;

out vec4 outputColor;

void main()
{
outputColor = color * texture(FontTexture, texCoord);
}
20 changes: 20 additions & 0 deletions src/Veldrid.ImGui/Assets/GLSLES/imgui-vertex.glsles
@@ -0,0 +1,20 @@
#version 300 es

uniform ProjectionMatrixBuffer
{
mat4 projection_matrix;
};

in vec2 in_position;
in vec2 in_texCoord;
in vec4 in_color;

out vec4 color;
out vec2 texCoord;

void main()
{
gl_Position = projection_matrix * vec4(in_position, 0, 1);
color = in_color;
texCoord = in_texCoord;
}
6 changes: 5 additions & 1 deletion src/Veldrid.ImGui/ImGuiRenderer.cs
Expand Up @@ -209,11 +209,15 @@ private byte[] LoadEmbeddedShaderCode(ResourceFactory factory, string name, Shad
return GetEmbeddedResourceBytes(resourceName);
}
case GraphicsBackend.OpenGL:
case GraphicsBackend.OpenGLES:
{
string resourceName = name + ".glsl";
return GetEmbeddedResourceBytes(resourceName);
}
case GraphicsBackend.OpenGLES:
{
string resourceName = name + ".glsles";
return GetEmbeddedResourceBytes(resourceName);
}
case GraphicsBackend.Vulkan:
{
string resourceName = name + ".spv";
Expand Down
6 changes: 6 additions & 0 deletions src/Veldrid.ImGui/Veldrid.ImGui.csproj
Expand Up @@ -13,6 +13,12 @@
<EmbeddedResource Include="Assets/GLSL/imgui-frag.glsl">
<LogicalName>imgui-frag.glsl</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Assets/GLSLES/imgui-vertex.glsles">
<LogicalName>imgui-vertex.glsles</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Assets/GLSLES/imgui-frag.glsles">
<LogicalName>imgui-frag.glsles</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Assets/HLSL/imgui-vertex.hlsl.bytes">
<LogicalName>imgui-vertex.hlsl.bytes</LogicalName>
</EmbeddedResource>
Expand Down
46 changes: 42 additions & 4 deletions src/Veldrid.OpenGLBindings/OpenGLNative.cs
Expand Up @@ -189,11 +189,19 @@ public static FramebufferErrorCode glCheckFramebufferStatus(FramebufferTarget ta
public static void glViewportIndexed(uint index, float x, float y, float w, float h)
=> p_glViewportIndexedf(index, x, y, w, h);

private delegate void glViewport_t(int x, int y, uint width, uint height);
private static glViewport_t p_glViewport;
public static void glViewport(int x, int y, uint width, uint height) => p_glViewport(x, y, width, height);

private delegate void glDepthRangeIndexed_t(uint index, double nearVal, double farVal);
private static glDepthRangeIndexed_t p_glDepthRangeIndexed;
public static void glDepthRangeIndexed(uint index, double nearVal, double farVal)
=> p_glDepthRangeIndexed(index, nearVal, farVal);

private delegate void glDepthRangef_t(float n, float f);
private static glDepthRangef_t p_glDepthRangef;
public static void glDepthRangef(float n, float f) => p_glDepthRangef(n, f);

private delegate void glBufferSubData_t(BufferTarget target, IntPtr offset, UIntPtr size, void* data);
private static glBufferSubData_t p_glBufferSubData;
public static void glBufferSubData(BufferTarget target, IntPtr offset, UIntPtr size, void* data)
Expand All @@ -209,6 +217,10 @@ public static void glNamedBufferSubData(uint buffer, IntPtr offset, uint size, v
public static void glScissorIndexed(uint index, int left, int bottom, uint width, uint height)
=> p_glScissorIndexed(index, left, bottom, width, height);

private delegate void glScissor_t(int x, int y, uint width, uint height);
private static glScissor_t p_glScissor;
public static void glScissor(int x, int y, uint width, uint height) => p_glScissor(x, y, width, height);

private delegate void glPixelStorei_t(PixelStoreParameter pname, int param);
private static glPixelStorei_t p_glPixelStorei;
public static void glPixelStorei(PixelStoreParameter pname, int param) => p_glPixelStorei(pname, param);
Expand Down Expand Up @@ -1237,13 +1249,20 @@ public static void glGetCompressedTextureImage(uint texture, int level, uint buf
GetTextureParameter pname,
int* @params) => p_glGetTexLevelParameteriv(target, level, pname, @params);

public static void LoadAllFunctions(IntPtr glContext, Func<string, IntPtr> getProcAddress)
public static void LoadGetString(IntPtr glContext, Func<string, IntPtr> getProcAddress)
{
s_getProcAddress = getProcAddress;
LoadFunction("glGetString", out p_glGetString);
}

public static void LoadAllFunctions(IntPtr glContext, Func<string, IntPtr> getProcAddress, bool gles)
{
s_getProcAddress = getProcAddress;

// Common functions

LoadFunction("glCompressedTexSubImage2D", out p_glCompressedTexSubImage2D);
LoadFunction("glCompressedTexSubImage3D", out p_glCompressedTexSubImage3D);
LoadFunction("glCopyImageSubData", out p_glCopyImageSubData);
LoadFunction("glStencilFuncSeparate", out p_glStencilFuncSeparate);
LoadFunction("glStencilOpSeparate", out p_glStencilOpSeparate);
LoadFunction("glStencilMask", out p_glStencilMask);
Expand Down Expand Up @@ -1286,7 +1305,6 @@ public static void LoadAllFunctions(IntPtr glContext, Func<string, IntPtr> getPr
LoadFunction("glDeleteTextures", out p_glDeleteTextures);
LoadFunction("glCheckFramebufferStatus", out p_glCheckFramebufferStatus);
LoadFunction("glBindBuffer", out p_glBindBuffer);
LoadFunction("glViewportIndexedf", out p_glViewportIndexedf);
LoadFunction("glDepthRangeIndexed", out p_glDepthRangeIndexed);
LoadFunction("glBufferSubData", out p_glBufferSubData);
LoadFunction("glNamedBufferSubData", out p_glNamedBufferSubData);
Expand Down Expand Up @@ -1347,7 +1365,6 @@ public static void LoadAllFunctions(IntPtr glContext, Func<string, IntPtr> getPr
LoadFunction("glBindTextureUnit", out p_glBindTextureUnit);
LoadFunction("glTexParameteri", out p_glTexParameteri);
LoadFunction("glGetStringi", out p_glGetStringi);
LoadFunction("glGetString", out p_glGetString);
LoadFunction("glObjectLabel", out p_glObjectLabel);
LoadFunction("glTexImage2DMultisample", out p_glTexImage2DMultisample);
LoadFunction("glTexImage3DMultisample", out p_glTexImage3DMultisample);
Expand Down Expand Up @@ -1384,6 +1401,27 @@ public static void LoadAllFunctions(IntPtr glContext, Func<string, IntPtr> getPr
LoadFunction("glCopyNamedBufferSubData", out p_glCopyNamedBufferSubData);
LoadFunction("glCreateBuffers", out p_glCreateBuffers);
LoadFunction("glCreateTextures", out p_glCreateTextures);

if (!gles)
{
LoadFunction("glViewportIndexedf", out p_glViewportIndexedf);
LoadFunction("glCopyImageSubData", out p_glCopyImageSubData);
}
else
{
LoadFunction("glViewport", out p_glViewport);
LoadFunction("glDepthRangef", out p_glDepthRangef);
LoadFunction("glScissor", out p_glScissor);
LoadFunction("glCopyImageSubData", out p_glCopyImageSubData);
if (p_glCopyImageSubData == null)
{
LoadFunction("glCopyImageSubDataOES", out p_glCopyImageSubData);
}
if (p_glCopyImageSubData == null)
{
LoadFunction("glCopyImageSubDataEXT", out p_glCopyImageSubData);
}
}
}

private static void LoadFunction<T>(string name, out T field)
Expand Down
64 changes: 47 additions & 17 deletions src/Veldrid/OpenGL/OpenGLCommandExecutor.cs
Expand Up @@ -2,13 +2,12 @@
using static Veldrid.OpenGLBinding.OpenGLNative;
using static Veldrid.OpenGL.OpenGLUtil;
using Veldrid.OpenGLBinding;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Veldrid.OpenGL
{
internal unsafe class OpenGLCommandExecutor
{
private readonly GraphicsBackend _backend;
private readonly OpenGLTextureSamplerManager _textureSamplerManager;
private readonly StagingMemoryPool _stagingMemoryPool;
private OpenGLExtensions _extensions;
Expand All @@ -32,10 +31,12 @@ internal unsafe class OpenGLCommandExecutor
private bool _graphicsPipelineActive;

public OpenGLCommandExecutor(
GraphicsBackend backend,
OpenGLTextureSamplerManager textureSamplerManager,
OpenGLExtensions extensions,
StagingMemoryPool stagingMemoryPool)
{
_backend = backend;
_extensions = extensions;
_textureSamplerManager = textureSamplerManager;
_stagingMemoryPool = stagingMemoryPool;
Expand All @@ -49,7 +50,6 @@ public void ClearColorTarget(uint index, RgbaFloat clearColor)
{
if (!_isSwapchainFB)
{
//glDrawBuffer((DrawBufferMode)((uint)DrawBufferMode.ColorAttachment0 + index));
DrawBuffersEnum bufs = (DrawBuffersEnum)((uint)DrawBuffersEnum.ColorAttachment0 + index);
glDrawBuffers(1, &bufs);
CheckLastError();
Expand Down Expand Up @@ -703,13 +703,28 @@ private uint GetShaderStorageBaseIndex(uint slot, bool graphics)

public void SetScissorRect(uint index, uint x, uint y, uint width, uint height)
{
glScissorIndexed(
index,
(int)x,
(int)(_viewports[(int)index].Height - (int)height - y),
width,
height);
CheckLastError();
if (_backend == GraphicsBackend.OpenGL)
{
glScissorIndexed(
index,
(int)x,
(int)(_viewports[(int)index].Height - (int)height - y),
width,
height);
CheckLastError();
}
else
{
if (index == 0)
{
glScissor(
(int)x,
(int)(_viewports[(int)index].Height - (int)height - y),
width,
height);
CheckLastError();
}
}
}

public void SetVertexBuffer(uint index, DeviceBuffer vb)
Expand All @@ -725,14 +740,29 @@ public void SetViewport(uint index, ref Viewport viewport)
{
_viewports[(int)index] = viewport;

float left = viewport.X;
float bottom = _fb.Height - (viewport.Y + viewport.Height);
if (_backend == GraphicsBackend.OpenGL)
{
float left = viewport.X;
float bottom = _fb.Height - (viewport.Y + viewport.Height);

glViewportIndexed(index, left, bottom, viewport.Width, viewport.Height);
CheckLastError();
glViewportIndexed(index, left, bottom, viewport.Width, viewport.Height);
CheckLastError();

glDepthRangeIndexed(index, viewport.MinDepth, viewport.MaxDepth);
CheckLastError();
glDepthRangeIndexed(index, viewport.MinDepth, viewport.MaxDepth);
CheckLastError();
}
else
{
// TODO CAPABILITY
if (index == 0)
{
glViewport((int)viewport.X, (int)viewport.Y, (uint)viewport.Width, (uint)viewport.Height);
CheckLastError();

glDepthRangef(viewport.MinDepth, viewport.MaxDepth);
CheckLastError();
}
}
}

public void UpdateBuffer(DeviceBuffer buffer, uint bufferOffsetInBytes, IntPtr dataPtr, uint sizeInBytes)
Expand Down Expand Up @@ -1115,7 +1145,7 @@ public void CopyBuffer(DeviceBuffer source, uint sourceOffset, DeviceBuffer dest
srcGLTexture.EnsureResourcesCreated();
dstGLTexture.EnsureResourcesCreated();

if (_extensions.ARB_CopyImage && depth == 1)
if (_extensions.CopyImage && depth == 1)
{
// glCopyImageSubData does not work properly when depth > 1, so use the awful roundabout copy.
uint srcZOrLayer = Math.Max(srcBaseArrayLayer, srcZ);
Expand Down
47 changes: 39 additions & 8 deletions src/Veldrid/OpenGL/OpenGLExtensions.cs
@@ -1,32 +1,45 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Veldrid.OpenGL
{
internal class OpenGLExtensions
{
private readonly HashSet<string> _extensions;
private readonly int _major;
private readonly int _minor;

public OpenGLExtensions(HashSet<string> extensions)
public OpenGLExtensions(HashSet<string> extensions, GraphicsBackend backend, int major, int minor)
{
_extensions = extensions;
ARB_TextureStorage = IsExtensionSupported("GL_ARB_texture_storage"); // OpenGL 4.2 / 4.3 (multisampled)
_major = major;
_minor = minor;

TextureStorage = IsExtensionSupported("GL_ARB_texture_storage") // OpenGL 4.2 / 4.3 (multisampled)
|| GLESVersion(backend, 3, 0);
ARB_DirectStateAccess = IsExtensionSupported("GL_ARB_direct_state_access");
ARB_MultiBind = IsExtensionSupported("GL_ARB_multi_bind");
ARB_TextureView = IsExtensionSupported("GL_ARB_texture_view"); // OpenGL 4.3
ARB_CopyImage = IsExtensionSupported("GL_ARB_copy_image");
CopyImage = IsExtensionSupported("GL_ARB_copy_image")
|| GLESVersion(backend, 3, 2)
|| IsExtensionSupported("GL_OES_copy_image")
|| IsExtensionSupported("GL_EXT_copy_image");
ARB_DebugOutput = IsExtensionSupported("GL_ARB_debug_output");
KHR_Debug = IsExtensionSupported("GL_KHR_debug");
ARB_ComputeShader = IsExtensionSupported("GL_ARB_compute_shader");

ComputeShaders = IsExtensionSupported("GL_ARB_compute_shader") || GLESVersion(backend, 3, 1);
}

public readonly bool ARB_DirectStateAccess;
public readonly bool ARB_MultiBind;
public readonly bool ARB_TextureStorage;
public readonly bool ARB_TextureView;
public readonly bool ARB_CopyImage;
public readonly bool ARB_DebugOutput;
public readonly bool KHR_Debug;
public readonly bool ARB_ComputeShader;

// Differs between GL / GLES
public readonly bool TextureStorage;
public readonly bool CopyImage;
public readonly bool ComputeShaders;

/// <summary>
/// Returns a value indicating whether the given extension is supported.
Expand All @@ -37,5 +50,23 @@ public bool IsExtensionSupported(string extension)
{
return _extensions.Contains(extension);
}


private bool GLESVersion(GraphicsBackend backend, int major, int minor)
{
if (backend == GraphicsBackend.OpenGLES)
{
if (_major > major)
{
return true;
}
else
{
return _major == major && _minor >= minor;
}
}

return false;
}
}
}

0 comments on commit 78eff33

Please sign in to comment.