2016-04-21 15 views
5

मैं प्राइमेटिव्स को सीधे स्क्रीन में नहीं, बल्कि स्क्रीन स्क्रीन बनावट में कैसे प्रस्तुत करूं?मल्टी-नमूनाकरण के साथ धातु ऑफ-स्क्रीन ड्राइंग

मेरे पास त्रिकोण और संबंधित रंग का एक सेट है, मैं बस उन्हें स्क्रीन पर करने के लिए उसी तरह से आकर्षित करना चाहता हूं, लेकिन स्क्रीन बनावट में, कि मैं एक फ़ाइल में सहेज सकता हूं।

क्या कोई मुझे उस का कोड नमूना दिखा सकता है?

+1

आपने क्या प्रयास किया है? धातु में, सभी प्रतिपादन एक बनावट के लिए किया जाता है; एकमात्र अंतर यह है कि क्या आप एक ड्रॉइंग में लिपटे बनावट में चित्रित कर रहे हैं जिसे स्क्रीन पर प्रस्तुत किया गया है, या एक बनावट जिसे आप स्वयं प्रबंधित करते हैं। यदि आप बाद वाले को आकर्षित करना चाहते हैं, तो उचित आकार और प्रारूप का 'एमटीएलटेक्चर' बनाएं और इसे अपने रेंडर पास डिस्क्रिप्टर के पहले रंग अनुलग्नक के बनावट के रूप में सेट करें। फिर, आप छवि डेटा प्राप्त करने और इसे लिखने के लिए 'GetBytes' API का उपयोग कर सकते हैं। – warrenm

+0

मेरे कोड को देखें, मैंने – s1ddok

+0

पर सवाल उठाया है, मुझे लगता है कि मेरी समस्या यह है कि मेरे निर्देशांक गलत हैं, शायद मुझे व्यूपोर्ट या शायद कस्टम प्रोजेक्शन सेट करना चाहिए ... – s1ddok

उत्तर

4

ठीक है, मुझे यह एहसास हुआ। यह कोड नौकरी करता है, केवल अपवाद के साथ कि यह बहुत बड़ा त्रिकोण खींचता है, लेकिन यह वर्टेक्स फ़ंक्शन के लिए एक अलग विषय है।

let fragmentProgram = defaultLibrary.newFunctionWithName("image_fragmentT") 
    let vertexProgram = defaultLibrary.newFunctionWithName("image_vertexT") 


    struct VertexT { 
     var x, y, z, w : Float 
     var r, g, b, a : Float 
    } 

    let vertexDescriptor = MTLVertexDescriptor() 
    vertexDescriptor.attributes[0].offset = 0 
    vertexDescriptor.attributes[0].format = .Float4 
    vertexDescriptor.attributes[0].bufferIndex = 0 

    vertexDescriptor.attributes[1].offset = 0 
    vertexDescriptor.attributes[1].format = .Float4 
    vertexDescriptor.attributes[1].bufferIndex = 0 

    vertexDescriptor.layouts[0].stepFunction = .PerVertex 
    vertexDescriptor.layouts[0].stride = sizeof(VertexT) 

    let pipelineStateDescriptor = MTLRenderPipelineDescriptor() 
    pipelineStateDescriptor.vertexDescriptor = vertexDescriptor 
    pipelineStateDescriptor.vertexFunction = vertexProgram 
    pipelineStateDescriptor.fragmentFunction = fragmentProgram 
    pipelineStateDescriptor.colorAttachments[0].pixelFormat = .RGBA8Unorm; 
    pipelineStateDescriptor.colorAttachments[0].blendingEnabled = true 
    pipelineStateDescriptor.sampleCount = 4 
    pipelineStateDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperation.Add 
    pipelineStateDescriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperation.Add 
    pipelineStateDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactor.SourceAlpha 
    pipelineStateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactor.SourceAlpha 
    pipelineStateDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlpha 
    pipelineStateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlpha 


    let sampleDesc = MTLTextureDescriptor() 
    sampleDesc.textureType = MTLTextureType.Type2DMultisample 
    sampleDesc.width = inTexture.width 
    sampleDesc.height = inTexture.height 
    sampleDesc.sampleCount = 4 
    sampleDesc.pixelFormat = .RGBA8Unorm 
    sampleDesc.storageMode = .Private 
    sampleDesc.usage = .RenderTarget 

    let sampletex = device.device.newTextureWithDescriptor(sampleDesc) 
    let renderPassDescriptor = MTLRenderPassDescriptor() 

    renderPassDescriptor.colorAttachments[0].texture = sampletex 
    renderPassDescriptor.colorAttachments[0].resolveTexture = outTexture 
    renderPassDescriptor.colorAttachments[0].loadAction = .Clear 
    renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0) 
    renderPassDescriptor.colorAttachments[0].storeAction = .MultisampleResolve 

    let renderCB = commandQueue.commandBuffer() 

    let renderCommandEncoder = renderCB.renderCommandEncoderWithDescriptor(renderPassDescriptor) 
    let pipelineState = try! device.device.newRenderPipelineStateWithDescriptor(pipelineStateDescriptor) 
    renderCommandEncoder.setRenderPipelineState(pipelineState) 

    let vertexBuf = device.device.newBufferWithLength(triangles.count * 3 * sizeof(VertexT), options: .CPUCacheModeDefaultCache) 

    var vBufPointer = [VertexT]() 

    for i in 0..<triangles.count { 

     // create buffer here 
    } 

    memcpy(vertexBuf.contents(), &vBufPointer, triangles.count * 3 * sizeof(VertexT)) 

    renderCommandEncoder.setVertexBuffer(vertexBuf, offset: 0, atIndex: 0) 
    renderCommandEncoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: triangles.count * 3) 
    renderCommandEncoder.endEncoding() 
    renderCB.commit() 
    renderCB.waitUntilCompleted() 

आप छवि अब outTexture चर में है:

यहाँ मेरी कोड है।

+1

मुझे संयोग से आज भी यही समस्या है, लेकिन अभी भी अजीब मुद्दे हैं। मुझे लगता है कि मेरे अपने मुद्दे का संकल्प उपयोग करना है। MultisampleResolve, लेकिन आउटटेक्चर के वर्णनकर्ता के लिए सेटिंग्स क्या हैं? –

संबंधित मुद्दे