轻量封装WebGPU渲染系统示例<33>- 单精度浮点纹理(源码)

news2024/10/6 6:45:23

在WebGPU中创建纹理使用纹理很方便,只是js中只有Float32Array而默认不支持Float16Array,所以略微费点事。不过网上的大神多的是,摇摇小手就能获得解决方案。

废话多了容易挨胖揍,看代码。

js中float16单精度float数值转换:

// thanks: https://esdiscuss.org/topic/float16array
const toFloat16 = (function() {
	var floatView = new Float32Array(1);
	var int32View = new Int32Array(floatView.buffer);
	/* This method is faster than the OpenEXR implementation (very often
	 * used, eg. in Ogre), with the additional benefit of rounding, inspired
	 * by James Tursa?s half-precision code. */
	return function toHalf(val: number) {
		floatView[0] = val;
		var x = int32View[0];

		var bits = (x >> 16) & 0x8000; /* Get the sign */
		var m = (x >> 12) & 0x07ff; /* Keep one extra bit for rounding */
		var e = (x >> 23) & 0xff; /* Using int is faster here */

		/* If zero, or denormal, or exponent underflows too much for a denormal
		 * half, return signed zero. */
		if (e < 103) {
			return bits;
		}

		/* If NaN, return NaN. If Inf or exponent overflow, return Inf. */
		if (e > 142) {
			bits |= 0x7c00;
			/* If exponent was 0xff and one mantissa bit was set, it means NaN,
			 * not Inf, so make sure we set one mantissa bit too. */
			bits |= (e == 255 ? 0 : 1) && x & 0x007fffff;
			return bits;
		}

		/* If exponent underflows but not too much, return a denormal */
		if (e < 113) {
			m |= 0x0800;
			/* Extra rounding may overflow and set mantissa to 0 and exponent
			 * to 1, which is OK. */
			bits |= (m >> (114 - e)) + ((m >> (113 - e)) & 1);
			return bits;
		}

		bits |= ((e - 112) << 10) | (m >> 1);
		/* Extra rounding. An overflow will set mantissa to 0 and increment
		 * the exponent, which is OK. */
		bits += m & 1;
		return bits;
	};
})();

基于上述数值转换,实现单精度浮点数纹理(rgba16float格式纹理):

private createFloat16Texture(width: number, height: number): GPUTexture {

		let data = new Uint16Array(width * height * 4);
		let scale = 1.0;
		let k = 0;
		for (let i = 0; i < height; ++i) {
			for (let j = 0; j < width; ++j) {
				k = (width * i + j) * 4;
				data[k] = toFloat16(scale * (j/width));
				data[k+1] = toFloat16(scale * (0.5 + 0.5 * Math.sin(10.0 * (1.0 - j/width))));
				data[k+2] = toFloat16(scale * (1.0 - (i * j)/(width * height)));
				data[k+3] = toFloat16(scale * 1.0);
			}
		}

		const texture = device.createTexture({
			size: { width, height },
			format: "rgba16float",
			usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
		});
        // 下面的8表示一个像素需要4个float16也就是8个字节的内存来读写
		device.queue.writeTexture({ texture }, data, {bytesPerRow: width * 8, rowsPerImage: height}, { width, height });
		return texture;
	}

上述代码呈现的纹理效果:

此引擎系统当前示例RGBA8纹理和Float16纹理运行效果:

此示例基于此渲染系统实现,当前示例TypeScript源码如下:

export class DataTextureTest {
	private mRscene = new RendererScene();

	initialize(): void {

		this.initScene();
	}
	private applyRGBAFloat16Tex(): void {
		let rc = this.mRscene;

		let width = 256;
		let height = 256;

		let dataFs32 = new Float32Array(width * height * 4);
		let scale = 10.0;
		let k = 0;
		for (let i = 0; i < height; ++i) {
			for (let j = 0; j < width; ++j) {
				k = (width * i + j) * 4;
				dataFs32[k] = scale * (j / width);
				dataFs32[k + 1] = scale * (0.5 + 0.5 * Math.sin(10.0 * (1.0 - j / width)));
				dataFs32[k + 2] = scale * (1.0 - (i * j) / (width * height));
				dataFs32[k + 3] = scale * 1.0;
			}
		}
		const tex = {
			diffuse: { uuid: "rtt0", dataTexture: { data: dataFs32, width, height }, format: "rgba16float", generateMipmaps: true }
		};

		let entity = new FixScreenPlaneEntity({ extent: [-0.8, -0.8, 0.8, 0.8], textures: [tex] });
		entity.color = [0.1, 0.1, 0.1, 0.1];
		rc.addEntity(entity);
	}

	private applyRGBA8Tex(): void {
		let rc = this.mRscene;

		let width = 256;
		let height = 256;

		let dataU8 = new Uint8Array(width * height * 4);
		let k = 0;
		for (let i = 0; i < height; ++i) {
			for (let j = 0; j < width; ++j) {
				k = (width * i + j) * 4;
				dataU8[k] = ((j / width) * 255) | 0;
				dataU8[k + 1] = ((0.5 + 0.5 * Math.sin(10.0 * (1.0 - j / width))) * 255) | 0;
				dataU8[k + 2] = ((1.0 - (i * j) / (width * height)) * 255) | 0;
				dataU8[k + 3] = 255;
			}
		}

		let tex = {
			diffuse: { uuid: "rtt1", dataTexture: { data: dataU8, width, height }, format: "rgba8unorm", generateMipmaps: true }
		};

		let entity = new FixScreenPlaneEntity({ extent: [0.0, 0.0, 0.8, 0.8], textures: [tex] });
		rc.addEntity(entity);
	}
	private initScene(): void {
		this.applyRGBAFloat16Tex();
		this.applyRGBA8Tex();
	}

	run(): void {
		this.mRscene.run();
	}
}

注: 这里的纹理应用基于系统的对应功能的封装实现。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1214328.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【vue2绘制echarts环状图】vue2使用echarts绘制环状图

效果图&#xff1a; 鼠标悬浮的效果图&#xff1a; 1&#xff1a;安装echarts yarn add echarts5.3.2 或 npm install echarts5.3.2 --save2.局部引入使用 在vue页面引入 <template><div><divref"myChart"style"{width: 400px;height: 350…

C语言再学习 -- 编程规范

C语言编程规范这部分一直想总结一下。现在终于付诸行动了。 其实之前讲过一些面试题&#xff0c;参看&#xff1a;嵌入式面试知识点总结 – C语言篇 里面已经有包含一部分了&#xff0c;比如《高质量C C编程指南》.林锐着.pdf。 此次主要参考 华为技术有限公司c语言编程规范 …

Radau Quadrature

https://mathworld.wolfram.com/RadauQuadrature.html 这一部分&#xff0c;见书籍《JShen_TTang_LLWang_Spectral Methods-Algorithms Analysis and applications》

BMS系统项目

1、通过电压监测是否冲满&#xff0c;通过电压可以监测是否放完电 电池得参数 单体过压&#xff08;充满电&#xff09; 过压恢复&#xff08;百分之90多&#xff09; 欠压保护&#xff08;百分之几得电&#xff0c;快关机了&#xff09; 欠压恢复&#xff08;就是欠压之上…

2023年【陕西省安全员C证】最新解析及陕西省安全员C证试题及解析

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 陕西省安全员C证最新解析根据新陕西省安全员C证考试大纲要求&#xff0c;安全生产模拟考试一点通将陕西省安全员C证模拟考试试题进行汇编&#xff0c;组成一套陕西省安全员C证全真模拟考试试题&#xff0c;学员可通过…

百面深度学习-图神经网络

百面深度学习-图神经网络部分 什么是图神经网络&#xff1f; 图神经网络&#xff08;Graph Neural Networks&#xff0c;GNNs&#xff09;是深度学习模型的一个类别&#xff0c;专门设计来处理图结构数据。在图结构数据中&#xff0c;实体以节点&#xff08;vertex&#xff0…

复现YOLO v1 PyTorch

复现YOLO v1 PyTorch Paper: [1506.02640] You Only Look Once: Unified, Real-Time Object Detection (arxiv.org) Github: EclipseR33/yolo_v1_pytorch (github.com) 数据集 VOC2007&#xff1a;The PASCAL Visual Object Classes Challenge 2007 (VOC2007) VOC2012&…

Python in Visual Studio Code 2023年11月发布

排版&#xff1a;Alan Wang 我们很高兴地宣布 Visual Studio Code 的 Python 和 Jupyter 扩展将于 2023 年 11 月发布&#xff01; 此版本包括以下公告&#xff1a; 改进了使用 Shift Enter 在终端中运行当前行弃用内置 linting 和格式设置功能对 Python linting 扩展的改进重…

单片机的冷启动、热启动、复位

一文看懂STC单片机冷启动和复位有什么区别-电子发烧友网 单片机的冷启动、热启动和复位是不同的启动或重置方式&#xff0c;它们在系统状态和初始化方面有所不同&#xff1a; 1.冷启动&#xff08;Cold Start&#xff09;&#xff1a; 定义&#xff1a; 冷启动是指系统从完全关…

2023.11.14 关于 Spring Boot 创建和使用

目录 Spring Boot Spring Boot 项目的创建 网页版创建 Spring Boot 项目 Spring Boot 目录说明 项目运行 Spring Boot Spring Boot 是基于 Spring 设计的一个全新的框架&#xff0c;其目的是用来简化 Spring 的应用、初始搭建、开发的整个过程Spring Boot 就是一个整合了…

Redis配置、Redis类型

系列文章目录 第一章 Java线程池技术应用 第二章 CountDownLatch和Semaphone的应用 第三章 Spring Cloud 简介 第四章 Spring Cloud Netflix 之 Eureka 第五章 Spring Cloud Netflix 之 Ribbon 第六章 Spring Cloud 之 OpenFeign 第七章 Spring Cloud 之 GateWay 第八章 Sprin…

Opengauss到Oracle增量同步, 使用debezium

一、概述 PG到Oracle的同步方案使用debezium kafka kafka-connect-jdbc。debezium是一款开源的变更捕获软件&#xff0c;它以kafka的connector形式运行&#xff0c;可以捕获PostgreSQL、MySQL、Oracle中的变更数据&#xff0c;保存到kafka。kafka-connect-jdbc是confluent公…

stable diffusion到底是如何工作的

stable diffusion简单入门 stable diffusion是一个文生图模型&#xff0c;主要由CompVis、Stability AI和LAION的研究者们创建。这个模型主要是在512X512分辨率的图像上训练的&#xff0c;训练数据集是LAION-5B&#xff0c;该数据集是目前可访问的最大的多模态数据集。 在这篇…

springboot服务和python服务如何自定义启动banner

shigen日更文章的博客写手&#xff0c;擅长Java、python、vue、shell等编程语言和各种应用程序、脚本的开发。记录成长&#xff0c;分享认知&#xff0c;留住感动。 shigen最近在修改ruoyi的框架&#xff0c;看到了框架自带的banner图&#xff0c;就是一个不爽&#xff0c;于是…

基于JavaWeb+SSM+微信小程序基金优选系统的设计和实现

基于JavaWebSSM微信小程序基金优选系统的设计和实现 源码获取入口前言主要技术系统设计功能截图Lun文目录订阅经典源码专栏Java项目精品实战案例《500套》 源码获取 源码获取入口 前言 基金优选是金融机构的核心&#xff0c;是必不可少的一个部分。在金融机构的整个服务行业中…

怎么恢复删除的数据? 8个有效的数据恢复方法

无论您在保存备份方面多么小心&#xff0c;灾难有时还是会发生。有时您的备份软件无法按预期运行。 如果您的外部驱动器靠近您的设备&#xff0c;发生火灾/洪水/故障时&#xff0c;有时备份会与原始文件一起丢失。即使是云存储也不能避免故障。 还有一个事实是&#xff0c;我…

微信@all bug复现及原理分析

1、复现 条件&#xff1a; 1、Windows PC 端微信 2、自建一个群聊拥有群管权限可以所有人 废话不多说&#xff0c;直接上图 所有人 剪切后&#xff0c;到另一个群中&#xff0c;引用任意一个群里成员的消息&#xff0c;并将刚才剪切的粘贴至此&#xff0c;发送 便可完成非群…

Ubuntu搭建openvpn服务器

文章目录 一、基于ubuntu搭建openvpn服务器二、制作相关证书2.1 制作ca证书 ./build-ca2.2 制作Server端证书2.3 制作Client端证书 三、配置服务器3.1 配置Server端3.2. 配置Client端 四、安装openvpn客户端&#xff1a;http://build.openvpn.net/downloads/releases/ 一、基于…

电脑技巧:U盘装系统跟光盘装系统有什么区别,看完你就懂了!

目录 一、制作方法 二、优点比较 2.1 U盘 2.2 光盘 三、缺点比较 一、制作方法 U盘&#xff1a;是通过制作U盘系统盘&#xff0c;插在电脑上启动U盘&#xff0c;然后从U盘上启动PE系统&#xff0c;在PE系统里加载预先下载好的镜像&#xff0c;然后开始安装系统。 光盘&am…

算法实战:亲自写红黑树之二 完整代码

此文承接&#xff1a;算法实战&#xff1a;亲自写红黑树之一-CSDN博客 目录 一、项目结构 二、辅助代码a.h 三、红黑树代码rbtree.h 四、测试代码main.cpp 五、运行效果 六、代码详解 一、项目结构 这里给出的代码是实际可以运行的代码。 运行环境&#xff1a;VS2022&am…