Beispiel #1
0
 public (bool TextureExists, IntPtr TextureHandle) GetFfxTextureIntPtr(int textureId)
 {
     if (!_loadedTexturesDictionary.ContainsKey(textureId))
     {
         var a = _ffxTexturesIEnumerable.Where(item => item.Name.Contains($"s{textureId.ToString("00000")}.tpf"));
         if (a.Any())
         {
             var tpfBytes        = a.First().Bytes;
             var tpfTexturesList = TPF.Read(tpfBytes).Textures;
             if (tpfTexturesList.Any())
             {
                 var            ddsBytes       = tpfTexturesList.First().Bytes;
                 DdsImage       ddsImage       = new DdsImage(ddsBytes);
                 Image <Rgba32> image          = Image.LoadPixelData <Rgba32>(ddsImage.Data, ddsImage.Width, ddsImage.Height);
                 var            img            = new ImageSharpTexture(image);
                 var            veldridTexture = img.CreateDeviceTexture(MainUserInterface.Gd, MainUserInterface.Gd.ResourceFactory);
                 var            textureHandle  = MainUserInterface.Controller.GetOrCreateImGuiBinding(MainUserInterface.Gd.ResourceFactory, veldridTexture);
                 veldridTexture.Dispose();
                 _loadedTexturesDictionary.Add(textureId, textureHandle);
                 return(true, textureHandle);
             }
         }
         return(false, IntPtr.Zero);
     }
     else
     {
         return(true, _loadedTexturesDictionary[textureId]);
     }
 }
Beispiel #2
0
 /// <summary>Projects the on change project.</summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">if set to <c>true</c> [e].</param>
 private void Project_OnChangeProject(object sender, bool e)
 {
     image             = Image.LoadPixelData <Rgba32>(Project.Current.VideoGame.PreviewRender(), 512, 512);
     imageSharpTexture = new ImageSharpTexture(image, true);
     texture           = imageSharpTexture.CreateDeviceTexture(imGuiController.graphicsDevice, imGuiController.graphicsDevice.ResourceFactory);
     intPtr            = imGuiController.GetOrCreateImGuiBinding(imGuiController.graphicsDevice.ResourceFactory, texture);
 }
Beispiel #3
0
        public VxTexture(string imagePath)
        {
            ImageSharpTexture imageSharpTex = new ImageSharpTexture(imagePath);

            Tex  = imageSharpTex.CreateDeviceTexture(VxContext.Instance.Device, VxContext.Instance.Factory);
            View = VxContext.Instance.Factory.CreateTextureView(Tex);
        }
Beispiel #4
0
        public void ChooseMesh()
        {
            // CPU Resources

            // Texture
            ImageSharpTexture inputImage = new ImageSharpTexture("JoshIconC.png", false);

            // Vertices
            mVertices = GetCubeVertices();

            // Indices
            mIndices = GetCubeIndices();

            // GPU Resources

            // Texture
            mSurfaceTexture     = inputImage.CreateDeviceTexture(mGraphicsDevice, mFactory);
            mSurfaceTextureView = mFactory.CreateTextureView(mSurfaceTexture);

            // VertexBuffer
            mVertexBuffer = mFactory.CreateBuffer(new BufferDescription((uint)(VertexPositionTexture.SizeInBytes * mVertices.Length), BufferUsage.VertexBuffer));
            mGraphicsDevice.UpdateBuffer(mVertexBuffer, 0, mVertices);

            // IndexBuffer
            mIndexBuffer = mFactory.CreateBuffer(new BufferDescription(sizeof(ushort) * (uint)mIndices.Length, BufferUsage.IndexBuffer));
            mGraphicsDevice.UpdateBuffer(mIndexBuffer, 0, mIndices);
        }
Beispiel #5
0
        private static Texture2D CreateTexture(GraphicsDevice device, ResourceFactory factory, ImageSharpTexture tex)
        {
            var texture     = tex.CreateDeviceTexture(device, factory);
            var textureView = factory.CreateTextureView(texture);

            return(new Texture2D(texture, textureView));
        }
        private void InitTextures()
        {
            _tileSetAddresser = new TileSetAddresser();
            var imageSharpTexture = new ImageSharpTexture(_tileSetAddresser.TileSet);

            _skeletonTexture     = imageSharpTexture.CreateDeviceTexture(_graphicsDevice, _graphicsDevice.ResourceFactory);
            _skeletonTextureView = _graphicsDevice.ResourceFactory.CreateTextureView(_skeletonTexture);
        }
Beispiel #7
0
        public static GraphicsDevice CreateTextureFromFile(this GraphicsDevice device,
                                                           string filename, out Texture texture)
        {
            var img = new ImageSharpTexture(filename);

            texture = img.CreateDeviceTexture(device, device.ResourceFactory);
            return(device);
        }
Beispiel #8
0
        protected System.IntPtr NewImage(string path)
        {
            var     ist = new ImageSharpTexture(path);
            Texture t   = ist.CreateDeviceTexture(_gd, _gd.ResourceFactory);

            //_newImguiTex.Add(path, t);
            return(_ren.GetOrCreateImGuiBinding(_gd.ResourceFactory, t));
        }
Beispiel #9
0
        public Texture CreateDeviceTexture(GraphicsDevice gd, ResourceFactory rf, TextureUsage usage)
        {
            IsDirty = false;
            var texture = _texture.CreateDeviceTexture(gd, rf);

            texture.Name = "T_" + Name;
            return(texture);
        }
Beispiel #10
0
        private void UpdateResources(GraphicsDevice device, RenderingContext context)
        {
            var factory       = device.ResourceFactory;
            var texture       = new ImageSharpTexture(_image.Data, false);
            var deviceTexture = texture.CreateDeviceTexture(device, factory);

            _textureView     = factory.CreateTextureView(new TextureViewDescription(deviceTexture));
            _worldTextureSet = context.Renderer.MakeTextureViewSet(_textureView);
        }
Beispiel #11
0
        public ImageView(Stream stream, GraphicsDevice gd, ImGuiRenderer renderer)
        {
            _graphicsDevice = gd;
            _imguiRenderer  = renderer;

            var imageSharpTex = new ImageSharpTexture(stream);

            _texture      = imageSharpTex.CreateDeviceTexture(_graphicsDevice, _graphicsDevice.ResourceFactory);
            _textureViews = new Dictionary <TextureViewDescription, TextureView>();
        }
Beispiel #12
0
        public Texture GenerateRgbaVeldridTextureFromPixelData(Rgba32[] data, uint width, uint height, bool mipMap)
        {
            var image = SixLabors.ImageSharp.Image.LoadPixelData(data, (int)width, (int)height);

            var imageSharpTexture = new ImageSharpTexture(image, mipMap);

            var veldridTexture = imageSharpTexture.CreateDeviceTexture(_systemComponents.Device.RawVeldridDevice, _systemComponents.Factory.RawFactory);

            return(veldridTexture);
        }
Beispiel #13
0
        // TODO: Change how sampler is passed in
        public Texture2D(string path, Sampler sampler)
        {
            Sampler       = sampler;
            _textureData  = new ImageSharpTexture(path);
            DeviceTexture = _textureData.CreateDeviceTexture(Renderer.GraphicsDevice, Renderer.ResourceFactory);
            TextureView   = Renderer.ResourceFactory.CreateTextureView(DeviceTexture);

            Width  = (int)_textureData.Width;
            Height = (int)_textureData.Height;
        }
        public static Texture FromFile(GraphicsDevice device, string filename)
        {
            var tex     = new Texture();
            var factory = device.ResourceFactory;
            var image   = new ImageSharpTexture(filename);

            tex.DeviceTexture     = image.CreateDeviceTexture(device, factory);
            tex.DeviceTextureView = factory.CreateTextureView(new TextureViewDescription(tex.DeviceTexture));

            return(tex);
        }
Beispiel #15
0
        public Texture GenerateSingleWhitePixel()
        {
            var image = SixLabors.ImageSharp.Image.LoadPixelData(new Rgba32[] {
                new Rgba32(1.0f, 1.0f, 1.0f, 1.0f)
            }, 1, 1);
            var imageSharpTexture = new ImageSharpTexture(image, false);

            var veldridTexture = imageSharpTexture.CreateDeviceTexture(_systemComponents.Device.RawVeldridDevice, _systemComponents.Factory.RawFactory);

            return(veldridTexture);
        }
        public static Texture FromImage(GraphicsDevice device, Image <Rgba32> img)
        {
            var tex     = new Texture();
            var factory = device.ResourceFactory;
            var image   = new ImageSharpTexture(img);

            tex.DeviceTexture     = image.CreateDeviceTexture(device, factory);
            tex.DeviceTextureView = factory.CreateTextureView(new TextureViewDescription(tex.DeviceTexture));

            return(tex);
        }
        /// <summary>
        /// Adds the image to the Graphic Device as a texture.
        /// Then returns the pointer of the added texture. It also
        /// cache the image internally rather than creating a new texture on every call,
        /// so this function can be called multiple times per image (per FPS).
        /// </summary>
        /// <param name="filePath">
        /// Path to the image on disk. If the image is loaded in the memory
        /// save it on the disk before sending to this function. Reason for this
        /// is to cache the Image Texture using filePath as the key.
        /// </param>
        /// <returns>
        /// A pointer to the Texture in the Graphic Device.
        /// </returns>
        public IntPtr AddOrGetImagePointer(string filePath)
        {
            if (!loadedImages.TryGetValue(filePath, out Texture texture))
            {
                ImageSharpTexture imgSharpTexture = new ImageSharpTexture(filePath);
                texture = imgSharpTexture.CreateDeviceTexture(graphicsDevice, graphicsDevice.ResourceFactory);
                loadedImages.Add(filePath, texture);
            }

            return imController.GetOrCreateImGuiBinding(graphicsDevice.ResourceFactory, texture);
        }
Beispiel #18
0
        public static ResourceSet GenerateTextureResourceSetForDiffuseMapping <T>(ModelRuntimeDescriptor <T> modelRuntimeState, int meshIndex, DisposeCollectorResourceFactory factory, GraphicsDevice graphicsDevice) where T : struct, VertexLocateable
        {
            RealtimeMaterial material = modelRuntimeState.Model.GetMaterial(meshIndex);

            ImageSharpTexture diffuseTextureIS   = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, modelRuntimeState.Model.BaseDir, material.textureDiffuse));
            Texture           diffuseTexture     = diffuseTextureIS.CreateDeviceTexture(graphicsDevice, factory);
            TextureView       diffuseTextureView = factory.CreateTextureView(diffuseTexture);

            return(factory.CreateResourceSet(new ResourceSetDescription(
                                                 modelRuntimeState.TextureResourceLayout,
                                                 diffuseTextureView,
                                                 modelRuntimeState.TextureSampler
                                                 )));
        }
Beispiel #19
0
        public MaterialTexture(GraphicsDevice device, ResourceLayout layout, Resource <Image <Rgba32> > image, RgbaFloat colour)
        {
            ImageWidth  = image.Data.Width;
            ImageHeight = image.Data.Height;

            var factory       = device.ResourceFactory;
            var texture       = new ImageSharpTexture(image.Data, false);
            var deviceTexture = texture.CreateDeviceTexture(device, factory);

            TextureColourBuffer = factory.CreateBuffer(new BufferDescription(sizeof(float) * 4, BufferUsage.UniformBuffer));
            device.UpdateBuffer(TextureColourBuffer, 0, ref colour);

            TextureView = factory.CreateTextureView(new TextureViewDescription(deviceTexture));
            ResourceSet = factory.CreateResourceSet(new ResourceSetDescription(layout, TextureView, device.PointSampler, TextureColourBuffer));
        }
        public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            batch.CreateDeviceObjects(gd, cl, sc);

            tex0 = image0.CreateDeviceTexture(gd, gd.ResourceFactory);

            sampler0 = gd.PointSampler;

            SetupBatchPipeline(
                gd,
                gd.ResourceFactory,
                sc.MainSceneFramebuffer.OutputDescription);

            needsbuild = true;
        }
Beispiel #21
0
        public static Texture CreateSimpleTexture(GraphicsDevice gd, TextureUsage usage, SixLabors.ImageSharp.Image <Rgba32> image, string name)
        {
            if (gd == null)
            {
                throw new ArgumentNullException(nameof(gd));
            }
            if (usage != TextureUsage.Sampled && usage != (TextureUsage.Sampled | TextureUsage.GenerateMipmaps))
            {
                throw new ArgumentOutOfRangeException(nameof(usage), "Only sampled texture usage is currently supported (with optional mipmapping)");
            }

            ImageSharpTexture imageSharpTexture = new ImageSharpTexture(image, (usage & TextureUsage.GenerateMipmaps) != 0);
            var texture = imageSharpTexture.CreateDeviceTexture(gd, gd.ResourceFactory);

            texture.Name = "T_" + name;
            return(texture);
        }
Beispiel #22
0
        /// <summary>
        /// Adds the image to the Graphic Device as a texture.
        /// Then returns the pointer of the added texture. It also
        /// cache the image internally rather than creating a new texture on every call,
        /// so this function can be called multiple times per frame.
        /// </summary>
        /// <param name="filePath">
        /// Path to the image on disk. If the image is loaded in the memory
        /// save it on the disk before sending to this function. Reason for this
        /// is to cache the Image Texture using filePath as the key.
        /// </param>
        /// <param name="handle">output pointer to the image in the graphic device.</param>
        /// <param name="width">width of the loaded image.</param>
        /// <param name="height">height of the loaded image.</param>
        public void AddOrGetImagePointer(
            string filePath,
            out IntPtr handle,
            out uint width,
            out uint height)
        {
            if (!loadedImages.TryGetValue(filePath, out Texture texture))
            {
                ImageSharpTexture imgSharpTexture = new ImageSharpTexture(filePath);
                texture = imgSharpTexture.CreateDeviceTexture(graphicsDevice, graphicsDevice.ResourceFactory);
                loadedImages.Add(filePath, texture);
            }

            width  = texture.Width;
            height = texture.Height;
            handle = imController.GetOrCreateImGuiBinding(graphicsDevice.ResourceFactory, texture);
        }
Beispiel #23
0
        protected override Texture LoadEntry(FileSystemEntry entry, ContentManager contentManager, Game game, LoadOptions loadOptions)
        {
            var generateMipMaps = (loadOptions as TextureLoadOptions)?.GenerateMipMaps ?? true;

            Texture applyDebugName(Texture texture)
            {
                texture.Name = entry.FilePath;
                return(texture);
            }

            switch (Path.GetExtension(entry.FilePath).ToLowerInvariant())
            {
            case ".dds":
                if (!DdsFile.IsDdsFile(entry))
                {
                    goto case ".tga";
                }
                var ddsFile = DdsFile.FromFileSystemEntry(entry);
                return(applyDebugName(CreateTextureFromDds(
                                          contentManager.GraphicsDevice,
                                          ddsFile)));

            case ".tga":
                var tgaFile = TgaFile.FromFileSystemEntry(entry);
                return(applyDebugName(CreateTextureFromTga(
                                          contentManager.GraphicsDevice,
                                          tgaFile,
                                          generateMipMaps)));

            case ".jpg":
                using (var stream = entry.Open())
                {
                    var jpgFile = new ImageSharpTexture(stream);
                    return(jpgFile.CreateDeviceTexture(
                               contentManager.GraphicsDevice,
                               contentManager.GraphicsDevice.ResourceFactory));
                }

            default:
                throw new InvalidOperationException();
            }
        }
        public bool Load(GraphicsDevice graphicsDevice, string fileName)
        {
            try
            {
                ImageSharpTexture image = new ImageSharpTexture(fileName);
                _texture    = image.CreateDeviceTexture(graphicsDevice, graphicsDevice.ResourceFactory);
                TextureView = graphicsDevice.ResourceFactory.CreateTextureView(_texture);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

                TextureView.Dispose();
                _texture.Dispose();

                return(false);
            }

            return(true);
        }
Beispiel #25
0
        private static Texture CreateTextureFromTga(
            GraphicsDevice graphicsDevice,
            TgaFile tgaFile,
            bool generateMipMaps)
        {
            var rgbaData = TgaFile.ConvertPixelsToRgba8(tgaFile);

            using (var tgaImage = Image.LoadPixelData <Rgba32>(
                       rgbaData,
                       tgaFile.Header.Width,
                       tgaFile.Header.Height))
            {
                var imageSharpTexture = new ImageSharpTexture(
                    tgaImage,
                    generateMipMaps);

                return(imageSharpTexture.CreateDeviceTexture(
                           graphicsDevice,
                           graphicsDevice.ResourceFactory));
            }
        }
Beispiel #26
0
        public static Texture FromFile(string path, GraphicsDevice graphicsDevice, ResourceFactory resourceFactory)
        {
            var executablePath = System.Reflection.Assembly.GetEntryAssembly().Location;

            if (String.IsNullOrEmpty(executablePath))
            {
                executablePath = AppDomain.CurrentDomain.BaseDirectory;
            }
            if (String.IsNullOrEmpty(executablePath))
            {
                throw new Exception("Executable path cannot be null or empty");
            }
            var executableDirectory = Path.GetDirectoryName(executablePath);

            path = Path.Combine(executableDirectory, path);
            Console.WriteLine($"Loading texture at {path}");
            if (File.Exists(path))
            {
                var texture       = new ImageSharpTexture(path);
                var deviceTexture = texture.CreateDeviceTexture(graphicsDevice, resourceFactory);
                var textureView   = resourceFactory.CreateTextureView(deviceTexture);
                var hash          = 0l;
                using (var sha = SHA256Managed.Create())
                {
                    using (var stream = File.OpenRead(path))
                    {
                        var rawHash = sha.ComputeHash(stream);
                        hash = BitConverter.ToInt64(rawHash, 0);
                    }
                }

                return(new Texture(texture, deviceTexture, textureView, hash));
            }
            else
            {
                throw new Exception($"Texture {path} you are trying to load cannot be found");
            }
        }
Beispiel #27
0
 public void LoadAllFfxTexturesInMemory(IEnumerable <BinderFile> ffxTexturesIEnumerable)
 {
     foreach (var a in ffxTexturesIEnumerable)
     {
         var tpfBytes        = a.Bytes;
         var tpfTexturesList = TPF.Read(tpfBytes).Textures;
         if (tpfTexturesList.Any())
         {
             var tpf = tpfTexturesList.First();
             if (int.TryParse(tpf.Name.TrimStart('s'), out int textureId) && !_loadedTexturesDictionary.ContainsKey(textureId))
             {
                 var            ddsBytes = tpf.Bytes;
                 DdsImage       ddsImage = new DdsImage(ddsBytes);
                 Image <Rgba32> image    = Image.LoadPixelData <Rgba32>(ddsImage.Data, ddsImage.Width, ddsImage.Height);
                 for (int y = 0; y < image.Height; y++)
                 {
                     for (int x = 0; x < image.Width; x++)
                     {
                         var rgba32 = image[x, y];
                         var r      = rgba32.R;
                         var g      = rgba32.G;
                         var b      = rgba32.B;
                         rgba32.R    = b;
                         rgba32.G    = g;
                         rgba32.B    = r;
                         image[x, y] = rgba32; //Set Inverted Pixels
                     }
                 }
                 var img            = new ImageSharpTexture(image);
                 var veldridTexture = img.CreateDeviceTexture(MainUserInterface.Gd, MainUserInterface.Gd.ResourceFactory);
                 var textureHandle  = MainUserInterface.Controller.GetOrCreateImGuiBinding(MainUserInterface.Gd.ResourceFactory, veldridTexture);
                 veldridTexture.Dispose();
                 _loadedTexturesDictionary.Add(textureId, textureHandle);
             }
         }
     }
 }
Beispiel #28
0
 public bool LoadTexture(YALCTFilePickerItem item)
 {
     try
     {
         ImageSharpTexture imageSharpTex = new ImageSharpTexture(item.FullPath);
         Texture           texture       = imageSharpTex.CreateDeviceTexture(graphicsDevice, factory);
         TextureView       textureView   = factory.CreateTextureView(texture);
         textureView.Name = item.Name;
         // as per https://github.com/mellinoe/veldrid/issues/188
         TextureView imguiTextureView = factory.CreateTextureView(new TextureViewDescription(texture, PixelFormat.R8_G8_B8_A8_UNorm_SRgb));
         imguiTextureView.Name = item.Name;
         IntPtr imguiBinding = imGuiRenderer.GetOrCreateImGuiBinding(factory, imguiTextureView);
         textures.Add(texture);
         textureViews.Add(textureView);
         imguiTextureViews.Add(imguiTextureView);
         imguiTextures.Add(new YALCTShaderResource(item, new Vector2(texture.Width, texture.Height), imguiBinding));
     }
     catch (Exception e)
     {
         uiController.SetError(e.Message);
         return(false);
     }
     return(true);
 }
        protected unsafe override void CreateResources(ResourceFactory factory)
        {
            _camera.Position     = new Vector3(10f, 0f, 0f);
            _camera.NearDistance = 0.001f;
            _camera.FarDistance  = 100f;
            _camera.Pitch        = 0f;
            _camera.Yaw          = (float)Math.PI / 2;
            //_camera.
            //_camera.



            _projectionBuffer = factory.CreateBuffer(new BufferDescription(192, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
            // _viewBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));
            //_worldBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer));

            _vertexBuffer = factory.CreateBuffer(new BufferDescription((uint)(VertexPositionColorTexture.SizeInBytes * 100000), BufferUsage.VertexBuffer));
            GraphicsDevice.UpdateBuffer(_vertexBuffer, 0, _vertices);

            _indexBuffer = factory.CreateBuffer(new BufferDescription(sizeof(ushort) * 400000, BufferUsage.IndexBuffer));
            GraphicsDevice.UpdateBuffer(_indexBuffer, 0, _indices);



            var inPath = @"E:\swyy\Lib\veldrid-samples\assets\Earth.png";
            ImageSharpTexture inputImage = new ImageSharpTexture(inPath, false);

            _surfaceTexture = inputImage.CreateDeviceTexture(GraphicsDevice, factory);

            _surfaceTextureView = factory.CreateTextureView(_surfaceTexture);
            //_surfaceTexture = _stoneTexData.CreateDeviceTexture(GraphicsDevice, ResourceFactory,TextureUsage.Storage);
            //_surfaceTextureView = factory.CreateTextureView(_surfaceTexture);

            ShaderSetDescription shaderSet = new ShaderSetDescription(
                new[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3))
            },
                new[]
            {
                LoadShader(ShaderStages.Vertex),
                LoadShader(ShaderStages.Fragment)
            });

            ResourceLayout projViewLayout = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex)
                    ));

            ResourceLayout worldTextureLayout = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("SurfaceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                    new ResourceLayoutElementDescription("SurfaceSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            var rd = RasterizerStateDescription.Default;

            rd.CullMode = FaceCullMode.Front;
            //rd.c
            //rd.FillMode = PolygonFillMode.Wireframe;
            _pipeline = factory.CreateGraphicsPipeline(new GraphicsPipelineDescription(
                                                           BlendStateDescription.SingleOverrideBlend,
                                                           DepthStencilStateDescription.DepthOnlyLessEqual,
                                                           rd,
                                                           PrimitiveTopology.TriangleList,
                                                           shaderSet,
                                                           new[] { projViewLayout, worldTextureLayout },
                                                           MainSwapchain.Framebuffer.OutputDescription));

            _projViewSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         projViewLayout,
                                                         _projectionBuffer
                                                         ));
            //var sampler = Veldrid.SamplerDescription.Aniso4x;
            //sampler.AddressModeU=SamplerAddressMode.

            _worldTextureSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                             worldTextureLayout,
                                                             _surfaceTextureView,
                                                             GraphicsDevice.Aniso4xSampler));

            _cl = factory.CreateCommandList();
        }
Beispiel #30
0
        private List <IDisposable> createCubeResources()
        {
            ImageSharpTexture NameImage       = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "Textures", "Name.png"));
            Texture           cubeTexture     = NameImage.CreateDeviceTexture(GraphicsDevice, _factory);
            TextureView       cubeTextureView = _factory.CreateTextureView(cubeTexture);

            ResourceLayout textureLayout = _factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                    new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            _textureNameResourceSet = _factory.CreateResourceSet(new ResourceSetDescription(
                                                                     textureLayout,
                                                                     cubeTextureView,
                                                                     GraphicsDevice.LinearSampler));

            Mesh <VertexPositionTexture> texturedCube
                = GeometryFactory.GenerateTexturedCube(false);

            ushort[] cubeIndicies = GeometryFactory.generateCubeIndicies_TriangleList_CW();

            // declare (VBO) buffers
            _vertexBufferCube
                = _factory.CreateBuffer(new BufferDescription(texturedCube.Vertices.LengthUnsigned() * VertexPositionTexture.SizeInBytes, BufferUsage.VertexBuffer));
            _indexBufferCube
                = _factory.CreateBuffer(new BufferDescription(cubeIndicies.LengthUnsigned() * sizeof(ushort), BufferUsage.IndexBuffer));

            // fill buffers with data
            GraphicsDevice.UpdateBuffer(_vertexBufferCube, 0, texturedCube.Vertices);
            GraphicsDevice.UpdateBuffer(_indexBufferCube, 0, cubeIndicies);

            VertexLayoutDescription vertexLayout
                = new VertexLayoutDescription(
                      new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3),
                      new VertexElementDescription("UV", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2)
                      );

            _worldTransCube = Matrix4x4.CreateWorld(new Vector3(0, 0, 2), -Vector3.UnitZ, Vector3.UnitY);

            _vertexShaderCube   = IO.LoadShader("Texture", ShaderStages.Vertex, GraphicsDevice);
            _fragmentShaderCube = IO.LoadShader("Texture", ShaderStages.Fragment, GraphicsDevice);

            GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription()
            {
                BlendState        = BlendStateDescription.SingleOverrideBlend,
                DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual,
                RasterizerState   = new RasterizerStateDescription(
                    cullMode: FaceCullMode.Back,
                    fillMode: PolygonFillMode.Solid,
                    frontFace: FrontFace.Clockwise,
                    depthClipEnabled: true,
                    scissorTestEnabled: false
                    ),
                PrimitiveTopology = PrimitiveTopology.TriangleList,
                ResourceLayouts   = new ResourceLayout[] { _transformationPipelineResourceLayout, textureLayout },
                ShaderSet         = new ShaderSetDescription(
                    vertexLayouts: new VertexLayoutDescription[] { vertexLayout },
                    shaders: new Shader[] { _vertexShaderCube, _fragmentShaderCube }
                    ),
                Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription
            };

            _pipelineCube = _factory.CreateGraphicsPipeline(pipelineDescription);

            pipelineDescription = new GraphicsPipelineDescription()
            {
                BlendState        = BlendStateDescription.SingleOverrideBlend,
                DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual,
                RasterizerState   = new RasterizerStateDescription(
                    cullMode: FaceCullMode.Back,
                    fillMode: PolygonFillMode.Solid,
                    frontFace: FrontFace.Clockwise,
                    depthClipEnabled: true,
                    scissorTestEnabled: false
                    ),
                PrimitiveTopology = PrimitiveTopology.TriangleList,
                ResourceLayouts   = new ResourceLayout[] { _transformationPipelineResourceLayout, textureLayout },
                ShaderSet         = new ShaderSetDescription(
                    vertexLayouts: new VertexLayoutDescription[] { vertexLayout },
                    shaders: new Shader[] { _vertexShaderCube, _fragmentShaderCube }
                    ),
                Outputs = _offScreenFBO.OutputDescription
            };

            _pipelineCubeOffscreen = _factory.CreateGraphicsPipeline(pipelineDescription);

            return(new List <IDisposable>()
            {
                _vertexBufferCube,
                _indexBufferCube,
                _textureNameResourceSet,
                _vertexShaderCube,
                _fragmentShaderCube,
                _pipelineCube
            });
        }