internal void Draw(PrimitiveType primitiveType, Matrix world) { Threading.EnsureUIThread(); GL.FrontFace(FrontFaceDirection.Ccw); GL.CullFace(CullFaceMode.Back); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.DepthTest); GL.DepthFunc(DepthFunction.Less); Material.Shader.Bind(); Material.Apply(); VertexBuffer.BindVertexArray(); VertexBuffer.Bind(); IndexBuffer.Bind(); VertexBuffer.VertexDeclaration.Apply(Material.Shader, IntPtr.Zero); REngine.CheckGLError(); Material.Shader.BindSemantics(Matrix.Identity * world, REngine.camera.View, REngine.camera.Projection); REngine.CheckGLError(); GL.DrawElements(primitiveType, IndexBuffer.IndexCount, DrawElementsType.UnsignedInt, IntPtr.Zero); Material.Shader.Unbind(); IndexBuffer.Unbind(); VertexBuffer.Unbind(); VertexBuffer.UnbindVertexArray(); }
/// <summary> /// Convert from ARGB to ABGR /// </summary> /// <param name="pixelHeight"></param> /// <param name="pixelWidth"></param> /// <param name="colors"></param> //private static void ConvertToABGR(int pixelHeight, int pixelWidth, byte[] colors) //{ // int pixelCount = pixelHeight * pixelWidth; // //int pixelCount = colors.Length; // for(int i = 0; i < pixelCount; i++) // { // uint pixel = colors[i]; // colors[i] = (byte)((pixel & 0xFF00FF00) | ((pixel & 0x00FF0000) >> 16) | ((pixel & 0x000000FF) << 16)); // } //} //public static void ConvertTARGB(byte[] colors) { // for (int i = 0; i < colors.Length; i++) { // uint pixel = colors[i]; // colors[i] = (byte)((pixel & 0xFFFFFF00) | ((pixel&0x000000FF)<<24)); // } //} //An array of RGBA public void SetData(uint[,] colors) { Threading.EnsureUIThread(); var width = colors.GetUpperBound(1) + 1; var height = colors.GetUpperBound(0) + 1; if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height)) { throw new InvalidDataException(string.Format("Non-power-of-two array {0}x{1}", width, height)); } Size = new Size(width, height); unsafe { fixed(uint *ptr = &colors[0, 0]) { var intPtr = new IntPtr((void *)ptr); PrepareTexture(); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, width, height, 0, PixelFormat.BGRA_EXT, PixelType.UnsignedByte, intPtr); //GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, width, height, PixelFormat.Rgba, PixelType.UnsignedByte, intPtr); } } }
public void SetVec(string name, float[] vec, int length) { Threading.EnsureUIThread(); var param = GL.GetUniformLocation(program, name); GraphicsExtensions.CheckGLError(); unsafe { fixed(float *pVec = vec) { var ptr = new IntPtr(pVec); switch (length) { case 1: GL.Uniform1fv(param, 1, ptr); break; case 2: GL.Uniform2fv(param, 1, ptr); break; case 3: GL.Uniform3fv(param, 1, ptr); break; case 4: GL.Uniform4fv(param, 1, ptr); break; default: throw new InvalidDataException("Invalid vector length"); } } } GraphicsExtensions.CheckGLError(); }
public void SetData(byte[] colors, int width, int height) { Threading.EnsureUIThread(); if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height)) { throw new InvalidDataException(string.Format("Non-power-of-two array {0} X {1}", width, height)); } Size = new Size(width, height); //ConvertToABGR(height, width, colors); colors = ConvertToRGBA(colors); unsafe { fixed(byte *ptr = &colors[0]) { var intPtr = new IntPtr((void *)ptr); PrepareTexture(); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, intPtr); //GL.TexImage2D(TextureTarget.Texture2D, 0, // PixelInternalFormat.Rgba8, width, height, 0, PixelFormat.BGRA_EXT, PixelType.UnsignedByte, intPtr); //GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, width, height, PixelFormat.Rgba, PixelType.UnsignedByte, intPtr); GraphicsExtensions.CheckGLError(); } } }
internal void SetTextures(GraphicsDevice device) { Threading.EnsureUIThread(); if (this._dirty == 0) { return; } for (int index = 0; index < this._textures.Length; ++index) { int num = 1 << index; if ((this._dirty & num) != 0) { Texture texture = this._textures[index]; GL.ActiveTexture((TextureUnit)(33984 + index)); if (this._targets[index] != (TextureTarget)0 && (texture == null || this._targets[index] != texture.glTarget)) { GL.BindTexture(this._targets[index], 0); } if (texture != null) { this._targets[index] = texture.glTarget; GL.BindTexture(texture.glTarget, texture.glTexture); } this._dirty &= ~num; if (this._dirty == 0) { break; } } } this._dirty = 0; }
private void PlatformGetData <T>(CubeMapFace cubeMapFace, int level, Rectangle rect, T[] data, int startIndex, int elementCount) where T : struct { Threading.EnsureUIThread(); #if OPENGL && (MONOMAC || DESKTOPGL) var target = GetGLCubeFace(cubeMapFace); var tSizeInByte = ReflectionHelpers.SizeOf <T> .Get(); GL.BindTexture(TextureTarget.TextureCubeMap, this.glTexture); if (glFormat == (PixelFormat)GLPixelFormat.CompressedTextureFormats) { // Note: for compressed format Format.GetSize() returns the size of a 4x4 block var pixelToT = Format.GetSize() / tSizeInByte; var tFullWidth = Math.Max(this.size >> level, 1) / 4 * pixelToT; var temp = new T[Math.Max(this.size >> level, 1) / 4 * tFullWidth]; #if MONOMAC var tempHandle = GCHandle.Alloc(temp, GCHandleType.Pinned); var ptr = tempHandle.AddrOfPinnedObject(); GL.GetCompressedTexImage(TextureTarget.Texture2D, level, ptr); tempHandle.Free(); #else GL.GetCompressedTexImage(target, level, temp); #endif GraphicsExtensions.CheckGLError(); var rowCount = rect.Height / 4; var tRectWidth = rect.Width / 4 * Format.GetSize() / tSizeInByte; for (var r = 0; r < rowCount; r++) { var tempStart = rect.X / 4 * pixelToT + (rect.Top / 4 + r) * tFullWidth; var dataStart = startIndex + r * tRectWidth; Array.Copy(temp, tempStart, data, dataStart, tRectWidth); } } else { // we need to convert from our format size to the size of T here var tFullWidth = Math.Max(this.size >> level, 1) * Format.GetSize() / tSizeInByte; var temp = new T[Math.Max(this.size >> level, 1) * tFullWidth]; GL.GetTexImage(target, level, glFormat, glType, temp); GraphicsExtensions.CheckGLError(); var pixelToT = Format.GetSize() / tSizeInByte; var rowCount = rect.Height; var tRectWidth = rect.Width * pixelToT; for (var r = 0; r < rowCount; r++) { var tempStart = rect.X * pixelToT + (r + rect.Top) * tFullWidth; var dataStart = startIndex + r * tRectWidth; Array.Copy(temp, tempStart, data, dataStart, tRectWidth); } } #else throw new NotImplementedException(); #endif }
public ITexture CreateTexture() { Threading.EnsureUIThread(); var texture = new Texture(); texture.GraphicsDevice = this; return(texture); }
public void Clear(Color color) { Threading.EnsureUIThread(); GL.ClearColor(color.R, color.G, color.B, color.A); GraphicsExtensions.CheckGLError(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GraphicsExtensions.CheckGLError(); }
public void Unbind() { Threading.EnsureUIThread(); GL.Flush(); GraphicsExtensions.CheckGLError(); GL.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); GraphicsExtensions.CheckGLError(); GL.Viewport(cv[0], cv[1], cv[2], cv[3]); GraphicsExtensions.CheckGLError(); }
public void EnableDepthBuffer() { Threading.EnsureUIThread(); GL.Clear(ClearBufferMask.DepthBufferBit); GraphicsExtensions.CheckGLError(); GL.Enable(EnableCap.DepthTest); GraphicsExtensions.CheckGLError(); GL.DepthFunc(DepthFunction.Lequal); GraphicsExtensions.CheckGLError(); }
public void SetVec(string name, float x, float y, float z) { Threading.EnsureUIThread(); GL.UseProgram(program); GraphicsExtensions.CheckGLError(); var param = GL.GetUniformLocation(program, name); GraphicsExtensions.CheckGLError(); GL.Uniform3f(param, x, y, z); GraphicsExtensions.CheckGLError(); }
public void SetBool(string name, bool value) { Threading.EnsureUIThread(); GL.UseProgram(program); GraphicsExtensions.CheckGLError(); var param = GL.GetUniformLocation(program, name); GraphicsExtensions.CheckGLError(); GL.Uniform1i(param, value ? 1 : 0); GraphicsExtensions.CheckGLError(); }
public void Bind() { Threading.EnsureUIThread(); GL.BindBuffer(BufferTarget.ArrayBuffer, buffer); GraphicsExtensions.CheckGLError(); GL.VertexAttribPointer(Shader.VertexPosAttributeIndex, 3, VertexAttribPointerType.Float, false, VertexSize, IntPtr.Zero); GraphicsExtensions.CheckGLError(); GL.VertexAttribPointer(Shader.TexCoordAttributeIndex, 4, VertexAttribPointerType.Float, false, VertexSize, new IntPtr(12)); GraphicsExtensions.CheckGLError(); GL.VertexAttribPointer(Shader.TexMetadataAttributeIndex, 2, VertexAttribPointerType.Float, false, VertexSize, new IntPtr(28)); GraphicsExtensions.CheckGLError(); }
public void SetBlendMode(BlendMode mode) { Threading.EnsureUIThread(); //GL.BlendEquationSeparate(BlendEquationMode.FuncAdd, BlendEquationMode.FuncAdd); GL.BlendEquation((int)BlendEquationMode.FuncAdd); GraphicsExtensions.CheckGLError(); switch (mode) { case BlendMode.None: GL.Disable(EnableCap.Blend); break; case BlendMode.Alpha: GL.Enable(EnableCap.Blend); GraphicsExtensions.CheckGLError(); GL.BlendFunc((int)BlendingFactorSrc.One, (int)BlendingFactorSrc.OneMinusSrcAlpha); break; case BlendMode.Additive: case BlendMode.Subtractive: GL.Enable(EnableCap.Blend); GraphicsExtensions.CheckGLError(); GL.BlendFunc((int)BlendingFactorSrc.One, (int)BlendingFactorDest.One); if (mode == BlendMode.Subtractive) { GraphicsExtensions.CheckGLError(); GL.BlendEquation((int)BlendEquationMode.FuncReverseSubtract); } break; case BlendMode.Multiply: GL.Enable(EnableCap.Blend); GraphicsExtensions.CheckGLError(); GL.BlendFunc((int)BlendingFactorDest.DstColor, (int)BlendingFactorSrc.OneMinusSrcAlpha); break; case BlendMode.Multiplicative: GL.Enable(EnableCap.Blend); GraphicsExtensions.CheckGLError(); GL.BlendFunc((int)BlendingFactorSrc.Zero, (int)BlendingFactorDest.SrcColor); break; case BlendMode.DoubleMultiplicative: GL.Enable(EnableCap.Blend); GraphicsExtensions.CheckGLError(); GL.BlendFunc((int)BlendingFactorDest.DstColor, (int)BlendingFactorSrc.SrcColor); break; } GraphicsExtensions.CheckGLError(); }
public void SetEmpty(int width, int height) { Threading.EnsureUIThread(); if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height)) { throw new InvalidDataException(string.Format("Non-power-of-two array {0}x{1}", width, height)); } Size = new Size(width, height); PrepareTexture(); //GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, width, height, 0, PixelFormat.BGRA_EXT, PixelType.UnsignedByte, IntPtr.Zero); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); //GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, width, height, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); GraphicsExtensions.CheckGLError(); }
public void SetTexture(string name, ITexture t) { Threading.EnsureUIThread(); if (t == null) { return; } int texUnit; if (samplers.TryGetValue(name, out texUnit)) { textures[texUnit] = t; } }
public void Render(Action a) { Threading.EnsureUIThread(); GL.UseProgram(program); //bind the textures foreach (var kv in textures) { GL.ActiveTexture(TextureUnit.Texture0 + kv.Key); GL.BindTexture(TextureTarget.Texture2D, ((Texture)kv.Value).ID); } GraphicsExtensions.CheckGLError(); a(); GraphicsExtensions.CheckGLError(); }
internal void PlatformApplyState(bool applyShaders) { // TODO: This was on both the OpenGL and PSM path previously - is it necessary? Threading.EnsureUIThread(); if (_scissorRectangleDirty) { _scissorRectangleDirty = false; } if (_blendStateDirty) { _blendState.PlatformApplyState(this); _blendStateDirty = false; } if (_depthStencilStateDirty) { _depthStencilState.PlatformApplyState(this); _depthStencilStateDirty = false; } if (_rasterizerStateDirty) { _rasterizerState.PlatformApplyState(this); _rasterizerStateDirty = false; } // If we're not applying shaders then early out now. if (!applyShaders) { return; } if (_indexBufferDirty) { _indexBufferDirty = false; } // Nothing was in here for PSM //if (_vertexBufferDirty) //{ //} Textures.SetTextures(this); SamplerStates.PlatformSetSamplers(this); }
public void Bind() { Threading.EnsureUIThread(); //Cache viewport rect to restore when unbinding cv = ViewportRectangle(); GL.Flush(); GraphicsExtensions.CheckGLError(); GL.BindFramebuffer(FramebufferTarget.FramebufferExt, framebuffer); GraphicsExtensions.CheckGLError(); GL.Viewport(0, 0, size.Width, size.Height); GraphicsExtensions.CheckGLError(); GL.ClearColor(0, 0, 0, 0); GraphicsExtensions.CheckGLError(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GraphicsExtensions.CheckGLError(); }
internal void SetTextures() { Threading.EnsureUIThread(); if (_dirty == 0) { return; // nothing has changed } for (int i = 0; i < _textures.Length; i++) { int mask = 1 << i; if ((_dirty & mask) == 0) { continue; } Texture tex = _textures[i]; GL.ActiveTexture(TextureUnit.Texture0 + i); Utilities.CheckGLError(); // clear the previous binding if the tartet is different from the new one if (_targets[i] != 0 && (tex == null || _targets[i] != tex.Target)) { GL.BindTexture(_targets[i], 0); Utilities.CheckGLError(); } if (tex != null) { _targets[i] = tex.Target; GL.BindTexture(tex.Target, tex.ID); Utilities.CheckGLError(); } _dirty &= ~mask; if (_dirty == 0) { break; } } _dirty = 0; }
public void EnableScissor(int left, int top, int width, int height) { Threading.EnsureUIThread(); if (width < 0) { width = 0; } if (height < 0) { height = 0; } var bottom = PresentationParameters.BackBufferHeight - (top + height); GL.Scissor(left, bottom, width, height); GraphicsExtensions.CheckGLError(); GL.Enable(EnableCap.ScissorTest); GraphicsExtensions.CheckGLError(); }
public void SetMatrix(string name, float[] mtx) { Threading.EnsureUIThread(); if (mtx.Length != 16) { throw new InvalidDataException("Invalid 4x4 matrix"); } GL.UseProgram(program); GraphicsExtensions.CheckGLError(); var param = GL.GetUniformLocation(program, name); GraphicsExtensions.CheckGLError(); unsafe { fixed(float *pMtx = mtx) GL.UniformMatrix4fv(param, 1, false, new IntPtr(pMtx)); } GraphicsExtensions.CheckGLError(); }
public byte[] GetData() { Threading.EnsureUIThread(); var data = new byte[4 * Size.Width * Size.Height]; GraphicsExtensions.CheckGLError(); GL.BindTexture(TextureTarget.Texture2D, texture); //unsafe //{ // fixed(byte*ptr = &data[0]) // { // var intPtr = new IntPtr((void*)ptr); // GL.GetTexImageInternal(TextureTarget.Texture2D, 0, PixelFormat.BGRA_EXT, PixelType.UnsignedByte, intPtr); // } //} GL.GetTexImage(TextureTarget.Texture2D, 0, PixelFormat.BGRA_EXT, PixelType.UnsignedByte, data); GraphicsExtensions.CheckGLError(); return(data); }
public void DisableScissor() { Threading.EnsureUIThread(); GL.Disable(EnableCap.ScissorTest); GraphicsExtensions.CheckGLError(); }
public IVertexBuffer <Vertex> CreateVertexBuffer(int size) { Threading.EnsureUIThread(); return(new VertexBuffer <Vertex>(size)); }
internal void PlatformBeginApplyState() { Threading.EnsureUIThread(); }
internal void SetTextures(GraphicsDevice device) { #if !DIRECTX Threading.EnsureUIThread(); #endif // Skip out if nothing has changed. if (_dirty == 0) { return; } #if DIRECTX // NOTE: We make the assumption here that the caller has // locked the d3dContext for us to use. var pixelShaderStage = device._d3dContext.PixelShader; #endif for (var i = 0; i < _textures.Length; i++) { var mask = 1 << i; if ((_dirty & mask) == 0) { continue; } var tex = _textures[i]; #if OPENGL GL.ActiveTexture(TextureUnit.Texture0 + i); GraphicsExtensions.CheckGLError(); // Clear the previous binding if the // target is different from the new one. if (_targets[i] != 0 && (tex == null || _targets[i] != tex.glTarget)) { GL.BindTexture(_targets[i], 0); GraphicsExtensions.CheckGLError(); } if (tex != null) { _targets[i] = tex.glTarget; GL.BindTexture(tex.glTarget, tex.glTexture); GraphicsExtensions.CheckGLError(); } #elif DIRECTX if (_textures[i] == null || _textures[i].IsDisposed) { pixelShaderStage.SetShaderResource(i, null); } else { pixelShaderStage.SetShaderResource(i, _textures[i].GetShaderResourceView()); } #elif PSM // FIXME: 1d/3d textures var texture2d = _textures[i] as Texture2D; if (texture2d == null) { device.Context.SetTexture(i, null); } else { device.Context.SetTexture(i, texture2d._texture2D); } #endif _dirty &= ~mask; if (_dirty == 0) { break; } } _dirty = 0; }
public IFrameBuffer CreateFrameBuffer(Size s) { Threading.EnsureUIThread(); return(new FrameBuffer(s)); }
private void PlatformGetData <T>(int level, int arraySlice, Rectangle rect, T[] data, int startIndex, int elementCount) where T : struct { Threading.EnsureUIThread(); #if GLES // TODO: check for for non renderable formats (formats that can't be attached to FBO) var framebufferId = 0; GL.GenFramebuffers(1, out framebufferId); GraphicsExtensions.CheckGLError(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebufferId); GraphicsExtensions.CheckGLError(); GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, this.glTexture, 0); GraphicsExtensions.CheckGLError(); GL.ReadPixels(rect.X, rect.Y, rect.Width, rect.Height, this.glFormat, this.glType, data); GraphicsExtensions.CheckGLError(); GraphicsDevice.DisposeFramebuffer(framebufferId); #else var tSizeInByte = ReflectionHelpers.SizeOf <T> .Get(); GL.BindTexture(TextureTarget.Texture2D, this.glTexture); GL.PixelStore(PixelStoreParameter.PackAlignment, Math.Min(tSizeInByte, 8)); if (glFormat == (PixelFormat)GLPixelFormat.CompressedTextureFormats) { // Note: for compressed format Format.GetSize() returns the size of a 4x4 block var pixelToT = Format.GetSize() / tSizeInByte; var tFullWidth = Math.Max(this.width >> level, 1) / 4 * pixelToT; var temp = new T[Math.Max(this.height >> level, 1) / 4 * tFullWidth]; GL.GetCompressedTexImage(TextureTarget.Texture2D, level, temp); GraphicsExtensions.CheckGLError(); var rowCount = rect.Height / 4; var tRectWidth = rect.Width / 4 * Format.GetSize() / tSizeInByte; for (var r = 0; r < rowCount; r++) { var tempStart = rect.X / 4 * pixelToT + (rect.Top / 4 + r) * tFullWidth; var dataStart = startIndex + r * tRectWidth; Array.Copy(temp, tempStart, data, dataStart, tRectWidth); } } else { // we need to convert from our format size to the size of T here var tFullWidth = Math.Max(this.width >> level, 1) * Format.GetSize() / tSizeInByte; var temp = new T[Math.Max(this.height >> level, 1) * tFullWidth]; GL.GetTexImage(TextureTarget.Texture2D, level, glFormat, glType, temp); GraphicsExtensions.CheckGLError(); var pixelToT = Format.GetSize() / tSizeInByte; var rowCount = rect.Height; var tRectWidth = rect.Width * pixelToT; for (var r = 0; r < rowCount; r++) { var tempStart = rect.X * pixelToT + (r + rect.Top) * tFullWidth; var dataStart = startIndex + r * tRectWidth; Array.Copy(temp, tempStart, data, dataStart, tRectWidth); } } #endif }
public void Present() { Threading.EnsureUIThread(); Game.Instance.Platform.Present(); }