Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Veldrid.Android/AppGlobals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Veldrid;

namespace mellinoe.VeldridActivityExample
{
// Using Veldrid, only a single GraphicsDevice needs to be created,
// even when rendering to many Swapchains in an application.
// A Veldrid GraphicsDevice is responsible for creating all useful graphics
// resources, including Swapchains. The GraphicsDevice in this class is used
// to create a Swapchain in ViewController.ViewDidLoad.
public static class AppGlobals
{
public static GraphicsDevice Device { get; private set; }

public static void InitDevice(SwapchainDescription swapchainDescription)
{
GraphicsDeviceOptions options = new GraphicsDeviceOptions(
debug: false,
swapchainDepthFormat: null,
syncToVerticalBlank: false,
resourceBindingModel: ResourceBindingModel.Improved,
preferDepthRangeZeroToOne: true,
preferStandardClipSpaceYDirection: true);

//Device = GraphicsDevice.CreateVulkan(options);
Device = GraphicsDevice.CreateOpenGLES(options, swapchainDescription);
}

internal static void DisposeDevice()
{
Device.Dispose();
}
}
}
19 changes: 19 additions & 0 deletions Veldrid.Android/Assets/AboutAssets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Any raw assets you want to be deployed with your application can be placed in
this directory (and child directories) and given a Build Action of "AndroidAsset".

These files will be deployed with your package and will be accessible using Android's
AssetManager, like this:

public class ReadAsset : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

InputStream input = Assets.Open ("my_asset.txt");
}
}

Additionally, some Android functions will automatically load asset files:

Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
84 changes: 84 additions & 0 deletions Veldrid.Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Android.App;
using Android.Content.PM;
using Android.OS;
using System.Diagnostics;
using Veldrid;

namespace mellinoe.VeldridActivityExample
{
[Activity(Label = "VeldridActivityExample", MainLauncher = true, Icon = "@mipmap/icon", ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation | ConfigChanges.ScreenSize)]
public class MainActivity : Activity
{
private GraphicsDeviceOptions _options;
private VeldridSurfaceView _view;
private ResourceFactory _disposeFactory;
private Stopwatch _sw;

private CommandList _cl;
private int _frameIndex = 0;
private RgbaFloat[] _clearColors =
{
RgbaFloat.Red,
RgbaFloat.Orange,
RgbaFloat.Yellow,
RgbaFloat.Green,
RgbaFloat.Blue,
new RgbaFloat(0.8f, 0.1f, 0.3f, 1f),
new RgbaFloat(0.8f, 0.1f, 0.9f, 1f),
};

private readonly int _frameRepeatCount = 20;

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

_options = new GraphicsDeviceOptions(false, PixelFormat.R16_UNorm, false);
//GraphicsBackend backend = GraphicsDevice.IsBackendSupported(GraphicsBackend.Vulkan) ? GraphicsBackend.Vulkan : GraphicsBackend.OpenGLES;
GraphicsBackend backend = GraphicsBackend.OpenGLES;


_view = new VeldridSurfaceView(this, backend, _options);
_view.Rendering += OnViewRendering;
_view.DeviceCreated += OnViewCreatedDevice;
_sw = Stopwatch.StartNew();

SetContentView(_view);
}

protected override void OnPause()
{
base.OnPause();
_view.OnPause();
}

protected override void OnResume()
{
base.OnResume();
_view.OnResume();
}

private void OnViewCreatedDevice()
{
_disposeFactory = _view.GraphicsDevice.ResourceFactory;
_cl = _disposeFactory.CreateCommandList();
_view.RunContinuousRenderLoop();
}

private void OnViewRendering()
{
// Each frame, we clear the Swapchain's color target.
// Several different colors are cycled.
_cl.Begin();
_cl.SetFramebuffer(_view.MainSwapchain.Framebuffer);
_cl.ClearColorTarget(0, _clearColors[(_frameIndex / _frameRepeatCount)]);
_cl.End();
_view.GraphicsDevice.SubmitCommands(_cl);
_view.GraphicsDevice.SwapBuffers(_view.MainSwapchain);

// Do some math to loop our color picker index.
_frameIndex = (_frameIndex + 1) % (_clearColors.Length * _frameRepeatCount);
}
}
}

6 changes: 6 additions & 0 deletions Veldrid.Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="VeldridActivityExample.VeldridActivityExample">
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="28" />
<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name"></application>
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
</manifest>
27 changes: 27 additions & 0 deletions Veldrid.Android/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Android.App;

// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.

[assembly: AssemblyTitle("mellinoe.VeldridActivityExample")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("${AuthorCopyright}")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("1.0.0")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.

//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]
44 changes: 44 additions & 0 deletions Veldrid.Android/Resources/AboutResources.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Images, layout descriptions, binary blobs and string dictionaries can be included
in your application as resource files. Various Android APIs are designed to
operate on the resource IDs instead of dealing with images, strings or binary blobs
directly.

For example, a sample Android app that contains a user interface layout (main.axml),
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
would keep its resources in the "Resources" directory of the application:

Resources/
drawable/
icon.png

layout/
main.axml

values/
strings.xml

In order to get the build system to recognize Android resources, set the build action to
"AndroidResource". The native Android APIs do not operate directly with filenames, but
instead operate on resource IDs. When you compile an Android application that uses resources,
the build system will package the resources for distribution and generate a class called "R"
(this is an Android convention) that contains the tokens for each one of the resources
included. For example, for the above Resources layout, this is what the R class would expose:

public class R {
public class drawable {
public const int icon = 0x123;
}

public class layout {
public const int main = 0x456;
}

public class strings {
public const int first_string = 0xabc;
public const int second_string = 0xbcd;
}
}

You would then use R.drawable.icon to reference the drawable/icon.png file, or R.layout.main
to reference the layout/main.axml file, or R.strings.first_string to reference the first
string in the dictionary file values/strings.xml.
Loading