private void InitializeContextObjects(AssetDatabase ad, RenderContext rc) { ResourceFactory factory = rc.ResourceFactory; _vb = factory.CreateVertexBuffer(_meshData.Vertices.Length * VertexPositionNormalTexture.SizeInBytes, false); _vb.SetVertexData( _meshData.Vertices, new VertexDescriptor( VertexPositionNormalTexture.SizeInBytes, VertexPositionNormalTexture.ElementCount, IntPtr.Zero)); _ib = factory.CreateIndexBuffer(sizeof(int) * _meshData.Indices.Length, false); _ib.SetIndices(_meshData.Indices); _shadowPassMaterial = CreateShadowPassMaterial(rc.ResourceFactory); _regularPassMaterial = CreateRegularPassMaterial(rc.ResourceFactory); _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); _inverseTransposeWorldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); _surfaceTexture = _textureData.CreateDeviceTexture(factory); _surfaceTextureBinding = factory.CreateShaderTextureBinding(_surfaceTexture); _shadowMapSampler = rc.ResourceFactory.CreateSamplerState( SamplerAddressMode.Border, SamplerAddressMode.Border, SamplerAddressMode.Border, SamplerFilter.MinMagMipPoint, 1, RgbaFloat.White, DepthComparison.Always, 0, int.MaxValue, 0); }
private void RecreateTexture() { _deviceTexture.Dispose(); _textureBinding.Dispose(); _texture = _textureRef.Get(_ad); _deviceTexture = _texture.CreateDeviceTexture(_gs.Context.ResourceFactory); _textureBinding = _gs.Context.ResourceFactory.CreateShaderTextureBinding(_deviceTexture); }
private void InitializeContextObjects(RenderContext context) { _currentContext = context; ResourceFactory factory = context.ResourceFactory; _vb = factory.CreateVertexBuffer(VertexPositionNormalTexture.SizeInBytes * _vertices.Length, false); VertexDescriptor desc = new VertexDescriptor( VertexPositionNormalTexture.SizeInBytes, VertexPositionNormalTexture.ElementCount, IntPtr.Zero); _vb.SetVertexData(_vertices, desc); _ib = factory.CreateIndexBuffer(sizeof(ushort) * _indices.Length, false); _ib.SetIndices(_indices); VertexInputDescription materialInputs = new VertexInputDescription( VertexPositionNormalTexture.SizeInBytes, new VertexInputElement[] { new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("in_normal", VertexSemanticType.Normal, VertexElementFormat.Float3), new VertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2) }); ShaderResourceDescription[] resources = new[] { new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("LightBuffer", Unsafe.SizeOf <DirectionalLightBuffer>()), new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("InverseTransposeWorldMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("surfaceTexture", ShaderResourceType.Texture), new ShaderResourceDescription("surfaceTexture", ShaderResourceType.Sampler) }; _material = factory.CreateMaterial( context, VertexShaderSource, FragmentShaderSource, materialInputs, resources); _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); _inverseTransposeWorldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); DeviceTexture2D texture = _texture.CreateDeviceTexture(factory); _textureBinding = factory.CreateShaderTextureBinding(texture); s_wireframeRasterizerState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Wireframe, true, true); }
public static DeviceTexture2D GetCachedTexture(RenderContext rc, TextureData texData) { if (s_previousRC != rc) { s_previousRC = rc; Clear(); } if (!s_deviceTextures.TryGetValue(texData, out DeviceTexture2D ret)) { ret = texData.CreateDeviceTexture(rc.ResourceFactory); s_deviceTextures.Add(texData, ret); } return(ret); }
private void InitializeContextObjects(AssetDatabase ad, RenderContext rc) { ResourceFactory factory = rc.ResourceFactory; _vb = factory.CreateVertexBuffer(_vertices.Length * VertexPositionNormalTexture.SizeInBytes, false); _vb.SetVertexData( _vertices, new VertexDescriptor( VertexPositionNormalTexture.SizeInBytes, VertexPositionNormalTexture.ElementCount, 0, IntPtr.Zero)); _ib = factory.CreateIndexBuffer(sizeof(int) * _indices.Length, false); _ib.SetIndices(_indices); _shadowPassMaterial = _shadowPassMaterialAsset.Create(ad, rc); _regularPassMaterial = _regularPassMaterialAsset.Create(ad, rc); if (_overrideTextureData != null) { _overrideTexture = _overrideTextureData.CreateDeviceTexture(factory); _overrideTextureBinding = factory.CreateShaderTextureBinding(_overrideTexture); } }
private async void InitializeContextObjects(RenderContext context, MaterialCache materialCache, BufferCache bufferCache) { ResourceFactory factory = context.ResourceFactory; Debug.Assert(_vb == null); Debug.Assert(_ib == null); Debug.Assert(_deviceTexture == null); Debug.Assert(_textureBinding == null); _vb = bufferCache.GetVertexBuffer(_mesh); CreateIndexBuffer(wasTransparent: false); if (s_regularGlobalInputs == null) { s_regularGlobalInputs = new MaterialInputs <MaterialGlobalInputElement>( new MaterialGlobalInputElement[] { new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, "ProjectionMatrix"), new MaterialGlobalInputElement("ViewMatrixBuffer", MaterialInputType.Matrix4x4, "ViewMatrix"), new MaterialGlobalInputElement("LightProjectionMatrixBuffer", MaterialInputType.Matrix4x4, "LightProjMatrix"), new MaterialGlobalInputElement("LightViewMatrixBuffer", MaterialInputType.Matrix4x4, "LightViewMatrix"), new MaterialGlobalInputElement("LightInfoBuffer", MaterialInputType.Custom, "LightBuffer"), new MaterialGlobalInputElement("CameraInfoBuffer", MaterialInputType.Custom, "CameraInfo"), new MaterialGlobalInputElement("PointLightsBuffer", MaterialInputType.Custom, "PointLights") }); } _regularPassMaterial = materialCache.GetMaterial( context, RegularPassVertexShaderSource, RegularPassFragmentShaderSource, s_vertexInputs, s_regularGlobalInputs, s_perObjectInputs, s_textureInputs); _regularPassTransparentMaterial = materialCache.GetMaterial( context, RegularPassTransparentVertexShaderSource, RegularPassTransparentFragmentShaderSource, s_vertexInputs, s_regularGlobalInputs, s_transparentPerObjectInputs, s_transparentTextureInputs); if (_texture == null) { _texture = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Pink); } _deviceTexture = await _gs.ExecuteOnMainThread(() => _texture.CreateDeviceTexture(factory)); _textureBinding = await _gs.ExecuteOnMainThread(() => factory.CreateShaderTextureBinding(_deviceTexture)); if (s_shadowmapGlobalInputs == null) { s_shadowmapGlobalInputs = new MaterialInputs <MaterialGlobalInputElement>( new MaterialGlobalInputElement[] { new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, "LightProjMatrix"), new MaterialGlobalInputElement("ViewMatrixBuffer", MaterialInputType.Matrix4x4, "LightViewMatrix") }); } _shadowPassMaterial = materialCache.GetMaterial( context, ShadowMapPassVertexShaderSource, ShadowMapPassFragmentShaderSource, s_vertexInputs, s_shadowmapGlobalInputs, s_shadowmapPerObjectInputs, MaterialTextureInputs.Empty); if (s_wireframeRS == null) { s_wireframeRS = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Wireframe, true, true); } if (s_noCullRS == null) { s_noCullRS = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true); } _initialized = true; }