Add Chromium-only Blender WebEngine parity work

This commit is contained in:
mes123456
2026-08-12 04:47:48 -04:00
commit 9fd26010f6
18225 changed files with 11622124 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
#
# Copyright 2013 Pixar
#
# Licensed under the terms set forth in the LICENSE.txt file available at
# https://opensubdiv.org/license.
#
# *** dxViewer ***
set(SHADER_FILES
shader.hlsl
skyshader.hlsl
)
set(PLATFORM_LIBRARIES
"${OSD_LINK_TARGET}"
"${DXSDK_LIBRARIES}"
"${PTEX_LIBRARY}"
"${ZLIB_LIBRARY}"
)
include_directories(
"${OPENSUBDIV_INCLUDE_DIR}"
"${DXSDK_INCLUDE_DIR}"
"${PTEX_INCLUDE_DIR}"
)
if (OPENCL_FOUND)
include_directories("${OPENCL_INCLUDE_DIRS}")
list(APPEND PLATFORM_LIBRARIES OpenGL::GL)
endif()
set(SOURCE_FILES
dxPtexViewer.cpp
sky.cpp
)
osd_stringify("${SHADER_FILES}" INC_FILES)
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
osd_add_possibly_cuda_executable(dxPtexViewer "examples" WIN32
"${SOURCE_FILES}"
"${INC_FILES}"
$<TARGET_OBJECTS:regression_common_obj>
$<TARGET_OBJECTS:regression_far_utils_obj>
$<TARGET_OBJECTS:examples_common_dx11_obj>
$<TARGET_OBJECTS:examples_common_ptex_obj>
)
target_link_libraries(dxPtexViewer
${PLATFORM_LIBRARIES}
)
install(TARGETS dxPtexViewer DESTINATION "${CMAKE_BINDIR_BASE}")

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,604 @@
//
// Copyright 2013 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://opensubdiv.org/license.
//
struct OutputPointVertex {
float4 positionOut : SV_Position;
};
cbuffer Transform : register( b0 ) {
float4x4 ModelViewMatrix;
float4x4 ProjectionMatrix;
float4x4 ModelViewProjectionMatrix;
float4x4 ModelViewInverseMatrix;
};
cbuffer Tessellation : register( b1 ) {
float TessLevel;
int PrimitiveIdBase;
};
cbuffer Config : register( b3 ) {
float displacementScale;
float mipmapBias;
};
float4x4 OsdModelViewMatrix()
{
return ModelViewMatrix;
}
float4x4 OsdProjectionMatrix()
{
return ProjectionMatrix;
}
float4x4 OsdModelViewProjectionMatrix()
{
return ModelViewProjectionMatrix;
}
float OsdTessLevel()
{
return TessLevel;
}
int OsdGregoryQuadOffsetBase()
{
return 0;
}
int OsdPrimitiveIdBase()
{
return PrimitiveIdBase;
}
// ---------------------------------------------------------------------------
#if defined(DISPLACEMENT_HW_BILINEAR) \
|| defined(DISPLACEMENT_BILINEAR) \
|| defined(DISPLACEMENT_BIQUADRATIC) \
|| defined(NORMAL_HW_SCREENSPACE) \
|| defined(NORMAL_SCREENSPACE) \
|| defined(NORMAL_BIQUADRATIC) \
|| defined(NORMAL_BIQUADRATIC_WG)
Texture2DArray textureDisplace_Data : register(t6);
Buffer<uint> textureDisplace_Packing : register(t7);
#endif
#if defined(DISPLACEMENT_HW_BILINEAR) \
|| defined(DISPLACEMENT_BILINEAR) \
|| defined(DISPLACEMENT_BIQUADRATIC)
#undef OSD_DISPLACEMENT_CALLBACK
#define OSD_DISPLACEMENT_CALLBACK \
output.position = \
displacement(output.position, \
output.normal, \
output.patchCoord);
float4 displacement(float4 position, float3 normal, float4 patchCoord)
{
#if defined(DISPLACEMENT_HW_BILINEAR)
float disp = PtexLookupFast(patchCoord,
textureDisplace_Data,
textureDisplace_Packing).x;
#elif defined(DISPLACEMENT_BILINEAR)
float disp = PtexMipmapLookup(patchCoord, mipmapBias,
textureDisplace_Data,
textureDisplace_Packing).x;
#elif defined(DISPLACEMENT_BIQUADRATIC)
float disp = PtexMipmapLookupQuadratic(patchCoord, mipmapBias,
textureDisplace_Data,
textureDisplace_Packing).x;
#else
float disp(0);
#endif
return position + float4(disp*normal, 0) * displacementScale;
}
#endif
float4 GeneratePatchCoord(float2 uv, int primitiveID) // for non-adaptive
{
int3 patchParam = OsdGetPatchParam(OsdGetPatchIndex(primitiveID));
return OsdInterpolatePatchCoord(uv, patchParam);
}
// ---------------------------------------------------------------------------
// Vertex Shader
// ---------------------------------------------------------------------------
void vs_main( in InputVertex input,
out OutputVertex output )
{
output.positionOut = mul(ModelViewProjectionMatrix, input.position);
output.position = mul(ModelViewMatrix, input.position);
output.normal = mul(ModelViewMatrix,float4(input.normal, 0)).xyz;
output.patchCoord = float4(0,0,0,0);
output.tangent = float3(0,0,0);
output.bitangent = float3(0,0,0);
output.edgeDistance = float4(0,0,0,0);
}
// ---------------------------------------------------------------------------
// Geometry Shader
// ---------------------------------------------------------------------------
struct GS_OUT
{
OutputVertex v;
uint primitiveID : SV_PrimitiveID;
};
GS_OUT
outputVertex(OutputVertex input, float3 normal, uint primitiveID)
{
GS_OUT gsout;
gsout.v = input;
gsout.v.normal = normal;
gsout.primitiveID = primitiveID;
return gsout;
}
GS_OUT
outputVertex(OutputVertex input, float3 normal, float4 patchCoord, uint primitiveID)
{
GS_OUT gsout;
gsout.v = input;
gsout.v.normal = normal;
gsout.v.patchCoord = patchCoord;
gsout.primitiveID = primitiveID;
return gsout;
}
#if defined(GEOMETRY_OUT_WIRE) || defined(GEOMETRY_OUT_LINE)
#ifdef PRIM_TRI
#define EDGE_VERTS 3
#endif
#ifdef PRIM_QUAD
#define EDGE_VERTS 4
#endif
static float VIEWPORT_SCALE = 1024.0; // XXXdyu
float edgeDistance(float2 p, float2 p0, float2 p1)
{
return VIEWPORT_SCALE *
abs((p.x - p0.x) * (p1.y - p0.y) -
(p.y - p0.y) * (p1.x - p0.x)) / length(p1.xy - p0.xy);
}
GS_OUT
outputWireVertex(OutputVertex input, float3 normal,
int index, float2 edgeVerts[EDGE_VERTS], uint primitiveID)
{
GS_OUT gsout;
gsout.v = input;
gsout.v.normal = normal;
gsout.v.edgeDistance[0] =
edgeDistance(edgeVerts[index], edgeVerts[0], edgeVerts[1]);
gsout.v.edgeDistance[1] =
edgeDistance(edgeVerts[index], edgeVerts[1], edgeVerts[2]);
#ifdef PRIM_TRI
gsout.v.edgeDistance[2] =
edgeDistance(edgeVerts[index], edgeVerts[2], edgeVerts[0]);
#endif
#ifdef PRIM_QUAD
gsout.v.edgeDistance[2] =
edgeDistance(edgeVerts[index], edgeVerts[2], edgeVerts[3]);
gsout.v.edgeDistance[3] =
edgeDistance(edgeVerts[index], edgeVerts[3], edgeVerts[0]);
#endif
gsout.primitiveID = primitiveID;
return gsout;
}
#endif
#ifdef PRIM_QUAD
[maxvertexcount(6)]
void gs_main( lineadj OutputVertex input[4],
inout TriangleStream<GS_OUT> triStream,
uint primitiveID : SV_PrimitiveID)
{
float3 A = (input[0].position - input[1].position).xyz;
float3 B = (input[3].position - input[1].position).xyz;
float3 C = (input[2].position - input[1].position).xyz;
float3 n0 = normalize(cross(B, A));
float4 patchCoord[4];
patchCoord[0] = GeneratePatchCoord(float2(0, 0), primitiveID);
patchCoord[1] = GeneratePatchCoord(float2(1, 0), primitiveID);
patchCoord[2] = GeneratePatchCoord(float2(1, 1), primitiveID);
patchCoord[3] = GeneratePatchCoord(float2(0, 1), primitiveID);
triStream.Append(outputVertex(input[0], n0, patchCoord[0], primitiveID));
triStream.Append(outputVertex(input[1], n0, patchCoord[1], primitiveID));
triStream.Append(outputVertex(input[3], n0, patchCoord[3], primitiveID));
triStream.RestartStrip();
triStream.Append(outputVertex(input[3], n0, patchCoord[3], primitiveID));
triStream.Append(outputVertex(input[1], n0, patchCoord[1], primitiveID));
triStream.Append(outputVertex(input[2], n0, patchCoord[2], primitiveID));
triStream.RestartStrip();
}
#else // PRIM_TRI
[maxvertexcount(3)]
void gs_main( triangle OutputVertex input[3],
inout TriangleStream<GS_OUT> triStream,
uint primitiveID : SV_PrimitiveID)
{
float4 position[3];
float4 patchCoord[3];
float3 normal[3];
// patch coords are computed in tessellation shader
patchCoord[0] = input[0].patchCoord;
patchCoord[1] = input[1].patchCoord;
patchCoord[2] = input[2].patchCoord;
position[0] = input[0].position;
position[1] = input[1].position;
position[2] = input[2].position;
#ifdef NORMAL_FACET
// emit flat normals for displaced surface
float3 A = (position[0] - position[1]).xyz;
float3 B = (position[2] - position[1]).xyz;
normal[0]= normalize(cross(B, A));
normal[1] = normal[0];
normal[2] = normal[0];
#else
normal[0] = input[0].normal;
normal[1] = input[1].normal;
normal[2] = input[2].normal;
#endif
#if defined(GEOMETRY_OUT_WIRE) || defined(GEOMETRY_OUT_LINE)
float2 edgeVerts[3];
edgeVerts[0] = input[0].positionOut.xy / input[0].positionOut.w;
edgeVerts[1] = input[1].positionOut.xy / input[1].positionOut.w;
edgeVerts[2] = input[2].positionOut.xy / input[2].positionOut.w;
triStream.Append(outputWireVertex(input[0], normal[0], 0, edgeVerts, primitiveID));
triStream.Append(outputWireVertex(input[1], normal[1], 1, edgeVerts, primitiveID));
triStream.Append(outputWireVertex(input[2], normal[2], 2, edgeVerts, primitiveID));
#else
triStream.Append(outputVertex(input[0], normal[0], primitiveID));
triStream.Append(outputVertex(input[1], normal[1], primitiveID));
triStream.Append(outputVertex(input[2], normal[2], primitiveID));
#endif
}
#endif
// ---------------------------------------------------------------------------
// IBL lighting
// ---------------------------------------------------------------------------
Texture2D diffuseEnvironmentMap : register(t12);
Texture2D specularEnvironmentMap : register(t13);
SamplerState iblSampler : register(s0);
#define M_PI 3.14159265358
float4
gamma(float4 value, float g) {
return float4(pow(value.xyz, float3(g,g,g)), 1);
}
float4
getEnvironmentHDR(Texture2D tx, SamplerState sm, float3 dir)
{
dir = mul(ModelViewInverseMatrix, float4(dir, 0)).xyz;
float2 uv = float2((atan2(dir.x,dir.z)/M_PI+1)*0.5, (1-dir.y)*0.5);
return tx.Sample(sm, uv);
}
// ---------------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------------
#define NUM_LIGHTS 2
struct LightSource {
float4 position;
float4 ambient;
float4 diffuse;
float4 specular;
};
cbuffer Lighting : register( b2 ) {
LightSource lightSource[NUM_LIGHTS];
};
float4
lighting(float4 texColor, float3 Peye, float3 Neye, float occ)
{
float4 color = float4(0.0, 0.0, 0.0, 0.0);
float3 n = Neye;
for (int i = 0; i < NUM_LIGHTS; ++i) {
float4 Plight = lightSource[i].position;
float3 l = (Plight.w == 0.0)
? normalize(Plight.xyz) : normalize(Plight.xyz - Peye);
float3 h = normalize(l + float3(0,0,1)); // directional viewer
float d = max(0.0, dot(n, l));
float s = pow(max(0.0, dot(n, h)), 64.0f);
color += (1.0 - occ) * ((lightSource[i].ambient +
d * lightSource[i].diffuse) * texColor +
s * lightSource[i].specular);
}
color.a = 1.0;
return color;
}
// ---------------------------------------------------------------------------
// Pixel Shader
// ---------------------------------------------------------------------------
float4
edgeColor(float4 Cfill, float4 edgeDistance)
{
#if defined(GEOMETRY_OUT_WIRE) || defined(GEOMETRY_OUT_LINE)
#ifdef PRIM_TRI
float d =
min(edgeDistance[0], min(edgeDistance[1], edgeDistance[2]));
#endif
#ifdef PRIM_QUAD
float d =
min(min(edgeDistance[0], edgeDistance[1]),
min(edgeDistance[2], edgeDistance[3]));
#endif
float4 Cedge = float4(1.0, 1.0, 0.0, 1.0);
float p = exp2(-2 * d * d);
#if defined(GEOMETRY_OUT_WIRE)
if (p < 0.25) discard;
#endif
Cfill.rgb = lerp(Cfill.rgb, Cedge.rgb, p);
#endif
return Cfill;
}
// ---------------------------------------------------------------------------
// Pixel Shader
// ---------------------------------------------------------------------------
#if defined(COLOR_PTEX_NEAREST) || \
defined(COLOR_PTEX_HW_BILINEAR) || \
defined(COLOR_PTEX_BILINEAR) || \
defined(COLOR_PTEX_BIQUADRATIC)
Texture2DArray textureImage_Data : register(t4);
Buffer<uint> textureImage_Packing : register(t5);
#endif
#ifdef USE_PTEX_OCCLUSION
Texture2DArray textureOcclusion_Data : register(t8);
Buffer<uint> textureOcclusion_Packing : register(t9);
#endif
#ifdef USE_PTEX_SPECULAR
Texture2DArray textureSpecular_Data : register(t10);
Buffer<uint> textureSpecular_Packing : register(t11);
#endif
float4
getAdaptivePatchColor(int3 patchParam, float sharpness)
{
const float4 patchColors[7*6] = {
float4(1.0f, 1.0f, 1.0f, 1.0f), // regular
float4(0.0f, 1.0f, 1.0f, 1.0f), // regular pattern 0
float4(0.0f, 0.5f, 1.0f, 1.0f), // regular pattern 1
float4(0.0f, 0.5f, 0.5f, 1.0f), // regular pattern 2
float4(0.5f, 0.0f, 1.0f, 1.0f), // regular pattern 3
float4(1.0f, 0.5f, 1.0f, 1.0f), // regular pattern 4
float4(1.0f, 0.5f, 0.5f, 1.0f), // single crease
float4(1.0f, 0.70f, 0.6f, 1.0f), // single crease pattern 0
float4(1.0f, 0.65f, 0.6f, 1.0f), // single crease pattern 1
float4(1.0f, 0.60f, 0.6f, 1.0f), // single crease pattern 2
float4(1.0f, 0.55f, 0.6f, 1.0f), // single crease pattern 3
float4(1.0f, 0.50f, 0.6f, 1.0f), // single crease pattern 4
float4(0.8f, 0.0f, 0.0f, 1.0f), // boundary
float4(0.0f, 0.0f, 0.75f, 1.0f), // boundary pattern 0
float4(0.0f, 0.2f, 0.75f, 1.0f), // boundary pattern 1
float4(0.0f, 0.4f, 0.75f, 1.0f), // boundary pattern 2
float4(0.0f, 0.6f, 0.75f, 1.0f), // boundary pattern 3
float4(0.0f, 0.8f, 0.75f, 1.0f), // boundary pattern 4
float4(0.0f, 1.0f, 0.0f, 1.0f), // corner
float4(0.25f, 0.25f, 0.25f, 1.0f), // corner pattern 0
float4(0.25f, 0.25f, 0.25f, 1.0f), // corner pattern 1
float4(0.25f, 0.25f, 0.25f, 1.0f), // corner pattern 2
float4(0.25f, 0.25f, 0.25f, 1.0f), // corner pattern 3
float4(0.25f, 0.25f, 0.25f, 1.0f), // corner pattern 4
float4(1.0f, 1.0f, 0.0f, 1.0f), // gregory
float4(1.0f, 1.0f, 0.0f, 1.0f), // gregory
float4(1.0f, 1.0f, 0.0f, 1.0f), // gregory
float4(1.0f, 1.0f, 0.0f, 1.0f), // gregory
float4(1.0f, 1.0f, 0.0f, 1.0f), // gregory
float4(1.0f, 1.0f, 0.0f, 1.0f), // gregory
float4(1.0f, 0.5f, 0.0f, 1.0f), // gregory boundary
float4(1.0f, 0.5f, 0.0f, 1.0f), // gregory boundary
float4(1.0f, 0.5f, 0.0f, 1.0f), // gregory boundary
float4(1.0f, 0.5f, 0.0f, 1.0f), // gregory boundary
float4(1.0f, 0.5f, 0.0f, 1.0f), // gregory boundary
float4(1.0f, 0.5f, 0.0f, 1.0f), // gregory boundary
float4(1.0f, 0.7f, 0.3f, 1.0f), // gregory basis
float4(1.0f, 0.7f, 0.3f, 1.0f), // gregory basis
float4(1.0f, 0.7f, 0.3f, 1.0f), // gregory basis
float4(1.0f, 0.7f, 0.3f, 1.0f), // gregory basis
float4(1.0f, 0.7f, 0.3f, 1.0f), // gregory basis
float4(1.0f, 0.7f, 0.3f, 1.0f) // gregory basis
};
int patchType = 0;
int edgeCount = countbits(OsdGetPatchBoundaryMask(patchParam));
if (edgeCount == 1) {
patchType = 2; // BOUNDARY
}
if (edgeCount == 2) {
patchType = 3; // CORNER
}
#if defined OSD_PATCH_ENABLE_SINGLE_CREASE
if (sharpness > 0) {
patchType = 1;
}
#elif defined OSD_PATCH_GREGORY
patchType = 4;
#elif defined OSD_PATCH_GREGORY_BOUNDARY
patchType = 5;
#elif defined OSD_PATCH_GREGORY_BASIS
patchType = 6;
#endif
int pattern = countbits(OsdGetPatchTransitionMask(patchParam));
return patchColors[6*patchType + pattern];
}
void
ps_main(in OutputVertex input,
uint primitiveID : SV_PrimitiveID,
out float4 outColor : SV_Target )
{
// ------------ normal ---------------
#if defined(NORMAL_HW_SCREENSPACE) || defined(NORMAL_SCREENSPACE)
float3 normal = perturbNormalFromDisplacement(input.position.xyz,
input.normal,
input.patchCoord);
#elif defined(NORMAL_BIQUADRATIC) || defined(NORMAL_BIQUADRATIC_WG)
float4 du, dv;
float4 disp = PtexMipmapLookupQuadratic(du, dv, input.patchCoord,
mipmapBias,
textureDisplace_Data,
textureDisplace_Packing);
disp *= displacementScale;
du *= displacementScale;
dv *= displacementScale;
float3 n = normalize(cross(input.tangent, input.bitangent));
float3 tangent = input.tangent + n * du.x;
float3 bitangent = input.bitangent + n * dv.x;
#if defined(NORMAL_BIQUADRATIC_WG)
tangent += input.Nu * disp.x;
bitangent += input.Nv * disp.x;
#endif
float3 normal = normalize(cross(tangent, bitangent));
#else
float3 normal = input.normal;
#endif
// ------------ color ---------------
#if defined(COLOR_PTEX_NEAREST)
float4 texColor = PtexLookupNearest(input.patchCoord,
textureImage_Data,
textureImage_Packing);
#elif defined(COLOR_PTEX_HW_BILINEAR)
float4 texColor = PtexLookupFast(input.patchCoord,
textureImage_Data,
textureImage_Packing);
#elif defined(COLOR_PTEX_BILINEAR)
float4 texColor = PtexMipmapLookup(input.patchCoord, mipmapBias,
textureImage_Data,
textureImage_Packing);
#elif defined(COLOR_PTEX_BIQUADRATIC)
float4 texColor = PtexMipmapLookupQuadratic(input.patchCoord, mipmapBias,
textureImage_Data,
textureImage_Packing);
#elif defined(COLOR_PATCHTYPE)
float4 patchColor = getAdaptivePatchColor(
OsdGetPatchParam(OsdGetPatchIndex(primitiveID)), 0);
float4 texColor = edgeColor(lighting(patchColor, input.position.xyz, normal, 0),
input.edgeDistance);
outColor = texColor;
return;
#elif defined(COLOR_PATCHCOORD)
float4 texColor = edgeColor(lighting(input.patchCoord, input.position.xyz, normal, 0),
input.edgeDistance);
outColor = texColor;
return;
#elif defined(COLOR_NORMAL)
float4 texColor = edgeColor(float4(normal.x, normal.y, normal.z, 1),
input.edgeDistance);
outColor = texColor;
return;
#else // COLOR_NONE
float4 texColor = float4(0.5, 0.5, 0.5, 1);
#endif
// ------------ occlusion ---------------
#ifdef USE_PTEX_OCCLUSION
float occ = PtexMipmapLookup(input.patchCoord, mipmapBias,
textureOcclusion_Data,
textureOcclusion_Packing).x;
#else
float occ = 0.0;
#endif
// ------------ specular ---------------
#ifdef USE_PTEX_SPECULAR
float specular = PtexMipmapLookup(input.patchCoord, mipmapBias,
textureSpecular_Data,
textureSpecular_Packing).x;
#else
float specular = 1.0;
#endif
// ------------ lighting ---------------
#ifdef USE_IBL
// non-plausible BRDF
float4 a = float4(0, 0, 0, 1); //ambientColor;
float4 d = getEnvironmentHDR(diffuseEnvironmentMap, iblSampler, normal);
float3 eye = normalize(input.position.xyz - float3(0,0,0));
float3 r = reflect(eye, normal);
float4 s = getEnvironmentHDR(specularEnvironmentMap, iblSampler, r);
const float fresnelBias = 0.01;
const float fresnelScale = 1.0;
const float fresnelPower = 3.5;
float F = fresnelBias + fresnelScale * pow(1.0+dot(normal,eye), fresnelPower);
// Geometric attenuation term (
float NoV = dot(normal, -eye);
float alpha = 0.75 * 0.75; // roughness ^ 2
float k = alpha * 0.5;
float G = NoV/(NoV*(1-k)+k);
a *= (1-occ);
d *= (1-occ);
s *= min(specular, (1-occ)) * (F*G);
float4 Cf = (a+d)*texColor*(1-F)/M_PI + s;
//Cf = gamma(Cf, 2.2);
#else
float4 Cf = lighting(texColor, input.position.xyz, normal, occ);
#endif
// ------------ wireframe ---------------
outColor = edgeColor(Cf, input.edgeDistance);
}

View File

@@ -0,0 +1,240 @@
//
// Copyright 2013 Nvidia
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://opensubdiv.org/license.
//
#include "./sky.h"
#include "../common/d3d11Utils.h"
#include <cassert>
#include <vector>
static const char *g_skyShaderSource =
#include "skyshader.gen.h"
;
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
// shader constants
__declspec(align(16)) struct CB_CONSTANTS {
float ModelViewMatrix[16];
};
Sky::Sky(ID3D11Device * device, ID3D11Texture2D * environmentMap) :
numIndices(0),
vertexShader(0),
pixelShader(0),
shaderConstants(0),
texture(environmentMap), // we do not own this - we do not release it !
textureSRV(0),
textureSS(0),
inputLayout(0),
rasterizerState(0),
depthStencilState(0),
sphere(0),
sphereIndices(0) {
initialize(device);
}
Sky::~Sky() {
SAFE_RELEASE(vertexShader);
SAFE_RELEASE(pixelShader);
SAFE_RELEASE(shaderConstants);
SAFE_RELEASE(inputLayout);
SAFE_RELEASE(rasterizerState);
SAFE_RELEASE(depthStencilState);
SAFE_RELEASE(textureSS);
SAFE_RELEASE(textureSRV);
SAFE_RELEASE(sphere);
SAFE_RELEASE(sphereIndices);
}
void
Sky::initialize(ID3D11Device * device) {
// compile shaders
ID3DBlob * pVSBlob = D3D11Utils::CompileShader(g_skyShaderSource, "vs_main", "vs_5_0"),
* pPSBlob = D3D11Utils::CompileShader(g_skyShaderSource, "ps_main", "ps_5_0");
assert(pVSBlob && pPSBlob);
device->CreateVertexShader(pVSBlob->GetBufferPointer(),
pVSBlob->GetBufferSize(), NULL, &vertexShader);
assert(vertexShader);
device->CreatePixelShader(pPSBlob->GetBufferPointer(),
pPSBlob->GetBufferSize(), NULL, &pixelShader);
assert(pixelShader);
// VBO layout
D3D11_INPUT_ELEMENT_DESC inputElementDesc[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, sizeof(float)*3, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
device->CreateInputLayout(inputElementDesc, ARRAYSIZE(inputElementDesc),
pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &inputLayout);
assert(inputLayout);
// shader constants
D3D11_BUFFER_DESC cbDesc;
ZeroMemory(&cbDesc, sizeof(cbDesc));
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbDesc.MiscFlags = 0;
cbDesc.ByteWidth = sizeof(CB_CONSTANTS);
device->CreateBuffer(&cbDesc, NULL, &shaderConstants);
assert(shaderConstants);
// texture SRV
assert(texture);
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
ZeroMemory(&srvDesc, sizeof(srvDesc));
srvDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = 1;
device->CreateShaderResourceView(texture, &srvDesc, &textureSRV);
assert(textureSRV);
// texture sampler
D3D11_SAMPLER_DESC samplerDesc;
ZeroMemory(&samplerDesc, sizeof(samplerDesc));
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
samplerDesc.AddressU = samplerDesc.AddressV = samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MaxAnisotropy = 0;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
samplerDesc.BorderColor[0] = samplerDesc.BorderColor[1] = samplerDesc.BorderColor[2] = samplerDesc.BorderColor[3] = 0.0f;
device->CreateSamplerState(&samplerDesc, &textureSS);
// depth stencil state
D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
depthStencilDesc.StencilEnable = false;
device->CreateDepthStencilState(&depthStencilDesc, &depthStencilState);
// rasterizer state
D3D11_RASTERIZER_DESC rasDesc;
rasDesc.FillMode = D3D11_FILL_SOLID;
rasDesc.CullMode = D3D11_CULL_NONE;
rasDesc.FrontCounterClockwise = FALSE;
rasDesc.DepthBias = 0;
rasDesc.DepthBiasClamp = 0;
rasDesc.DepthClipEnable = FALSE;
rasDesc.SlopeScaledDepthBias = 0.0f;
rasDesc.ScissorEnable = FALSE;
rasDesc.MultisampleEnable = FALSE;
rasDesc.AntialiasedLineEnable = FALSE;
device->CreateRasterizerState(&rasDesc, &rasterizerState);
assert(rasterizerState);
const int U_DIV = 20,
V_DIV = 20;
std::vector<float> vbo;
std::vector<int> indices;
for (int u = 0; u <= U_DIV; ++u) {
for (int v = 0; v < V_DIV; ++v) {
float s = float(2*M_PI*float(u)/U_DIV);
float t = float(M_PI*float(v)/(V_DIV-1));
vbo.push_back(-sinf(t)*sinf(s));
vbo.push_back(cosf(t));
vbo.push_back(-sinf(t)*cosf(s));
vbo.push_back(u/float(U_DIV));
vbo.push_back(v/float(V_DIV));
if (v > 0 && u > 0) {
indices.push_back((u-1)*V_DIV+v-1);
indices.push_back(u*V_DIV+v-1);
indices.push_back((u-1)*V_DIV+v);
indices.push_back((u-1)*V_DIV+v);
indices.push_back(u*V_DIV+v-1);
indices.push_back(u*V_DIV+v);
}
}
}
D3D11_BUFFER_DESC bufferDesc;
D3D11_SUBRESOURCE_DATA subData;
// topology indices
ZeroMemory(&bufferDesc, sizeof(bufferDesc));
bufferDesc.ByteWidth = (int)indices.size() * sizeof(int);
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
bufferDesc.StructureByteStride = sizeof(int);
ZeroMemory(&subData, sizeof(subData));
subData.pSysMem = &indices[0];
subData.SysMemPitch = 0;
subData.SysMemSlicePitch = 0;
device->CreateBuffer(&bufferDesc, &subData, &sphereIndices);
assert(sphereIndices);
// VBO
ZeroMemory(&bufferDesc, sizeof(bufferDesc));
bufferDesc.ByteWidth = (int)vbo.size() * sizeof(float);
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
ZeroMemory(&subData, sizeof(subData));
subData.pSysMem = &vbo[0];
device->CreateBuffer(&bufferDesc, &subData, &sphere);
assert(sphere);
numIndices = (int)indices.size();
}
void
Sky::Draw(ID3D11DeviceContext * deviceContext, float const mvp[16]) {
if (vertexShader==0 || pixelShader==0 || shaderConstants==0) return;
if (texture==0 || textureSRV==0 || textureSS==0) return;
if (sphere==0 || sphereIndices==0) return;
// update shader constants
D3D11_MAPPED_SUBRESOURCE MappedResource;
deviceContext->Map(shaderConstants, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
CB_CONSTANTS* pData = (CB_CONSTANTS*)MappedResource.pData;
memcpy(pData->ModelViewMatrix, mvp, 16*sizeof(float));
deviceContext->Unmap(shaderConstants, 0);
// draw
deviceContext->RSSetState(rasterizerState);
deviceContext->OMSetDepthStencilState(depthStencilState, 1);
deviceContext->VSSetShader(vertexShader, NULL, 0);
deviceContext->VSSetConstantBuffers(0, 1, &shaderConstants);
deviceContext->PSSetShader(pixelShader, NULL, 0);
deviceContext->PSSetShaderResources(0, 1, &textureSRV);
deviceContext->PSSetSamplers(0, 1, &textureSS);
UINT hStrides = 5*sizeof(float);
UINT hOffsets = 0;
deviceContext->IASetVertexBuffers(0, 1, &sphere, &hStrides, &hOffsets);
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
deviceContext->IASetInputLayout(inputLayout);
deviceContext->IASetIndexBuffer(sphereIndices, DXGI_FORMAT_R32_UINT, 0);
deviceContext->DrawIndexed(numIndices, 0, 0);
}

View File

@@ -0,0 +1,48 @@
//
// Copyright 2013 Nvidia
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://opensubdiv.org/license.
//
#include <D3D11.h>
#include <D3Dcompiler.h>
//
// Draws an environment sphere centered on the camera w/ a texture
//
class Sky {
public:
// Constructor (Sky does not own the texture asset)
Sky(ID3D11Device * device, ID3D11Texture2D * environmentMap);
~Sky();
void Draw(ID3D11DeviceContext * deviceContext, float const mvp[16]);
private:
void initialize(ID3D11Device * device);
private:
int numIndices;
ID3D11VertexShader * vertexShader;
ID3D11PixelShader * pixelShader;
ID3D11Buffer * shaderConstants;
ID3D11InputLayout * inputLayout;
ID3D11RasterizerState * rasterizerState;
ID3D11DepthStencilState * depthStencilState;
ID3D11Texture2D * texture;
ID3D11ShaderResourceView * textureSRV;
ID3D11SamplerState * textureSS;
ID3D11Buffer * sphere;
ID3D11Buffer * sphereIndices;
};

View File

@@ -0,0 +1,63 @@
//
// Copyright 2013 Nvidia
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://opensubdiv.org/license.
//
struct VS_InputVertex {
float3 position : POSITION0;
float2 texCoord : TEXCOORD0;
};
struct VS_OutputVertex {
float4 position : SV_POSITION0;
float2 texCoord : TEXCOORD0;
};
cbuffer Transform : register( b0 ) {
float4x4 ModelViewMatrix;
};
//--------------------------------------------------------------
// sky vertex shader
//--------------------------------------------------------------
void vs_main(in VS_InputVertex input,
out VS_OutputVertex output) {
output.position = mul(ModelViewMatrix, float4(input.position,1));
output.texCoord = input.texCoord;
}
//--------------------------------------------------------------
// sky pixel shader
//--------------------------------------------------------------
struct PS_InputVertex {
float4 position : SV_POSITION0;
float2 texCoord : TEXCOORD0;
};
Texture2D tx : register(t0);
SamplerState sm : register(s0);
float4
gamma(float4 value, float g) {
return float4(pow(value.xyz, float3(g,g,g)), 1);
}
float4
ps_main(in PS_InputVertex input) : SV_Target {
float4 tex = tx.Sample(sm, input.texCoord.xy);
//float4 outColor = gamma(tex,0.4545);
float4 outColor = tex;
return outColor;
}