Skip to content

Commit

Permalink
Feature/improve generation (#10)
Browse files Browse the repository at this point in the history
* minor generation changes 
* add new draft project

---------

Co-authored-by: Alexandre Mommers <[email protected]>
  • Loading branch information
ygdrasil-io and Alexandre Mommers authored Dec 7, 2023
1 parent f812bb1 commit 20cc4d2
Show file tree
Hide file tree
Showing 45 changed files with 2,874 additions and 26 deletions.
9 changes: 9 additions & 0 deletions bindings/angle/example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

plugins {
kotlin("jvm") version "1.9.10"
}

dependencies {
implementation(project(":angle4k"))
}

73 changes: 73 additions & 0 deletions bindings/angle/example/src/main/kotlin/example/HelloTriangle.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package example

import com.sun.jna.Memory
import example.toolkit.CompileProgram
import example.toolkit.SampleApplication
import libangle.libEGLLibrary
import libgles.*

class HelloTriangle : SampleApplication("HelloTriangle") {
private var mProgram: GLuint = 0

override fun initialize(): Boolean {
val kVS = """
attribute vec4 vPosition;
void main()
{
gl_Position = vPosition;
}"""

val kFS = """precision mediump float;
void main()
{
libEGLLibrary.gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
})"""

mProgram = CompileProgram(kVS, kFS);
if (mProgram == 0) {
return false
}


libGLESv2Library.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

return true;
}

override fun destroy() { libGLESv2Library.glDeleteProgram(mProgram); }

/**
*
*/
override fun draw() {
val vertices = floatArrayOf(
0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f,
)
val verticesBuffer = Memory(vertices.size * 4L).apply {
write(0, vertices, 0, vertices.size)
}

// Set the viewport
libGLESv2Library.glViewport(0, 0, window.width, window.height)

// Clear the color buffer
libGLESv2Library.glClear(GL_COLOR_BUFFER_BIT)

// Use the program object
libGLESv2Library.glUseProgram(mProgram)

// Load the vertex data
libGLESv2Library.glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE.toByte(), 0, verticesBuffer)
libGLESv2Library.glEnableVertexAttribArray(0)

libGLESv2Library.glDrawArrays(GL_TRIANGLES, 0, 3)
}

}


fun main() {
HelloTriangle()
.run()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package example.toolkit

import libangle.EGL_DONT_CARE
import libangle.EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE
import libangle.EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE
import libangle.Feature
import java.util.*

class PlatformMethods

class EGLPlatformParameters {
var renderer: Int = EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE
var majorVersion: Int = EGL_DONT_CARE
var minorVersion: Int = EGL_DONT_CARE
var deviceType: Int = EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE
var presentPath: Int = EGL_DONT_CARE
var debugLayersEnabled: Int = EGL_DONT_CARE
var robustness: Int = EGL_DONT_CARE
var displayPowerPreference: Int = EGL_DONT_CARE
var enabledFeatureOverrides: MutableList<Feature> = ArrayList()
var disabledFeatureOverrides: MutableList<Feature> = ArrayList()
var platformMethods: PlatformMethods? = null

constructor() // Constructor por defecto

constructor(renderer: Int) {
this.renderer = renderer
}

constructor(renderer: Int, majorVersion: Int, minorVersion: Int, deviceType: Int) {
this.renderer = renderer
this.majorVersion = majorVersion
this.minorVersion = minorVersion
this.deviceType = deviceType
}

constructor(renderer: Int, majorVersion: Int, minorVersion: Int, deviceType: Int, presentPath: Int) {
this.renderer = renderer
this.majorVersion = majorVersion
this.minorVersion = minorVersion
this.deviceType = deviceType
this.presentPath = presentPath
}

fun tie(): List<Any?> {
return listOf(renderer, majorVersion, minorVersion, deviceType, presentPath, debugLayersEnabled, robustness, displayPowerPreference, disabledFeatureOverrides, enabledFeatureOverrides, platformMethods)
}

fun enable(feature: Feature): EGLPlatformParameters {
enabledFeatureOverrides.add(feature)
return this
}

fun disable(feature: Feature): EGLPlatformParameters {
disabledFeatureOverrides.add(feature)
return this
}
}
246 changes: 246 additions & 0 deletions bindings/angle/example/src/main/kotlin/example/toolkit/EGLWindow.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
package example.toolkit

import libangle.*
import libgles.libGLESv2Library

class EGLWindow(window: OSWindow) {

private lateinit var mContext: Any

fun initializeGLWithResult(
osWindow: OSWindow,
glWindowingLibrary: Library,
driverType: GLESDriverType,
platformParams: EGLPlatformParameters,
configParams: ConfigParameters
): GLWindowResult {
if (!initializeDisplay(osWindow, glWindowingLibrary, driverType, platformParams)) {
return GLWindowResult.Error
}

val res = initializeSurface(osWindow, glWindowingLibrary, configParams)
if (res != GLWindowResult.NoError) {
return res
}

if (!initializeContext()) {
return GLWindowResult.Error
}

return GLWindowResult.NoError
}

fun initializeDisplay(osWindow: OSWindow, glWindowingLibrary: Library, driverType: GLESDriverType, params: EGLPlatformParameters): Boolean {
if (driverType == GLESDriverType.ZinkEGL) {
val driDirStream = StringBuilder()
val s = GetPathSeparator()
driDirStream.append(GetModuleDirectory()).append("mesa").append(s).append("src").append(s).append("gallium").append(s).append("targets").append(s).append("dri")

val driDir = driDirStream.toString()

SetEnvironmentVar("MESA_LOADER_DRIVER_OVERRIDE", "zink")
SetEnvironmentVar("LIBGL_DRIVERS_PATH", driDir)
}

if (ANGLE_USE_UTIL_LOADER) {
var getProcAddress: PFNEGLGETPROCADDRESSPROC
glWindowingLibrary.getAs("eglGetProcAddress", getProcAddress)
if (getProcAddress == null) {
println("Cannot load eglGetProcAddress")
return false
}

// Likely we will need to use a fallback to Library::getAs on non-ANGLE platforms.
LoadUtilEGL(getProcAddress)
}

// EGL_NO_DISPLAY + EGL_EXTENSIONS returns NULL before Android 10
val extensionString = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS) as String?
if (extensionString == null) {
// fallback to an empty string for strstr
extensionString = ""
}

val displayAttributes = mutableListOf(
EGL_PLATFORM_ANGLE_TYPE_ANGLE,
params.renderer,
EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE,
params.majorVersion,
EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE,
params.minorVersion
)

if (params.deviceType != EGL_DONT_CARE) {
displayAttributes.addAll(listOf(
EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
params.deviceType
))
}

if (params.presentPath != EGL_DONT_CARE) {
if (extensionString.indexOf("EGL_ANGLE_experimental_present_path") == -1) {
destroyGL()
return false
}

displayAttributes.addAll(listOf(
EGL_EXPERIMENTAL_PRESENT_PATH_ANGLE,
params.presentPath
))
}

// Set debug layer settings if requested.
if (params.debugLayersEnabled != EGL_DONT_CARE) {
displayAttributes.addAll(listOf(
EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE,
params.debugLayersEnabled
))
}

if (params.platformMethods != null) {
displayAttributes.addAll(listOf(
EGL_PLATFORM_ANGLE_PLATFORM_METHODS_ANGLEX,
params.platformMethods.toLong()
))
}

if (params.displayPowerPreference != EGL_DONT_CARE) {
displayAttributes.addAll(listOf(
EGL_POWER_PREFERENCE_ANGLE,
params.displayPowerPreference
))
}

val enabledFeatureOverrides = mutableListOf<String>()
val disabledFeatureOverrides = mutableListOf<String>()

for (feature in params.enabledFeatureOverrides) {
enabledFeatureOverrides.add(GetFeatureName(feature))
}
for (feature in params.disabledFeatureOverrides) {
disabledFeatureOverrides.add(GetFeatureName(feature))
}

val hasFeatureControlANGLE = extensionString.indexOf("EGL_ANGLE_feature_control") != -1

if (!hasFeatureControlANGLE &&
(!enabledFeatureOverrides.isEmpty() || !disabledFeatureOverrides.isEmpty())) {
println("Missing EGL_ANGLE_feature_control.")
destroyGL()
return false
}

if (!disabledFeatureOverrides.isEmpty()) {
disabledFeatureOverrides.add(null)

displayAttributes.addAll(listOf(
EGL_FEATURE_OVERRIDES_DISABLED_ANGLE,
disabledFeatureOverrides.toLongArray()
))
}

if (hasFeatureControlANGLE) {
// Always enable exposeNonConformantExtensionsAndVersions in ANGLE tests.
enabledFeatureOverrides.addAll(listOf(
"exposeNonConformantExtensionsAndVersions",
null
))

displayAttributes.addAll(listOf(
EGL_FEATURE_OVERRIDES_ENABLED_ANGLE,
enabledFeatureOverrides.toLongArray()
))
}

displayAttributes.add(EGL_NONE)

if (driverType == GLESDriverType.SystemWGL) {
return false
}

val mDisplay: EGLDisplay
if (IsANGLE(driverType) && extensionString.indexOf("EGL_ANGLE_platform_angle") != -1) {
mDisplay = eglGetPlatformDisplay(
EGL_PLATFORM_ANGLE_ANGLE,
osWindow.getNativeDisplay().toLong(),
displayAttributes.toTypedArray()
)
} else {
mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY)
}

if (mDisplay == EGL_NO_DISPLAY) {
println("Failed to get display: 0x%X".format(eglGetError()))
destroyGL()
return false
}

val majorVersion = intArrayOf(0)
val minorVersion = intArrayOf(0)
if (eglInitialize(mDisplay, majorVersion, minorVersion) == EGL_FALSE) {
println("eglInitialize failed: 0x%X".format(eglGetError()))
destroyGL()
return false
}

queryFeatures()

mPlatform = params
return true
}

fun initializeContext(): Boolean {
mContext = createContext(EGL_NO_CONTEXT, null)
if (mContext == EGL_NO_CONTEXT) {
destroyGL()
return false
}

if (!makeCurrent()) {
destroyGL()
return false
}

return true
}
}

enum class GLESDriverType {
AngleEGL,
AngleVulkanSecondariesEGL,
SystemEGL,
SystemWGL,
ZinkEGL,
}

enum class GLWindowResult {
NoError,
NoColorspaceSupport,
NoMutableRenderBufferSupport,
Error,
}

data class ConfigParameters(
var redBits: Int = -1,
var greenBits: Int = -1,
var blueBits: Int = -1,
var alphaBits: Int = -1,
var depthBits: Int = -1,
var stencilBits: Int = -1,
var webGLCompatibility: Boolean? = null,
var robustResourceInit: Boolean? = null,
var componentType: EGLenum = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT,
var multisample: Boolean = false,
var debug: Boolean = false,
var noError: Boolean = false,
var extensionsEnabled: Boolean? = null,
var bindGeneratesResource: Boolean = false,
var clientArraysEnabled: Boolean = false,
var robustAccess: Boolean = false,
var mutableRenderBuffer: Boolean = false,
var samples: EGLint = 0,
var contextProgramCacheEnabled: Boolean? = null,
var resetStrategy: EGLenum = EGL_NO_RESET_NOTIFICATION_EXT,
var colorSpace: EGLenum = EGL_COLORSPACE_LINEAR,
var swapInterval: EGLint = 0
)
Loading

0 comments on commit 20cc4d2

Please sign in to comment.