Skip to content

Commit 3ae3d1c

Browse files
committed
updated GL samples - pin data buffers when passing to native code
1 parent 28e9076 commit 3ae3d1c

14 files changed

+102
-45
lines changed

GLCube-1.0/GLCube.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@
2626
<WarningLevel>4</WarningLevel>
2727
<MonoDroidLinkMode>None</MonoDroidLinkMode>
2828
<AndroidLinkMode>None</AndroidLinkMode>
29+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2930
</PropertyGroup>
3031
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
31-
<DebugType>pdbonly</DebugType>
3232
<Optimize>True</Optimize>
3333
<OutputPath>bin\Release\</OutputPath>
3434
<DefineConstants>TRACE</DefineConstants>
3535
<ErrorReport>prompt</ErrorReport>
3636
<WarningLevel>4</WarningLevel>
3737
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
3838
<MonoDroidLinkMode>Full</MonoDroidLinkMode>
39-
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
39+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4040
</PropertyGroup>
4141
<ItemGroup>
4242
<Reference Include="Mono.Android" />

GLCube-1.0/PaintingView.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,19 @@ void RenderCube ()
132132
GL.ClearColor (0, 0, 0, 1.0f);
133133
GL.Clear (ClearBufferMask.ColorBufferBit);
134134

135-
GL.VertexPointer(3, All.Float, 0, cube);
136-
GL.EnableClientState (All.VertexArray);
137-
GL.ColorPointer (4, All.Float, 0, cubeColors);
138-
GL.EnableClientState (All.ColorArray);
139-
GL.DrawElements(All.Triangles, 36, All.UnsignedByte, triangles);
135+
// pin the data, so that GC doesn't move them, while used
136+
// by native code
137+
unsafe {
138+
fixed (float* pcube = cube, pcubeColors = cubeColors) {
139+
fixed (byte* ptriangles = triangles) {
140+
GL.VertexPointer(3, All.Float, 0, new IntPtr (pcube));
141+
GL.EnableClientState (All.VertexArray);
142+
GL.ColorPointer (4, All.Float, 0, new IntPtr (pcubeColors));
143+
GL.EnableClientState (All.ColorArray);
144+
GL.DrawElements(All.Triangles, 36, All.UnsignedByte, new IntPtr (ptriangles));
145+
}
146+
}
147+
}
140148

141149
SwapBuffers ();
142150
}

GLCube/GLCube.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<WarningLevel>4</WarningLevel>
2727
<MonoDroidLinkMode>None</MonoDroidLinkMode>
2828
<AndroidLinkMode>None</AndroidLinkMode>
29+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2930
</PropertyGroup>
3031
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3132
<DebugType>pdbonly</DebugType>
@@ -36,7 +37,7 @@
3637
<WarningLevel>4</WarningLevel>
3738
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
3839
<MonoDroidLinkMode>Full</MonoDroidLinkMode>
39-
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
40+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4041
</PropertyGroup>
4142
<ItemGroup>
4243
<Reference Include="Mono.Android" />

GLCube/PaintingView.cs

+14-6
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,20 @@ void RenderCube ()
132132
GL.ClearColor (0, 0, 0, 1.0f);
133133
GL.Clear ((uint) All.ColorBufferBit);
134134

135-
GL.VertexPointer(3, All.Float, 0, cube);
136-
GL.EnableClientState (All.VertexArray);
137-
GL.ColorPointer (4, All.Float, 0, cubeColors);
138-
GL.EnableClientState (All.ColorArray);
139-
GL.DrawElements(All.Triangles, 36, All.UnsignedByte, triangles);
140-
135+
// pin the data, so that GC doesn't move them, while used
136+
// by native code
137+
unsafe {
138+
fixed (float* pcube = cube, pcubeColors = cubeColors) {
139+
fixed (byte* ptriangles = triangles) {
140+
GL.VertexPointer (3, All.Float, 0, new IntPtr (pcube));
141+
GL.EnableClientState (All.VertexArray);
142+
GL.ColorPointer (4, All.Float, 0, new IntPtr (pcubeColors));
143+
GL.EnableClientState (All.ColorArray);
144+
GL.DrawElements (All.Triangles, 36, All.UnsignedByte, new IntPtr (ptriangles));
145+
GL.Finish ();
146+
}
147+
}
148+
}
141149
SwapBuffers ();
142150
}
143151

GLTriangle20-1.0/GLTriangle20.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<WarningLevel>4</WarningLevel>
2727
<MonoDroidLinkMode>None</MonoDroidLinkMode>
2828
<AndroidLinkMode>None</AndroidLinkMode>
29+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2930
</PropertyGroup>
3031
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3132
<DebugType>pdbonly</DebugType>
@@ -36,7 +37,7 @@
3637
<WarningLevel>4</WarningLevel>
3738
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
3839
<MonoDroidLinkMode>Full</MonoDroidLinkMode>
39-
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
40+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4041
</PropertyGroup>
4142
<ItemGroup>
4243
<Reference Include="Mono.Android" />

GLTriangle20-1.0/PaintingView.cs

+20-12
Original file line numberDiff line numberDiff line change
@@ -203,25 +203,33 @@ void RenderTriangle ()
203203
// Enable a handle to the triangle vertices
204204
GL.EnableVertexAttribArray (mPositionHandle);
205205

206-
// Prepare the triangle coordinate data
207-
GL.VertexAttribPointer (0, 3, All.Float, false, 0, vertices);
206+
// pin the data, so that GC doesn't move them, while used
207+
// by native code
208+
unsafe {
209+
fixed (float* pvertices = vertices) {
210+
// Prepare the triangle coordinate data
211+
GL.VertexAttribPointer (0, 3, All.Float, false, 0, new IntPtr (pvertices));
208212

209-
// get handle to fragment shader's vColor member
210-
mColorHandle = GL.GetUniformLocation(mProgramHandle, new StringBuilder("vColor"));
213+
// get handle to fragment shader's vColor member
214+
mColorHandle = GL.GetUniformLocation(mProgramHandle, new StringBuilder("vColor"));
211215

212-
// Set color for drawing the triangle
213-
GL.Uniform4(mColorHandle, 1, color);
216+
// Set color for drawing the triangle
217+
GL.Uniform4(mColorHandle, 1, color);
214218

215-
// get handle to shape's transformation matrix
216-
mMVPMatrixHandle = GL.GetUniformLocation(mProgramHandle, new StringBuilder("uMVPMatrix"));
219+
// get handle to shape's transformation matrix
220+
mMVPMatrixHandle = GL.GetUniformLocation(mProgramHandle, new StringBuilder("uMVPMatrix"));
217221

218-
// Apply the projection and view transformation
219-
GL.UniformMatrix4(mMVPMatrixHandle, false, ref mModelViewProjectionMatrix);
222+
// Apply the projection and view transformation
223+
GL.UniformMatrix4(mMVPMatrixHandle, false, ref mModelViewProjectionMatrix);
220224

221-
GL.DrawArrays (All.Triangles, 0, 3);
225+
GL.DrawArrays (All.Triangles, 0, 3);
226+
227+
GL.Finish ();
228+
}
229+
}
222230

223231
// Disable vertex array
224-
GL.DisableVertexAttribArray(mPositionHandle);
232+
GL.DisableVertexAttribArray(mPositionHandle);
225233

226234
SwapBuffers ();
227235
}

GLTriangle20/GLTriangle20.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<WarningLevel>4</WarningLevel>
2727
<MonoDroidLinkMode>None</MonoDroidLinkMode>
2828
<AndroidLinkMode>None</AndroidLinkMode>
29+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2930
</PropertyGroup>
3031
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3132
<DebugType>pdbonly</DebugType>
@@ -36,7 +37,7 @@
3637
<WarningLevel>4</WarningLevel>
3738
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
3839
<MonoDroidLinkMode>Full</MonoDroidLinkMode>
39-
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
40+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4041
</PropertyGroup>
4142
<ItemGroup>
4243
<Reference Include="Mono.Android" />

GLTriangle20/PaintingView.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,16 @@ void RenderTriangle ()
174174
GL.Viewport (0, 0, viewportWidth, viewportHeight);
175175
GL.UseProgram (program);
176176

177-
GL.VertexAttribPointer (0, 3, All.Float, false, 0, vertices);
178-
GL.EnableVertexAttribArray (0);
179-
180-
GL.DrawArrays (All.Triangles, 0, 3);
181-
177+
// pin the data, so that GC doesn't move them, while used
178+
// by native code
179+
unsafe {
180+
fixed (float* pvertices = vertices) {
181+
GL.VertexAttribPointer (0, 3, All.Float, false, 0, new IntPtr (pvertices));
182+
GL.EnableVertexAttribArray (0);
183+
GL.DrawArrays (All.Triangles, 0, 3);
184+
GL.Finish ();
185+
}
186+
}
182187
SwapBuffers ();
183188
}
184189

GLTriangle30/GLTriangle30.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<WarningLevel>4</WarningLevel>
2828
<MonoDroidLinkMode>None</MonoDroidLinkMode>
2929
<AndroidLinkMode>None</AndroidLinkMode>
30+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3031
</PropertyGroup>
3132
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3233
<DebugType>pdbonly</DebugType>
@@ -37,6 +38,7 @@
3738
<WarningLevel>4</WarningLevel>
3839
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
3940
<MonoDroidLinkMode>Full</MonoDroidLinkMode>
41+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4042
</PropertyGroup>
4143
<ItemGroup>
4244
<Reference Include="Mono.Android" />

GLTriangle30/PaintingView.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,16 @@ void RenderTriangle ()
174174
GL.Viewport (0, 0, viewportWidth, viewportHeight);
175175
GL.UseProgram (program);
176176

177-
GL.VertexAttribPointer (0, 3, All.Float, false, 0, vertices);
178-
GL.EnableVertexAttribArray (0);
179-
180-
GL.DrawArrays (All.Triangles, 0, 3);
177+
// pin the data, so that GC doesn't move them, while used
178+
// by native code
179+
unsafe {
180+
fixed (float* pvertices = vertices) {
181+
GL.VertexAttribPointer (0, 3, All.Float, false, 0, new IntPtr (pvertices));
182+
GL.EnableVertexAttribArray (0);
183+
GL.DrawArrays (All.Triangles, 0, 3);
184+
GL.Finish ();
185+
}
186+
}
181187

182188
SwapBuffers ();
183189
}

TexturedCube-1.0/PaintingView.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,16 @@ void RenderCube ()
224224
{
225225
float [] v = cubeVertexCoords [i];
226226
float [] t = cubeTextureCoords [i];
227-
GL.VertexPointer(3, All.Float, 0, v);
228-
GL.TexCoordPointer(2, All.Float, 0, t);
229-
GL.DrawArrays(All.TriangleFan, 0, 4);
227+
// pin the data, so that GC doesn't move them, while used
228+
// by native code
229+
unsafe {
230+
fixed (float* pv = v, pt = t) {
231+
GL.VertexPointer(3, All.Float, 0, new IntPtr (pv));
232+
GL.TexCoordPointer(2, All.Float, 0, new IntPtr (pt));
233+
GL.DrawArrays(All.TriangleFan, 0, 4);
234+
GL.Finish ();
235+
}
236+
}
230237
}
231238
GL.DisableClientState(All.VertexArray);
232239
GL.DisableClientState(All.TextureCoordArray);

TexturedCube-1.0/TexturedCube.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<WarningLevel>4</WarningLevel>
2727
<MonoDroidLinkMode>None</MonoDroidLinkMode>
2828
<AndroidLinkMode>None</AndroidLinkMode>
29+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2930
</PropertyGroup>
3031
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3132
<DebugType>pdbonly</DebugType>
@@ -36,7 +37,7 @@
3637
<WarningLevel>4</WarningLevel>
3738
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
3839
<MonoDroidLinkMode>Full</MonoDroidLinkMode>
39-
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
40+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4041
</PropertyGroup>
4142
<ItemGroup>
4243
<Reference Include="Mono.Android" />

TexturedCube/PaintingView.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,21 @@ void RenderCube ()
220220
GL.BindTexture(All.Texture2D, textureIds [cur_texture]);
221221
GL.EnableClientState(All.VertexArray);
222222
GL.EnableClientState(All.TextureCoordArray);
223+
223224
for (int i = 0; i < 6; i++) // draw each face
224225
{
225226
float [] v = cubeVertexCoords [i];
226227
float [] t = cubeTextureCoords [i];
227-
GL.VertexPointer(3, All.Float, 0, v);
228-
GL.TexCoordPointer(2, All.Float, 0, t);
229-
GL.DrawArrays(All.TriangleFan, 0, 4);
228+
// pin the data, so that GC doesn't move them, while used
229+
// by native code
230+
unsafe {
231+
fixed (float* pv = v, pt = t) {
232+
GL.VertexPointer(3, All.Float, 0, new IntPtr (pv));
233+
GL.TexCoordPointer(2, All.Float, 0, new IntPtr (pt));
234+
GL.DrawArrays(All.TriangleFan, 0, 4);
235+
GL.Finish ();
236+
}
237+
}
230238
}
231239
GL.DisableClientState(All.VertexArray);
232240
GL.DisableClientState(All.TextureCoordArray);

TexturedCube/TexturedCube.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<WarningLevel>4</WarningLevel>
2727
<MonoDroidLinkMode>None</MonoDroidLinkMode>
2828
<AndroidLinkMode>None</AndroidLinkMode>
29+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2930
</PropertyGroup>
3031
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3132
<DebugType>pdbonly</DebugType>
@@ -36,7 +37,7 @@
3637
<WarningLevel>4</WarningLevel>
3738
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
3839
<MonoDroidLinkMode>Full</MonoDroidLinkMode>
39-
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
40+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4041
</PropertyGroup>
4142
<ItemGroup>
4243
<Reference Include="Mono.Android" />

0 commit comments

Comments
 (0)