public void Dispose() { if (_gpu != null) { _gpu = null; } }
public static Texture2D FromResource(GraphicsProcessor gpu, Assembly ass, string resource) { if (Resource.GetStream(ass, resource, out var stream)) { return(FromStream(gpu, stream)); } else { throw new InvalidOperationException("Resource not found."); } }
public static Effect CompileShader(GraphicsProcessor gpu, string sourceCode) { var programs = TryPreprocess(sourceCode); var effect = new Effect(gpu); foreach (var program in programs) { effect.AddProgram(program); } return(effect); }
public uint Compile(GraphicsProcessor gpu) { var programID = gpu.CreateShaderProgram(); foreach (var compilation in _compilations) { var shader = _shaders.First(x => x.Name == compilation.Key); gpu.CompileGLSL(programID, compilation.Value, shader.SourceCode); } gpu.VerifyShaderProgram(programID); return(programID); }
public BasicEffect(GraphicsProcessor gpu) { // Retrieve the built-in effect shader program. var text = string.Empty; if (!Resource.TryGetString(this.GetType().Assembly, "Thundershock.Core.Resources.Effects.BasicEffect.glsl", out text)) { throw new InvalidOperationException( "Cannot create the BasicEffect effect, the embedded GLSL shader resource is missing. This should never happen. Rebuild/reinstall Thundershock or your game now."); } // Compile the shader. _effect = ShaderPipeline.CompileShader(gpu, text); }
public static Texture2D FromStream(GraphicsProcessor gpu, Stream stream) { // load the stream as a bitmap using System.Drawing var bmp = (Bitmap)Image.FromStream(stream); // We now know the width and height. var width = bmp.Width; var height = bmp.Height; // Now we can copy the bitmap data from the bitmap into an array that we'll eventually // upload to the graphics card. // // We start by locking the bitmap data in memory and allocating space for us to pull the data // into the land of managed code. var bmpData = Array.Empty <byte>(); var rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height); var lck = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); // Now that the data is locked in place, we can allocate space for it. Array.Resize(ref bmpData, Math.Abs(lck.Stride) * lck.Height); // Now we can copy the data from native land into managed land. Marshal.Copy(lck.Scan0, bmpData, 0, bmpData.Length); // Now we can unlock the bitmap and then free it, we're done with it. bmp.UnlockBits(lck); bmp.Dispose(); // GDI+ gives us the bytes in the order of BGRA, but the Thundershock graphics pipeline works in // RGBA. So we need to swap the bytes. for (var i = 0; i < bmpData.Length; i += 4) { var b = bmpData[i]; var r = bmpData[i + 2]; bmpData[i] = r; bmpData[i + 2] = b; } // Create the texture. var texture = new Texture2D(gpu, width, height); // Upload the pixel data to the texture. texture.Upload(new ReadOnlySpan <byte>(bmpData, 0, bmpData.Length)); // Return the texture! return(texture); }
public Texture(GraphicsProcessor gpu, int width, int height) { if (width <= 0) { throw new InvalidOperationException("Texture width must be above zero."); } if (height < 0) { throw new InvalidOperationException("Texture height must be above zero."); } _width = width; _height = height; _gpu = gpu ?? throw new ArgumentNullException(nameof(gpu)); // create the texture on the GPU _id = _gpu.CreateTexture(width, height); _bounds = new Rectangle(0, 0, Width, Height); }
public Renderer(GraphicsProcessor gpu) { _gpu = gpu ?? throw new ArgumentNullException(nameof(gpu)); _vertexBuffer = new VertexBuffer(_gpu); _defaultEffect = new BasicEffect(_gpu); }
public VertexBuffer(GraphicsProcessor gpu) { _gpu = gpu ?? throw new ArgumentNullException(nameof(gpu)); }
public TextureCollection(GraphicsProcessor gpu) { _gpu = gpu ?? throw new ArgumentNullException(nameof(gpu)); }
public Texture2D(GraphicsProcessor gpu, int width, int height) : base(gpu, width, height) { }