コード例 #1
0
ファイル: test_mtltypes.py プロジェクト: rongilson/pyobjc
    def test_functions(self):
        v = Metal.MTLOriginMake(1, 2, 3)
        self.assertIsInstance(v, Metal.MTLOrigin)
        self.assertEqual(v, (1, 2, 3))

        v = Metal.MTLSizeMake(1, 2, 3)
        self.assertIsInstance(v, Metal.MTLSize)
        self.assertEqual(v, (1, 2, 3))

        v = Metal.MTLRegionMake1D(1, 2)
        self.assertIsInstance(v, Metal.MTLRegion)

        v = Metal.MTLRegionMake2D(1, 2, 3, 4)
        self.assertIsInstance(v, Metal.MTLRegion)

        v = Metal.MTLRegionMake3D(1, 2, 3, 4, 5, 6)
        self.assertIsInstance(v, Metal.MTLRegion)

        v = Metal.MTLSamplePositionMake(0.5, 1.5)
        self.assertIsInstance(v, Metal.MTLSamplePosition)
        self.assertEqual(v, (0.5, 1.5))
コード例 #2
0
ファイル: metal_compute.py プロジェクト: 0x0L/python_metal
m[:] = bytes(vA)

vB = np.random.randn(arrayLength).astype(np.float32)
t = bufferB.contents()
m = t.as_buffer(bufferSize)
m[:] = bytes(vB)

commandBuffer = commandQueue.commandBuffer()
computeEncoder = commandBuffer.computeCommandEncoder()

computeEncoder.setComputePipelineState_(addFunctionPSO)
computeEncoder.setBuffer_offset_atIndex_(bufferA, 0, 0)
computeEncoder.setBuffer_offset_atIndex_(bufferB, 0, 1)
computeEncoder.setBuffer_offset_atIndex_(bufferResult, 0, 2)

gridSize = Metal.MTLSizeMake(arrayLength, 1, 1)

threadGroupSize = addFunctionPSO.maxTotalThreadsPerThreadgroup()
if threadGroupSize > arrayLength:
    threadGroupSize = arrayLength

threadgroupSize = Metal.MTLSizeMake(threadGroupSize, 1, 1)
computeEncoder.dispatchThreads_threadsPerThreadgroup_(gridSize,
                                                      threadgroupSize)
computeEncoder.endEncoding()

commandBuffer.commit()
commandBuffer.waitUntilCompleted()

t = bufferResult.contents()
result = np.frombuffer(t.as_buffer(bufferSize), dtype=np.float32)