package com.codelearning.vulkaninfo
import android.Manifest
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.pm.PackageManager
import android.content.res.AssetManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.util.Log
import com.codelearning.vulkaninfo.databinding.ActivityMainBinding
import java.io.IOException
import java.io.RandomAccessFile
import java.nio.charset.StandardCharsets
private external fun doVulkanWork(assetMgr: AssetManager, logBuffer: ByteArray): Int
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var m_isVulkanAvailable = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// Example of a call to a native method
binding.sampleText.text = stringFromJNI()
if(binding.sampleText.text.contains("Failed", true)) {
m_isVulkanAvailable = false
}
binding.runVulkanButton.setOnClickListener {
if(!m_isVulkanAvailable) return@setOnClickListener
val logBuffer = ByteArray(10 * 1024 * 1024)
val dataLen = doVulkanWork(assets, logBuffer)
val logStr = String(logBuffer, 0, dataLen, StandardCharsets.UTF_8)
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText("vulkaninfo", logStr)
clipboard.setPrimaryClip(clipData)
binding.sampleText.text = resources.getString(R.string.contents_are_copied)
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 101)
}
val file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) ?: return@setOnClickListener
try {
val filePath = file.absolutePath + "/vulkan_info.log"
Log.i(null, "The file path to be saved: $filePath")
val fileAccess = RandomAccessFile(filePath, "rw")
fileAccess.write(logBuffer, 0, dataLen)
fileAccess.close()
}
catch (e: IOException) {
Log.i(null, "File write error: ${e.localizedMessage}")
}
binding.sampleText.text = resources.getString(R.string.file_is_saved)
}
}
/**
* A native method that is implemented by the 'vulkaninfo' native library,
* which is packaged with this application.
*/
external fun stringFromJNI(): String
companion object {
// Used to load the 'vulkaninfo' library on application startup.
init {
System.loadLibrary("vulkaninfo")
}
}
}
print"Hello, Lua!"
function test_func()
local num = 0
return function()
num = num + 1
return num
end
end
all: main.o module.o
gcc main.o module.o -o target_bin
main.o: main.c module.h
gcc -I . -c main.c
module.o: module.c module.h
gcc -I . -c module.c
clean:
rm -rf *.o
rm target_bin
// 控制点结构体
struct ControlPoint {
float2 position [[ attribute(0) ]];
float4 color [[ attribute(1) ]];
};
// Patch结构体
struct PatchIn {
patch_control_point control_points;
};
// 顶点着色器输出到片段着色器的结构体
struct FunctionOutIn {
float4 position [[ position ]];
half4 color [[ flat ]];
};
// 三角形细分曲面后处理顶点着色器
[[ patch(triangle, 3) ]]
vertex struct FunctionOutIn triangle_vertex(struct PatchIn patchIn [[stage_in]],
float3 patch_coord [[ position_in_patch ]])
{
float u = patch_coord.x;
float v = patch_coord.y;
float w = patch_coord.z;
// 将当前控制点坐标(u, v, w)通过线性插值转换为笛卡尔坐标(x, y)
float x = u * patchIn.control_points[0].position.x + v * patchIn.control_points[1].position.x + w * patchIn.control_points[2].position.x;
float y = u * patchIn.control_points[0].position.y + v * patchIn.control_points[1].position.y + w * patchIn.control_points[2].position.y;
// 顶点输出
struct FunctionOutIn vertexOut;
vertexOut.position = float4(x, y, 0.0, 1.0);
vertexOut.color = half4(u, v, w, 1.0);
return vertexOut;
}
// 四边形细分曲面后处理顶点着色器
[[ patch(quad, 4) ]]
vertex struct FunctionOutIn quad_vertex(struct PatchIn patchIn [[stage_in]],
float2 patch_coord [[ position_in_patch ]])
{
// 从tessellator处理之后所获得的规格化之后的控制点坐标——
// uv坐标的原点(即(0, 0)的位置)是在原patch的左下顶点
float u = patch_coord.x;
float v = patch_coord.y;
// 以下通过线性插值的算法将规格化后的控制点坐标再转换为相对于输入顶点的坐标
float2 lower_middle = mix(patchIn.control_points[0].position.xy, patchIn.control_points[1].position.xy, u);
float2 upper_middle = mix(patchIn.control_points[2].position.xy, patchIn.control_points[3].position.xy, 1-u);
// 顶点输出
struct FunctionOutIn vertexOut;
vertexOut.position = float4(mix(lower_middle, upper_middle, v), 0.0f, 1.0f);
vertexOut.color = half4(u, v, 1.0f - v, 1.0h);
// 靠左下的所有顶点使用原patch左下顶点的颜色
if(u < 0.5f && v < 0.5f) {
vertexOut.color = half4(patchIn.control_points[0].color);
}
// 靠右下的所有顶点使用原patch右下顶点的颜色
else if(u > 0.5f && v < 0.5f) {
vertexOut.color = half4(patchIn.control_points[1].color);
}
// 靠右上的所有顶点使用原patch右上顶点的颜色
else if(u > 0.5f && v > 0.5f) {
vertexOut.color = half4(patchIn.control_points[2].color);
}
// 靠左上的所有顶点使用原patch左上顶点的颜色
else if (u < 0.5f && v > 0.5f) {
vertexOut.color = half4(patchIn.control_points[3].color);
}
return vertexOut;
}
; 这是一个汇编文件
; YASM的注释风格使用分号形式
global MyASMTest
section .text
MyASMTest:
sub edi, esi
mov eax, edi
ret
@import Foundation;
int main(void)
{
@autoreleasepool {
NSLog(@"Hello, this is a modern Objective-C 2.0 program!");
unichar c = u'加';
NSLog(@"The character is: %C", c);
}
}
kernel void solve_sum(
global unsigned buffer[512],
global unsigned dest[512]
)
{
local volatile int flag = 0;
size_t gid = get_global_id(0);
const uint4 value = (uint4)(1, 2, 3, 4);
if(0 <= gid && gid < 32)
{
while(flag == 0);
vstore4(value, gid, buffer);
//write_mem_fence(CLK_GLOBAL_MEM_FENCE);
flag = 0;
}
else if(32 <= gid && gid < 64)
{
flag = 1;
while(flag == 1);
unsigned ret = buffer[127 + 32 - gid];
dest[gid - 32] = ret;
}
}
<?php
echo("Hello PHP!");
?>
hello = 10
if hello == 10:
print("This is a Python program")
lam1 = lambda x,y: x + y
lam2 = lambda lam: lam(20, 10) * 2
lam3 = lambda x, y: x + (lambda a: x * a)(hello) - y
lam4 = lambda x: lambda y: y / x
print(lam1(100, hello))
print(lam2(lam1))
print(lam3(4, 5))
print(lam4(2)(8))
fn main() {
println!("This is a Rust program!");
let a = "Hello A!";
let b = "Hello B!";
println!("{} {}", a, b);
}
(define x 2)
;Value: x
(+ x 10)
;Value: 12
(define y (/ 3 2))
;Value: y
(+ y 0.5 x)
;Value: 4.
(+ y 0.1)
;Value: 1.6
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 由于myMethod方法被重载,
// 因此这里对funcRef显式指定类型来指明指向不带任何参数的myMethod方法
let funcRef: () -> Void = self.myMethod
funcRef()
// 或者可以这么写:直接显式地在方法名后添加上类型
let funcRef2 = self.myMethod as () -> Void
funcRef2()
// 这里使用方法签名myMethod(a:)来指明metohdRef指向myMethod(a a: Int)方法,
// 而methodRef的类型被推导为:(a: Int) -> Void
var methodRef = self.myMethod(a:)
methodRef(a: 100)
methodRef = self.myMethod(_:)
methodRef(a: 200) // 这里即便有一个a:标签也无所谓,调用的仍然是myMethod(_:)方法
// 各位请注意,这里的mref的类型为:(_: Int) -> Void
// 注意,其形参不含外部标签
var mref = self.myMethod(_:)
mref(10)
// 这里又指向了myMethod(a: Int)方法
mref = self.myMethod(a:)
mref(20) // 仅管这里没有标签,但调用时仍然调用的是myMethod(a: Int)方法
/** 上述是隐式地做类型推导,而下面我们可以用显式的类型指定 */
// 显式指明ref是一个带有含外部标签b的形参的方法引用
let ref: (b: Int) -> Void = self.myMethod(a:)
ref(b: 30)
/** 以下是对应的selector的描述 */
var sel: Selector = #selector(self.myMethod(a:))
sel = #selector(self.myMethod(_:))
// 对于不带参数的方法,被用作selector时,必须在后面显式地加上函数类型
sel = #selector(self.myMethod as () -> Void)
}
func myMethod() {
print("My method!")
}
func myMethod(a a: Int) {
print("Method2 value = (a)")
}
func myMethod(_ a: Int) {
print("Method3 value = (a)")
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
以上编程语言的代码语法高亮都支持得不错,后续可以考虑直接在头条上发博文了~
页面更新:2024-04-27
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号