c# - SharpDX draw multiple primitives at the same time -
i trying create high level api sharpdx. has able draw, stuck on how make work multiple draw calls @ same time.
this how call class
directxfinald d33d = new directxfinald();
here create new object of type viereck(rectangle)
viereck viereck = new viereck(0, 0, 0.2, 0.1, mybrush, mybrush, 1, false);
here pass object class
d33d.drawdirectx(viereck);
and works, problem is, want able can pass more objects @ given time, , let them drawn. tried update vertexbuffer , += vertices, problem different shapes need different topologies. here class:
namespace drawhost { public class directxfinald : drawhost.directxbased<d3d11>, idrawable { ; public directxfinald(idrawable objecttodraw = null, datastream stream = null) { this.objecttodraw = objecttodraw; if (stream == null) stream = new datastream(32 * 612500, true, true); else this.stream = stream; } protected override void attach() { #region shader if (renderer == null) return; device = renderer.device; context = device.immediatecontext; // compile vertex , pixel shaders vertexshaderbytecode = shaderbytecode.compilefromfile("minitri.fx", "vs", "vs_4_0", shaderflags.none, effectflags.none); vertexshader = new vertexshader(device, vertexshaderbytecode); pixelshaderbytecode = shaderbytecode.compilefromfile("minitri.fx", "ps", "ps_4_0", shaderflags.none, effectflags.none); pixelshader = new pixelshader(device, pixelshaderbytecode); // layout vertexshader input signature layout = new inputlayout(device, shadersignature.getinputsignature(vertexshaderbytecode), new[] { new inputelement("position",0,format.r32g32b32a32_float,0,0), new inputelement("color",0,format.r32g32b32a32_float,16,0) }); #endregion if (objecttodraw == null) { } else { float r = 0; float g = 0; float b = 0; switch (objecttodraw.tostring()) { #region dreieck case "dreieck": dreieck dreieck = (dreieck)objecttodraw; topology = primitivetopology.trianglestrip; convertcolor(ref r, ref g, ref b, ((system.windows.media.solidcolorbrush)dreieck.color).color); streamlist.add(new vector4((convert.tosingle(dreieck.x)), (convert.tosingle(dreieck.y) / 10), 0f, 1.0f)); streamlist.add(new vector4(r, g, b, 1.0f)); streamlist.add(new vector4(((convert.tosingle(dreieck.x) + convert.tosingle(dreieck.width))), -(convert.tosingle(dreieck.y)), 0f, 1.0f)); streamlist.add(new vector4(r, g, b, 1.0f)); streamlist.add(new vector4(-(convert.tosingle(dreieck.x)), -((convert.tosingle(dreieck.y) + convert.tosingle(dreieck.height) )), 0f, 1.0f)); streamlist.add(new vector4(r, g, b, 1.0f)); break; #endregion #region viereck case "viereck": viereck viereck = (viereck)objecttodraw; topology = primitivetopology.trianglestrip; convertcolor(ref r, ref g, ref b, ((system.windows.media.solidcolorbrush)viereck.color).color); streamlist.add(new vector4((convert.tosingle(viereck.x)), (convert.tosingle(viereck.y)), 0f, 1.0f)); streamlist.add(new vector4(r, g, b, 1.0f));// ok streamlist.add(new vector4(((convert.tosingle(viereck.x))), (convert.tosingle(viereck.y) + convert.tosingle(viereck.height)), 0f, 1.0f)); streamlist.add(new vector4(r, g, b, 1.0f));// ok streamlist.add(new vector4((convert.tosingle(viereck.x) + convert.tosingle(viereck.width)), (convert.tosingle(viereck.y) ), 0f, 1.0f)); streamlist.add(new vector4(r, g, b, 1.0f));// ok streamlist.add(new vector4((convert.tosingle(viereck.x) + convert.tosingle(viereck.width)), ((convert.tosingle(viereck.y) + convert.tosingle(viereck.height))), 0f, 1.0f)); streamlist.add(new vector4(r, g, b, 1.0f));// ok break; #endregion #region kreis case "kreis": topology = primitivetopology.undefined; kreis kreis = (kreis)objecttodraw; convertcolor(ref r, ref g, ref b, ((system.windows.media.solidcolorbrush)kreis.color).color); (float j = 0; j <= 360; j++) { (double = 0; <= 360; i++) //254 { double rad = * (math.pi / 180); float x = (float)math.cos(rad) * ((float)kreis.width / 2); float y = (float)math.sin(rad) * ((float)kreis.height / 2); streamlist.add(new vector4(x , y, 0f, 1.0f)); streamlist.add(new vector4(r, g, b, 1.0f)); } } break; #endregion }; foreach (vector4 in streamlist) { stream.writerange(new[] { }); } stream.position = 0; streamgv streamgv = new streamgv(stream); //streamgv.getlist(streamlist); //streamlist = null; gc.collect(); } vertices = new buffer(device, stream, new bufferdescription() { bindflags = bindflags.vertexbuffer, cpuaccessflags = cpuaccessflags.none, optionflags = resourceoptionflags.none, sizeinbytes = (int)stream.length, usage = resourceusage.default, structurebytestride = 0, }); stream.dispose(); // prepare stages context.inputassembler.inputlayout = (layout); context.inputassembler.primitivetopology = topology; context.inputassembler.setvertexbuffers(0, new vertexbufferbinding(vertices, 32, 0)); context.vertexshader.set(vertexshader); context.pixelshader.set(pixelshader); } public override void renderscene(draweventargs args) { renderer.device.immediatecontext.clearrendertargetview(renderer.rendertargetview, new color4(0.6f, 0, 0, 0)); renderer.device.immediatecontext.draw((int)stream.length, 0); return; } public override void case(dxelement dxviewer) { dxviewer11 = dxviewer; } public override void drawdirectx(idrawable objecttodraw) { this.objecttodraw = objecttodraw; //dxviewer11.renderer = new scene_11(); //renderer = new d3d11(); stream = new datastream(32 * 612500, true, true); streamgv strean = new streamgv(); dxviewer11.renderer = new directxfinald(objecttodraw, stream) { renderer = new d3d11() }; } private void convertcolor(ref float r, ref float g, ref float b, system.windows.media.color color) { r = (float)(color.r * 255); g = (float)(color.g * 255); b = (float)(color.b * 255); } }
how can make possible draw of them @ same time? using sharpdx renderform. 1 problem have change topology, example triangle needs trianglelist
but circle use linestrip
. appreciated
as can see you're calling attach ones. in attach method you're creating 1 vertex buffer, mesh depending on objecttodraw
.
you should decouple shader compilation code , vertexbuffer setup.
you create class manage vertexbuffer , 'knows' how draw mesh.
for example: (pseudo)
[structlayout(layoutkind.sequential)] public struct vertex { public const int stride = 16 + 16; public vector4 pos; public color4 color; } public class mesh { private vertex[] _vertices; private int[] _indices; private sharpdx.direct3d11.buffer _indexbuffer; private sharpdx.direct3d11.buffer _vertexbuffer; private vertexbufferbinding _vertexbufferbinding; public mesh(vertex[] vertices, int[] indices) { // save vertices in field _vertices = value; var vbd = new bufferdescription( sharpdx.utilities.sizeof<vertex>() * _vertices.length, resourceusage.immutable, bindflags.vertexbuffer, cpuaccessflags.none, resourceoptionflags.none, 0); // setup vertex buffer _vertexbuffer = sharpdx.direct3d11.buffer.create<vertex>(dx11.device, _vertices, vbd); // create binding _vertexbufferbinding = new vertexbufferbinding(_vertexbuffer, vertex.stride, 0); _indices = value; var ibd = new bufferdescription( sizeof(int) * _indices.length, resourceusage.immutable, bindflags.indexbuffer, cpuaccessflags.none, resourceoptionflags.none, 0); // setup index buffer _indexbuffer = sharpdx.direct3d11.buffer.create<int>(dx11.device, _indices, ibd); } // selectbuffers select right vertex buffer. // combined draw method, rather not // should call ones when draw multiple same mesh. public void selectbuffers() { dx11.device.immediatecontext.inputassembler.setvertexbuffers(0, _vertexbufferbinding); dx11.device.immediatecontext.inputassembler.setindexbuffer(_indexbuffer, sharpdx.dxgi.format.r32_uint, 0); } public void draw() { dx11.device.immediatecontext.drawindexed(_indices.length, 0, 0); } }
list<mesh> _meshes = new list<mesh>(); public void setupshaders() { vertexshaderbytecode = shaderbytecode.compilefromfile("minitri.fx", "vs", "vs_4_0", shaderflags.none, effectflags.none); vertexshader = new vertexshader(device, vertexshaderbytecode); pixelshaderbytecode = shaderbytecode.compilefromfile("minitri.fx", "ps", "ps_4_0", shaderflags.none, effectflags.none); ...... etc } public mesh setupmesh(object objecttodraw) { switch(.....) { // .. implement beautiful switch ;-) } return new mesh(vertices, indices); } public void init() { setupshaders(); _meshes.add(setupmesh(new dreieck(.....))); _meshes.add(setupmesh(new viereck(.....))); _meshes.add(setupmesh(new kreis(.....))); } public override void renderscene(draweventargs args) { renderer.device.immediatecontext.clearrendertargetview(renderer.rendertargetview, new color4(0.6f, 0, 0, 0)); foreach(var mesh in _meshes) { mesh.selectbuffers(); mesh.draw(); } return; }
something that...
Comments
Post a Comment